@temps-sdk/react-analytics 0.0.1-beta.29 → 0.0.1-beta.30
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;
|
|
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,QA0EvE"}
|
|
@@ -1,31 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
import { useEffect } from
|
|
3
|
-
import { onCLS, onFID, onLCP, onTTFB, onFCP, onINP } from
|
|
4
|
-
import { sendAnalytics } from
|
|
1
|
+
'use client';
|
|
2
|
+
import { useEffect } from 'react';
|
|
3
|
+
import { onCLS, onFID, onLCP, onTTFB, onFCP, onINP } from 'web-vitals';
|
|
4
|
+
import { sendAnalytics } from './utils';
|
|
5
5
|
export function useSpeedAnalytics(options = {}) {
|
|
6
|
-
const { basePath =
|
|
6
|
+
const { basePath = '/_temps', disabled = false } = options;
|
|
7
7
|
useEffect(() => {
|
|
8
|
-
if (disabled || typeof window ===
|
|
8
|
+
if (disabled || typeof window === 'undefined')
|
|
9
9
|
return;
|
|
10
10
|
const initialMetrics = {};
|
|
11
11
|
const lateMetrics = {};
|
|
12
12
|
const sendInitialMetrics = () => {
|
|
13
13
|
if (Object.keys(initialMetrics).length === 4) {
|
|
14
14
|
const metricsPayload = {
|
|
15
|
-
event_name:
|
|
15
|
+
event_name: 'web_vitals',
|
|
16
16
|
ttfb: initialMetrics.TTFB?.value ?? null,
|
|
17
17
|
lcp: initialMetrics.LCP?.value ?? null,
|
|
18
18
|
fid: initialMetrics.FID?.value ?? null,
|
|
19
19
|
fcp: initialMetrics.FCP?.value ?? null,
|
|
20
20
|
request_path: window.location.pathname,
|
|
21
21
|
request_query: window.location.search,
|
|
22
|
+
event_data: {},
|
|
22
23
|
};
|
|
23
|
-
sendAnalytics(
|
|
24
|
+
sendAnalytics('event', metricsPayload, 'POST', basePath);
|
|
24
25
|
}
|
|
25
26
|
};
|
|
26
27
|
const sendLateMetric = (metricName, value) => {
|
|
27
28
|
const payload = {
|
|
28
|
-
event_name:
|
|
29
|
+
event_name: 'web_vitals_update',
|
|
29
30
|
ttfb: initialMetrics.TTFB?.value ?? null,
|
|
30
31
|
lcp: initialMetrics.LCP?.value ?? null,
|
|
31
32
|
fid: initialMetrics.FID?.value ?? null,
|
|
@@ -35,8 +36,9 @@ export function useSpeedAnalytics(options = {}) {
|
|
|
35
36
|
[metricName.toLowerCase()]: value,
|
|
36
37
|
request_path: window.location.pathname,
|
|
37
38
|
request_query: window.location.search,
|
|
39
|
+
event_data: {},
|
|
38
40
|
};
|
|
39
|
-
sendAnalytics(
|
|
41
|
+
sendAnalytics('event', payload, 'POST', basePath);
|
|
40
42
|
};
|
|
41
43
|
// Track metrics that can be gathered quickly
|
|
42
44
|
onTTFB((metric) => {
|
|
@@ -58,11 +60,11 @@ export function useSpeedAnalytics(options = {}) {
|
|
|
58
60
|
// Track metrics that take longer to stabilize
|
|
59
61
|
onCLS((metric) => {
|
|
60
62
|
lateMetrics.CLS = { value: metric.value, rating: metric.rating };
|
|
61
|
-
sendLateMetric(
|
|
63
|
+
sendLateMetric('cls', metric.value);
|
|
62
64
|
});
|
|
63
65
|
onINP((metric) => {
|
|
64
66
|
lateMetrics.INP = { value: metric.value, rating: metric.rating };
|
|
65
|
-
sendLateMetric(
|
|
67
|
+
sendLateMetric('inp', metric.value);
|
|
66
68
|
});
|
|
67
69
|
}, [basePath, disabled]);
|
|
68
70
|
}
|