Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3854 from alphagov/single-consent-api
Add single cookie consent API
- Loading branch information
Showing
16 changed files
with
882 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
app/assets/javascripts/govuk_publishing_components/domain-config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
window.GOVUK = window.GOVUK || {} | ||
window.GOVUK.vars = window.GOVUK.vars || {} | ||
window.GOVUK.vars.domains = [ | ||
{ | ||
// need to have this one at the start, see loadGa4 function | ||
name: 'development', | ||
domains: [ | ||
'localhost', | ||
'127.0.0.1', | ||
'0.0.0.0', | ||
'dev.gov.uk' | ||
], | ||
initialiseGA4: true, | ||
id: 'GTM-MG7HG5W', | ||
auth: 'bRiZ-jiEHtw6hHpGd6dF9w', | ||
preview: 'env-3', | ||
gaProperty: 'UA-UNSET', | ||
gaPropertyCrossDomain: 'UA-UNSET', | ||
consentApiUrl: 'staging' | ||
}, | ||
{ | ||
name: 'production', | ||
domains: [ | ||
'www.gov.uk', | ||
'www-origin.publishing.service.gov.uk', | ||
'assets.publishing.service.gov.uk' | ||
], | ||
initialiseGA4: true, | ||
id: 'GTM-MG7HG5W', | ||
gaProperty: 'UA-26179049-1', | ||
gaPropertyCrossDomain: 'UA-145652997-1', | ||
consentApiUrl: 'production' | ||
}, | ||
{ | ||
name: 'staging', | ||
domains: [ | ||
'www.staging.publishing.service.gov.uk', | ||
'www-origin.staging.publishing.service.gov.uk', | ||
'assets.staging.publishing.service.gov.uk' | ||
], | ||
initialiseGA4: true, | ||
id: 'GTM-MG7HG5W', | ||
auth: 'oJWs562CxSIjZKn_GlB5Bw', | ||
preview: 'env-5', | ||
gaProperty: 'UA-26179049-20', | ||
gaPropertyCrossDomain: 'UA-145652997-1', | ||
consentApiUrl: 'staging' | ||
}, | ||
{ | ||
name: 'integration', | ||
domains: [ | ||
'www.integration.publishing.service.gov.uk', | ||
'www-origin.integration.publishing.service.gov.uk', | ||
'assets.integration.publishing.service.gov.uk' | ||
], | ||
initialiseGA4: true, | ||
id: 'GTM-MG7HG5W', | ||
auth: 'C7iYdcsOlYgGmiUJjZKrHQ', | ||
preview: 'env-4', | ||
gaProperty: 'UA-26179049-22', | ||
gaPropertyCrossDomain: 'UA-145652997-1', | ||
consentApiUrl: 'staging' | ||
}, | ||
{ | ||
name: 'devdocs', | ||
domains: [ | ||
'docs.publishing.service.gov.uk' | ||
], | ||
initialiseGA4: true, | ||
id: 'GTM-TNKCK97', | ||
consentApiUrl: 'production' | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/assets/javascripts/govuk_publishing_components/lib/single-consent-functions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* global GovSingleConsent */ | ||
// = require govuk-single-consent/dist/singleconsent.iife.js | ||
|
||
(function (root) { | ||
'use strict' | ||
window.GOVUK = window.GOVUK || {} | ||
|
||
window.GOVUK.singleConsent = { | ||
init: function (callback) { | ||
if (!window.GOVUK.useSingleConsentApi) { | ||
return | ||
} | ||
callback = callback || this.apiCallback | ||
// determine where we are and set the consent api URL accordingly | ||
if (!this.url) { | ||
this.url = 'staging' | ||
var environment = window.GOVUK.loadAnalytics.getEnvironment(window.GOVUK.analyticsGa4.core.trackFunctions.getHostname()) | ||
if (environment) { | ||
this.url = environment.consentApiUrl | ||
} | ||
} | ||
// create the consent API object | ||
this.consentApiObj = new GovSingleConsent(callback, this.url) | ||
}, | ||
|
||
apiCallback: function (consents, consentsPreferencesSet, error) { | ||
if (error) { | ||
console.error('Single consent error: ', error, window.location) | ||
return | ||
} | ||
if (consentsPreferencesSet) { | ||
if (consents && consents.usage) { | ||
window.GOVUK.triggerEvent(window, 'cookie-consent') | ||
} | ||
} else { | ||
window.GOVUK.triggerEvent(window, 'show-cookie-banner') | ||
} | ||
}, | ||
|
||
setPreferences: function (type, options) { | ||
if (window.GOVUK.useSingleConsentApi) { | ||
try { | ||
switch (type) { | ||
case 'accept': | ||
this.consentApiObj.setConsents(GovSingleConsent.ACCEPT_ALL) | ||
break | ||
case 'reject': | ||
this.consentApiObj.setConsents(GovSingleConsent.REJECT_ALL) | ||
break | ||
default: | ||
this.consentApiObj.setConsents(options) | ||
} | ||
} catch (e) { | ||
console.error('Single consent ' + type + ' error: ', e, window.location) | ||
} | ||
} | ||
} | ||
} | ||
}(window)) |
Oops, something went wrong.