diff --git a/CHANGELOG.md b/CHANGELOG.md index 74bd5f98d4..22c79a48d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * Increase size of action link blue arrow ([PR #2343](https://github.com/alphagov/govuk_publishing_components/pull/2343)) * Remove scroll tracking on pages ([PR #2339](https://github.com/alphagov/govuk_publishing_components/pull/2339)) +* Accept cookie consent from Digital Identity query parameter ([PR #2340](https://github.com/alphagov/govuk_publishing_components/pull/2340)) ## 27.4.0 diff --git a/app/assets/javascripts/govuk_publishing_components/analytics/init.js b/app/assets/javascripts/govuk_publishing_components/analytics/init.js index 2d584d6b5d..a89a7324be 100644 --- a/app/assets/javascripts/govuk_publishing_components/analytics/init.js +++ b/app/assets/javascripts/govuk_publishing_components/analytics/init.js @@ -8,6 +8,25 @@ var analyticsInit = function () { var linkedDomains = window.GOVUK.analyticsVars.linkedDomains || false } + window.GOVUK.Analytics.checkDigitalIdentityConsent = function (location) { + if (!location || !location.search) return + // this checks for the presence of the Digital Identity cookie consent query parameter and updates our consent cookie accordingly + // This is so that users don't see multiple cookie banners when they move between the different account management pages, as some will be on GOV.UK and others will be on the DI domain. + var digitalIdentityConsent = /([?&]cookie_consent=)(accept|reject)/.exec(location.search) + if (digitalIdentityConsent) { + if (digitalIdentityConsent[2] === 'accept') { + window.GOVUK.setConsentCookie({ usage: true }) + // set cookies_preferences_set to true to prevent cookie banner showing + window.GOVUK.cookie('cookies_preferences_set', 'true') + } else if (digitalIdentityConsent[2] === 'reject') { + window.GOVUK.setConsentCookie({ usage: false }) + window.GOVUK.cookie('cookies_preferences_set', 'true') + } + } + } + + window.GOVUK.Analytics.checkDigitalIdentityConsent(window.location) + var consentCookie = window.GOVUK.getConsentCookie() var dummyAnalytics = { addLinkedTrackerDomain: function () {}, diff --git a/spec/javascripts/govuk_publishing_components/analytics/analytics.spec.js b/spec/javascripts/govuk_publishing_components/analytics/analytics.spec.js index 9ce2066022..045c98e8ca 100644 --- a/spec/javascripts/govuk_publishing_components/analytics/analytics.spec.js +++ b/spec/javascripts/govuk_publishing_components/analytics/analytics.spec.js @@ -43,6 +43,32 @@ describe('GOVUK.Analytics', function () { expect(universalSetupArguments[1]).toEqual(['set', 'anonymizeIp', true]) expect(universalSetupArguments[2]).toEqual(['set', 'allowAdFeatures', false]) }) + + it('sets usage consent cookie to false when cookie_consent query string parameter has a value of "reject"', function () { + var location = { + protocol: 'https:', + hostname: 'site.example.com', + href: 'https://site.example.com/a/path?cookie_consent=reject', + search: '?cookie_consent=reject', + origin: 'https://site.example.com' + } + GOVUK.Analytics.checkDigitalIdentityConsent(location) + expect(GOVUK.getCookie('cookies_preferences_set')).toBe('true') + expect(GOVUK.getConsentCookie().usage).toBe(false) + }) + + it('sets usage consent cookie to true when cookie_consent query string parameter has a value of "accept"', function () { + var location = { + protocol: 'https:', + hostname: 'site.example.com', + href: 'https://site.example.com/a/path?cookie_consent=accept', + search: '?cookie_consent=accept', + origin: 'https://site.example.com' + } + GOVUK.Analytics.checkDigitalIdentityConsent(location) + expect(GOVUK.getCookie('cookies_preferences_set')).toBe('true') + expect(GOVUK.getConsentCookie().usage).toBe(true) + }) }) describe('extracting the default path for a page view', function () {