Skip to content

Commit

Permalink
Merge pull request #739 from alphagov/PP-12560_upgrade_to_GA4
Browse files Browse the repository at this point in the history
[WIP - DO NOT MERGE]PP-12560 upgrade to GA4
  • Loading branch information
hjvoid committed Jun 21, 2024
2 parents 64ce0b0 + 6dddb8a commit ae0e5a8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 169 deletions.
49 changes: 16 additions & 33 deletions source/javascripts/analytics/analytics.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
window.GovUkPay = window.GovUkPay || {}
window.GovUkPay.Analytics = (function () {
// Stripped-down wra§pper for Google Analytics, based on:
// https://github.com/alphagov/static/blob/master/doc/analytics.md
SetupAnalytics = function (config) {
window.ga('create', config.trackingId, config.cookieDomain)
window.ga('set', 'anonymizeIp', config.anonymizeIp)
window.ga('set', 'displayFeaturesTask', config.displayFeaturesTask)
window.ga('set', 'transport', config.transport)
}

LoadGoogleAnalytics = function () {
/* eslint-disable */
// Copied from Google Analytics installation instructions
/* jshint ignore:start */
;(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r
;(i[r] =
i[r] ||
function () {
;(i[r].q = i[r].q || []).push(arguments)
}),
(i[r].l = 1 * new Date())
;(a = s.createElement(o)), (m = s.getElementsByTagName(o)[0])
a.async = 1
a.src = g
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga')
/* jshint ignore:end */
}
var gtagScript = document.createElement('script')
gtagScript.async = true
gtagScript.setAttribute('src', 'https://www.googletagmanager.com/gtag/js?id=G-XE9K05CFFE')
document.head.appendChild(gtagScript)

TrackPageview = function (path, title, options) {
var page = window.location.pathname + window.location.search
window.ga('send', 'pageview', page)
}
window.dataLayer = window.dataLayer || []

// Disabling eslint as the Google snippet conflicts with the our linting rules
/* eslint-disable */
function gtag() {
dataLayer.push(arguments)
}
/* eslint-enable */
gtagScript.onload = function () {
gtag('js', new Date())
gtag('config', 'G-XE9K05CFFE')
}
}
return {
TrackPageview: TrackPageview,
LoadGoogleAnalytics: LoadGoogleAnalytics,
SetupAnalytics: SetupAnalytics,
}
})()
86 changes: 0 additions & 86 deletions source/javascripts/analytics/analytics.test.js

This file was deleted.

17 changes: 0 additions & 17 deletions source/javascripts/analytics/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,8 @@

window.GovUkPay = window.GovUkPay || {}
window.GovUkPay.InitAnalytics = (function () {
// TODO: Remove hard coded tracking IDs to make this more generic and useful to others
var TRACKING_ID = 'UA-72121642-9'

InitialiseAnalytics = function () {
window.GovUkPay.Analytics.LoadGoogleAnalytics()

// Configure profiles and make interface public
// for custom dimensions, virtual pageviews and events
window.GovUkPay.analytics = window.GovUkPay.Analytics.SetupAnalytics({
trackingId: TRACKING_ID,
cookieDomain: window.GovUkPay.Cookie.getCookieDomain(),
anonymizeIp: true,
displayFeaturesTask: null,
transport: 'beacon',
expires: 365,
})

// Track initial pageview
window.GovUkPay.Analytics.TrackPageview()
}

return {
Expand Down
33 changes: 0 additions & 33 deletions source/javascripts/analytics/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,9 @@
jest.mock('./analytics')

beforeAll(() => {
// add the script GA looks for in the document
document.body.appendChild(document.createElement('script'))

// pretend we're on the /privacy-notice page
jest.spyOn(window, 'location', 'get').mockImplementation(() => {
return {
pathname: '/privacy-notice',
search: '',
}
})

require('./init')
window.GovUkPay.Analytics = {
LoadGoogleAnalytics: jest.fn(),
SetupAnalytics: jest.fn(),
TrackPageview: jest.fn(),
}

window.GovUkPay.Cookie = {
getCookieDomain: jest.fn(),
}
})

Expand All @@ -37,21 +20,5 @@ describe('InitialiseAnalytics component', () => {
it('the Google Analytics libraries will have been loaded', () => {
expect(window.GovUkPay.Analytics.LoadGoogleAnalytics).toHaveBeenCalled()
})

it('the Analytics tracker will have been configured', () => {
expect(window.GovUkPay.Analytics.SetupAnalytics).toHaveBeenCalledWith(
expect.objectContaining({
// Do not assert cookieDomain or trackingId for now
anonymizeIp: true,
displayFeaturesTask: null,
transport: 'beacon',
expires: 365,
})
)
})

it('fires an initial trackPageview', () => {
expect(window.GovUkPay.Analytics.TrackPageview).toHaveBeenCalled()
})
})
})

0 comments on commit ae0e5a8

Please sign in to comment.