/
analytics.es6.js
216 lines (191 loc) · 7.38 KB
/
analytics.es6.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Ensure window.dataLayer is always defined, even if GTM might not have loaded.
window.dataLayer = window.dataLayer || [];
const PocketAnalytics = {
loaded: false,
loadGA: () => {
// Google Tag Manager used to load GA4 (gtm.js)
const GTM_CONTAINER_ID = document
.getElementsByTagName('html')[0]
.getAttribute('data-gtm-container-id');
window.gtag = function () {
window.dataLayer.push(arguments);
};
if (GTM_CONTAINER_ID) {
(function (w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({
'gtm.start': new Date().getTime(),
event: 'gtm.js'
});
const f = d.getElementsByTagName(s)[0],
j = d.createElement(s),
dl = l !== 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', [GTM_CONTAINER_ID]);
}
// Google Universal Analytics (analytics.js)
const GOOGLE_ANALYTICS_ID = document
.getElementsByTagName('html')[0]
.getAttribute('data-google-analytics-id');
if (GOOGLE_ANALYTICS_ID) {
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
(i[r] =
i[r] ||
function () {
(i[r].q = i[r].q || []).push(arguments);
}),
(i[r].l = 1 * new Date());
(a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]);
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m);
})(
window,
document,
'script',
'https://www.google-analytics.com/analytics.js',
'ga'
);
try {
window.ga('create', GOOGLE_ANALYTICS_ID, 'auto');
window.ga('require', 'displayfeatures');
window.ga('send', 'pageview');
} catch (e) {
//do nothing
}
}
},
// SnowPlow Analytics.
loadSnowPlow: (config) => {
const SNOWPLOW_APP_ID = document
.getElementsByTagName('html')[0]
.getAttribute('data-snowplow-app-id');
const SNOWPLOW_SCRIPT_SRC = document
.getElementsByTagName('html')[0]
.getAttribute('data-snowplow-script-src');
const SNOWPLOW_CONNECT_URL = document
.getElementsByTagName('html')[0]
.getAttribute('data-snowplow-connect-url');
if (!SNOWPLOW_APP_ID || !SNOWPLOW_SCRIPT_SRC || !SNOWPLOW_CONNECT_URL) {
return;
}
function getUserData() {
const data = {
hashed_user_id: Mozilla.Cookies.getItem('a95b4b6'), // set only when logged-in through the Pocket app.
hashed_guid: Mozilla.Cookies.getItem('sess_guid') // set only when visiting non-marketing pages.
};
Object.keys(data).forEach((key) => {
if (data[key] === null) {
delete data[key];
}
});
if (Object.keys(data).length) {
return data;
} else {
return null;
}
}
// https://docs.snowplowanalytics.com/docs/collecting-data/collecting-from-own-applications/javascript-trackers/javascript-tracker/javascript-tracker-v3/tracker-setup/loading/
(function (p, l, o, w, i, n, g) {
if (!p[i]) {
p.GlobalSnowplowNamespace = p.GlobalSnowplowNamespace || [];
p.GlobalSnowplowNamespace.push(i);
p[i] = function () {
(p[i].q = p[i].q || []).push(arguments);
};
p[i].q = p[i].q || [];
n = l.createElement(o);
g = l.getElementsByTagName(o)[0];
n.async = 1;
n.src = w;
g.parentNode.insertBefore(n, g);
}
})(window, document, 'script', SNOWPLOW_SCRIPT_SRC, 'snowplow');
// https://docs.snowplowanalytics.com/docs/collecting-data/collecting-from-own-applications/javascript-trackers/javascript-tracker/javascript-tracker-v3/tracker-setup/initialization-options/
window.snowplow('newTracker', 'sp', SNOWPLOW_CONNECT_URL, {
appId: SNOWPLOW_APP_ID,
platform: 'web',
eventMethod: config.eventMethod,
respectDoNotTrack: false,
stateStorageStrategy: config.stateStorageStrategy,
contexts: {
webPage: true,
performanceTiming: true
},
anonymousTracking: config.anonymousTracking
});
const data = getUserData();
if (data) {
window.snowplow('addGlobalContexts', {
context: [
{
schema: `iglu:com.pocket/user/jsonschema/1-0-0`,
data
}
]
});
}
window.snowplow('enableActivityTracking', {
// integers are in seconds
minimumVisitLength: 10,
heartbeatDelay: 10
});
window.snowplow('enableLinkClickTracking');
window.snowplow('enableFormTracking');
window.snowplow('trackPageView');
},
loadAnalyticsScripts: (cookieConsent) => {
// Configure snowplow based on cookie consent.
const config = {
eventMethod: cookieConsent ? 'beacon' : 'post',
stateStorageStrategy: cookieConsent
? 'cookieAndLocalStorage'
: 'none',
anonymousTracking: cookieConsent
? false
: { withServerAnonymisation: true }
};
try {
PocketAnalytics.loadSnowPlow(config);
// Only load GA if user consents to analytics cookies.
if (cookieConsent) {
PocketAnalytics.loadGA();
}
} catch (e) {
// do nothing
}
PocketAnalytics.loaded = true;
},
reloadPage: () => {
window.location.reload();
}
};
window.PocketAnalytics = PocketAnalytics;
// Callback when OneTrust banner is either loaded or updated.
// This function *must* remain in global scope.
window.OptanonWrapper = function () {
const activeGroups = window.OptanonActiveGroups;
// C0002 is group ID for analytics cookies.
const cookieConsent =
typeof activeGroups === 'string' &&
activeGroups.indexOf('C0002') !== -1;
if (!PocketAnalytics.loaded) {
PocketAnalytics.loadAnalyticsScripts(cookieConsent);
return;
} else if (!cookieConsent) {
// Clear user data for existing snowplow session
if (typeof window.snowplow === 'function') {
window.snowplow('clearUserData', true, true);
}
// Reload the page to remove existing scripts.
PocketAnalytics.reloadPage();
}
};