govuk_publishing_components 31.1.0 → 31.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23492ecba6eaaa3ed895868bf827524702313449a11c8ca79eed1e0d25565122
4
- data.tar.gz: fddbfc77800da9f9999b164930528661bffaed54e70849e14d6ef9e1b1ab549d
3
+ metadata.gz: 6228168725fc509c3b6313611fb75d4700597c685bb059c58ab4058173adfee5
4
+ data.tar.gz: 4ecefeead36ad35884f76edc0cb679e8449b300e08b95bf05ef9a1bd2ba9c7fe
5
5
  SHA512:
6
- metadata.gz: f40277a05d20137b4695853f5c2a80bfab0ee90fb01ccf73d961e4e4c3e59a3c31c917022ed048c5e8ceff38bc1cc5a8fd9d62ceb5c5b7eef57893c30a3d4257
7
- data.tar.gz: 0dc8abb57df600b8309abf46571ccb864a41b82f3f7b4cf6a2e3dcab4124777719d017719a87f51dde41855472b9d15b9920040a94a267acdf56d8222b51a472
6
+ metadata.gz: 60b6aa5e85acf22e1bb663dc8e87d626f60ded689eb6809ec269868a4dba96bd1c41ae8766a30f8f97e27a5b56ef5044e65db4a2fee5814d150de6aa9731bd68
7
+ data.tar.gz: 06a0a8bd5ca1a14ff13bb0aec9869dba18d3e0e9942d48a712befde02c6738c7b62a2ca9e300f5c3ae320a86adfe507faa74598d74a76740b9da5fbcbf898200
@@ -22,6 +22,7 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};
22
22
  } else {
23
23
  // initialise GTM
24
24
  window.dataLayer = window.dataLayer || []
25
+ window.dataLayer.push({ 'gtm.blocklist': ['customPixels', 'customScripts', 'html', 'nonGoogleScripts'] })
25
26
  window.dataLayer.push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' })
26
27
 
27
28
  var auth = window.GOVUK.analyticsGa4.vars.auth || ''
@@ -36,7 +37,6 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};
36
37
  this.googleSrc = 'https://www.googletagmanager.com/gtm.js?id=' + window.GOVUK.analyticsGa4.vars.id + auth + preview
37
38
  newScript.src = this.googleSrc
38
39
  firstScript.parentNode.insertBefore(newScript, firstScript)
39
- window.dataLayer.push({ 'gtm.blocklist': ['customPixels', 'customScripts', 'html', 'nonGoogleScripts'] })
40
40
  }
41
41
  },
42
42
 
@@ -44,17 +44,15 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
44
44
  return
45
45
  }
46
46
 
47
- var clickData = {}
48
47
  var href = element.getAttribute('href')
49
48
 
50
49
  if (!href) {
51
50
  return
52
51
  }
53
-
52
+ var clickData = {}
54
53
  var linkAttributes = element.getAttribute('data-ga4-link')
55
54
  if (linkAttributes) {
56
- linkAttributes = JSON.parse(linkAttributes)
57
- clickData = window.GOVUK.extendObject(clickData, linkAttributes)
55
+ clickData = JSON.parse(linkAttributes)
58
56
 
59
57
  /* Since external links can't be determined in the template, we use populated-via-js as a signal
60
58
  for our JavaScript to determine this value. */
@@ -62,41 +60,47 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
62
60
  clickData.external = this.isExternalLink(clickData.url) ? 'true' : 'false'
63
61
  }
64
62
 
65
- if (clickData.link_method === 'populated-via-js') {
66
- clickData.link_method = this.getClickType(event)
63
+ if (clickData.method === 'populated-via-js') {
64
+ clickData.method = this.getClickType(event)
67
65
  }
68
66
 
69
67
  if (clickData.index) {
70
- clickData.index = parseInt(linkAttributes.index)
68
+ clickData.index = parseInt(clickData.index)
71
69
  }
72
70
 
73
71
  if (clickData.index_total) {
74
- clickData.index_total = parseInt(linkAttributes.index_total)
72
+ clickData.index_total = parseInt(clickData.index_total)
75
73
  }
76
74
  } else if (this.isMailToLink(href)) {
77
75
  clickData.event_name = 'navigation'
78
76
  clickData.type = 'email'
79
77
  clickData.external = 'true'
80
78
  clickData.url = href
81
- clickData.text = element.textContent.trim()
82
- clickData.link_method = this.getClickType(event)
79
+ clickData.text = this.removeLinesAndExtraSpaces(element.textContent)
80
+ clickData.method = this.getClickType(event)
83
81
  } else if (this.isDownloadLink(href)) {
84
82
  clickData.event_name = 'file_download'
85
83
  clickData.type = this.isPreviewLink(href) ? 'preview' : 'generic download'
86
84
  clickData.external = this.isExternalLink(href) ? 'true' : 'false'
87
85
  clickData.url = href
88
- clickData.text = element.textContent.trim()
89
- clickData.link_method = this.getClickType(event)
86
+ clickData.text = this.removeLinesAndExtraSpaces(element.textContent)
87
+ clickData.method = this.getClickType(event)
90
88
  } else if (this.isExternalLink(href)) {
91
89
  clickData.event_name = 'navigation'
92
90
  clickData.type = 'generic link'
93
91
  clickData.external = 'true'
94
92
  clickData.url = href
95
- clickData.text = element.textContent.trim()
96
- clickData.link_method = this.getClickType(event)
93
+ clickData.text = this.removeLinesAndExtraSpaces(element.textContent)
94
+ clickData.method = this.getClickType(event)
97
95
  }
