Skip to content

Commit

Permalink
Merge pull request #3228 from alphagov/add-analytics-modules-error-check
Browse files Browse the repository at this point in the history
Add GA4 modules error checking
  • Loading branch information
andysellick authored Feb 2, 2023
2 parents 22c9b67 + 5ba385e commit 32787e8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

## Unreleased

* Add GA4 modules error checking ([PR #3228](https://github.com/alphagov/govuk_publishing_components/pull/3228))
* Add classes to align text in Govspeak tables ([PR #3217](https://github.com/alphagov/govuk_publishing_components/pull/3217))
* Make links bold at all viewports on navbar menu ([PR #3219](https://github.com/alphagov/govuk_publishing_components/pull/3219))
* Add our 'assets' domain as a domain that should run our analytics ([PR #3224](https://github.com/alphagov/govuk_publishing_components/pull/3224))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ window.GOVUK = window.GOVUK || {}
window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}

var initFunction = function () {
// to be added: digital identity consent mechanism

var consentCookie = window.GOVUK.getConsentCookie()

if (consentCookie && consentCookie.usage) {
Expand All @@ -16,10 +14,14 @@ var initFunction = function () {
for (var property in analyticsModules) {
var module = analyticsModules[property]
if (typeof module.init === 'function') {
module.init()
try {
module.init()
} catch (e) {
// if there's a problem with the module, catch the error to allow other modules to start
console.warn('Error starting analytics module ' + property + ': ' + e.message, window.location)
}
}
}
// to be added: cross domain tracking code
} else {
window.addEventListener('cookie-consent', window.GOVUK.analyticsGa4.init)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ describe('Initialising GA4', function () {

describe('when consent is given', function () {
var test = {
functionThatMightBeCalled: function () {}
functionThatMightBeCalled: function () {},
functionThatShouldNotBeCalled: function () {}
}

beforeEach(function () {
spyOn(test, 'functionThatMightBeCalled')
spyOn(test, 'functionThatShouldNotBeCalled')
GOVUK.analyticsGa4.analyticsModules.Test = function () {}
GOVUK.analyticsGa4.analyticsModules.Test.init = function () { test.functionThatMightBeCalled() }
})
Expand Down Expand Up @@ -46,6 +48,27 @@ describe('Initialising GA4', function () {

expect(GOVUK.analyticsGa4).not.toEqual({})
})

it('initialises following modules even if this one errors', function () {
// module with a deliberate error in it
GOVUK.analyticsGa4.analyticsModules.TestError = function () {}
GOVUK.analyticsGa4.analyticsModules.TestError.init = function () {
throw new Error('This is a deliberate error')
test.functionThatShouldNotBeCalled() // eslint-disable-line no-unreachable
}

GOVUK.analyticsGa4.analyticsModules.TestNotError = function () {}
GOVUK.analyticsGa4.analyticsModules.TestNotError.init = function () { test.functionThatMightBeCalled() }

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

expect(test.functionThatShouldNotBeCalled.calls.count()).toEqual(0)
expect(test.functionThatMightBeCalled.calls.count()).toEqual(2)

delete GOVUK.analyticsGa4.analyticsModules.TestError
delete GOVUK.analyticsGa4.analyticsModules.TestNotError
})
})

describe('Modules depending on cookie consent to run', function () {
Expand Down

0 comments on commit 32787e8

Please sign in to comment.