@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,QAoFvE"}
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 allMetrics = {
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 hasSent = false;
20
+ let hasInitialSent = false;
19
21
  let timeout = null;
20
- const sendAllMetrics = () => {
21
- if (hasSent)
22
+ const sendInitialMetrics = () => {
23
+ if (hasInitialSent)
22
24
  return;
23
- hasSent = true;
25
+ hasInitialSent = true;
24
26
  if (timeout) {
25
27
  clearTimeout(timeout);
26
28
  timeout = null;
27
29
  }
28
30
  const metricsPayload = {
29
- event_name: 'web_vitals',
30
- ttfb: allMetrics.ttfb,
31
- lcp: allMetrics.lcp,
32
- fid: allMetrics.fid,
33
- fcp: allMetrics.fcp,
34
- cls: allMetrics.cls,
35
- inp: allMetrics.inp,
36
- request_path: window.location.pathname,
37
- request_query: window.location.search,
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('event', metricsPayload, 'POST', basePath);
42
+ sendAnalytics('speed/update', payload, 'POST', basePath);
40
43
  };
41
- const collectMetric = (metric) => {
42
- if (hasSent)
44
+ const collectInitialMetric = (metric) => {
45
+ if (hasInitialSent)
43
46
  return;
44
47
  const metricName = metric.name.toLowerCase();
45
- allMetrics[metricName] = metric.value;
46
- // Check if all metrics are collected
47
- const allCollected = Object.values(allMetrics).every((value) => value !== null);
48
- if (allCollected) {
49
- sendAllMetrics();
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 (!hasSent) {
55
- sendAllMetrics();
66
+ if (!hasInitialSent) {
67
+ sendInitialMetrics();
56
68
  }
57
69
  }, 20000);
58
70
  // Send on page unload
59
71
  const handleBeforeUnload = () => {
60
- if (!hasSent) {
61
- sendAllMetrics();
72
+ if (!hasInitialSent) {
73
+ sendInitialMetrics();
62
74
  }
63
75
  };
64
76
  window.addEventListener('beforeunload', handleBeforeUnload);
65
- // Track all metrics
66
- onTTFB(collectMetric);
67
- onLCP(collectMetric);
68
- onFID(collectMetric);
69
- onFCP(collectMetric);
70
- onCLS(collectMetric);
71
- onINP(collectMetric);
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temps-sdk/react-analytics",
3
- "version": "0.0.1-beta.32",
3
+ "version": "0.0.1-beta.33",
4
4
  "private": false,
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",