Skip to content

Commit

Permalink
Use cookie consent in event tracker
Browse files Browse the repository at this point in the history
- update module to have a delayed start if consent has not been given
- if consent is given, the cookie-consent event fires and the module is immediately started (no need for page reload)
  • Loading branch information
andysellick committed Sep 8, 2022
1 parent c2ab4bf commit 49711a3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
}

Ga4EventTracker.prototype.init = function () {
var consentCookie = window.GOVUK.getConsentCookie()

if (consentCookie && consentCookie.settings) {
this.startModule()
} else {
this.startModule = this.startModule.bind(this)
window.addEventListener('cookie-consent', this.startModule)
}
}

// triggered by cookie-consent event, which happens when users consent to cookies
Ga4EventTracker.prototype.startModule = function () {
if (window.dataLayer) {
this.module.addEventListener('click', this.trackClick.bind(this), true) // useCapture must be true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,65 @@ describe('Google Analytics event tracking', function () {
var element
var expected

function agreeToCookies () {
GOVUK.setCookie('cookies_policy', '{"essential":true,"settings":true,"usage":true,"campaigns":true}')
}

function denyCookies () {
GOVUK.setCookie('cookies_policy', '{"essential":false,"settings":false,"usage":false,"campaigns":false}')
}

beforeEach(function () {
window.dataLayer = []
element = document.createElement('div')
agreeToCookies()
})

afterEach(function () {
document.body.removeChild(element)
})

describe('when the user has a cookie consent choice', function () {
it('starts the module if consent has already been given', function () {
agreeToCookies()
document.body.appendChild(element)
var tracker = new GOVUK.Modules.Ga4EventTracker(element)
spyOn(tracker, 'trackClick')
tracker.init()

element.click()
expect(tracker.trackClick).toHaveBeenCalled()
})

it('starts the module on the same page as cookie consent is given', function () {
denyCookies()
document.body.appendChild(element)
var tracker = new GOVUK.Modules.Ga4EventTracker(element)
spyOn(tracker, 'trackClick')
tracker.init()

element.click()
expect(tracker.trackClick).not.toHaveBeenCalled()

// page has not been reloaded, user consents to cookies
window.GOVUK.triggerEvent(window, 'cookie-consent')

element.click()
expect(tracker.trackClick).toHaveBeenCalled()
})

it('does not do anything if consent is not given', function () {
denyCookies()
document.body.appendChild(element)
var tracker = new GOVUK.Modules.Ga4EventTracker(element)
spyOn(tracker, 'trackClick')
tracker.init()

element.click()
expect(tracker.trackClick).not.toHaveBeenCalled()
})
})

describe('configuring tracking without any data', function () {
beforeEach(function () {
element.setAttribute('data-ga4', '')
Expand Down

0 comments on commit 49711a3

Please sign in to comment.