98
96
 
99
97
  if (Object.keys(clickData).length > 0) {
98
+ if (clickData.url) {
99
+ clickData.url = this.removeCrossDomainParams(clickData.url)
100
+ clickData.link_domain = this.populateLinkDomain(clickData.url)
101
+ clickData.link_path_parts = this.populateLinkPathParts(clickData.url)
102
+ }
103
+
100
104
  var schema = new window.GOVUK.analyticsGa4.Schemas().eventSchema()
101
105
  schema.event = 'event_data'
102
106
 
@@ -112,6 +116,49 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
112
116
  }
113
117
  },
114
118
 
119
+ populateLinkPathParts: function (href) {
120
+ var path = ''
121
+ if (this.hrefIsRelative(href) || this.isMailToLink(href)) {
122
+ path = href
123
+ } else {
124
+ // This regex matches a protocol and domain name at the start of a string such as https://www.gov.uk, http://gov.uk, //gov.uk
125
+ path = href.replace(/^(http:||https:)?(\/\/)([^\/]*)/, '') // eslint-disable-line no-useless-escape
126
+ }
127
+
128
+ if (path === '/' || path.length === 0) {
129
+ return
130
+ }
131
+
132
+ /*
133
+ This will create an object with 5 keys that are indexes ("1", "2", etc.)
134
+ The values will be each part of the link path split every 100 characters, or undefined.
135
+ For example: {"1": "/hello/world/etc...", "2": "/more/path/text...", "3": undefined, "4": undefined, "5": undefined}
136
+ Undefined values are needed to override the persistent object in GTM so that any values from old pushes are overwritten.
137
+ */
138
+ var parts = path.match(/.{1,100}/g)
139
+ var obj = {}
140
+ for (var i = 0; i < 5; i++) {
141
+ obj[(i + 1).toString()] = parts[i]
142
+ }
143
+ return obj
144
+ },
145
+
146
+ populateLinkDomain: function (href) {
147
+ // We always want mailto links to have an undefined link_domain
148
+ if (this.isMailToLink(href)) {
149
+ return undefined
150
+ }
151
+
152
+ if (this.hrefIsRelative(href)) {
153
+ return this.getProtocol() + '//' + this.getHostname()
154
+ } else {
155
+ // This regex matches a protocol and domain name at the start of a string such as https://www.gov.uk, http://gov.uk, //gov.uk
156
+ var domainRegex = /^(http:||https:)?(\/\/)([^\/]*)/ // eslint-disable-line no-useless-escape
157
+ var domain = domainRegex.exec(href)[0]
158
+ return domain
159
+ }
160
+ },
161
+
115
162
  appendDomainsWithoutWWW: function (domainsArrays) {
116
163
  // Add domains with www. removed, in case site hrefs are marked up without www. included.
117
164
  for (var i = 0; i < domainsArrays.length; i++) {
@@ -123,6 +170,13 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
123
170
  }
124
171
  },
125
172
 
173
+ removeLinesAndExtraSpaces: function (text) {
174
+ text = text.trim()
175
+ text = text.replace(/(\r\n|\n|\r)/gm, ' ') // Replace line breaks with 1 space
176
+ text = text.replace(/\s+/g, ' ') // Replace instances of 2+ spaces with 1 space
177
+ return text
178
+ },
179
+
126
180
  getClickType: function (event) {
127
181
  switch (event.type) {
128
182
  case 'click':
@@ -236,6 +290,10 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
236
290
  return string.substring(0, stringToFind.length) === stringToFind
237
291
  },
238
292
 
293
+ stringEndsWith: function (string, stringToFind) {
294
+ return string.substring(string.length - stringToFind.length, string.length) === stringToFind
295
+ },
296
+
239
297
  hrefIsRelative: function (href) {
240
298
  // Checks that a link is relative, but is not a protocol relative url
241
299
  return href[0] === '/' && href[1] !== '/'
@@ -247,6 +305,25 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
247
305
 
248
306
  getHostname: function () {
249
307
  return window.location.hostname
308
+ },
309
+
310
+ getProtocol: function () {
311
+ return window.location.protocol
312
+ },
313
+
314
+ removeCrossDomainParams: function (href) {
315
+ if (href.indexOf('_ga') !== -1 || href.indexOf('_gl') !== -1) {
316
+ // _ga & _gl are values needed for cross domain tracking, but we don't want them included in our click tracking.
317
+ href = href.replaceAll(/_g[al]=([^&]*)/g, '')
318
+
319
+ // The following code cleans up inconsistencies such as gov.uk/&&, gov.uk/?&hello=world, gov.uk/?, and gov.uk/&.
320
+ href = href.replaceAll(/(&&)+/g, '&')
321
+ href = href.replace('?&', '?')
322
+ if (this.stringEndsWith(href, '?') || this.stringEndsWith(href, '&')) {
323
+ href = href.substring(0, href.length - 1)
324
+ }
325
+ }
326
+ return href
250
327
  }
251
328
  }
252
329
 
@@ -21,7 +21,9 @@
21
21
  section: this.undefined,
22
22
  action: this.undefined,
23
23
  external: this.undefined,
24
- link_method: this.undefined
24
+ method: this.undefined,
25
+ link_domain: this.undefined,
26
+ link_path_parts: this.undefined
25
27
  }
26
28
  }
27
29
  }