govuk_publishing_components 34.5.1 → 34.6.0

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: 00b74c7f41c6927b9df6d0650ab54bcf7450f766c8b1ec4117c7b7f98d88256f
4
- data.tar.gz: 53d9e813a9e4b7e314b91a6726946ff016f05c09cb0717f034fbb81dfe6c7dc2
3
+ metadata.gz: e64d34524575e781292e650596fdb06fc0cf9644329d27aa66fd32e8d0dbdfd5
4
+ data.tar.gz: 757292d30401fc24dc3d99c2f67a5c7fc11f528e531a4e2605004bbd367b1c0c
5
5
  SHA512:
6
- metadata.gz: e58cbdbd7db7ea39b606796243d2df07a462c2d9059bfa5adf0300b67de530474062b26336e23407e81b094a8588a3992ff124dcc7f3d34b65c56cf72e29ca37
7
- data.tar.gz: e73250888c0eeafe7c2666840a78bd72845f9bdbfb6e3adb4c26febc7f96499c7f5239699661d27747bfc2c9e0599a11bd5f8edaeb5147f38412a35f877cf6d9
6
+ metadata.gz: 67e4ebd3c3796d54b3d514b65138f800db901996f39bf2a55559230eb3cd9c475c2ac5344d41b53ad41c0f24f42d6d92a9a90fd772dc2bc06d686e0642c75b4e
7
+ data.tar.gz: 70758cb00c5f1d4726cae67de5036258f1dc5545397d9432227086b46b4ba98bf7f5644712be779c327f9065a693e749e5b42fc3d5288ef7d64166787ede4af5
@@ -6,11 +6,39 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};
6
6
  'use strict'
7
7
 
