@temps-sdk/react-analytics 0.0.1-beta.32 → 0.0.1-beta.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSpeedAnalytics.d.ts","sourceRoot":"","sources":["../src/useSpeedAnalytics.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,wBAAwB;IACxC,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,wBAA6B,
|
|
1
|
+
{"version":3,"file":"useSpeedAnalytics.d.ts","sourceRoot":"","sources":["../src/useSpeedAnalytics.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,wBAAwB;IACxC,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,wBAA6B,QAqGvE"}
|
|
@@ -7,68 +7,81 @@ export function useSpeedAnalytics(options = {}) {
|
|
|
7
7
|
useEffect(() => {
|
|
8
8
|
if (disabled || typeof window === 'undefined')
|
|
9
9
|
return;
|
|
10
|
-
const
|
|
10
|
+
const initialMetrics = {
|
|
11
11
|
ttfb: null,
|
|
12
12
|
lcp: null,
|
|
13
13
|
fid: null,
|
|
14
14
|
fcp: null,
|
|
15
|
+
};
|
|
16
|
+
const lateMetrics = {
|
|
15
17
|
cls: null,
|
|
16
18
|
inp: null,
|
|
17
19
|
};
|
|
18
|
-
let
|
|
20
|
+
let hasInitialSent = false;
|
|
19
21
|
let timeout = null;
|
|
20
|
-
const
|
|
21
|
-
if (
|
|
22
|
+
const sendInitialMetrics = () => {
|
|
23
|
+
if (hasInitialSent)
|
|
22
24
|
return;
|
|
23
|
-
|
|
25
|
+
hasInitialSent = true;
|
|
24
26
|
if (timeout) {
|
|
25
27
|
clearTimeout(timeout);
|
|
26
28
|
timeout = null;
|
|
27
29
|
}
|
|
28
30
|
const metricsPayload = {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
ttfb: initialMetrics.ttfb,
|
|
32
|
+
lcp: initialMetrics.lcp,
|
|
33
|
+
fid: initialMetrics.fid,
|
|
34
|
+
fcp: initialMetrics.fcp,
|
|
35
|
+
};
|
|
36
|
+
sendAnalytics('speed', metricsPayload, 'POST', basePath);
|
|
37
|
+
};
|
|
38
|
+
const sendLateMetric = (metricName, value) => {
|
|
39
|
+
const payload = {
|
|
40
|
+
[metricName.toLowerCase()]: value,
|
|
38
41
|
};
|
|
39
|
-
sendAnalytics('
|
|
42
|
+
sendAnalytics('speed/update', payload, 'POST', basePath);
|
|
40
43
|
};
|
|
41
|
-
const
|
|
42
|
-
if (
|
|
44
|
+
const collectInitialMetric = (metric) => {
|
|
45
|
+
if (hasInitialSent)
|
|
43
46
|
return;
|
|
44
47
|
const metricName = metric.name.toLowerCase();
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
if (metricName in initialMetrics) {
|
|
49
|
+
initialMetrics[metricName] = metric.value;
|
|
50
|
+
// Check if all initial metrics are collected
|
|
51
|
+
const allCollected = Object.values(initialMetrics).every((value) => value !== null);
|
|
52
|
+
if (allCollected) {
|
|
53
|
+
sendInitialMetrics();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const collectLateMetric = (metric) => {
|
|
58
|
+
const metricName = metric.name.toLowerCase();
|
|
59
|
+
if (metricName in lateMetrics) {
|
|
60
|
+
lateMetrics[metricName] = metric.value;
|
|
61
|
+
sendLateMetric(metricName, metric.value);
|
|
50
62
|
}
|
|
51
63
|
};
|
|
52
|
-
// Set timeout to send metrics even if not all are collected
|
|
64
|
+
// Set timeout to send initial metrics even if not all are collected
|
|
53
65
|
timeout = setTimeout(() => {
|
|
54
|
-
if (!
|
|
55
|
-
|
|
66
|
+
if (!hasInitialSent) {
|
|
67
|
+
sendInitialMetrics();
|
|
56
68
|
}
|
|
57
69
|
}, 20000);
|
|
58
70
|
// Send on page unload
|
|
59
71
|
const handleBeforeUnload = () => {
|
|
60
|
-
if (!
|
|
61
|
-
|
|
72
|
+
if (!hasInitialSent) {
|
|
73
|
+
sendInitialMetrics();
|
|
62
74
|
}
|
|
63
75
|
};
|
|
64
76
|
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
65
|
-
// Track
|
|
66
|
-
onTTFB(
|
|
67
|
-
onLCP(
|
|
68
|
-
onFID(
|
|
69
|
-
onFCP(
|
|
70
|
-
|
|
71
|
-
|
|
77
|
+
// Track initial metrics (fast-loading)
|
|
78
|
+
onTTFB(collectInitialMetric);
|
|
79
|
+
onLCP(collectInitialMetric);
|
|
80
|
+
onFID(collectInitialMetric);
|
|
81
|
+
onFCP(collectInitialMetric);
|
|
82
|
+
// Track late metrics (slow-loading)
|
|
83
|
+
onCLS(collectLateMetric);
|
|
84
|
+
onINP(collectLateMetric);
|
|
72
85
|
return () => {
|
|
73
86
|
if (timeout) {
|
|
74
87
|
clearTimeout(timeout);
|