@temps-sdk/react-analytics 0.0.1-beta.30 → 0.0.1-beta.31

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,QA0EvE"}
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"}
@@ -7,64 +7,73 @@ export function useSpeedAnalytics(options = {}) {
7
7
  useEffect(() => {
8
8
  if (disabled || typeof window === 'undefined')
9
9
  return;
10
- const initialMetrics = {};
11
- const lateMetrics = {};
12
- const sendInitialMetrics = () => {
13
- if (Object.keys(initialMetrics).length === 4) {
14
- const metricsPayload = {
15
- event_name: 'web_vitals',
16
- ttfb: initialMetrics.TTFB?.value ?? null,
17
- lcp: initialMetrics.LCP?.value ?? null,
18
- fid: initialMetrics.FID?.value ?? null,
19
- fcp: initialMetrics.FCP?.value ?? null,
20
- request_path: window.location.pathname,
21
- request_query: window.location.search,
22
- event_data: {},
23
- };
24
- sendAnalytics('event', metricsPayload, 'POST', basePath);
25
- }
10
+ const allMetrics = {
11
+ ttfb: null,
12
+ lcp: null,
13
+ fid: null,
14
+ fcp: null,
15
+ cls: null,
16
+ inp: null,
26
17
  };
27
- const sendLateMetric = (metricName, value) => {
28
- const payload = {
29
- event_name: 'web_vitals_update',
30
- ttfb: initialMetrics.TTFB?.value ?? null,
31
- lcp: initialMetrics.LCP?.value ?? null,
32
- fid: initialMetrics.FID?.value ?? null,
33
- fcp: initialMetrics.FCP?.value ?? null,
34
- cls: lateMetrics.CLS?.value ?? null,
35
- inp: lateMetrics.INP?.value ?? null,
36
- [metricName.toLowerCase()]: value,
18
+ let hasSent = false;
19
+ let timeout = null;
20
+ const sendAllMetrics = () => {
21
+ if (hasSent)
22
+ return;
23
+ hasSent = true;
24
+ if (timeout) {
25
+ clearTimeout(timeout);
26
+ timeout = null;
27
+ }
28
+ 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,
37
36
  request_path: window.location.pathname,
38
37
  request_query: window.location.search,
39
- event_data: {},
40
38
  };
41
- sendAnalytics('event', payload, 'POST', basePath);
39
+ sendAnalytics('event', metricsPayload, 'POST', basePath);
40
+ };
41
+ const collectMetric = (metric) => {
42
+ if (hasSent)
43
+ return;
44
+ 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();
50
+ }
51
+ };
52
+ // Set timeout to send metrics even if not all are collected
53
+ timeout = setTimeout(() => {
54
+ if (!hasSent) {
55
+ sendAllMetrics();
56
+ }
57
+ }, 20000);
58
+ // Send on page unload
59
+ const handleBeforeUnload = () => {
60
+ if (!hasSent) {
61
+ sendAllMetrics();
62
+ }
63
+ };
64
+ 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);
72
+ return () => {
73
+ if (timeout) {
74
+ clearTimeout(timeout);
75
+ }
76
+ window.removeEventListener('beforeunload', handleBeforeUnload);
42
77
  };
43
- // Track metrics that can be gathered quickly
44
- onTTFB((metric) => {
45
- initialMetrics.TTFB = { value: metric.value, rating: metric.rating };
46
- sendInitialMetrics();
47
- });
48
- onLCP((metric) => {
49
- initialMetrics.LCP = { value: metric.value, rating: metric.rating };
50
- sendInitialMetrics();
51
- });
52
- onFID((metric) => {
53
- initialMetrics.FID = { value: metric.value, rating: metric.rating };
54
- sendInitialMetrics();
55
- });
56
- onFCP((metric) => {
57
- initialMetrics.FCP = { value: metric.value, rating: metric.rating };
58
- sendInitialMetrics();
59
- });
60
- // Track metrics that take longer to stabilize
61
- onCLS((metric) => {
62
- lateMetrics.CLS = { value: metric.value, rating: metric.rating };
63
- sendLateMetric('cls', metric.value);
64
- });
65
- onINP((metric) => {
66
- lateMetrics.INP = { value: metric.value, rating: metric.rating };
67
- sendLateMetric('inp', metric.value);
68
- });
69
78
  }, [basePath, disabled]);
70
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temps-sdk/react-analytics",
3
- "version": "0.0.1-beta.30",
3
+ "version": "0.0.1-beta.31",
4
4
  "private": false,
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",