Add Google Tag Manager for analytics (#541)

This commit is contained in:
Steven Reilly
2023-06-12 13:52:38 -04:00
committed by GitHub
parent d98a8ceada
commit da623c87d6
8 changed files with 18 additions and 364 deletions

View File

@@ -1,127 +0,0 @@
const helpers = require('../support/helpers');
beforeAll(() => {
// add the script GA looks for in the document
document.body.appendChild(document.createElement('script'));
require('../../../app/assets/javascripts/govuk/cookie-functions.js');
require('../../../app/assets/javascripts/analytics/analytics.js');
require('../../../app/assets/javascripts/analytics/init.js');
});
afterAll(() => {
require('../support/teardown.js');
});
describe("Analytics", () => {
let analytics;
beforeEach(() => {
window.ga = jest.fn();
analytics = new GOVUK.Analytics({
trackingId: 'UA-75215134-1',
cookieDomain: 'auto',
anonymizeIp: true,
allowAdFeatures: false,
transport: 'beacon',
expires: 365
});
});
afterEach(() => {
window.ga.mockClear();
});
describe("When created", () => {
test("It configures a tracker", () => {
setUpArguments = window.ga.mock.calls;
expect(setUpArguments[0]).toEqual(['create', { 'trackingId': 'UA-75215134-1', 'cookieDomain': 'auto', 'cookieExpires': 31536000 }]);
expect(setUpArguments[1]).toEqual(['set', 'anonymizeIp', true]);
expect(setUpArguments[2]).toEqual(['set', 'allowAdFeatures', false]);
expect(setUpArguments[3]).toEqual(['set', 'transport', 'beacon']);
expect(setUpArguments[4]).toEqual(['set', 'title', 'U.S. Notify']);
});
});
describe("When tracking pageviews", () => {
beforeEach(() => {
// clear calls to window.ga from set up
window.ga.mockClear();
});
test("It sends the right URL for the page if no arguments", () => {
jest.spyOn(window, 'location', 'get').mockImplementation(() => {
return {
'pathname': '/privacy',
'search': ''
};
});
analytics.trackPageview();
expect(window.ga.mock.calls[0]).toEqual(['send', 'pageview', '/privacy']);
});
test("It strips the UUIDs from URLs", () => {
jest.spyOn(window, 'location', 'get').mockImplementation(() => {
return {
'pathname': '/services/6658542f-0cad-491f-bec8-ab8457700ead',
'search': ''
};
});
analytics.trackPageview();
expect(window.ga.mock.calls[0]).toEqual(['send', 'pageview', '/services/…']);
});
});
describe("When tracking events", () => {
beforeEach(() => {
// clear calls to window.ga from set up
window.ga.mockClear();
});
test("It sends the right arguments to `ga`", () => {
analytics.trackEvent('Error', 'Enter a valid email address', {
'label': 'email_address'
});
expect(window.ga.mock.calls[0]).toEqual(['send', 'event', {
'eventCategory': 'Error',
'eventAction': 'Enter a valid email address',
'eventLabel': 'email_address'
}]);
});
});
});

View File

@@ -1,123 +0,0 @@
const helpers = require('../support/helpers');
beforeAll(() => {
// add the script GA looks for in the document
document.body.appendChild(document.createElement('script'));
require('../../../app/assets/javascripts/govuk/cookie-functions.js');
require('../../../app/assets/javascripts/analytics/analytics.js');
require('../../../app/assets/javascripts/analytics/init.js');
});
afterAll(() => {
require('../support/teardown.js');
});
describe("Analytics init", () => {
beforeAll(() => {
window.ga = jest.fn();
jest.spyOn(window.GOVUK.Analytics, 'load');
// pretend we're on the /privacy page
jest.spyOn(window, 'location', 'get').mockImplementation(() => {
return {
'pathname': '/privacy',
'search': ''
};
});
});
afterEach(() => {
window.GOVUK.Analytics.load.mockClear();
window.ga.mockClear();
});
test("After the init.js script has been loaded, Google Analytics will be disabled", () => {
expect(window['ga-disable-UA-75215134-1']).toBe(true);
});
describe("If initAnalytics has already been called", () => {
beforeAll(() => {
// Fake a tracker instance
window.GOVUK.analytics = {};
});
beforeEach(() => {
window.GOVUK.initAnalytics();
});
afterAll(() => {
delete window.GOVUK.analytics;
});
test("The Google Analytics libraries will not be loaded", () => {
expect(window.GOVUK.Analytics.load).not.toHaveBeenCalled();
});
});
describe("If initAnalytics has not been called", () => {
beforeEach(() => {
window.GOVUK.initAnalytics();
});
afterEach(() => {
// window.GOVUK.initAnalytics sets up a new window.GOVUK.analytics which needs clearing
delete window.GOVUK.analytics;
});
test("Google Analytics will not be disabled", () => {
expect(window['ga-disable-UA-75215134-1']).toBe(false);
});
test("The Google Analytics libraries will have been loaded", () => {
expect(window.GOVUK.Analytics.load).toHaveBeenCalled();
});
test("There will be an interface with the Google Analytics API", () => {
expect(window.GOVUK.analytics).toBeDefined();
});
test("A pageview will be registered", () => {
expect(window.ga.mock.calls.length).toEqual(6);
// The first 5 calls configure the analytics tracker. All subsequent calls send data
expect(window.ga.mock.calls[5]).toEqual(['send', 'pageview', '/privacy']);
});
});
});