Skip to content

Commit

Permalink
Convert page views to new format
Browse files Browse the repository at this point in the history
- update page views code to use the new format for GA4 code initialisation
  • Loading branch information
andysellick committed Sep 8, 2022
1 parent d39995f commit c2ab4bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
;(function (global) {
'use strict'
window.GOVUK = window.GOVUK || {}
window.GOVUK.analyticsGA4 = window.GOVUK.analyticsGA4 || {}
window.GOVUK.analyticsGA4.analyticsModules = window.GOVUK.analyticsGA4.analyticsModules || {};

var GOVUK = global.GOVUK || {}
GOVUK.analyticsGA4 = GOVUK.analyticsGA4 || {}
(function (analyticsModules) {
'use strict'

GOVUK.analyticsGA4.pageViewTracker = {
PIIRemover: new GOVUK.analyticsGA4.PIIRemover(), // imported in analytics-ga4.js
var PageViewTracker = {
PIIRemover: new window.GOVUK.analyticsGA4.PIIRemover(), // imported in analytics-ga4.js
nullValue: null,

sendPageView: function () {
init: function () {
if (window.dataLayer) {
var data = {
event: 'page_view',
Expand Down Expand Up @@ -61,7 +62,7 @@
},

// window.httpStatusCode is set in the source of the error page in static
// https://github.com/alphagov/static/blob/main/app/views/root/_error_page.html.erb#L32
// https://github.com/alphagov/static/blob/1c734451f2dd6fc0c7e80beccbdcbfa5aaffd0e4/app/views/root/_error_page.html.erb#L41-L43
getStatusCode: function () {
if (window.httpStatusCode) {
return window.httpStatusCode.toString()
Expand Down Expand Up @@ -109,5 +110,5 @@
}
}

global.GOVUK = GOVUK
})(window)
analyticsModules.PageViewTracker = PageViewTracker
})(window.GOVUK.analyticsGA4.analyticsModules)
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ describe('Google Tag Manager page view tracking', function () {
}

it('returns a standard page view', function () {
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a page view with a specific status code', function () {
window.httpStatusCode = 404
expected.page_view.status_code = '404'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Google Tag Manager page view tracking', function () {
expected.page_view[tag.gtmName] = tag.value
}

GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

Expand Down Expand Up @@ -156,7 +156,7 @@ describe('Google Tag Manager page view tracking', function () {
expected.page_view[tag.gtmName] = tag.value
}

GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

Expand All @@ -177,99 +177,99 @@ describe('Google Tag Manager page view tracking', function () {
content.setAttribute('lang', 'wakandan')
expected.page_view.language = 'wakandan'

GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('set incorrectly', function () {
expected.page_view.language = nullValue

GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
})

it('returns a pageview without a language', function () {
expected.page_view.language = nullValue

GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview with history', function () {
createMetaTags('content-has-history', 'true')
expected.page_view.history = 'true'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview without history', function () {
createMetaTags('content-has-history', 'banana')
expected.page_view.history = 'false'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview on a withdrawn page', function () {
createMetaTags('withdrawn', 'withdrawn')
expected.page_view.withdrawn = 'true'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview on a page with a first published date', function () {
createMetaTags('first-published-at', '2022-03-28T19:11:00.000+00:00')
expected.page_view.first_published_at = '2022-03-28'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview on a page with a last updated date', function () {
createMetaTags('updated-at', '2021-03-28T19:11:00.000+00:00')
expected.page_view.updated_at = '2021-03-28'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview on a page with a last public updated date', function () {
createMetaTags('public-updated-at', '2020-03-28T19:11:00.000+00:00')
expected.page_view.public_updated_at = '2020-03-28'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview on a page marked with a publishing government', function () {
createMetaTags('publishing-government', 'labour')
expected.page_view.publishing_government = 'labour'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview on a page marked with a political status', function () {
createMetaTags('political-status', 'ongoing')
expected.page_view.political_status = 'ongoing'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview on a page marked with a primary publishing organisation', function () {
createMetaTags('primary-publishing-organisation', 'Home Office')
expected.page_view.primary_publishing_organisation = 'Home Office'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview on a page marked with ids for contributing organisations', function () {
createMetaTags('analytics:organisations', 'some organisations')
expected.page_view.organisations = 'some organisations'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('returns a pageview on a page marked with world locations', function () {
createMetaTags('analytics:world-locations', 'some world locations')
expected.page_view.world_locations = 'some world locations'
GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

Expand All @@ -294,7 +294,7 @@ describe('Google Tag Manager page view tracking', function () {
linkForURLMock.href = '#example@gov.uk'
linkForURLMock.click()

GOVUK.analyticsGA4.pageViewTracker.sendPageView()
GOVUK.analyticsGA4.analyticsModules.PageViewTracker.init()

// Reset the page location for other tests
linkForURLMock.href = '#'
Expand Down

0 comments on commit c2ab4bf

Please sign in to comment.