-
Notifications
You must be signed in to change notification settings - Fork 25
/
vitals.js
59 lines (55 loc) · 2.17 KB
/
vitals.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
import {
onCLS,
onFID,
onLCP,
onINP
} from 'https://unpkg.com/web-vitals@3/dist/web-vitals.attribution.js?module';
function sendToGoogleAnalytics({name, delta, value, id, rating, navigationType, attribution}) {
const eventParams = {
value: delta,
metric_id: id,
metric_value: value,
metric_delta: delta,
metric_rating: rating,
navigation_type: navigationType
};
if (navigator.deviceMemory) {
eventParams.debug_device_memory = navigator.deviceMemory;
}
switch (name) {
case 'CLS':
eventParams.debug_target = attribution.largestShiftTarget;
eventParams.largest_shift_time = attribution.largestShiftTime;
eventParams.largest_shift_score = attribution.largestShiftValue;
break;
case 'FID':
eventParams.debug_target = attribution.eventTarget;
eventParams.interaction_type = attribution.eventType;
eventParams.interaction_time = Math.round(attribution.eventTime);
break;
case 'LCP':
eventParams.debug_target = attribution.element;
eventParams.time_to_first_byte = attribution.timeToFirstByte;
eventParams.resource_load_time = attribution.resourceLoadTime;
eventParams.resource_load_delay = attribution.resourceLoadDelay;
eventParams.element_render_delay = attribution.elementRenderDelay;
eventParams.scrollY = scrollY;
break;
case 'INP':
eventParams.debug_target = attribution.eventTarget;
eventParams.interaction_type = attribution.eventType;
eventParams.interaction_time = Math.round(attribution.eventTime);
if (!attribution.eventEntry) {
break;
}
eventParams.interaction_delay = Math.round(attribution.eventEntry.processingStart - attribution.eventEntry.startTime);
eventParams.processing_time = Math.round(attribution.eventEntry.processingEnd - attribution.eventEntry.processingStart);
eventParams.presentation_delay = Math.round(attribution.eventEntry.duration + attribution.eventEntry.startTime - attribution.eventEntry.processingEnd);
break;
}
gtag('event', name, eventParams);
}
onCLS(sendToGoogleAnalytics);
onFID(sendToGoogleAnalytics);
onLCP(sendToGoogleAnalytics);
onINP(sendToGoogleAnalytics);