/
ga4-page-views.js
225 lines (194 loc) · 9.31 KB
/
ga4-page-views.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
window.GOVUK = window.GOVUK || {}
window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}
window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analyticsModules || {};
(function (analyticsModules) {
'use strict'
var PageViewTracker = {
PIIRemover: new window.GOVUK.analyticsGa4.PIIRemover(), // imported in analytics-ga4.js
nullValue: undefined,
init: function (referrer) {
if (window.dataLayer) {
var data = {
event: 'page_view',
page_view: {
location: this.getLocation(),
/* If the init() function receives a referrer parameter, this indicates that it has been called as a part of an AJAX request and
this.getReferrer() will not return the correct value. Therefore we need to rely on the referrer parameter. */
referrer: this.getReferrer(referrer),
title: this.getTitle(),
status_code: this.getStatusCode(),
viewport_size: this.getViewPort(),
ab_test: this.getMetaContent('ab-test'),
document_type: this.getMetaContent('format'),
publishing_app: this.getMetaContent('publishing-app'),
rendering_app: this.getMetaContent('rendering-app'),
schema_name: this.getMetaContent('schema-name'),
content_id: this.getMetaContent('content-id'),
browse_topic: this.getMetaContent('ga4-browse-topic'),
navigation_page_type: this.getMetaContent('navigation-page-type'),
navigation_list_type: this.getMetaContent('navigation-list-type'),
step_navs: this.getMetaContent('stepnavs'),
taxonomy_level1: this.getMetaContent('themes'),
taxonomy_main: this.getMetaContent('taxon-slug'),
taxonomy_main_id: this.getMetaContent('taxon-id'),
taxonomy_all: this.getMetaContent('taxon-slugs'),
taxonomy_all_ids: this.getMetaContent('taxon-ids'),
language: this.getLanguage(),
history: this.getHistory(),
withdrawn: this.getWithDrawn(),
first_published_at: this.stripTimeFrom(this.getMetaContent('first-published-at')),
updated_at: this.stripTimeFrom(this.getMetaContent('updated-at')),
public_updated_at: this.stripTimeFrom(this.getMetaContent('public-updated-at')),
publishing_government: this.removeHyphensAndDowncase(this.getMetaContent('publishing-government') || this.getMetaContent('ga4-publishing-government')),
political_status: this.getMetaContent('political-status') || this.getMetaContent('ga4-political-status'),
primary_publishing_organisation: this.getMetaContent('primary-publishing-organisation'),
organisations: this.getMetaContent('analytics:organisations'),
world_locations: this.getMetaContent('analytics:world-locations'),
/* The existence of a referrer parameter indicates that the page has been dynamically updated via an AJAX request
and therefore we can use it to set the dynamic property appropriately. This value is used by PA's to differentiate
between fresh page loads and dynamic page updates. */
dynamic: referrer ? 'true' : 'false',
emergency_banner: document.querySelector('[data-ga4-emergency-banner]') ? 'true' : undefined,
phase_banner: this.getElementAttribute('data-ga4-phase-banner') || undefined,
devolved_nations_banner: this.getElementAttribute('data-ga4-devolved-nations-banner') || undefined,
cookie_banner: this.getBannerPresence('[data-ga4-cookie-banner]'),
intervention: this.getBannerPresence('[data-ga4-intervention-banner]'),
query_string: this.getQueryString(),
search_term: this.getSearchTerm(),
spelling_suggestion: this.getMetaContent('spelling-suggestion'),
discovery_engine_attribution_token: this.getMetaContent('discovery-engine-attribution-token')
}
}
window.GOVUK.analyticsGa4.core.sendData(data)
}
},
removeHyphensAndDowncase: function (text) {
if (text) {
return text.replace(/-/g, ' ').toLowerCase()
}
},
getLocation: function () {
return this.PIIRemover.stripPIIWithOverride(this.stripGaParam(document.location.href), true, true)
},
getSearchTerm: function () {
// Using the data-attribute has the benefit that none of the characters are URI encoded i.e. [] remains [] instead of %5B%5D.
var searchTerm = this.getElementAttribute('data-ga4-search-query')
/*
Fallback to the keywords URL parameter if the attribute does not exist.
There is a small downside to this method. If the user uses an & in their search term, it breaks as we can't determine
if this & is a part of their search term or the start of another query parameter.
The potential solution may be to URL encode the keywords parameter in finder-frontend.
*/
if (!searchTerm) {
var queryString = window.GOVUK.analyticsGa4.core.trackFunctions.getSearch()
searchTerm = queryString.match(/keywords=([^&]*)/)
if (!searchTerm) {
return undefined
}
searchTerm = searchTerm[0].replace('keywords=', '')
searchTerm = decodeURIComponent(searchTerm).replace(/'"'/g, '"')
}
searchTerm = window.GOVUK.analyticsGa4.core.trackFunctions.standardiseSearchTerm(searchTerm)
return searchTerm
},
getQueryString: function () {
var queryString = window.GOVUK.analyticsGa4.core.trackFunctions.getSearch()
if (queryString) {
queryString = this.stripGaParam(queryString)
queryString = this.PIIRemover.stripPIIWithOverride(queryString, true, true)
queryString = queryString.substring(1) // removes the '?' character from the start.
return queryString
}
},
getReferrer: function (referrer) {
referrer = this.stripGaParam(referrer || document.referrer)
return this.PIIRemover.stripPIIWithOverride(referrer, true, true)
},
// remove GA parameters of the form _ga=2320.021-012302 or _gl=02.10320.01230-123
stripGaParam: function (str) {
str = str.replace(/(_ga=[0-9.-]+)/g, '_ga=[id]')
str = str.replace(/(_gl=[a-zA-Z0-9._\-*]+)/g, '_gl=[id]')
return str
},
getTitle: function () {
return this.PIIRemover.stripPIIWithOverride(document.title, true, true)
},
// window.httpStatusCode is set in the source of the error page in static
// 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()
} else {
return '200'
}
},
getViewPort: function () {
var vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
var vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
return vw + 'x' + vh
},
getMetaContent: function (name) {
var tag = document.querySelector('meta[name="govuk:' + name + '"]')
if (tag) {
var contentAttribute = tag.getAttribute('content')
if (contentAttribute === '') {
return undefined
}
return contentAttribute
} else {
return this.nullValue
}
},
getElementAttribute: function (attributeName) {
var el = document.querySelector('[' + attributeName + ']')
if (el) {
return el.getAttribute(attributeName)
}
},
getLanguage: function () {
var content = document.getElementById('content')
var html = document.querySelector('html')
if (content) {
var contentLanguage = content.getAttribute('lang')
if (contentLanguage) {
return contentLanguage
}
}
// html.getAttribute('lang') is untested - Jasmine would not allow lang to be set on <html>.
return html.getAttribute('lang') || this.nullValue
},
getHistory: function () {
var history = this.getMetaContent('content-has-history')
return (history === 'true') ? 'true' : 'false'
},
getWithDrawn: function () {
var withdrawn = this.getMetaContent('withdrawn')
return (withdrawn === 'withdrawn') ? 'true' : 'false'
},
getBannerPresence: function (bannerSelector) {
/* If the user hides the banner using JS, a cookie is set to hide it on future page loads.
* Therefore we need to start the banner module early so that it hides if this cookie exists.
* Without this, our pageview object will track the banner as visible before it gets hidden. */
var banner = document.querySelector(bannerSelector)
if (banner) {
window.GOVUK.modules.start(banner)
var bannerHidden = banner.getAttribute('hidden') === '' || banner.getAttribute('hidden')
if (bannerHidden) {
return undefined
}
return 'true'
}
return undefined
},
// return only the date from given timestamps of the form
// 2022-03-28T19:11:00.000+00:00
stripTimeFrom: function (value) {
if (value !== undefined) {
return value.split('T')[0]
} else {
return this.nullValue
}
}
}
analyticsModules.PageViewTracker = PageViewTracker
})(window.GOVUK.analyticsGa4.analyticsModules)