-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
315 lines (292 loc) · 10.5 KB
/
index.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
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-env browser */
import { KNOWN_PROPERTIES, DEFAULT_TRACKING_EVENTS } from './defaults.js';
import { urlSanitizers } from './utils.js';
import { targetSelector, sourceSelector } from './dom.js';
import {
addAdsParametersTracking,
addCookieConsentTracking,
addEmailParameterTracking,
addUTMParametersTracking,
} from './martech.js';
import { fflags } from './fflags.js';
const { sampleRUM, queue, isSelected } = (window.hlx && window.hlx.rum) ? window.hlx.rum
/* c8 ignore next */ : {};
// blocks mutation observer
// eslint-disable-next-line no-use-before-define, max-len
const blocksMO = window.MutationObserver ? new MutationObserver(blocksMCB)
/* c8 ignore next */ : {};
// media mutation observer
// eslint-disable-next-line no-use-before-define, max-len
const mediaMO = window.MutationObserver ? new MutationObserver(mediaMCB)
/* c8 ignore next */ : {};
function trackCheckpoint(checkpoint, data, t) {
const { weight, id } = window.hlx.rum;
if (isSelected) {
const sendPing = (pdata = data) => {
// eslint-disable-next-line object-curly-newline, max-len
const body = JSON.stringify({ weight, id, referer: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'](), checkpoint, t, ...data }, KNOWN_PROPERTIES);
const urlParams = window.RUM_PARAMS ? `?${new URLSearchParams(window.RUM_PARAMS).toString()}` : '';
const { href: url, origin } = new URL(`.rum/${weight}${urlParams}`, sampleRUM.collectBaseURL);
if (window.location.origin === origin) {
const headers = { type: 'application/json' };
navigator.sendBeacon(url, new Blob([body], headers));
/* c8 ignore next 3 */
} else {
navigator.sendBeacon(url, body);
}
// eslint-disable-next-line no-console
console.debug(`ping:${checkpoint}`, pdata);
};
sendPing(data);
}
}
function processQueue() {
while (queue && queue.length) {
const ck = queue.shift();
trackCheckpoint(...ck);
}
}
function addCWVTracking() {
setTimeout(() => {
try {
const cwvScript = new URL('.rum/web-vitals/dist/web-vitals.iife.js', sampleRUM.baseURL).href;
if (document.querySelector(`script[src="${cwvScript}"]`)) {
// web vitals script has been loaded already
return;
}
const script = document.createElement('script');
script.src = cwvScript;
script.onload = () => {
const storeCWV = (measurement) => {
const data = { cwv: {} };
data.cwv[measurement.name] = measurement.value;
if (measurement.name === 'LCP' && measurement.entries.length > 0) {
const { element } = measurement.entries.pop();
data.target = targetSelector(element);
data.source = sourceSelector(element) || (element && element.outerHTML.slice(0, 30));
}
sampleRUM('cwv', data);
};
const isEager = (metric) => ['CLS', 'LCP'].includes(metric);
// When loading `web-vitals` using a classic script, all the public
// methods can be found on the `webVitals` global namespace.
['FID', 'INP', 'TTFB', 'CLS', 'LCP'].forEach((metric) => {
const metricFn = window.webVitals[`on${metric}`];
if (typeof metricFn === 'function') {
const opts = isEager(metric) ? { reportAllChanges: true } : undefined;
metricFn(storeCWV, opts);
}
});
};
document.head.appendChild(script);
/* c8 ignore next 3 */
} catch (error) {
// something went wrong
}
}, 2000); // wait for delayed
}
function addNavigationTracking() {
// enter checkpoint when referrer is not the current page url
const navigate = (source, type, redirectCount) => {
// target can be 'visible', 'hidden' (background tab) or 'prerendered' (speculation rules)
const payload = { source, target: document.visibilityState };
/* c8 ignore next 13 */
// prerendering cannot be tested yet with headless browsers
if (document.prerendering) {
// listen for "activation" of the current pre-rendered page
document.addEventListener('prerenderingchange', () => {
// pre-rendered page is now "activated"
payload.target = 'prerendered';
sampleRUM('navigate', payload); // prerendered navigation
}, {
once: true,
});
if (type === 'navigate') {
sampleRUM('prerender', payload); // prerendering page
}
} else if (type === 'reload' || source === window.location.href) {
sampleRUM('reload', payload);
} else if (type && type !== 'navigate') {
sampleRUM(type, payload); // back, forward, prerender, etc.
} else if (source && window.location.origin === new URL(source).origin) {
sampleRUM('navigate', payload); // internal navigation
} else {
sampleRUM('enter', payload); // enter site
}
fflags.enabled('redirect', () => {
const from = new URLSearchParams(window.location.search).get('redirect-from');
if (redirectCount || from) {
sampleRUM('redirect', { source: from, target: redirectCount || 1 });
}
});
};
new PerformanceObserver((list) => list
.getEntries().map((e) => navigate(
window.hlx.referrer || document.referrer,
e.type,
e.redirectCount,
)))
.observe({ type: 'navigation', buffered: true });
}
function addLoadResourceTracking() {
const observer = new PerformanceObserver((list) => {
try {
list.getEntries()
.filter((e) => !e.responseStatus || e.responseStatus < 400)
.filter((e) => window.location.hostname === new URL(e.name).hostname)
.filter((e) => new URL(e.name).pathname.match('.*(\\.plain\\.html$|\\.json|graphql|api)'))
.forEach((e) => {
sampleRUM('loadresource', { source: e.name, target: Math.round(e.duration) });
});
list.getEntries()
.filter((e) => e.responseStatus === 404)
.forEach((e) => {
sampleRUM('missingresource', { source: e.name, target: e.hostname });
});
/* c8 ignore next 3 */
} catch (error) {
// something went wrong
}
});
observer.observe({ type: 'resource', buffered: true });
}
// activate blocks mutation observer
function activateBlocksMO() {
if (!blocksMO || blocksMO.active) {
return;
}
blocksMO.active = true;
blocksMO.observe(
document.body,
// eslint-disable-next-line object-curly-newline
{ subtree: true, attributes: true, attributeFilter: ['data-block-status'] },
);
}
// activate media mutation observer
function activateMediaMO() {
if (!mediaMO || mediaMO.active) {
return;
}
mediaMO.active = true;
mediaMO.observe(
document.body,
// eslint-disable-next-line object-curly-newline
{ subtree: true, attributes: false, childList: true },
);
}
function getIntersectionObsever(checkpoint) {
/* c8 ignore next 3 */
if (!window.IntersectionObserver) {
return null;
}
activateBlocksMO();
activateMediaMO();
const observer = new IntersectionObserver((entries) => {
try {
entries
.filter((e) => e.isIntersecting)
.forEach((e) => {
observer.unobserve(e.target); // observe only once
const target = targetSelector(e.target);
const source = sourceSelector(e.target);
sampleRUM(checkpoint, { target, source });
});
/* c8 ignore next 3 */
} catch (error) {
// something went wrong
}
});
return observer;
}
function addViewBlockTracking(element) {
const blockobserver = getIntersectionObsever('viewblock');
if (blockobserver) {
const blocks = element.getAttribute('data-block-status') ? [element] : element.querySelectorAll('div[data-block-status="loaded"]');
blocks.forEach((b) => blockobserver.observe(b));
}
}
const observedMedia = new Set();
function addViewMediaTracking(parent) {
const mediaobserver = getIntersectionObsever('viewmedia');
if (mediaobserver) {
parent.querySelectorAll('img, video, audio, iframe').forEach((m) => {
if (!observedMedia.has(m)) {
observedMedia.add(m);
mediaobserver.observe(m);
}
});
}
}
function addFormTracking(parent) {
activateBlocksMO();
activateMediaMO();
parent.querySelectorAll('form').forEach((form) => {
form.addEventListener('submit', (e) => sampleRUM('formsubmit', { target: targetSelector(e.target), source: sourceSelector(e.target) }), { once: true });
});
}
function addObserver(ck, fn, block) {
return DEFAULT_TRACKING_EVENTS.includes(ck) && fn(block);
}
// blocks mutation observer callback
function blocksMCB(mutations) {
// block specific mutations
mutations
.filter((m) => m.type === 'attributes' && m.attributeName === 'data-block-status')
.filter((m) => m.target.dataset.blockStatus === 'loaded')
.forEach((m) => {
addObserver('form', addFormTracking, m.target);
addObserver('viewblock', addViewBlockTracking, m.target);
});
}
// media mutation observer callback
function mediaMCB(mutations) {
// media mutations
mutations
.forEach((m) => {
addObserver('viewmedia', addViewMediaTracking, m.target);
});
}
function addTrackingFromConfig() {
document.addEventListener('click', (event) => {
sampleRUM('click', { target: targetSelector(event.target), source: sourceSelector(event.target) });
});
addCWVTracking();
addFormTracking(window.document.body);
addNavigationTracking();
addLoadResourceTracking();
addUTMParametersTracking(sampleRUM);
addViewBlockTracking(window.document.body);
addViewMediaTracking(window.document.body);
addCookieConsentTracking(sampleRUM);
addAdsParametersTracking(sampleRUM);
addEmailParameterTracking(sampleRUM);
fflags.enabled('language', () => {
const target = navigator.language;
const source = document.documentElement.lang;
sampleRUM('language', { source, target });
});
}
function initEnhancer() {
try {
if (sampleRUM) {
addTrackingFromConfig();
window.hlx.rum.collector = trackCheckpoint;
processQueue();
}
/* c8 ignore next 3 */
} catch (error) {
// something went wrong
}
}
initEnhancer();