/
static-tracker.js
189 lines (148 loc) · 5.79 KB
/
static-tracker.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
(function() {
"use strict";
window.GOVUK = window.GOVUK || {};
var StaticTracker = function(config) {
var classicQueue,
tracker;
classicQueue = getClassicAnalyticsQueue();
resetClassicAnalyticsQueue();
// Create universal and classic analytics tracker
// https://github.com/alphagov/govuk_frontend_toolkit/blob/master/docs/analytics.md
// https://github.com/alphagov/govuk_frontend_toolkit/blob/master/javascripts/govuk/analytics/tracker.js
tracker = new GOVUK.Tracker(config);
this.tracker = tracker;
setPixelDensityDimension();
setHTTPStatusCodeDimension();
shimNextPageParams();
shimClassicAnalyticsQueue(classicQueue);
this.setDimensionsFromMetaTags();
this.callMethodRequestedByPreviousPage();
// Track initial pageview
tracker.trackPageview();
// Begin error and print tracking
GOVUK.analyticsPlugins.error();
GOVUK.analyticsPlugins.printIntent();
function getClassicAnalyticsQueue() {
// Slimmer inserts custom variables into the ga-params script tag
// https://github.com/alphagov/slimmer/blob/master/lib/slimmer/processors/google_analytics_configurator.rb
// Pickout these variables before continuing
return (window._gaq && window._gaq.length) > 0 ? window._gaq.slice() : [];
}
function resetClassicAnalyticsQueue() {
window._gaq = [];
}
function setPixelDensityDimension() {
if (window.devicePixelRatio) {
tracker.setDimension(11, window.devicePixelRatio, 'Pixel Ratio', 2);
}
}
function setHTTPStatusCodeDimension() {
tracker.setDimension(15, window.httpStatusCode || 200, 'httpStatusCode');
}
function shimNextPageParams() {
// A cookie is sometimes set by apps to declare the GA parameters that
// should run on the subsequent page. These declare the actual methods
// to call in classic analytics. This is a temporary shim to ensure these
// are applied to both classic and universal before updating apps.
if (GOVUK.cookie && GOVUK.cookie('ga_nextpage_params') !== null){
var classicParams = GOVUK.cookie('ga_nextpage_params').split(',');
if (classicParams[0] == "_setCustomVar") {
setDimensionFromCustomVariable(classicParams);
}
// Delete cookie
GOVUK.cookie('ga_nextpage_params', null);
}
}
function shimClassicAnalyticsQueue(queue) {
$.each(queue, function(index, classicParams) {
if (classicParams[0] == "_setCustomVar") {
setDimensionFromCustomVariable(classicParams);
}
});
}
function setDimensionFromCustomVariable(customVar) {
// index, value, name, scope
tracker.setDimension(customVar[1], customVar[3], customVar[2], customVar[4]);
}
};
StaticTracker.prototype.callOnNextPage = function(method, params) {
params = params || [];
if (!$.isArray(params)) {
params = [params];
}
if (GOVUK.cookie && typeof this[method] === "function") {
params.unshift(method);
GOVUK.cookie('analytics_next_page_call', JSON.stringify(params));
}
};
StaticTracker.prototype.callMethodRequestedByPreviousPage = function() {
if (GOVUK.cookie && GOVUK.cookie('analytics_next_page_call') !== null) {
var params, method;
try {
params = JSON.parse(GOVUK.cookie('analytics_next_page_call'));
method = params.shift();
} catch(e) {}
if (method && typeof this[method] === "function") {
this[method].apply(this, params);
}
// Delete cookie
GOVUK.cookie('analytics_next_page_call', null);
}
};
StaticTracker.load = function() {
GOVUK.Tracker.load();
};
StaticTracker.prototype.setDimensionsFromMetaTags = function() {
var $metas = $('meta[name^="govuk:"]'),
dimensions = {};
$metas.each(function() {
var $meta = $(this),
key = $meta.attr('name').split('govuk:')[1],
value = $meta.attr('content');
dimensions[key] = value;
});
this.setSectionDimension(dimensions['section']);
this.setFormatDimension(dimensions['format']);
this.setNeedIDsDimension(dimensions['need-ids']);
this.setResultCountDimension(dimensions['search-result-count']);
this.setOrganisationsDimension(dimensions['analytics:organisations']);
this.setWorldLocationsDimension(dimensions['analytics:world-locations']);
};
StaticTracker.prototype.trackPageview = function(path, title) {
this.tracker.trackPageview(path, title);
};
StaticTracker.prototype.trackEvent = function(category, action, options) {
this.tracker.trackEvent(category, action, options);
};
StaticTracker.prototype.setDimension = function(index, value, name, scope) {
if (typeof value === "undefined") {
return;
}
this.tracker.setDimension(index, value, name, scope);
};
StaticTracker.prototype.trackShare = function(network) {
this.tracker.trackShare(network);
};
StaticTracker.prototype.setSectionDimension = function(section) {
this.setDimension(1, section, 'Section');
};
StaticTracker.prototype.setFormatDimension = function(format) {
this.setDimension(2, format, 'Format');
};
StaticTracker.prototype.setNeedIDsDimension = function(ids) {
this.setDimension(3, ids, 'NeedID');
};
StaticTracker.prototype.setResultCountDimension = function(count) {
this.setDimension(5, count, 'ResultCount');
};
StaticTracker.prototype.setOrganisationsDimension = function(orgs) {
this.setDimension(9, orgs, 'Organisations');
};
StaticTracker.prototype.setWorldLocationsDimension = function(locations) {
this.setDimension(10, locations, 'WorldLocations');
};
StaticTracker.prototype.setSearchPositionDimension = function(position) {
this.setDimension(21, position, 'searchPosition');
};
GOVUK.StaticTracker = StaticTracker;
})();