-
Notifications
You must be signed in to change notification settings - Fork 20
/
ga4-page-views.spec.js
655 lines (560 loc) · 26.3 KB
/
ga4-page-views.spec.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/* eslint-env jasmine */
describe('Google Tag Manager page view tracking', function () {
var GOVUK = window.GOVUK
var saved = {}
var expected
beforeAll(function () {
spyOn(GOVUK.analyticsGa4.core, 'getGemVersion').and.returnValue('aVersion')
})
beforeEach(function () {
saved.title = document.title
document.title = 'This here page'
expected = {
event: 'page_view',
govuk_gem_version: 'aVersion',
timestamp: '123456',
page_view: {
location: document.location.href,
referrer: document.referrer,
title: 'This here page',
status_code: '200',
viewport_size: GOVUK.analyticsGa4.analyticsModules.PageViewTracker.getViewPort(),
ab_test: undefined,
document_type: undefined,
publishing_app: undefined,
rendering_app: undefined,
schema_name: undefined,
content_id: undefined,
browse_topic: undefined,
navigation_page_type: undefined,
navigation_list_type: undefined,
step_navs: undefined,
taxonomy_main: undefined,
taxonomy_main_id: undefined,
taxonomy_level1: undefined,
taxonomy_all: undefined,
taxonomy_all_ids: undefined,
language: undefined,
history: 'false',
withdrawn: 'false',
first_published_at: undefined,
updated_at: undefined,
public_updated_at: undefined,
publishing_government: undefined,
political_status: undefined,
primary_publishing_organisation: undefined,
organisations: undefined,
world_locations: undefined,
dynamic: 'false',
emergency_banner: undefined,
phase_banner: undefined,
devolved_nations_banner: undefined,
cookie_banner: undefined,
intervention: undefined,
query_string: undefined,
search_term: undefined,
spelling_suggestion: undefined,
discovery_engine_attribution_token: undefined
}
}
spyOn(GOVUK.analyticsGa4.core, 'getTimestamp').and.returnValue('123456')
window.dataLayer = []
})
afterEach(function () {
document.title = saved.title
window.httpStatusCode = false
var head = document.getElementsByTagName('head')[0]
var metas = document.querySelectorAll("[name^='govuk']")
for (var i = 0; i < metas.length; i++) {
head.removeChild(metas[i])
}
var html = document.querySelector('html')
html.removeAttribute('lang')
})
afterAll(function () {
window.dataLayer = []
})
function createMetaTags (key, value) {
var metatag = document.createElement('meta')
metatag.setAttribute('name', 'govuk:' + key)
metatag.setAttribute('content', value)
document.getElementsByTagName('head')[0].appendChild(metatag)
}
it('returns a standard page view', function () {
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.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('returns a page view with specific publishing information', function () {
var tags = [
{
gtmName: 'document_type',
tagName: 'format',
value: 'detailed_guide'
},
{
gtmName: 'publishing_app',
tagName: 'publishing-app',
value: 'whitehall'
},
{
gtmName: 'rendering_app',
tagName: 'rendering-app',
value: 'government-frontend'
},
{
gtmName: 'schema_name',
tagName: 'schema-name',
value: 'html_publication'
},
{
gtmName: 'content_id',
tagName: 'content-id',
value: '123456'
}
]
for (var i = 0; i < tags.length; i++) {
var tag = tags[i]
createMetaTags(tag.tagName, tag.value)
expected.page_view[tag.gtmName] = tag.value
}
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('returns a page view with specific taxonomy information', function () {
var tags = [
{
gtmName: 'browse_topic',
tagName: 'ga4-browse-topic',
value: 'this section'
},
{
gtmName: 'taxonomy_main',
tagName: 'taxon-slug',
value: 'this taxon slug'
},
{
gtmName: 'taxonomy_main_id',
tagName: 'taxon-id',
value: 'this taxon id'
},
{
gtmName: 'taxonomy_level1',
tagName: 'themes',
value: 'this theme'
},
{
gtmName: 'taxonomy_all',
tagName: 'taxon-slugs',
value: 'this taxon slugs'
},
{
gtmName: 'taxonomy_all_ids',
tagName: 'taxon-ids',
value: 'this taxon ids'
}
]
for (var i = 0; i < tags.length; i++) {
var tag = tags[i]
createMetaTags(tag.tagName, tag.value)
expected.page_view[tag.gtmName] = tag.value
}
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
describe('returns a pageview with a language', function () {
var content
beforeEach(function () {
content = document.createElement('div')
content.setAttribute('id', 'content')
document.body.appendChild(content)
})
afterEach(function () {
document.body.removeChild(content)
})
it('set correctly', function () {
content.setAttribute('lang', 'wakandan')
expected.page_view.language = 'wakandan'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('set incorrectly', function () {
expected.page_view.language = undefined
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
})
it('returns a pageview without a language', function () {
expected.page_view.language = undefined
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.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.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.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.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.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.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.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('returns a pageview on a page marked with a publishing government (GA4 alternative meta tag)', function () {
createMetaTags('ga4-publishing-government', 'labour')
expected.page_view.publishing_government = 'labour'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('handles removing hyphens and downcases from the publishing government meta tag', function () {
createMetaTags('ga4-publishing-government', '2005-To-2010-Labour-GOVERNMENT')
expected.page_view.publishing_government = '2005 to 2010 labour government'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
createMetaTags('publishing-government', '2005-To-2010-Labour-GOVERNMENT-2')
expected.page_view.publishing_government = '2005 to 2010 labour government 2'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[1]).toEqual(expected)
})
it('handles removing hyphens and downcases when the publishing government meta tags are falsy', function () {
createMetaTags('ga4-publishing-government', '')
expected.page_view.publishing_government = undefined
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
createMetaTags('publishing-government', '')
expected.page_view.publishing_government = undefined
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[1]).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.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('returns a pageview on a page marked with a political status (GA4 alternative meta tag)', function () {
createMetaTags('ga4-political-status', 'historic')
expected.page_view.political_status = 'historic'
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.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.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.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('returns undefined if the content attribute is an empty string', function () {
createMetaTags('spelling-suggestion', '')
expected.page_view.spelling_suggestion = undefined
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('removes email, postcode, and date pii from the title, location and referrer', function () {
document.title = 'example@gov.uk - SW12AA - 2020-01-01'
expected.page_view.title = '[email] - [postcode] - [date]'
spyOnProperty(document, 'referrer', 'get').and.returnValue('https://gov.uk/example@gov.uk/SW12AA/2020-01-01')
expected.page_view.referrer = 'https://gov.uk/[email]/[postcode]/[date]'
// We can't spy on location, so instead we use an anchor link to change the URL temporarily
// Reset the URL so we can build the expected link string
var linkForURLMock = document.createElement('a')
linkForURLMock.href = '#'
linkForURLMock.click()
var location = document.location.href
expected.page_view.location = location + '[email]/[postcode]/[date]'
// Add personally identifiable information to the current page location
linkForURLMock.href = '#example@gov.uk/SW12AA/2020-01-01'
linkForURLMock.click()
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
// Reset the page location for other tests
linkForURLMock.href = '#'
linkForURLMock.click()
expect(window.dataLayer[0]).toEqual(expected)
})
it('can redact ga parameters as expected', function () {
var url = 'https://www.gov.uk/welfare?'
var param1 = '_ga=2.237932419.220854294.1692605006-1618308030.1687790776'
var param2 = '_gl=32.23434234.243-32434220-230442300234.234324.42304-09320'
var param3 = '_gl=1*18346yi*_ga*OTkxOTU4OTU5LjE2OTQ0MzQyNTg.*_ga_HYBY4V8XVT*MTY5NDQzNDI1OC4xLjAuMTY5NDQzNDI1OC4wLjAuMA..'
expect(GOVUK.analyticsGa4.analyticsModules.PageViewTracker.stripGaParam(url + param1)).toEqual(url + '_ga=[id]')
expect(GOVUK.analyticsGa4.analyticsModules.PageViewTracker.stripGaParam(url + param2)).toEqual(url + '_gl=[id]')
expect(GOVUK.analyticsGa4.analyticsModules.PageViewTracker.stripGaParam(url + param1 + '&' + param2)).toEqual(url + '_ga=[id]&_gl=[id]')
expect(GOVUK.analyticsGa4.analyticsModules.PageViewTracker.stripGaParam(url + param3)).toEqual(url + '_gl=[id]')
})
it('redacts ga parameters from the location and referrer before they are mistaken for PII', function () {
// We can't spy on location, so instead we use an anchor link to change the URL temporarily
// Reset the URL so we can build the expected link string
var linkForURLMock = document.createElement('a')
linkForURLMock.href = '#'
linkForURLMock.click()
var location = document.location.href
expected.page_view.location = location + '[email]'
// Add email address to the current page location
linkForURLMock.href = '#example@gov.uk?_gl=2012-01-05.2022-01-23'
linkForURLMock.click()
expected.page_view.location = document.location.href.replace('#example@gov.uk?_gl=2012-01-05.2022-01-23', '#[email]?_gl=[id]')
spyOnProperty(document, 'referrer', 'get').and.returnValue('https://gov.uk/?_ga=2012-01-05.2022-01-23&date=2021-01-02')
expected.page_view.referrer = 'https://gov.uk/?_ga=[id]&date=[date]'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
// Reset the page location for other tests
linkForURLMock.href = '#'
linkForURLMock.click()
})
describe('correctly sets the referrer and dynamic properties', function () {
it('when not passed a parameter', function () {
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('when passed a parameter for the referrer', function () {
expected.page_view.referrer = 'https://gov.uk/referrer'
expected.page_view.dynamic = 'true'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init('https://gov.uk/referrer')
expect(window.dataLayer[0]).toEqual(expected)
})
})
it('correctly sets the ab-test parameter', function () {
createMetaTags('ab-test', 'BankHolidaysTest:A')
expected.page_view.ab_test = 'BankHolidaysTest:A'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('correctly sets the navigation-page-type parameter', function () {
createMetaTags('navigation-page-type', 'Browse level 2')
expected.page_view.navigation_page_type = 'Browse level 2'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('correctly sets the navigation-list-type parameter', function () {
createMetaTags('navigation-list-type', 'curated')
expected.page_view.navigation_list_type = 'curated'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('correctly sets the step-navs parameter', function () {
createMetaTags('stepnavs', 'e01e924b-9c7c-4c71-8241-66a575c2f61f')
expected.page_view.step_navs = 'e01e924b-9c7c-4c71-8241-66a575c2f61f'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('correctly sets the emergency_banner parameter', function () {
var div = document.createElement('div')
div.setAttribute('data-ga4-emergency-banner', '')
document.body.appendChild(div)
expected.page_view.emergency_banner = 'true'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
document.body.removeChild(div)
})
it('correctly sets the phase_banner parameter', function () {
var div = document.createElement('div')
div.setAttribute('data-ga4-phase-banner', 'beta')
document.body.appendChild(div)
expected.page_view.phase_banner = 'beta'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
document.body.removeChild(div)
})
it('correctly sets the devolved_nations_banner parameter', function () {
var div = document.createElement('div')
div.setAttribute('data-ga4-devolved-nations-banner', 'England, Scotland, Wales')
document.body.appendChild(div)
expected.page_view.devolved_nations_banner = 'England, Scotland, Wales'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
document.body.removeChild(div)
})
it('correctly sets the cookie_banner parameter when the banner exists and no cookie exists', function () {
var div = document.createElement('div')
div.setAttribute('data-ga4-cookie-banner', '')
document.body.appendChild(div)
expected.page_view.cookie_banner = 'true'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
document.body.removeChild(div)
})
it('doesn\'t set the cookie parameter when the banner is hidden via the cookie', function () {
var div = document.createElement('div')
div.setAttribute('data-ga4-cookie-banner', '')
div.setAttribute('data-module', 'cookie-banner')
window.GOVUK.setCookie('cookies_policy', '{"essential":true,"settings":false,"usage":false,"campaigns":false}')
expected.page_view.cookie_banner = undefined
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
window.GOVUK.setCookie('cookie_policy', '')
})
describe('intervention banner', function () {
var div
beforeEach(function () {
div = document.createElement('div')
div.setAttribute('data-ga4-intervention-banner', '')
document.body.appendChild(div)
})
afterEach(function () {
document.body.removeChild(div)
})
it('correctly sets the parameter when the banner exists and no cookie exists', function () {
div.setAttribute('data-intervention-name', 'hello-world')
div.setAttribute('data-module', 'intervention')
expected.page_view.intervention = 'true'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('doesn\'t set the intervention parameter when the banner is hidden via the cookie', function () {
div.setAttribute('data-ga4-intervention-banner', '')
div.setAttribute('data-module', 'intervention')
div.setAttribute('data-intervention-name', 'hello-world')
window.GOVUK.setCookie('intervention_campaign', 'hello-world')
expected.page_view.intervention = undefined
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
window.GOVUK.setCookie('intervention_campaign', '')
})
})
it('correctly sets the query_string parameter with PII and _ga/_gl values redacted', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('?query1=hello&query2=world&email=email@example.com&postcode=SW12AA&birthday=1990-01-01&_ga=19900101.567&_gl=19900101.567')
expected.page_view.query_string = 'query1=hello&query2=world&email=[email]&postcode=[postcode]&birthday=[date]&_ga=[id]&_gl=[id]'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
describe('search_term parameter', function () {
describe('using the query parameter "keywords"', function () {
it('correctly sets the parameter using ?keywords= with PII values redacted', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('?keywords=hello+world+email@example.com+SW12AA+1990-01-01&another=one')
expected.page_view.query_string = 'keywords=hello+world+[email]+[postcode]+[date]&another=one'
expected.page_view.search_term = 'hello world [email] [postcode] [date]'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('correctly sets the parameter using &keywords= with PII values redacted', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('?test=true&keywords=hello+world+email@example.com+SW12AA+1990-01-01&another=one')
expected.page_view.query_string = 'test=true&keywords=hello+world+[email]+[postcode]+[date]&another=one'
expected.page_view.search_term = 'hello world [email] [postcode] [date]'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('replaces plusses with spaces, and removes extra lines and spaces', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('?test=true&keywords=hello++%2B+world+there+are+spaces++in+++a+lot++++of++\n++places')
expected.page_view.query_string = 'test=true&keywords=hello++%2B+world+there+are+spaces++in+++a+lot++++of++\n++places'
expected.page_view.search_term = 'hello world there are spaces in a lot of places'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('sets the search term to lowercase', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('?test=true&keywords=I+AM+LOWERCASE')
expected.page_view.query_string = 'test=true&keywords=I+AM+LOWERCASE'
expected.page_view.search_term = 'i am lowercase'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('correctly ignores other query string values', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('?keywordss=not+search&keyywords=not+search+either&hello=world')
expected.page_view.query_string = 'keywordss=not+search&keyywords=not+search+either&hello=world'
expected.page_view.search_term = undefined
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('decodes URL encoded characters in the search term', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('?keywords=%5Btesting%5Dtesting!+"123"')
expected.page_view.query_string = 'keywords=%5Btesting%5Dtesting!+"123"'
expected.page_view.search_term = '[testing]testing! "123"'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
})
describe('using the data-ga4-search-query attribute', function () {
var div
beforeEach(function () {
div = document.createElement('div')
document.body.appendChild(div)
})
afterEach(function () {
document.body.removeChild(div)
})
it('correctly sets the parameter with PII values redacted', function () {
div.setAttribute('data-ga4-search-query', 'hello world email@example.com SW12AA 1990-01-01')
expected.page_view.search_term = 'hello world [email] [postcode] [date]'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('replaces plusses with spaces, and removes extra lines and spaces', function () {
div.setAttribute('data-ga4-search-query', 'hello++%2B+world+there+are+spaces++in+++a+lot++++of++\n++places')
expected.page_view.search_term = 'hello world there are spaces in a lot of places'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('sets the search term to lowercase', function () {
div.setAttribute('data-ga4-search-query', 'I AM LOWERCASE')
expected.page_view.search_term = 'i am lowercase'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
})
it('functions correctly when there is no query string or data-ga4-search-query attribute', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('')
expected.page_view.search_term = undefined
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
})
it('correctly sets the spelling_suggestion parameter', function () {
createMetaTags('spelling-suggestion', 'tax')
expected.page_view.spelling_suggestion = 'tax'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
it('correctly sets the discovery_engine_attribution_token parameter', function () {
createMetaTags('discovery-engine-attribution-token', 'searchyMcSearch')
expected.page_view.discovery_engine_attribution_token = 'searchyMcSearch'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
})