forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
73 lines (66 loc) · 2.76 KB
/
utils.ts
File metadata and controls
73 lines (66 loc) · 2.76 KB
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
import { LOG } from '@snowplow/tracker-core';
import { MetricType } from './types';
/**
* Attach page listeners to collect the Web Vitals values
* @param {() => void} callback
*/
export function attachWebVitalsPageListeners(callback: () => void) {
// Safari does not fire "visibilitychange" on the tab close
// So we have 2 options: lose Safari data, or lose LCP/CLS that depends on "visibilitychange" logic.
// Current solution: if LCP/CLS supported, use `onHidden` otherwise, use `pagehide` to fire the callback in the end.
//
// More details: https://github.com/treosh/web-vitals-reporter/issues/3
const supportedEntryTypes = (PerformanceObserver && PerformanceObserver.supportedEntryTypes) || [];
const isLatestVisibilityChangeSupported = supportedEntryTypes.indexOf('layout-shift') !== -1;
if (isLatestVisibilityChangeSupported) {
const onVisibilityChange = () => {
if (document.visibilityState === 'hidden') {
callback();
window.removeEventListener('visibilitychange', onVisibilityChange, true);
}
};
window.addEventListener('visibilitychange', onVisibilityChange, true);
} else {
window.addEventListener('pagehide', callback, { capture: true, once: true });
}
}
/**
*
* @param {string} webVitalsSource Web Vitals script source.
* @returns {string} The script element of the Web Vitals script. Used for attaching listeners on it.
*/
export function createWebVitalsScript(webVitalsSource: string) {
const webVitalsScript = document.createElement('script');
webVitalsScript.setAttribute('src', webVitalsSource);
webVitalsScript.setAttribute('async', '1');
webVitalsScript.addEventListener('error', () => {
LOG.error(`Failed to load ${webVitalsSource}`);
});
document.head.appendChild(webVitalsScript);
return webVitalsScript;
}
/**
*
* Adds the Web Vitals measurements on the object used by the trackers to store metric properties.
* @param {Record<string, unknown>} webVitalsObject
* @return {void}
*/
export function webVitalsListener(webVitalsObject: Record<string, unknown>) {
function addWebVitalsMeasurement(metricSchemaName: string): (arg: MetricType) => void {
return (arg) => {
webVitalsObject[metricSchemaName] = arg.value;
webVitalsObject.navigationType = arg.navigationType;
};
}
const webVitals = window.webVitals;
if (!webVitals) {
LOG.warn('The window.webVitals API is currently unavailable. web_vitals events will not be collected.');
return;
}
webVitals.onCLS?.(addWebVitalsMeasurement('cls'));
webVitals.onFID?.(addWebVitalsMeasurement('fid'));
webVitals.onLCP?.(addWebVitalsMeasurement('lcp'));
webVitals.onFCP?.(addWebVitalsMeasurement('fcp'));
webVitals.onINP?.(addWebVitalsMeasurement('inp'));
webVitals.onTTFB?.(addWebVitalsMeasurement('ttfb'));
}