govuk_frontend_toolkit 8.2.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,72 +0,0 @@
1
- /* global describe it expect beforeEach afterEach spyOn */
2
-
3
- var $ = window.jQuery
4
-
5
- describe('GOVUK.analyticsPlugins.downloadLinkTracker', function () {
6
- 'use strict'
7
- var GOVUK = window.GOVUK
8
-
9
- var $links
10
-
11
- beforeEach(function () {
12
- $links = $(
13
- '<div class="download-links">' +
14
- '<a href="/one.pdf">PDF</a>' +
15
- '<a href="/two.xslt">Spreadsheet</a>' +
16
- '<a href="/something/uploads/system/three.doc">Document</a>' +
17
- '<a href="/an/image/link.png"><img src="/img" /></a>' +
18
- '</div>' +
19
- '<div class="normal-links">' +
20
- '<a href="/normal-link">Normal link</a>' +
21
- '<a href="/another-link">Another link</a>' +
22
- '</div>'
23
- )
24
-
25
- $('html').on('click', function (evt) { evt.preventDefault() })
26
- $('body').append($links)
27
- GOVUK.analytics = {trackEvent: function () {}}
28
- GOVUK.analyticsPlugins.downloadLinkTracker({selector: 'a[href$=".pdf"], a[href$=".xslt"], a[href$=".doc"], a[href$=".png"]'})
29
- })
30
-
31
- afterEach(function () {
32
- $('html').off()
33
- $('body').off()
34
- $links.remove()
35
- delete GOVUK.analytics
36
- })
37
-
38
- it('listens to clicks on links that match the selector', function () {
39
- spyOn(GOVUK.analytics, 'trackEvent')
40
-
41
- $('.download-links a').each(function () {
42
- $(this).trigger('click')
43
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalled()
44
- GOVUK.analytics.trackEvent.calls.reset()
45
- })
46
-
47
- $('.normal-links a').each(function () {
48
- $(this).trigger('click')
49
- expect(GOVUK.analytics.trackEvent).not.toHaveBeenCalled()
50
- GOVUK.analytics.trackEvent.calls.reset()
51
- })
52
- })
53
-
54
- it('listens to click events on elements within download links', function () {
55
- spyOn(GOVUK.analytics, 'trackEvent')
56
-
57
- $('.download-links a img').trigger('click')
58
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('Download Link Clicked', '/an/image/link.png', {transport: 'beacon'})
59
- })
60
-
61
- it('tracks a download link as an event with link text as the label', function () {
62
- spyOn(GOVUK.analytics, 'trackEvent')
63
- $('.download-links a').trigger('click')
64
-
65
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
66
- 'Download Link Clicked', '/one.pdf', {label: 'PDF', transport: 'beacon'})
67
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
68
- 'Download Link Clicked', '/two.xslt', {label: 'Spreadsheet', transport: 'beacon'})
69
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
70
- 'Download Link Clicked', '/something/uploads/system/three.doc', {label: 'Document', transport: 'beacon'})
71
- })
72
- })
@@ -1,65 +0,0 @@
1
- /* global describe it expect beforeEach afterEach spyOn */
2
-
3
- describe('GOVUK.analyticsPlugins.error', function () {
4
- 'use strict'
5
- var GOVUK = window.GOVUK
6
-
7
- GOVUK.analyticsPlugins.error({filenameMustMatch: /gov\.uk/})
8
-
9
- beforeEach(function () {
10
- GOVUK.analytics = {trackEvent: function () {}}
11
- spyOn(GOVUK.analytics, 'trackEvent')
12
- })
13
-
14
- afterEach(function () {
15
- delete GOVUK.analytics
16
- })
17
-
18
- it('sends errors to Google Analytics', function () {
19
- triggerError('https://www.gov.uk/filename.js', 2, 'Error message')
20
-
21
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
22
- 'JavaScript Error',
23
- 'Error message',
24
- { label: 'https://www.gov.uk/filename.js: 2', value: 1, nonInteraction: true })
25
- })
26
-
27
- it('tracks only errors with a matching or blank filename', function () {
28
- triggerError('http://www.gov.uk/somefile.js', 2, 'Error message')
29
- triggerError('', 2, 'In page error')
30
- triggerError('http://www.broken-external-plugin-site.com/horrible.js', 2, 'Error message')
31
-
32
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
33
- 'JavaScript Error',
34
- 'Error message',
35
- {
36
- label: 'http://www.gov.uk/somefile.js: 2',
37
- value: 1,
38
- nonInteraction: true })
39
-
40
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
41
- 'JavaScript Error',
42
- 'In page error',
43
- {
44
- label: ': 2',
45
- value: 1,
46
- nonInteraction: true })
47
-
48
- expect(GOVUK.analytics.trackEvent).not.toHaveBeenCalledWith(
49
- 'JavaScript Error',
50
- 'Error message',
51
- {
52
- label: 'http://www.broken-external-plugin-site.com/horrible.js: 2',
53
- value: 1,
54
- nonInteraction: true })
55
- })
56
-
57
- function triggerError (filename, lineno, message) {
58
- var event = document.createEvent('Event')
59
- event.initEvent('error', true, true)
60
- event.filename = filename
61
- event.lineno = lineno
62
- event.message = message
63
- window.dispatchEvent(event)
64
- }
65
- })
@@ -1,109 +0,0 @@
1
- /* global describe it expect beforeEach afterEach spyOn */
2
-
3
- var $ = window.jQuery
4
-
5
- describe('GOVUK.analyticsPlugins.externalLinkTracker', function () {
6
- 'use strict'
7
- var GOVUK = window.GOVUK
8
-
9
- var $links
10
-
11
- beforeEach(function () {
12
- $links = $(
13
- '<div class="external-links">' +
14
- '<a href="http://www.nationalarchives.gov.uk"> National Archives </a>' +
15
- '<a href="https://www.nationalarchives.gov.uk"></a>' +
16
- '<a href="https://www.nationalarchives.gov.uk/one.pdf">National Archives PDF</a>' +
17
- '<a href="https://www.nationalarchives.gov.uk/an/image/link.png"><img src="/img" /></a>' +
18
- '</div>' +
19
- '<div class="internal-links">' +
20
- '<a href="/some-path">Local link</a>' +
21
- '<a href="http://fake-hostname.com/some-path">Another local link</a>' +
22
- '</div>'
23
- )
24
-
25
- $('html').on('click', function (evt) { evt.preventDefault() })
26
- $('body').append($links)
27
- GOVUK.analytics = {
28
- trackEvent: function () {},
29
- setDimension: function () {}
30
- }
31
-
32
- spyOn(GOVUK.analyticsPlugins.externalLinkTracker, 'getHostname').and.returnValue('fake-hostname.com')
33
- })
34
-
35
- afterEach(function () {
36
- $('html').off()
37
- $('body').off()
38
- $links.remove()
39
- delete GOVUK.analytics
40
- })
41
-
42
- it('listens to click events on only external links', function () {
43
- GOVUK.analyticsPlugins.externalLinkTracker({externalLinkUploadCustomDimension: 36})
44
-
45
- spyOn(GOVUK.analytics, 'trackEvent')
46
-
47
- $('.external-links a').each(function () {
48
- $(this).trigger('click')
49
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalled()
50
- GOVUK.analytics.trackEvent.calls.reset()
51
- })
52
-
53
- $('.internal-links a').each(function () {
54
- $(this).trigger('click')
55
- expect(GOVUK.analytics.trackEvent).not.toHaveBeenCalled()
56
- GOVUK.analytics.trackEvent.calls.reset()
57
- })
58
- })
59
-
60
- it('listens to click events on elements within external links', function () {
61
- GOVUK.analyticsPlugins.externalLinkTracker({externalLinkUploadCustomDimension: 36})
62
-
63
- spyOn(GOVUK.analytics, 'trackEvent')
64
-
65
- $('.external-links a img').trigger('click')
66
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
67
- 'External Link Clicked', 'https://www.nationalarchives.gov.uk/an/image/link.png', {transport: 'beacon'})
68
- })
69
-
70
- it('tracks an external link\'s href and link text', function () {
71
- GOVUK.analyticsPlugins.externalLinkTracker({externalLinkUploadCustomDimension: 36})
72
-
73
- spyOn(GOVUK.analytics, 'trackEvent')
74
- $('.external-links a').trigger('click')
75
-
76
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
77
- 'External Link Clicked', 'http://www.nationalarchives.gov.uk', {transport: 'beacon', label: 'National Archives'})
78
-
79
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
80
- 'External Link Clicked', 'https://www.nationalarchives.gov.uk', {transport: 'beacon'})
81
-
82
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
83
- 'External Link Clicked', 'https://www.nationalarchives.gov.uk/one.pdf', {transport: 'beacon', label: 'National Archives PDF'})
84
- })
85
-
86
- it('duplicates the url info in a custom dimension to be used to join with a Google Analytics upload', function () {
87
- GOVUK.analyticsPlugins.externalLinkTracker({externalLinkUploadCustomDimension: 36})
88
-
89
- spyOn(GOVUK.analytics, 'setDimension')
90
- spyOn(GOVUK.analytics, 'trackEvent')
91
- $('.external-links a').trigger('click')
92
-
93
- expect(GOVUK.analytics.setDimension).toHaveBeenCalledWith(36, 'http://www.nationalarchives.gov.uk')
94
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
95
- 'External Link Clicked', 'http://www.nationalarchives.gov.uk', {transport: 'beacon', label: 'National Archives'})
96
- })
97
-
98
- it('does not duplicate the url info if a custom dimension is not provided', function () {
99
- GOVUK.analyticsPlugins.externalLinkTracker()
100
-
101
- spyOn(GOVUK.analytics, 'setDimension')
102
- spyOn(GOVUK.analytics, 'trackEvent')
103
- $('.external-links a').trigger('click')
104
-
105
- expect(GOVUK.analytics.setDimension).not.toHaveBeenCalled()
106
- expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
107
- 'External Link Clicked', 'http://www.nationalarchives.gov.uk', {transport: 'beacon', label: 'National Archives'})
108
- })
109
- })
@@ -1,221 +0,0 @@
1
- /* global describe it expect beforeEach spyOn jasmine */
2
-
3
- var $ = window.jQuery
4
-
5
- describe('GOVUK.GoogleAnalyticsUniversalTracker', function () {
6
- 'use strict'
7
- var GOVUK = window.GOVUK
8
-
9
- function addGoogleAnalyticsSpy () {
10
- window.ga = function () {}
11
- spyOn(window, 'ga')
12
- }
13
-
14
- var universal
15
- var setupArguments
16
-
17
- beforeEach(function () {
18
- addGoogleAnalyticsSpy()
19
-
20
- universal = new GOVUK.GoogleAnalyticsUniversalTracker('id', {
21
- cookieDomain: 'cookie-domain.com',
22
- siteSpeedSampleRate: 100
23
- })
24
- })
25
-
26
- it('can load the libraries needed to run universal Google Analytics', function () {
27
- delete window.ga
28
- $('[src="https://www.google-analytics.com/analytics.js"]').remove()
29
- GOVUK.GoogleAnalyticsUniversalTracker.load()
30
- expect($('script[async][src="https://www.google-analytics.com/analytics.js"]').length).toBe(1)
31
- expect(typeof window.ga).toBe('function')
32
-
33
- window.ga('send message')
34
- expect(window.ga.q[0]).toEqual(jasmine.any(Object))
35
- })
36
-
37
- describe('when created', function () {
38
- beforeEach(function () {
39
- setupArguments = window.ga.calls.allArgs()
40
- })
41
-
42
- it('configures a Google tracker using the provided profile ID and config', function () {
43
- expect(setupArguments[0]).toEqual(['create', 'id', {cookieDomain: 'cookie-domain.com', siteSpeedSampleRate: 100}])
44
- })
45
-
46
- it('anonymises the IP', function () {
47
- expect(setupArguments[1]).toEqual(['set', 'anonymizeIp', true])
48
- })
49
-
50
- it('disables Ad features', function () {
51
- expect(setupArguments[3]).toEqual(['set', 'allowAdFeatures', false])
52
- })
53
- })
54
-
55
- describe('when created (with legacy non-object syntax)', function () {
56
- beforeEach(function () {
57
- addGoogleAnalyticsSpy()
58
-
59
- universal = new GOVUK.GoogleAnalyticsUniversalTracker('id', 'cookie-domain.com')
60
- setupArguments = window.ga.calls.allArgs()
61
- })
62
-
63
- it('configures a Google tracker using the provided profile ID and cookie domain', function () {
64
- expect(setupArguments[0]).toEqual(['create', 'id', {cookieDomain: 'cookie-domain.com'}])
65
- })
66
- })
67
-
68
- describe('when pageviews are tracked', function () {
69
- it('sends them to Google Analytics', function () {
70
- universal.trackPageview()
71
- expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview'])
72
- })
73
-
74
- it('sends them to Google Analytics, forcing a new session', function () {
75
- universal.trackPageview(undefined, undefined, { sessionControl: 'start' })
76
- expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview', {sessionControl: 'start'}])
77
- })
78
-
79
- it('can track a virtual pageview', function () {
80
- universal.trackPageview('/nicholas-page')
81
- expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview', {page: '/nicholas-page'}])
82
- })
83
-
84
- it('can track a virtual pageview with a custom title', function () {
85
- universal.trackPageview('/nicholas-page', 'Nicholas Page')
86
- expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview', {page: '/nicholas-page', title: 'Nicholas Page'}])
87
- })
88
-
89
- it('can set the transport method on a pageview', function () {
90
- universal.trackPageview('/t', 'T', {transport: 'beacon'})
91
- expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview', {page: '/t', title: 'T', transport: 'beacon'}])
92
- })
93
- })
94
-
95
- describe('when events are tracked', function () {
96
- function eventObjectFromSpy () {
97
- return window.ga.calls.mostRecent().args[1]
98
- }
99
-
100
- it('sends them to Google Analytics', function () {
101
- universal.trackEvent('category', 'action', {label: 'label'})
102
- expect(window.ga.calls.mostRecent().args).toEqual(
103
- ['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action', eventLabel: 'label'}]
104
- )
105
- })
106
-
107
- it('tracks custom dimensions', function () {
108
- universal.trackEvent('category', 'action', {dimension29: 'Home'})
109
- expect(window.ga.calls.mostRecent().args).toEqual(
110
- ['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action', dimension29: 'Home'}]
111
- )
112
- })
113
-
114
- it('the label is optional', function () {
115
- universal.trackEvent('category', 'action')
116
- expect(window.ga.calls.mostRecent().args).toEqual(
117
- ['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action'}]
118
- )
119
- })
120
-
121
- it('the option trackerName overrides the default tracker', function () {
122
- universal.trackEvent('category', 'action', {trackerName: 'testTracker'})
123
- expect(window.ga.calls.mostRecent().args).toEqual(
124
- ['testTracker.send', {hitType: 'event', eventCategory: 'category', eventAction: 'action'}]
125
- )
126
- })
127
-
128
- it('only sends values if they are parseable as numbers', function () {
129
- universal.trackEvent('category', 'action', {label: 'label', value: '10'})
130
- expect(eventObjectFromSpy()['eventValue']).toEqual(10)
131
-
132
- universal.trackEvent('category', 'action', {label: 'label', value: 10})
133
- expect(eventObjectFromSpy()['eventValue']).toEqual(10)
134
-
135
- universal.trackEvent('category', 'action', {label: 'label', value: 'not a number'})
136
- expect(eventObjectFromSpy()['eventValue']).toEqual(undefined)
137
- })
138
-
139
- it('can mark an event as non interactive', function () {
140
- universal.trackEvent('category', 'action', {label: 'label', value: 0, nonInteraction: true})
141
- expect(window.ga.calls.mostRecent().args).toEqual(
142
- ['send', {
143
- hitType: 'event',
144
- eventCategory: 'category',
145
- eventAction: 'action',
146
- eventLabel: 'label',
147
- eventValue: 0,
148
- nonInteraction: 1
149
- }]
150
- )
151
- })
152
-
153
- it('sends the page if supplied', function () {
154
- universal.trackEvent('category', 'action', {page: '/path/to/page'})
155
- expect(window.ga.calls.mostRecent().args).toEqual(
156
- ['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action', page: '/path/to/page'}]
157
- )
158
- })
159
-
160
- it('can set the transport method on an event', function () {
161
- universal.trackEvent('category', 'action', {transport: 'beacon'})
162
- expect(window.ga.calls.mostRecent().args).toEqual(
163
- ['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action', transport: 'beacon'}]
164
- )
165
- })
166
- })
167
-
168
- describe('when social events are tracked', function () {
169
- it('sends them to Google Analytics', function () {
170
- universal.trackSocial('network', 'action', 'target')
171
- expect(window.ga.calls.mostRecent().args).toEqual(['send', {
172
- 'hitType': 'social',
173
- 'socialNetwork': 'network',
174
- 'socialAction': 'action',
175
- 'socialTarget': 'target'
176
- }])
177
- })
178
- })
179
-
180
- describe('when setting a custom dimension', function () {
181
- it('sends the dimension to Google Analytics with the specified index and value', function () {
182
- universal.setDimension(1, 'value')
183
- expect(window.ga.calls.mostRecent().args).toEqual(['set', 'dimension1', 'value'])
184
- })
185
-
186
- it('coerces the value to a string', function () {
187
- universal.setDimension(1, 10)
188
- expect(window.ga.calls.mostRecent().args).toEqual(['set', 'dimension1', '10'])
189
- })
190
- })
191
-
192
- describe('when tracking all events', function () {
193
- window.history.replaceState(null, null, '?address=an.email@digital.cabinet-office.gov.uk')
194
- it('removes any email address from the location', function () {
195
- expect(window.ga.calls.mostRecent().args[2]).toContain('address=[email]')
196
- })
197
- })
198
-
199
- describe('adding a linked tracker', function () {
200
- var callIndex
201
-
202
- beforeEach(function () {
203
- callIndex = window.ga.calls.count()
204
- universal.addLinkedTrackerDomain('UA-123456', 'testTracker', 'some.service.gov.uk')
205
- })
206
- it('creates a tracker for the ID', function () {
207
- expect(window.ga.calls.argsFor(callIndex)).toEqual(['create', 'UA-123456', 'auto', Object({ name: 'testTracker' })])
208
- })
209
- it('requires and configures the linker plugin', function () {
210
- expect(window.ga.calls.argsFor(callIndex + 1)).toEqual(['require', 'linker'])
211
- expect(window.ga.calls.argsFor(callIndex + 2)).toEqual(['testTracker.require', 'linker'])
212
- })
213
- it('sends a pageview', function () {
214
- expect(window.ga.calls.mostRecent().args).toEqual(['testTracker.send', 'pageview'])
215
- })
216
- it('can omit sending a pageview', function () {
217
- universal.addLinkedTrackerDomain('UA-123456', 'testTracker', 'some.service.gov.uk', false)
218
- expect(window.ga.calls.mostRecent().args).not.toEqual(['testTracker.send', 'pageview'])
219
- })
220
- })
221
- })