From 49711a34d756b62d0c6b3f043f5b7889d6532218 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Tue, 6 Sep 2022 15:09:19 +0100 Subject: [PATCH] Use cookie consent in event tracker - 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) --- .../analytics-ga4/ga4-event-tracker.js | 12 +++++ .../analytics-ga4/ga4-event-tracker.spec.js | 50 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-event-tracker.js b/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-event-tracker.js index 496db6a103..b600a76b28 100644 --- a/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-event-tracker.js +++ b/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-event-tracker.js @@ -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 } diff --git a/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-event-tracker.spec.js b/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-event-tracker.spec.js index 3b9b37685b..094a47696e 100644 --- a/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-event-tracker.spec.js +++ b/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-event-tracker.spec.js @@ -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', '')