Skip to content

Commit

Permalink
Merge pull request #2340 from alphagov/msw/accept-di-cookie-consent
Browse files Browse the repository at this point in the history
Accept cookie consent from Digital Identity query parameter
  • Loading branch information
danacotoran authored Oct 8, 2021
2 parents dadb679 + 510201a commit 82e5eba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 82e5eba

Please sign in to comment.