8
8
  var core = {
9
+ load: function () {
10
+ var firstScript = document.getElementsByTagName('script')[0]
11
+ var newScript = document.createElement('script')
12
+ newScript.async = true
13
+
14
+ // initialise GTM
15
+ window.dataLayer = window.dataLayer || []
16
+ window.dataLayer.push({ 'gtm.blocklist': ['customPixels', 'customScripts', 'html', 'nonGoogleScripts'] })
17
+ window.dataLayer.push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' })
18
+
19
+ var auth = window.GOVUK.analyticsGa4.vars.auth || ''
20
+ var preview = window.GOVUK.analyticsGa4.vars.preview || ''
21
+ if (auth) {
22
+ auth = '&gtm_auth=' + auth
23
+ }
24
+ if (preview) {
25
+ preview = '&gtm_preview=' + preview + '&gtm_cookies_win=x'
26
+ }
27
+
28
+ this.googleSrc = 'https://www.googletagmanager.com/gtm.js?id=' + window.GOVUK.analyticsGa4.vars.id + auth + preview
29
+ newScript.src = this.googleSrc
30
+ firstScript.parentNode.insertBefore(newScript, firstScript)
31
+ },
32
+
9
33
  sendData: function (data) {
10
- data.govuk_gem_version = window.GOVUK.analyticsGa4.vars.gem_version
34
+ data.govuk_gem_version = this.getGemVersion()
11
35
  window.dataLayer.push(data)
12
36
  },
13
37
 
38
+ getGemVersion: function () {
39
+ return window.GOVUK.analyticsGa4.vars.gem_version || 'not found'
40
+ },
41
+
14
42
  trackFunctions: {
15
43
  findTrackingAttributes: function (clicked, trackingTrigger) {
16
44
  if (clicked.hasAttribute('[' + trackingTrigger + ']')) {
@@ -0,0 +1,28 @@
1
+ window.GOVUK = window.GOVUK || {}
2
+ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}
3
+
4
+ var initFunction = function () {
5
+ // to be added: digital identity consent mechanism
6
+
7
+ var consentCookie = window.GOVUK.getConsentCookie()
8
+
9
+ if (consentCookie && consentCookie.usage) {
10
+ window.GOVUK.analyticsGa4.vars.internalDomains = []
11
+ window.GOVUK.analyticsGa4.vars.internalDomains.push(window.GOVUK.analyticsGa4.core.trackFunctions.getHostname())
12
+ window.GOVUK.analyticsGa4.core.trackFunctions.appendDomainsWithoutWWW(window.GOVUK.analyticsGa4.vars.internalDomains)
13
+ window.GOVUK.analyticsGa4.core.load()
14
+
15
+ var analyticsModules = window.GOVUK.analyticsGa4.analyticsModules
16
+ for (var property in analyticsModules) {
17
+ var module = analyticsModules[property]
18
+ if (typeof module.init === 'function') {
19
+ module.init()
20
+ }
21
+ }
22
+ // to be added: cross domain tracking code
23
+ } else {
24
+ window.addEventListener('cookie-consent', window.GOVUK.analyticsGa4.init)
25
+ }
26
+ }
27
+
28
+ window.GOVUK.analyticsGa4.init = initFunction
@@ -7,3 +7,4 @@
7
7
  //= require ./analytics-ga4/ga4-link-tracker
8
8
  //= require ./analytics-ga4/ga4-event-tracker
9
9
  //= require ./analytics-ga4/ga4-ecommerce-tracker
10
+ //= require ./analytics-ga4/init-ga4
@@ -8,5 +8,15 @@ document.addEventListener('DOMContentLoaded', function () {
8
8
  window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}
9
9
  window.GOVUK.analyticsVars = window.GOVUK.analyticsVars || {}
10
10
 
11
+ // if statements ensure these functions don't execute during testing
12
+ if (typeof window.GOVUK.loadAnalytics !== 'undefined') {
13
+ if (typeof window.GOVUK.analyticsGa4.vars === 'undefined') {
14
+ window.GOVUK.loadAnalytics.loadGa4()
15
+ }
16
+ if (typeof window.GOVUK.analyticsVars.gaProperty === 'undefined') {
17
+ window.GOVUK.loadAnalytics.loadUa()
18
+ }
19
+ }
20
+
11
21
  window.GOVUK.modules.start()
12
22
  })
@@ -2,8 +2,6 @@
2
2
  //= require govuk_publishing_components/analytics-ga4
3
3
  //= require govuk_publishing_components/analytics/linked-domains
4
4
 
5
- window.GOVUK = window.GOVUK || {}
6
-
7
5
  window.GOVUK.loadAnalytics = {
8
6
  productionDomains: [
9
7
  'www.gov.uk',
@@ -47,7 +45,6 @@ window.GOVUK.loadAnalytics = {
47
45
  },
48
46
 
49
47
  loadUa: function (currentDomain) {
50
- // The domain can be overridden. This is needed for testing as we need to fake the domain that we are on to test each environment.
51
48
  currentDomain = currentDomain || window.location.hostname
52
49
 
53
50
  // Universal Analytics variables
@@ -57,13 +54,13 @@ window.GOVUK.loadAnalytics = {
57
54
  window.GOVUK.analyticsVars.gaProperty = 'UA-UNSET'
58
55
  window.GOVUK.analyticsVars.gaPropertyCrossDomain = 'UA-UNSET'
59
56
 
60
- if (this.arrayContains({ valueToFind: currentDomain, array: this.productionDomains })) {
57
+ if (this.arrayContains(currentDomain, this.productionDomains)) {
61
58
  window.GOVUK.analyticsVars.gaProperty = 'UA-26179049-1'
62
59
  window.GOVUK.analyticsVars.gaPropertyCrossDomain = 'UA-145652997-1'
63
- } else if (this.arrayContains({ valueToFind: currentDomain, array: this.stagingDomains })) {
60
+ } else if (this.arrayContains(currentDomain, this.stagingDomains)) {
64
61
  window.GOVUK.analyticsVars.gaProperty = 'UA-26179049-20'
65
62
  window.GOVUK.analyticsVars.gaPropertyCrossDomain = 'UA-145652997-1'
66
- } else if (this.arrayContains({ valueToFind: currentDomain, array: this.integrationDomains })) {
63
+ } else if (this.arrayContains(currentDomain, this.integrationDomains)) {
67
64
  window.GOVUK.analyticsVars.gaProperty = 'UA-26179049-22'
68
65
  window.GOVUK.analyticsVars.gaPropertyCrossDomain = 'UA-145652997-1'
69
66
  }
@@ -74,136 +71,43 @@ window.GOVUK.loadAnalytics = {
74
71
  }
75
72
  },
76
73
 
77
- loadGa4Vars: function () {
78
- // If we recognise the environment, load the GA4 vars
79
- if (this.ga4EnvironmentVariables[this.environment]) {
80
- window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}
81
- window.GOVUK.analyticsGa4.vars = window.GOVUK.analyticsGa4.vars || {}
82
- window.GOVUK.analyticsGa4.vars.id = 'GTM-MG7HG5W'
83
- window.GOVUK.analyticsGa4.vars.auth = this.ga4EnvironmentVariables[this.environment].auth
84
- window.GOVUK.analyticsGa4.vars.preview = this.ga4EnvironmentVariables[this.environment].preview
85
- window.GOVUK.analyticsGa4.vars.environment = this.environment // Used for testing and debugging
86
-
87
- window.GOVUK.analyticsGa4.vars.gem_version = 'not found'
88
- var gemMeta = document.querySelector('meta[name="govuk:components_gem_version"]')
89
- if (gemMeta) {
90
- window.GOVUK.analyticsGa4.vars.gem_version = gemMeta.getAttribute('content')
91
- }
92
-
93
- window.GOVUK.analyticsGa4.vars.internalDomains = []
94
- window.GOVUK.analyticsGa4.vars.internalDomains.push(window.GOVUK.analyticsGa4.core.trackFunctions.getHostname())
95
- window.GOVUK.analyticsGa4.core.trackFunctions.appendDomainsWithoutWWW(window.GOVUK.analyticsGa4.vars.internalDomains)
96
- }
97
- },
98
-
99
- addGtmScriptToDom: function () {
100
- var firstScript = document.getElementsByTagName('script')[0]
101
- var newScript = document.createElement('script')
102
- newScript.async = true
103
-
104
- // initialise GTM
105
- window.dataLayer = window.dataLayer || []
106
- window.dataLayer.push({ 'gtm.blocklist': ['customPixels', 'customScripts', 'html', 'nonGoogleScripts'] })
107
- window.dataLayer.push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' })
108
-
109
- var auth = window.GOVUK.analyticsGa4.vars.auth || ''
110
- var preview = window.GOVUK.analyticsGa4.vars.preview || ''
111
- if (auth) {
112
- auth = '&gtm_auth=' + auth
113
- }
114
- if (preview) {
115
- preview = '&gtm_preview=' + preview + '&gtm_cookies_win=x'
116
- }
117
-
118
- this.googleSrc = 'https://www.googletagmanager.com/gtm.js?id=' + window.GOVUK.analyticsGa4.vars.id + auth + preview
119
- newScript.src = this.googleSrc
120
- firstScript.parentNode.insertBefore(newScript, firstScript)
121
- },
122
-
123
74
  loadGa4: function (currentDomain) {
124
- // The domain can be overridden. This is needed for testing as we need to fake the domain that we are on to test each environment.
125
75
  currentDomain = currentDomain || window.location.hostname
76
+ var environment = ''
126
77
 
127
- window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}
128
-
129
- if (typeof window.GOVUK.analyticsGa4.core === 'undefined') {
130
- console.warn('load-analytics.js: window.GOVUK.analyticsGa4.core was not found - has ./analytics-ga4.js been imported?')
131
- return
132
- }
133
-
134
- this.environment = ''
135
78
  // Categorise current environment
136
- if (this.arrayContains({ valueToFind: currentDomain, array: this.productionDomains })) {
137
- this.environment = 'production'
138
- } else if (this.arrayContains({ valueToFind: currentDomain, array: this.stagingDomains })) {
139
- this.environment = 'staging'
140
- } else if (this.arrayContains({ valueToFind: currentDomain, array: this.integrationDomains })) {
141
- this.environment = 'integration'
142
- } else if (this.arrayContains({ valueToFind: currentDomain, array: this.developmentDomains }) || currentDomain.indexOf('.dev.gov.uk') !== -1) {
143
- this.environment = 'development'
144
- }
145
-
146
- if (!this.environment) {
147
- console.warn('load-analytics.js: environment for GA4 not recognised - ' + this.environment + ' on ' + currentDomain)
148
- return
149
- }
150
-
151
- // Don't load GA4 if initialiseGA4 is set to false. This allows us to disable analytics if required.
152
- if (!this.ga4EnvironmentVariables[this.environment].initialiseGA4) {
153
- return
79
+ if (this.arrayContains(currentDomain, this.productionDomains)) {
80
+ environment = 'production'
81
+ } else if (this.arrayContains(currentDomain, this.stagingDomains)) {
82
+ environment = 'staging'
83
+ } else if (this.arrayContains(currentDomain, this.integrationDomains)) {
84
+ environment = 'integration'
85
+ } else if (this.arrayContains(currentDomain, this.developmentDomains) || currentDomain.indexOf('.dev.gov.uk') !== -1) {
86
+ environment = 'development'
154
87
  }
155
88
 
156
- // to be added: digital identity consent mechanism
157
- var consentCookie = window.GOVUK.getConsentCookie()
158
- if (consentCookie && consentCookie.usage) {
159
- // Creates 'window.GOVUK.analyticsGa4.vars', then adds the gtm.js script to the DOM.
160
- window.GOVUK.loadAnalytics.loadGa4Vars()
161
- window.GOVUK.loadAnalytics.addGtmScriptToDom()
162
-
163
- /* Initialises our GA4 modules once the DOM has loaded.
164
- document.readyState is needed as when cookies aren't set the DOM is likely already loaded.
165
- Therefore, if they accept cookies after the page has loaded our modules wouldn't initialise,
166
- as DOMContentLoaded won't fire if the DOM has already loaded. */
167
- if (document.readyState === 'complete') {
168
- window.GOVUK.loadAnalytics.startGa4Modules()
169
- } else {
170
- window.addEventListener('DOMContentLoaded', function () {
171
- window.GOVUK.loadAnalytics.startGa4Modules()
172
- })
173
- }
174
-
175
- // to be added: cross domain tracking code
176
- } else {
177
- window.addEventListener('cookie-consent', function (e) {
178
- window.GOVUK.loadAnalytics.loadGa4()
179
- })
180
- }
181
- },
182
-
183
- startGa4Modules: function () {
184
- // Some tests initialise GA4, but we do not want our modules initialising, so this check is in place.
185
- if (!window.GOVUK.analyticsGa4.vars.disableModules) {
186
- var analyticsModules = window.GOVUK.analyticsGa4.analyticsModules
187
- for (var property in analyticsModules) {
188
- var module = analyticsModules[property]
189
- if (typeof module.init === 'function') {
190
- module.init()
89
+ // If we recognise the environment (i.e. the string isn't empty), load in GA4
90
+ if (environment) {
91
+ // If analytics-ga4.js exists and our detected environment has 'initialiseGA4' set to true, load GA4.
92
+ if (typeof window.GOVUK.analyticsGa4.init !== 'undefined' && this.ga4EnvironmentVariables[environment].initialiseGA4) {
93
+ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}
94
+ window.GOVUK.analyticsGa4.vars = window.GOVUK.analyticsGa4.vars || {}
95
+ window.GOVUK.analyticsGa4.vars.id = 'GTM-MG7HG5W'
96
+ window.GOVUK.analyticsGa4.vars.auth = this.ga4EnvironmentVariables[environment].auth
97
+ window.GOVUK.analyticsGa4.vars.preview = this.ga4EnvironmentVariables[environment].preview
98
+ window.GOVUK.analyticsGa4.vars.environment = environment // Used for testing and debugging
99
+
100
+ window.GOVUK.analyticsGa4.vars.gem_version = 'not found'
101
+ var gemMeta = document.querySelector('meta[name="govuk:components_gem_version"]')
102
+ if (gemMeta) {
103
+ window.GOVUK.analyticsGa4.vars.gem_version = gemMeta.getAttribute('content')
191
104
  }
105
+ window.GOVUK.analyticsGa4.init()
192
106
  }
193
107
  }
194
108
  },
195
109
 
196
- arrayContains: function (params) {
197
- if (!params || !params.valueToFind || !params.array) {
198
- return false
199
- }
200
-
201
- return params.array.indexOf(params.valueToFind) !== -1
110
+ arrayContains: function (valueToFind, array) {
111
+ return array.indexOf(valueToFind) !== -1
202
112
  }
203
113
  }
204
-
205
- // This if statement stops this function running during testing
206
- if (!window.jasmine) {
207
- window.GOVUK.loadAnalytics.loadUa()
208
- window.GOVUK.loadAnalytics.loadGa4()
209
- }
@@ -1,3 +1,3 @@
1
1
  module GovukPublishingComponents
2
- VERSION = "34.5.1".freeze
2
+ VERSION = "34.6.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_publishing_components
3
3
  version: !ruby/object:Gem::Version
4
- version: 34.5.1
4
+ version: 34.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-25 00:00:00.000000000 Z
11
+ date: 2023-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: govuk_app_config
@@ -446,6 +446,7 @@ files:
446
446
  - app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-page-views.js
447
447
  - app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-schemas.js
448
448
  - app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-specialist-link-tracker.js
449
+ - app/assets/javascripts/govuk_publishing_components/analytics-ga4/init-ga4.js
449
450
  - app/assets/javascripts/govuk_publishing_components/analytics-ga4/pii-remover.js
450
451
  - app/assets/javascripts/govuk_publishing_components/analytics.js
451
452
  - app/assets/javascripts/govuk_publishing_components/analytics/analytics.js