govuk_frontend_toolkit 3.5.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,16 @@
1
- #3.5.1
1
+ # 4.0.0
2
+
3
+ - Remove Google Analytics classic https://github.com/alphagov/govuk_frontend_toolkit/pull/194
4
+ - References to google-analytics-classic-tracker.js should be removed
5
+ - Rename GOVUK.Tracker to GOVUK.Analytics
6
+ - References to GOVUK.Tracker should be updated
7
+
8
+ # 3.5.1
9
+
2
10
  - Changes Analytics API library to accept one, both or neither of the
3
11
  analytics tracking codes. This means we can start removing classic
4
12
  tracking codes from apps.
5
13
 
6
-
7
14
  #3.5.0
8
15
 
9
16
  - Adds cross domain tracking to Analytics API library: https://github.com/alphagov/govuk_frontend_toolkit/pull/185
@@ -30,9 +30,8 @@ module.exports = function(grunt) {
30
30
  javascripts: {
31
31
  src: [
32
32
  'node_modules/jquery-browser/lib/jquery.js',
33
- 'javascripts/govuk/analytics/google-analytics-classic-tracker.js',
34
33
  'javascripts/govuk/analytics/google-analytics-universal-tracker.js',
35
- 'javascripts/govuk/analytics/tracker.js',
34
+ 'javascripts/govuk/analytics/analytics.js',
36
35
  'javascripts/**/*.js'
37
36
  ],
38
37
  options: {
@@ -1 +1 @@
1
- 3.5.1
1
+ 4.0.0
@@ -2,23 +2,20 @@
2
2
 
3
3
  The toolkit provides an abstraction around analytics to make tracking pageviews, events and dimensions across multiple analytics providers easier. Specifically it was created to ease the migration from Google’s Classic Analytics to Universal Analytics. It includes:
4
4
 
5
- * a Google Analytics classic tracker
6
- * a Google Analytics universal tracker
7
- * code to asynchronously load these libraries
8
- * a generic Analytics tracker that combines these into a single API
5
+ * a Google Analytics universal tracker wrapper
6
+ * code to asynchronously load universal analytics
7
+ * a generic Analytics wrapper that allows multiple trackers to be configured
9
8
  * sensible defaults such as anonymising IPs
10
- * data coercion into the format required by Google Analytics (eg a custom event value must be a number, a custom dimension’s value must be a string)
9
+ * data coercion into the format required by Google Analytics (eg a custom dimension’s value must be a string)
11
10
 
12
11
  ## Create an analytics tracker
13
12
 
14
13
  The minimum you need to use the analytics function is:
15
14
 
16
15
  1. Include the following files from /javascripts/govuk/analytics in your project:
17
- * google-analytics-classic-tracker.js
18
16
  * google-analytics-universal-tracker.js
19
- * tracker.js
17
+ * analytics.js
20
18
  2. Copy the following `init` script into your own project and replace the dummy IDs with your own (they begin with `UA-`).
21
- * Tracker.js accepts one or both analytics tracking code, so if you are only using one type of analytics (Classic or Universal) omit the one you don't need from the instantiation of GOVUK.Tracker. (NB you will still need to include both google-analytics-classic-tracker.js and google-analytics-universal-tracker.js in your project as they are currently loaded ahead of instantiation.)
22
19
  * This initialisation can occur immediately as this API has no dependencies.
23
20
  * Load and create the analytics tracker at the earliest opportunity so that:
24
21
  * the time until the first pageview is tracked is kept small and pageviews aren’t missed
@@ -29,7 +26,7 @@ The minimum you need to use the analytics function is:
29
26
  "use strict";
30
27
 
31
28
  // Load Google Analytics libraries
32
- GOVUK.Tracker.load();
29
+ GOVUK.Analytics.load();
33
30
 
34
31
  // Use document.domain in dev, preview and staging so that tracking works
35
32
  // Otherwise explicitly set the domain as www.gov.uk (and not gov.uk).
@@ -37,9 +34,8 @@ The minimum you need to use the analytics function is:
37
34
 
38
35
  // Configure profiles and make interface public
39
36
  // for custom dimensions, virtual pageviews and events
40
- GOVUK.analytics = new GOVUK.Tracker({
37
+ GOVUK.analytics = new GOVUK.Analytics({
41
38
  universalId: 'UA-XXXXXXXX-X',
42
- classicId: 'UA-XXXXXXXX-X',
43
39
  cookieDomain: cookieDomain
44
40
  });
45
41
 
@@ -61,7 +57,6 @@ Once instantiated, the `GOVUK.analytics` object can be used to track virtual pag
61
57
 
62
58
  > Page tracking allows you to measure the number of views you had of a particular page on your web site.
63
59
 
64
- * [Classic Analytics](https://developers.google.com/analytics/devguides/collection/gajs/asyncMigrationExamples#VirtualPageviews)
65
60
  * [Universal Analytics](https://developers.google.com/analytics/devguides/collection/analyticsjs/pages)
66
61
 
67
62
  Argument | Description
@@ -85,7 +80,6 @@ GOVUK.analytics.trackPageview('/path', 'Title');
85
80
 
86
81
  > Event tracking allows you to measure how users interact with the content of your website. For example, you might want to measure how many times a button was pressed, or how many times a particular item was used.
87
82
 
88
- * [Classic Analytics](https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide)
89
83
  * [Universal Analytics](https://developers.google.com/analytics/devguides/collection/analyticsjs/events)
90
84
 
91
85
  Argument | Description
@@ -112,45 +106,35 @@ GOVUK.analytics.trackEvent('category', 'action', {
112
106
  });
113
107
  ```
114
108
 
115
- ## Custom dimensions and custom variables
109
+ ## Custom dimensions
116
110
 
117
111
  > Custom dimensions and metrics are a powerful way to send custom data to Google Analytics. Use custom dimensions and metrics to segment and measure differences between: logged in and logged out users, authors of pages, or any other business data you have on a page.
118
112
 
119
- * [Classic Analytics](https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables)
120
113
  * [Universal Analytics](https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets)
121
114
 
122
- Universal custom dimensions are configured within analytics. Classic custom variables rely on the javascript to additionally pass in the name and scope of the variable.
115
+ Universal custom dimensions are configured within analytics.
123
116
 
124
117
  ### Set custom dimensions before tracking pageviews
125
118
 
126
119
  Many page level custom dimensions must be set before the initial pageview is tracked. Calls to `setDimension` should typically be made before the initial `trackPageview` is sent to analytics.
127
120
 
128
- ### Custom dimensions and variables must have matching indexes
129
-
130
- When calling `setDimension`, the first argument (`index`) becomes the index of the Universal custom dimension as well as the slot of the Classic custom variable.
131
-
132
121
  Argument | Description
133
122
  ---------|------------
134
- `index` (required) | The Universal dimension’s index and and Classic variable’s slot. These must be configured to the same slot and index within analytics profiles.
135
- `value` (required) | Value of the custom dimension/variable
136
- `name` (required) | The name of the custom variable (classic only)
137
- `scope` (optional) | The scope of the custom variable (classic only), defaults to a [page level scope](https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables#pagelevel) (3) if omitted
123
+ `index` (required) | The Universal dimension’s index as configured in the profile.
124
+ `value` (required) | Value of the custom dimension
138
125
 
139
126
  ```js
140
127
  // Set a custom dimension at index 1 with value and name
141
- GOVUK.analytics.setDimension(1, 'value', 'name');
142
-
143
- // Set a custom dimension with an alternative scope
144
- GOVUK.analytics.setDimension(1, 'value', 'name', 2);
128
+ GOVUK.analytics.setDimension(1, 'value');
145
129
  ```
146
130
 
147
131
  ### Create custom dimension helpers
148
132
 
149
- Because dimensions and variables rely on names and indexes being set correctly, it’s helpful to create methods that abstract away the details. For example:
133
+ Because dimensions rely on the correct index and that index doesn’t indicate its purpose, it’s helpful to create methods that abstract away the details. For example:
150
134
 
151
135
  ```js
152
136
  function setPixelDensityDimension(pixelDensity) {
153
- GOVUK.analytics.setDimension(1, pixelDensity, 'PixelDensity', 2);
137
+ GOVUK.analytics.setDimension(1, pixelDensity);
154
138
  }
155
139
  ```
156
140
 
@@ -164,7 +148,7 @@ Pull `error-tracking.js` into your project, and initialise it after analytics (s
164
148
 
165
149
  ## Tracking across domains
166
150
 
167
- Once a Tracker instance has been created, tracking across domains can be set up
151
+ Once an Analytics instance has been created, tracking across domains can be set up
168
152
  for pages like:
169
153
 
170
154
  ```js
@@ -0,0 +1,64 @@
1
+ (function() {
2
+ "use strict";
3
+ window.GOVUK = window.GOVUK || {};
4
+
5
+ // For usage and initialisation see:
6
+ // https://github.com/alphagov/govuk_frontend_toolkit/blob/master/docs/analytics.md#create-an-analytics-tracker
7
+
8
+ var Analytics = function(config) {
9
+ this.trackers = [];
10
+ if (typeof config.universalId != 'undefined') {
11
+ this.trackers.push(new GOVUK.GoogleAnalyticsUniversalTracker(config.universalId, config.cookieDomain));
12
+ }
13
+ };
14
+
15
+ Analytics.prototype.sendToTrackers = function(method, args) {
16
+ for (var i = 0, l = this.trackers.length; i < l; i++) {
17
+ var tracker = this.trackers[i],
18
+ fn = tracker[method];
19
+
20
+ if (typeof fn === "function") {
21
+ fn.apply(tracker, args);
22
+ }
23
+ }
24
+ };
25
+
26
+ Analytics.load = function() {
27
+ GOVUK.GoogleAnalyticsUniversalTracker.load();
28
+ };
29
+
30
+ Analytics.prototype.trackPageview = function(path, title) {
31
+ this.sendToTrackers('trackPageview', arguments);
32
+ };
33
+
34
+ /*
35
+ https://developers.google.com/analytics/devguides/collection/analyticsjs/events
36
+ options.label – Useful for categorizing events (eg nav buttons)
37
+ options.value – Values must be non-negative. Useful to pass counts
38
+ options.nonInteraction – Prevent event from impacting bounce rate
39
+ */
40
+ Analytics.prototype.trackEvent = function(category, action, options) {
41
+ this.sendToTrackers('trackEvent', arguments);
42
+ };
43
+
44
+ Analytics.prototype.trackShare = function(network) {
45
+ this.sendToTrackers('trackSocial', [network, 'share', location.pathname]);
46
+ };
47
+
48
+ /*
49
+ The custom dimension index must be configured within the
50
+ Universal Analytics profile
51
+ */
52
+ Analytics.prototype.setDimension = function(index, value) {
53
+ this.sendToTrackers('setDimension', arguments);
54
+ };
55
+
56
+ /*
57
+ Add a beacon to track a page in another GA account on another domain.
58
+ */
59
+ Analytics.prototype.addLinkedTrackerDomain = function(trackerId, name, domain) {
60
+ this.sendToTrackers('addLinkedTrackerDomain', arguments);
61
+ };
62
+
63
+ GOVUK.Analytics = Analytics;
64
+ })();
@@ -7,9 +7,8 @@ var manifest = {
7
7
  '../../javascripts/govuk/stick-at-top-when-scrolling.js',
8
8
  '../../javascripts/govuk/stop-scrolling-at-footer.js',
9
9
  '../../javascripts/govuk/selection-buttons.js',
10
- '../../javascripts/govuk/analytics/google-analytics-classic-tracker.js',
11
10
  '../../javascripts/govuk/analytics/google-analytics-universal-tracker.js',
12
- '../../javascripts/govuk/analytics/tracker.js'
11
+ '../../javascripts/govuk/analytics/analytics.js'
13
12
 
14
13
  ],
15
14
  test : [
@@ -17,8 +16,7 @@ var manifest = {
17
16
  '../unit/PrimaryLinksSpec.js',
18
17
  '../unit/StickAtTopWhenScrollingSpec.js',
19
18
  '../unit/SelectionButtonSpec.js',
20
- '../unit/analytics/GoogleAnalyticsClassicTrackerSpec.js',
21
19
  '../unit/analytics/GoogleAnalyticsUniversalTrackerSpec.js',
22
- '../unit/analytics/TrackerSpec.js'
20
+ '../unit/analytics/AnalyticsSpec.js'
23
21
  ]
24
22
  };
@@ -0,0 +1,63 @@
1
+ describe("GOVUK.Analytics", function() {
2
+ var analytics;
3
+
4
+ beforeEach(function() {
5
+ window.ga = function() {};
6
+ spyOn(window, 'ga');
7
+ this.config = {
8
+ universalId: 'universal-id',
9
+ cookieDomain: '.www.gov.uk'
10
+ };
11
+
12
+ analytics = new GOVUK.Analytics(this.config);
13
+ });
14
+
15
+ describe('when created', function() {
16
+ it('configures a universal tracker', function () {
17
+ var universalSetupArguments = window.ga.calls.allArgs();
18
+ expect(universalSetupArguments[0]).toEqual(['create', 'universal-id', {'cookieDomain': '.www.gov.uk'}]);
19
+ });
20
+ });
21
+
22
+ describe('when tracking pageviews, events and custom dimensions', function() {
23
+ it('tracks them in universal analytics', function() {
24
+ analytics.trackPageview('/path', 'Title');
25
+ expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview', {page: '/path', title: 'Title'}]);
26
+
27
+ analytics.trackEvent('category', 'action');
28
+ expect(window.ga.calls.mostRecent().args).toEqual(['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action'}]);
29
+
30
+ analytics.setDimension(1, 'value', 'name');
31
+ expect(window.ga.calls.mostRecent().args).toEqual(['set', 'dimension1', 'value']);
32
+ });
33
+ });
34
+
35
+ describe('when tracking social media shares', function() {
36
+ it('tracks in universal', function() {
37
+ analytics.trackShare('network');
38
+
39
+ expect(window.ga.calls.mostRecent().args).toEqual(['send', {
40
+ hitType: 'social',
41
+ socialNetwork: 'network',
42
+ socialAction: 'share',
43
+ socialTarget: jasmine.any(String)
44
+ }]);
45
+ });
46
+ });
47
+
48
+ describe('when adding a linked domain', function() {
49
+ it('adds a linked domain to universal analytics', function() {
50
+ analytics.addLinkedTrackerDomain('1234', 'test', 'www.example.com');
51
+
52
+ var allArgs = window.ga.calls.allArgs()
53
+ expect(allArgs).toContain(['create', '1234', 'auto', {'name': 'test'}]);
54
+ expect(allArgs).toContain(['require', 'linker']);
55
+ expect(allArgs).toContain(['test.require', 'linker']);
56
+ expect(allArgs).toContain(['linker:autoLink', ['www.example.com']]);
57
+ expect(allArgs).toContain(['test.linker:autoLink', ['www.example.com']]);
58
+ expect(allArgs).toContain(['test.set', 'anonymizeIp', true]);
59
+ expect(allArgs).toContain(['test.send', 'pageview']);
60
+ });
61
+ });
62
+
63
+ });
@@ -108,5 +108,5 @@ $alpha-colour: $pink; // Alpha badges and banners
108
108
  $beta-colour: $orange; // Beta badges and banners
109
109
  $live-colour: $grass-green; // Live badges and banners
110
110
  $banner-text-colour: #000; // Text colour for Alpha & Beta banners
111
- $error-colour: #af1324; // Error text and border colour
111
+ $error-colour: $red; // Error text and border colour
112
112
  $error-background: #fef7f7; // Error background colour
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_frontend_toolkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.5.1
4
+ version: 4.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-11 00:00:00.000000000 Z
12
+ date: 2015-05-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -230,11 +230,10 @@ files:
230
230
  - app/assets/images/player-icon-play.png
231
231
  - app/assets/images/player-icon-rewind.png
232
232
  - app/assets/images/player-icon-volume.png
233
+ - app/assets/javascripts/govuk/analytics/analytics.js
233
234
  - app/assets/javascripts/govuk/analytics/error-tracking.js
234
- - app/assets/javascripts/govuk/analytics/google-analytics-classic-tracker.js
235
235
  - app/assets/javascripts/govuk/analytics/google-analytics-universal-tracker.js
236
236
  - app/assets/javascripts/govuk/analytics/print-intent.js
237
- - app/assets/javascripts/govuk/analytics/tracker.js
238
237
  - app/assets/javascripts/govuk/multivariate-test.js
239
238
  - app/assets/javascripts/govuk/primary-links.js
240
239
  - app/assets/javascripts/govuk/selection-buttons.js
@@ -255,9 +254,8 @@ files:
255
254
  - app/assets/spec/unit/PrimaryLinksSpec.js
256
255
  - app/assets/spec/unit/SelectionButtonSpec.js
257
256
  - app/assets/spec/unit/StickAtTopWhenScrollingSpec.js
258
- - app/assets/spec/unit/analytics/GoogleAnalyticsClassicTrackerSpec.js
257
+ - app/assets/spec/unit/analytics/AnalyticsSpec.js
259
258
  - app/assets/spec/unit/analytics/GoogleAnalyticsUniversalTrackerSpec.js
260
- - app/assets/spec/unit/analytics/TrackerSpec.js
261
259
  - app/assets/stylesheets/.gitkeep
262
260
  - app/assets/stylesheets/_colours.scss
263
261
  - app/assets/stylesheets/_conditionals.scss
@@ -288,7 +286,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
288
286
  version: '0'
289
287
  segments:
290
288
  - 0
291
- hash: 609319140606558990
289
+ hash: -4368638161616366366
292
290
  required_rubygems_version: !ruby/object:Gem::Requirement
293
291
  none: false
294
292
  requirements:
@@ -297,7 +295,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
297
295
  version: '0'
298
296
  segments:
299
297
  - 0
300
- hash: 609319140606558990
298
+ hash: -4368638161616366366
301
299
  requirements: []
302
300
  rubyforge_project:
303
301
  rubygems_version: 1.8.23
@@ -1,130 +0,0 @@
1
- (function() {
2
- "use strict";
3
- window.GOVUK = window.GOVUK || {};
4
-
5
- var GoogleAnalyticsClassicTracker = function(id, cookieDomain) {
6
- window._gaq = window._gaq || [];
7
- configureProfile(id, cookieDomain);
8
- allowCrossDomainTracking();
9
- anonymizeIp();
10
-
11
- function configureProfile(id, cookieDomain) {
12
- _gaq.push(['_setAccount', id]);
13
- _gaq.push(['_setDomainName', cookieDomain]);
14
- }
15
-
16
- function allowCrossDomainTracking() {
17
- _gaq.push(['_setAllowLinker', true]);
18
- }
19
-
20
- // https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat#_gat._anonymizeIp
21
- function anonymizeIp() {
22
- _gaq.push(['_gat._anonymizeIp']);
23
- }
24
- };
25
-
26
- GoogleAnalyticsClassicTracker.load = function() {
27
- var ga = document.createElement('script'),
28
- s = document.getElementsByTagName('script')[0];
29
-
30
- ga.async = true;
31
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
32
- s.parentNode.insertBefore(ga, s);
33
- };
34
-
35
- // https://developers.google.com/analytics/devguides/collection/gajs/asyncMigrationExamples#VirtualPageviews
36
- GoogleAnalyticsClassicTracker.prototype.trackPageview = function(path) {
37
- var pageview = ['_trackPageview'];
38
-
39
- if (typeof path === "string") {
40
- pageview.push(path);
41
- }
42
-
43
- _gaq.push(pageview);
44
- };
45
-
46
- // https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
47
- GoogleAnalyticsClassicTracker.prototype.trackEvent = function(category, action, options) {
48
- var value,
49
- options = options || {},
50
- hasLabel = false,
51
- hasValue = false,
52
- evt = ["_trackEvent", category, action];
53
-
54
- // Label is optional
55
- if (typeof options.label === "string") {
56
- hasLabel = true;
57
- evt.push(options.label);
58
- }
59
-
60
- // Value is optional, but when used must be an
61
- // integer, otherwise the event will be invalid
62
- // and not logged
63
- if (options.value || options.value === 0) {
64
- value = parseInt(options.value, 10);
65
- if (typeof value === "number" && !isNaN(value)) {
66
- hasValue = true;
67
-
68
- // Push an empty label if not set for correct final argument order
69
- if (!hasLabel) {
70
- evt.push('');
71
- }
72
-
73
- evt.push(value);
74
- }
75
- }
76
-
77
- // Prevents an event from affecting bounce rate
78
- // https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#non-interaction
79
- if (options.nonInteraction) {
80
-
81
- // Push empty label/value if not already set, for correct final argument order
82
- if (!hasValue) {
83
- if (!hasLabel) {
84
- evt.push('');
85
- }
86
- evt.push(0);
87
- }
88
-
89
- evt.push(true);
90
- }
91
-
92
- _gaq.push(evt);
93
- };
94
-
95
- /*
96
- https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiSocialTracking
97
- network – The network on which the action occurs (e.g. Facebook, Twitter)
98
- action – The type of action that happens (e.g. Like, Send, Tweet)
99
- target – The text value that indicates the subject of the action
100
- */
101
- GoogleAnalyticsClassicTracker.prototype.trackSocial = function(network, action, target) {
102
- _gaq.push(['_trackSocial', network, action, target]);
103
- };
104
-
105
- // https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables
106
- GoogleAnalyticsClassicTracker.prototype.setCustomVariable = function(index, value, name, scope) {
107
- var PAGE_LEVEL_SCOPE = 3;
108
- scope = scope || PAGE_LEVEL_SCOPE;
109
-
110
- if (typeof index !== "number") {
111
- index = parseInt(index, 10);
112
- }
113
-
114
- if (typeof scope !== "number") {
115
- scope = parseInt(scope, 10);
116
- }
117
-
118
- _gaq.push(['_setCustomVar', index, name, String(value), scope]);
119
- };
120
-
121
- // Match tracker and universal API
122
- GoogleAnalyticsClassicTracker.prototype.setDimension = function(index, value, name, scope) {
123
- this.setCustomVariable(index, value, name, scope);
124
- };
125
-
126
- // Match tracker and universal API
127
- GoogleAnalyticsClassicTracker.prototype.addLinkedTrackerDomain = function() {};
128
-
129
- GOVUK.GoogleAnalyticsClassicTracker = GoogleAnalyticsClassicTracker;
130
- })();
@@ -1,68 +0,0 @@
1
- (function() {
2
- "use strict";
3
- window.GOVUK = window.GOVUK || {};
4
-
5
- // For usage and initialisation see:
6
- // https://github.com/alphagov/govuk_frontend_toolkit/blob/master/docs/analytics.md#create-an-analytics-tracker
7
-
8
- var Tracker = function(config) {
9
- this.trackers = [];
10
- if (typeof config.universalId != 'undefined') {
11
- this.trackers.push(new GOVUK.GoogleAnalyticsUniversalTracker(config.universalId, config.cookieDomain));
12
- }
13
- if (typeof config.classicId != 'undefined') {
14
- this.trackers.push(new GOVUK.GoogleAnalyticsClassicTracker(config.classicId, config.cookieDomain));
15
- }
16
- };
17
-
18
- Tracker.load = function() {
19
- GOVUK.GoogleAnalyticsClassicTracker.load();
20
- GOVUK.GoogleAnalyticsUniversalTracker.load();
21
- };
22
-
23
- Tracker.prototype.trackPageview = function(path, title) {
24
- for (var i=0; i < this.trackers.length; i++) {
25
- this.trackers[i].trackPageview(path, title);
26
- }
27
- };
28
-
29
- /*
30
- https://developers.google.com/analytics/devguides/collection/analyticsjs/events
31
- options.label – Useful for categorizing events (eg nav buttons)
32
- options.value – Values must be non-negative. Useful to pass counts
33
- options.nonInteraction – Prevent event from impacting bounce rate
34
- */
35
- Tracker.prototype.trackEvent = function(category, action, options) {
36
- for (var i=0; i < this.trackers.length; i++) {
37
- this.trackers[i].trackEvent(category, action, options);
38
- }
39
- };
40
-
41
- Tracker.prototype.trackShare = function(network) {
42
- var target = location.pathname;
43
- for (var i=0; i < this.trackers.length; i++) {
44
- this.trackers[i].trackSocial(network, 'share', target);
45
- }
46
- };
47
-
48
- /*
49
- Assumes that the index of the dimension is the same for both classic and universal.
50
- Check this for your app before using this
51
- */
52
- Tracker.prototype.setDimension = function(index, value, name, scope) {
53
- for (var i=0; i < this.trackers.length; i++) {
54
- this.trackers[i].setDimension(index, value, name, scope);
55
- }
56
- };
57
-
58
- /*
59
- Add a beacon to track a page in another GA account on another domain.
60
- */
61
- Tracker.prototype.addLinkedTrackerDomain = function(trackerId, name, domain) {
62
- for (var i=0; i < this.trackers.length; i++) {
63
- this.trackers[i].addLinkedTrackerDomain(trackerId, name, domain);
64
- }
65
- };
66
-
67
- GOVUK.Tracker = Tracker;
68
- })();
@@ -1,124 +0,0 @@
1
- describe("GOVUK.GoogleAnalyticsClassicTracker", function() {
2
- var classic;
3
-
4
- beforeEach(function() {
5
- window._gaq = [];
6
- classic = new GOVUK.GoogleAnalyticsClassicTracker('id', 'cookie-domain.com');
7
- });
8
-
9
- it('can load the libraries needed to run classic Google Analytics', function() {
10
- delete window._gaq;
11
- $('[src*="google-analytics.com/ga.js"]').remove();
12
-
13
- GOVUK.GoogleAnalyticsClassicTracker.load();
14
- expect($('script[async][src*="google-analytics.com/ga.js"]').length).toBe(1);
15
- });
16
-
17
- describe('when created', function() {
18
- it('configures a Google tracker using the provided profile ID and cookie domain', function() {
19
- expect(window._gaq[0]).toEqual(['_setAccount', 'id']);
20
- expect(window._gaq[1]).toEqual(['_setDomainName', 'cookie-domain.com']);
21
- });
22
-
23
- it('allows cross site linking', function() {
24
- expect(window._gaq[2]).toEqual(['_setAllowLinker', true]);
25
- });
26
-
27
- it('anonymises the IP', function() {
28
- expect(window._gaq[3]).toEqual(['_gat._anonymizeIp']);
29
- });
30
- });
31
-
32
- describe('when pageviews are tracked', function() {
33
- beforeEach(function() {
34
- // reset queue after setup
35
- window._gaq = [];
36
- });
37
-
38
- it('sends them to Google Analytics', function() {
39
- classic.trackPageview();
40
- expect(window._gaq[0]).toEqual(['_trackPageview']);
41
- });
42
-
43
- it('can track a virtual pageview', function() {
44
- classic.trackPageview('/nicholas-page');
45
- expect(window._gaq[0]).toEqual(['_trackPageview', '/nicholas-page']);
46
- });
47
- });
48
-
49
- describe('when events are tracked', function() {
50
- beforeEach(function() {
51
- // reset queue after setup
52
- window._gaq = [];
53
- });
54
-
55
- it('sends them to Google Analytics', function() {
56
- classic.trackEvent('category', 'action', {label: 'label'});
57
- expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', 'label']);
58
- });
59
-
60
- it('the label is optional', function() {
61
- classic.trackEvent('category', 'action');
62
- expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action']);
63
- });
64
-
65
- it('a value can be tracked if the label is omitted', function() {
66
- classic.trackEvent('category', 'action', {value: 10});
67
- expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', '', 10]);
68
- });
69
-
70
- it('only sends values if they are parseable as numbers', function() {
71
- classic.trackEvent('category', 'action', {label: 'label', value: '10'});
72
- expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', 'label', 10]);
73
-
74
- classic.trackEvent('category', 'action', {label: 'label', value: 10});
75
- expect(window._gaq[1]).toEqual(['_trackEvent', 'category', 'action', 'label', 10]);
76
-
77
- classic.trackEvent('category', 'action', {label: 'label', value: 'not a number'});
78
- expect(window._gaq[2]).toEqual(['_trackEvent', 'category', 'action', 'label']);
79
- });
80
-
81
- it('can mark an event as non interactive', function() {
82
- classic.trackEvent('category', 'action', {label: 'label', value: 1, nonInteraction: true});
83
- expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', 'label', 1, true]);
84
- });
85
-
86
- it('can mark an event as non interactive without a value', function() {
87
- classic.trackEvent('category', 'action', {label: 'label', nonInteraction: true});
88
- expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', 'label', 0, true]);
89
- });
90
-
91
- it('can mark an event as non interactive without a label or value', function() {
92
- classic.trackEvent('category', 'action', {nonInteraction: true});
93
- expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', '', 0, true]);
94
- });
95
- });
96
-
97
- describe('when social events are tracked', function() {
98
- beforeEach(function() {
99
- window._gaq = [];
100
- });
101
-
102
- it('sends them to Google Analytics', function() {
103
- classic.trackSocial('network', 'action', 'target');
104
- expect(window._gaq[0]).toEqual(['_trackSocial', 'network', 'action', 'target']);
105
- });
106
- });
107
-
108
- describe('when setting a custom variable', function() {
109
- beforeEach(function() {
110
- // reset queue after setup
111
- window._gaq = [];
112
- });
113
-
114
- it('sends the variable to Google Analytics', function() {
115
- classic.setCustomVariable(1, 'value', 'name', 10);
116
- expect(window._gaq[0]).toEqual(['_setCustomVar', 1, 'name', 'value', 10]);
117
- });
118
-
119
- it('coerces the value to a string', function() {
120
- classic.setCustomVariable(1, 100, 'name', 10);
121
- expect(window._gaq[0]).toEqual(['_setCustomVar', 1, 'name', '100', 10]);
122
- });
123
- });
124
- });
@@ -1,115 +0,0 @@
1
- describe("GOVUK.Tracker", function() {
2
- var tracker;
3
-
4
- beforeEach(function() {
5
- window._gaq = [];
6
- window.ga = function() {};
7
- spyOn(window, 'ga');
8
- this.config = {
9
- universalId: 'universal-id',
10
- classicId: 'classic-id',
11
- cookieDomain: '.www.gov.uk'
12
- };
13
- });
14
-
15
- describe('when created', function() {
16
- var universalSetupArguments;
17
-
18
- beforeEach(function () {
19
- tracker = new GOVUK.Tracker(this.config);
20
- universalSetupArguments = window.ga.calls.allArgs();
21
- });
22
-
23
- it('configures classic and universal trackers', function () {
24
- expect(window._gaq[0]).toEqual(['_setAccount', 'classic-id']);
25
- expect(window._gaq[1]).toEqual(['_setDomainName', '.www.gov.uk']);
26
- expect(universalSetupArguments[0]).toEqual(['create', 'universal-id', {'cookieDomain': '.www.gov.uk'}]);
27
- });
28
-
29
- });
30
-
31
- describe('when created with only universal analytics', function() {
32
- var universalSetupArguments;
33
-
34
- beforeEach(function () {
35
- });
36
-
37
- it ('doesn\'t require both trackers to be present', function() {
38
- universalOnlyConfig = { universalId: 'universal-id',
39
- cookieDomain: '.www.gov.uk'
40
- };
41
-
42
- var tracker = new GOVUK.Tracker(universalOnlyConfig);
43
-
44
- universalSetupArguments = window.ga.calls.allArgs();
45
- expect(typeof(window._gaq[0])).toEqual('undefined');
46
- expect(universalSetupArguments[0]).toEqual(['create', 'universal-id', {'cookieDomain': '.www.gov.uk'}]);
47
- });
48
- });
49
-
50
- describe('when tracking pageviews, events and custom dimensions', function() {
51
-
52
- beforeEach(function() {
53
- tracker = new GOVUK.Tracker(this.config);
54
- });
55
-
56
- it('tracks in both classic and universal', function() {
57
- window._gaq = [];
58
- tracker.trackPageview('/path', 'Title');
59
- expect(window._gaq[0]).toEqual(['_trackPageview', '/path']);
60
- expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview', {page: '/path', title: 'Title'}]);
61
-
62
- window._gaq = [];
63
- tracker.trackEvent('category', 'action');
64
- expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action']);
65
- expect(window.ga.calls.mostRecent().args).toEqual(['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action'}]);
66
-
67
- window._gaq = [];
68
- tracker.setDimension(1, 'value', 'name');
69
- expect(window._gaq[0]).toEqual(['_setCustomVar', 1, 'name', 'value', 3]);
70
- expect(window.ga.calls.mostRecent().args).toEqual(['set', 'dimension1', 'value']);
71
- });
72
- });
73
-
74
- describe('when tracking social media shares', function() {
75
-
76
- beforeEach(function() {
77
- tracker = new GOVUK.Tracker(this.config);
78
- });
79
-
80
- it('tracks in both classic and universal', function() {
81
- window._gaq = [];
82
- tracker.trackShare('network');
83
-
84
- expect(window._gaq[0]).toEqual(['_trackSocial', 'network', 'share', jasmine.any(String)]);
85
- expect(window.ga.calls.mostRecent().args).toEqual(['send', {
86
- hitType: 'social',
87
- socialNetwork: 'network',
88
- socialAction: 'share',
89
- socialTarget: jasmine.any(String)
90
- }]);
91
- });
92
- });
93
-
94
- describe('when adding a linked domain', function() {
95
- beforeEach(function() {
96
- tracker = new GOVUK.Tracker(this.config);
97
- });
98
-
99
- it('adds a linked domain to universal only', function() {
100
- window._gaq = [];
101
- tracker.addLinkedTrackerDomain('1234', 'test', 'www.example.com');
102
-
103
- expect(window._gaq).toEqual([]);
104
- var allArgs = window.ga.calls.allArgs()
105
- expect(allArgs).toContain(['create', '1234', 'auto', {'name': 'test'}]);
106
- expect(allArgs).toContain(['require', 'linker']);
107
- expect(allArgs).toContain(['test.require', 'linker']);
108
- expect(allArgs).toContain(['linker:autoLink', ['www.example.com']]);
109
- expect(allArgs).toContain(['test.linker:autoLink', ['www.example.com']]);
110
- expect(allArgs).toContain(['test.set', 'anonymizeIp', true]);
111
- expect(allArgs).toContain(['test.send', 'pageview']);
112
- });
113
- });
114
-
115
- });