@temps-sdk/react-analytics 0.0.1-beta.33 → 0.0.1-beta.35
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.
- package/dist/SessionRecorder.d.ts +10 -0
- package/dist/SessionRecorder.d.ts.map +1 -1
- package/dist/SessionRecorder.js +57 -25
- package/dist/useSessionRecording.d.ts.map +1 -1
- package/dist/useSessionRecording.js +3 -18
- package/dist/useSpeedAnalytics.d.ts.map +1 -1
- package/dist/useSpeedAnalytics.js +48 -80
- package/dist/utils.d.ts +0 -2
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +0 -45
- package/package.json +1 -1
|
@@ -13,7 +13,17 @@ interface SessionRecorderProps {
|
|
|
13
13
|
recordCanvas?: boolean;
|
|
14
14
|
collectFonts?: boolean;
|
|
15
15
|
slimDOMOptions?: Record<string, boolean>;
|
|
16
|
+
/**
|
|
17
|
+
* Number of events to batch before sending. Default: 500
|
|
18
|
+
* Events are sent when EITHER batchSize is reached OR flushInterval elapses (whichever comes first).
|
|
19
|
+
* Increased from 200 to reduce request frequency.
|
|
20
|
+
*/
|
|
16
21
|
batchSize?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Interval in milliseconds to flush events. Default: 60000 (60s)
|
|
24
|
+
* Events are sent when EITHER flushInterval elapses OR batchSize is reached (whichever comes first).
|
|
25
|
+
* Increased from 30s to reduce request frequency.
|
|
26
|
+
*/
|
|
17
27
|
flushInterval?: number;
|
|
18
28
|
ignoreSelector?: string;
|
|
19
29
|
blockSelector?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SessionRecorder.d.ts","sourceRoot":"","sources":["../src/SessionRecorder.tsx"],"names":[],"mappings":"AAMA,eAAO,MAAM,yBAAyB,mBAAmB,CAAC;AAE1D,UAAU,oBAAoB;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,O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CAAC,EAAE,OAAO,GAAG;YAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;YAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;YACnB,WAAW,CAAC,EAAE,OAAO,CAAC;YACtB,KAAK,CAAC,EAAE,OAAO,CAAC;YAChB,IAAI,CAAC,EAAE,OAAO,CAAC;YACf,UAAU,CAAC,EAAE,OAAO,CAAC;YACrB,QAAQ,CAAC,EAAE,OAAO,CAAC;YACnB,WAAW,CAAC,EAAE,OAAO,CAAC;YACtB,IAAI,CAAC,EAAE,OAAO,CAAC;YACf,KAAK,CAAC,EAAE,OAAO,CAAC;SACjB,CAAC;QACF,SAAS,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;QAC7B,KAAK,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;QACvB,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;KACzB,CAAC;CACH;AA2CD,wBAAgB,eAAe,CAAC,EAC9B,QAAQ,EAAG,uBAAuB;AAClC,MAAM,EACN,OAAe,EACf,aAAkB,EAClB,iBAAuB,EACvB,aAAoB,EACpB,gBAAgC,EAChC,UAAuB,EACvB,WAAyB,EACzB,aAAyB,EACzB,cAAgC,EAChC,aAAgC,EAChC,YAAoB,EACpB,YAAoB,EACpB,cAUC,EACD,SAAe,EACf,aAAqB,EACrB,QAAa,GACd,EAAE,oBAAoB,QA+dtB"}
|
package/dist/SessionRecorder.js
CHANGED
|
@@ -52,7 +52,7 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
52
52
|
headMetaHttpEquiv: false,
|
|
53
53
|
headMetaVerification: false,
|
|
54
54
|
headMetaDescKeywords: false
|
|
55
|
-
}, batchSize =
|
|
55
|
+
}, batchSize = 500, flushInterval = 60000, sampling = {}, }) {
|
|
56
56
|
const stopFnRef = useRef(null);
|
|
57
57
|
const eventsRef = useRef([]);
|
|
58
58
|
const sessionIdRef = useRef("");
|
|
@@ -60,14 +60,24 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
60
60
|
const flushTimeoutRef = useRef(null);
|
|
61
61
|
const initRetryCountRef = useRef(0);
|
|
62
62
|
const initFailedRef = useRef(false); // Track if init has permanently failed
|
|
63
|
+
const takeSnapshotRef = useRef(null); // Function to force a full snapshot
|
|
63
64
|
const maxInitRetries = 3;
|
|
64
65
|
const [isInitializing, setIsInitializing] = useState(false);
|
|
65
66
|
// Deep merge sampling defaults with proper handling of mouseInteraction
|
|
66
67
|
const defaultSampling = {
|
|
67
|
-
scroll:
|
|
68
|
-
media:
|
|
69
|
-
mouseInteraction:
|
|
70
|
-
|
|
68
|
+
scroll: 500,
|
|
69
|
+
media: 1000,
|
|
70
|
+
mouseInteraction: {
|
|
71
|
+
click: true,
|
|
72
|
+
dblclick: true,
|
|
73
|
+
contextmenu: true,
|
|
74
|
+
focus: true,
|
|
75
|
+
blur: true,
|
|
76
|
+
touchstart: false,
|
|
77
|
+
touchend: false,
|
|
78
|
+
touchcancel: false,
|
|
79
|
+
},
|
|
80
|
+
mousemove: 300,
|
|
71
81
|
input: "last",
|
|
72
82
|
};
|
|
73
83
|
// Merge sampling configs, handling mouseInteraction specially
|
|
@@ -255,22 +265,19 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
255
265
|
}
|
|
256
266
|
}, [basePath]);
|
|
257
267
|
const scheduleFlush = useCallback(() => {
|
|
268
|
+
// Clear any existing timeout
|
|
258
269
|
if (flushTimeoutRef.current) {
|
|
259
270
|
clearTimeout(flushTimeoutRef.current);
|
|
260
271
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
//
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
};
|
|
267
|
-
const scheduleNextFlush = () => {
|
|
268
|
-
if (flushTimeoutRef.current) {
|
|
269
|
-
clearTimeout(flushTimeoutRef.current);
|
|
272
|
+
// Schedule the next flush after the interval
|
|
273
|
+
flushTimeoutRef.current = setTimeout(() => {
|
|
274
|
+
// Only send if we have events
|
|
275
|
+
if (eventsRef.current.length > 0) {
|
|
276
|
+
sendEvents(false);
|
|
270
277
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
278
|
+
// Reschedule for the next interval
|
|
279
|
+
scheduleFlush();
|
|
280
|
+
}, flushInterval);
|
|
274
281
|
}, [sendEvents, flushInterval]);
|
|
275
282
|
const startRecording = useCallback(async () => {
|
|
276
283
|
if (stopFnRef.current)
|
|
@@ -295,10 +302,11 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
295
302
|
const stopFn = record({
|
|
296
303
|
emit(event) {
|
|
297
304
|
eventsRef.current.push(event);
|
|
298
|
-
// Send when we have accumulated a full batch
|
|
305
|
+
// Send when we have accumulated a full batch (whichever comes first: time or size)
|
|
299
306
|
if (eventsRef.current.length >= batchSize) {
|
|
300
307
|
sendEvents(false);
|
|
301
|
-
//
|
|
308
|
+
// Reset the flush timer since we just sent
|
|
309
|
+
scheduleFlush();
|
|
302
310
|
}
|
|
303
311
|
},
|
|
304
312
|
sampling: samplingConfig,
|
|
@@ -320,6 +328,9 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
320
328
|
});
|
|
321
329
|
if (stopFn) {
|
|
322
330
|
stopFnRef.current = stopFn;
|
|
331
|
+
// Store the takeFullSnapshot function from rrweb
|
|
332
|
+
// The record function returns an object with both stop and takeFullSnapshot methods
|
|
333
|
+
takeSnapshotRef.current = stopFn.takeFullSnapshot || null;
|
|
323
334
|
}
|
|
324
335
|
// Start the flush interval
|
|
325
336
|
scheduleFlush();
|
|
@@ -421,8 +432,15 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
421
432
|
sendEvents(false);
|
|
422
433
|
}
|
|
423
434
|
originalPushState.apply(window.history, args);
|
|
424
|
-
// Use setTimeout to allow DOM updates to complete
|
|
425
|
-
setTimeout(
|
|
435
|
+
// Use setTimeout to allow DOM updates to complete, then take snapshot
|
|
436
|
+
setTimeout(() => {
|
|
437
|
+
checkPathAndToggleRecording();
|
|
438
|
+
// Force a full snapshot after route change to capture new page state
|
|
439
|
+
// This ensures the replay shows the correct content after navigation
|
|
440
|
+
if (takeSnapshotRef.current) {
|
|
441
|
+
takeSnapshotRef.current();
|
|
442
|
+
}
|
|
443
|
+
}, 100); // Small delay to ensure DOM is updated
|
|
426
444
|
};
|
|
427
445
|
window.history.replaceState = function (...args) {
|
|
428
446
|
// Flush current events before route change to ensure they're captured
|
|
@@ -430,16 +448,30 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
430
448
|
sendEvents(false);
|
|
431
449
|
}
|
|
432
450
|
originalReplaceState.apply(window.history, args);
|
|
433
|
-
// Use setTimeout to allow DOM updates to complete
|
|
434
|
-
setTimeout(
|
|
451
|
+
// Use setTimeout to allow DOM updates to complete, then take snapshot
|
|
452
|
+
setTimeout(() => {
|
|
453
|
+
checkPathAndToggleRecording();
|
|
454
|
+
// Force a full snapshot after route change to capture new page state
|
|
455
|
+
// This ensures the replay shows the correct content after navigation
|
|
456
|
+
if (takeSnapshotRef.current) {
|
|
457
|
+
takeSnapshotRef.current();
|
|
458
|
+
}
|
|
459
|
+
}, 100); // Small delay to ensure DOM is updated
|
|
435
460
|
};
|
|
436
461
|
const handlePopState = () => {
|
|
437
462
|
// Flush current events before route change to ensure they're captured
|
|
438
463
|
if (stopFnRef.current && eventsRef.current.length > 0) {
|
|
439
464
|
sendEvents(false);
|
|
440
465
|
}
|
|
441
|
-
// Use setTimeout to allow DOM updates to complete
|
|
442
|
-
setTimeout(
|
|
466
|
+
// Use setTimeout to allow DOM updates to complete, then take snapshot
|
|
467
|
+
setTimeout(() => {
|
|
468
|
+
checkPathAndToggleRecording();
|
|
469
|
+
// Force a full snapshot after route change to capture new page state
|
|
470
|
+
// This ensures the replay shows the correct content after navigation
|
|
471
|
+
if (takeSnapshotRef.current) {
|
|
472
|
+
takeSnapshotRef.current();
|
|
473
|
+
}
|
|
474
|
+
}, 100); // Small delay to ensure DOM is updated
|
|
443
475
|
};
|
|
444
476
|
window.addEventListener("popstate", handlePopState);
|
|
445
477
|
return () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSessionRecording.d.ts","sourceRoot":"","sources":["../src/useSessionRecording.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,UAAU,4BAA4B;IACpC,kBAAkB,EAAE,OAAO,CAAC;IAC5B,eAAe,EAAE,MAAM,IAAI,CAAC;IAC5B,gBAAgB,EAAE,MAAM,IAAI,CAAC;IAC7B,eAAe,EAAE,MAAM,IAAI,CAAC;IAC5B,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAID,UAAU,6BAA6B;IACrC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,wBAAgB,wBAAwB,CAAC,EACvC,QAAQ,EACR,cAAsB,EACtB,iBAAwB,EACzB,EAAE,6BAA6B,
|
|
1
|
+
{"version":3,"file":"useSessionRecording.d.ts","sourceRoot":"","sources":["../src/useSessionRecording.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,UAAU,4BAA4B;IACpC,kBAAkB,EAAE,OAAO,CAAC;IAC5B,eAAe,EAAE,MAAM,IAAI,CAAC;IAC5B,gBAAgB,EAAE,MAAM,IAAI,CAAC;IAC7B,eAAe,EAAE,MAAM,IAAI,CAAC;IAC5B,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAID,UAAU,6BAA6B;IACrC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,wBAAgB,wBAAwB,CAAC,EACvC,QAAQ,EACR,cAAsB,EACtB,iBAAwB,EACzB,EAAE,6BAA6B,2CA8D/B;AAED,wBAAgB,mBAAmB,IAAI,4BAA4B,CAMlE;AAGD,wBAAgB,0BAA0B,CAAC,cAAc,UAAQ;;;;;EAyChE"}
|
|
@@ -24,35 +24,20 @@ export function SessionRecordingProvider({ children, defaultEnabled = false, per
|
|
|
24
24
|
const enableRecording = useCallback(() => {
|
|
25
25
|
setIsRecordingEnabled(true);
|
|
26
26
|
if (persistPreference && typeof localStorage !== "undefined") {
|
|
27
|
-
|
|
28
|
-
localStorage.setItem("temps_session_recording_enabled", "true");
|
|
29
|
-
}
|
|
30
|
-
catch (error) {
|
|
31
|
-
// Silently fail if localStorage is not available
|
|
32
|
-
}
|
|
27
|
+
localStorage.setItem("temps_session_recording_enabled", "true");
|
|
33
28
|
}
|
|
34
29
|
}, [persistPreference]);
|
|
35
30
|
const disableRecording = useCallback(() => {
|
|
36
31
|
setIsRecordingEnabled(false);
|
|
37
32
|
if (persistPreference && typeof localStorage !== "undefined") {
|
|
38
|
-
|
|
39
|
-
localStorage.setItem("temps_session_recording_enabled", "false");
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
// Silently fail if localStorage is not available
|
|
43
|
-
}
|
|
33
|
+
localStorage.setItem("temps_session_recording_enabled", "false");
|
|
44
34
|
}
|
|
45
35
|
}, [persistPreference]);
|
|
46
36
|
const toggleRecording = useCallback(() => {
|
|
47
37
|
setIsRecordingEnabled(prev => {
|
|
48
38
|
const newValue = !prev;
|
|
49
39
|
if (persistPreference && typeof localStorage !== "undefined") {
|
|
50
|
-
|
|
51
|
-
localStorage.setItem("temps_session_recording_enabled", String(newValue));
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
// Silently fail if localStorage is not available
|
|
55
|
-
}
|
|
40
|
+
localStorage.setItem("temps_session_recording_enabled", String(newValue));
|
|
56
41
|
}
|
|
57
42
|
return newValue;
|
|
58
43
|
});
|
|
@@ -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;IACvC,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,wBAA6B,QAgEvE"}
|
|
@@ -1,92 +1,60 @@
|
|
|
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
|
-
const initialMetrics = {
|
|
11
|
-
|
|
12
|
-
lcp: null,
|
|
13
|
-
fid: null,
|
|
14
|
-
fcp: null,
|
|
15
|
-
};
|
|
16
|
-
const lateMetrics = {
|
|
17
|
-
cls: null,
|
|
18
|
-
inp: null,
|
|
19
|
-
};
|
|
20
|
-
let hasInitialSent = false;
|
|
21
|
-
let timeout = null;
|
|
10
|
+
const initialMetrics = {};
|
|
11
|
+
const lateMetrics = {};
|
|
22
12
|
const sendInitialMetrics = () => {
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
13
|
+
if (Object.keys(initialMetrics).length === 4) {
|
|
14
|
+
const metricsPayload = {
|
|
15
|
+
ttfb: initialMetrics.TTFB?.value ?? null,
|
|
16
|
+
lcp: initialMetrics.LCP?.value ?? null,
|
|
17
|
+
fid: initialMetrics.FID?.value ?? null,
|
|
18
|
+
fcp: initialMetrics.FCP?.value ?? null,
|
|
19
|
+
path: window.location.pathname,
|
|
20
|
+
query: window.location.search,
|
|
21
|
+
};
|
|
22
|
+
sendAnalytics("speed", metricsPayload, "POST", basePath);
|
|
29
23
|
}
|
|
30
|
-
const metricsPayload = {
|
|
31
|
-
ttfb: initialMetrics.ttfb,
|
|
32
|
-
lcp: initialMetrics.lcp,
|
|
33
|
-
fid: initialMetrics.fid,
|
|
34
|
-
fcp: initialMetrics.fcp,
|
|
35
|
-
};
|
|
36
|
-
sendAnalytics('speed', metricsPayload, 'POST', basePath);
|
|
37
24
|
};
|
|
38
25
|
const sendLateMetric = (metricName, value) => {
|
|
39
26
|
const payload = {
|
|
40
27
|
[metricName.toLowerCase()]: value,
|
|
28
|
+
path: window.location.pathname,
|
|
29
|
+
query: window.location.search,
|
|
41
30
|
};
|
|
42
|
-
sendAnalytics(
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
// Send on page unload
|
|
71
|
-
const handleBeforeUnload = () => {
|
|
72
|
-
if (!hasInitialSent) {
|
|
73
|
-
sendInitialMetrics();
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
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);
|
|
85
|
-
return () => {
|
|
86
|
-
if (timeout) {
|
|
87
|
-
clearTimeout(timeout);
|
|
88
|
-
}
|
|
89
|
-
window.removeEventListener('beforeunload', handleBeforeUnload);
|
|
90
|
-
};
|
|
31
|
+
sendAnalytics("speed", payload, "POST", basePath);
|
|
32
|
+
};
|
|
33
|
+
// Track metrics that can be gathered quickly
|
|
34
|
+
onTTFB((metric) => {
|
|
35
|
+
initialMetrics.TTFB = { value: metric.value, rating: metric.rating };
|
|
36
|
+
sendInitialMetrics();
|
|
37
|
+
});
|
|
38
|
+
onLCP((metric) => {
|
|
39
|
+
initialMetrics.LCP = { value: metric.value, rating: metric.rating };
|
|
40
|
+
sendInitialMetrics();
|
|
41
|
+
});
|
|
42
|
+
onFID((metric) => {
|
|
43
|
+
initialMetrics.FID = { value: metric.value, rating: metric.rating };
|
|
44
|
+
sendInitialMetrics();
|
|
45
|
+
});
|
|
46
|
+
onFCP((metric) => {
|
|
47
|
+
initialMetrics.FCP = { value: metric.value, rating: metric.rating };
|
|
48
|
+
sendInitialMetrics();
|
|
49
|
+
});
|
|
50
|
+
// Track metrics that take longer to stabilize
|
|
51
|
+
onCLS((metric) => {
|
|
52
|
+
lateMetrics.CLS = { value: metric.value, rating: metric.rating };
|
|
53
|
+
sendLateMetric("cls", metric.value);
|
|
54
|
+
});
|
|
55
|
+
onINP((metric) => {
|
|
56
|
+
lateMetrics.INP = { value: metric.value, rating: metric.rating };
|
|
57
|
+
sendLateMetric("inp", metric.value);
|
|
58
|
+
});
|
|
91
59
|
}, [basePath, disabled]);
|
|
92
60
|
}
|
package/dist/utils.d.ts
CHANGED
|
@@ -2,8 +2,6 @@ import type { JsonValue } from "./types";
|
|
|
2
2
|
export declare function getRequestId(): string | undefined;
|
|
3
3
|
export declare function isLocalhostLike(): boolean;
|
|
4
4
|
export declare function isTestEnvironment(): boolean;
|
|
5
|
-
export declare function extractUtmParams(): Record<string, string>;
|
|
6
|
-
export declare function extractBrowserContext(): Record<string, unknown>;
|
|
7
5
|
export declare function sendAnalytics(endpoint: string, data: Record<string, JsonValue>, method: "POST" | "PUT" | "PATCH" | undefined, basePath: string): Promise<void>;
|
|
8
6
|
export declare function sendAnalyticsReliable(endpoint: string, data: Record<string, JsonValue>, basePath: string): boolean;
|
|
9
7
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,wBAAgB,YAAY,IAAI,MAAM,GAAG,SAAS,CAcjD;AAED,wBAAgB,eAAe,IAAI,OAAO,CASzC;AAED,wBAAgB,iBAAiB,IAAI,OAAO,CAY3C;AAED,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,wBAAgB,YAAY,IAAI,MAAM,GAAG,SAAS,CAcjD;AAED,wBAAgB,eAAe,IAAI,OAAO,CASzC;AAED,wBAAgB,iBAAiB,IAAI,OAAO,CAY3C;AAED,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,OAAO,YAAS,EACzC,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC,CAoBf;AAED,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,QAAQ,EAAE,MAAM,GACf,OAAO,CAqCT"}
|
package/dist/utils.js
CHANGED
|
@@ -41,51 +41,10 @@ export function isTestEnvironment() {
|
|
|
41
41
|
const allowTemps = Boolean(window.__temps);
|
|
42
42
|
return (isPhantom || isNightmare || isWebdriver || isCypress) && !allowTemps;
|
|
43
43
|
}
|
|
44
|
-
export function extractUtmParams() {
|
|
45
|
-
if (typeof window === "undefined")
|
|
46
|
-
return {};
|
|
47
|
-
const params = new URLSearchParams(window.location.search);
|
|
48
|
-
const utmParams = {};
|
|
49
|
-
const utmKeys = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
|
|
50
|
-
for (const key of utmKeys) {
|
|
51
|
-
const value = params.get(key);
|
|
52
|
-
if (value) {
|
|
53
|
-
utmParams[key] = value;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return utmParams;
|
|
57
|
-
}
|
|
58
|
-
export function extractBrowserContext() {
|
|
59
|
-
if (typeof window === "undefined")
|
|
60
|
-
return {};
|
|
61
|
-
try {
|
|
62
|
-
const nav = window.navigator || {};
|
|
63
|
-
const screen = window.screen || {};
|
|
64
|
-
const context = {
|
|
65
|
-
language: nav.language || undefined,
|
|
66
|
-
screen_width: screen.width || undefined,
|
|
67
|
-
screen_height: screen.height || undefined,
|
|
68
|
-
viewport_width: window.innerWidth || undefined,
|
|
69
|
-
viewport_height: window.innerHeight || undefined,
|
|
70
|
-
};
|
|
71
|
-
// Only add referrer if it exists
|
|
72
|
-
if (typeof document !== "undefined" && document.referrer) {
|
|
73
|
-
context.referrer = document.referrer;
|
|
74
|
-
}
|
|
75
|
-
return context;
|
|
76
|
-
}
|
|
77
|
-
catch {
|
|
78
|
-
return {};
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
44
|
export async function sendAnalytics(endpoint, data, method = "POST", basePath) {
|
|
82
45
|
try {
|
|
83
|
-
const utmParams = extractUtmParams();
|
|
84
|
-
const browserContext = extractBrowserContext();
|
|
85
46
|
const enrichedData = {
|
|
86
|
-
...browserContext,
|
|
87
47
|
...data,
|
|
88
|
-
...utmParams,
|
|
89
48
|
request_id: getRequestId(),
|
|
90
49
|
session_id: typeof localStorage !== "undefined"
|
|
91
50
|
? localStorage.getItem("session_id") || undefined
|
|
@@ -104,12 +63,8 @@ export async function sendAnalytics(endpoint, data, method = "POST", basePath) {
|
|
|
104
63
|
}
|
|
105
64
|
export function sendAnalyticsReliable(endpoint, data, basePath) {
|
|
106
65
|
try {
|
|
107
|
-
const utmParams = extractUtmParams();
|
|
108
|
-
const browserContext = extractBrowserContext();
|
|
109
66
|
const enrichedData = {
|
|
110
|
-
...browserContext,
|
|
111
67
|
...data,
|
|
112
|
-
...utmParams,
|
|
113
68
|
request_id: getRequestId(),
|
|
114
69
|
session_id: typeof localStorage !== "undefined"
|
|
115
70
|
? localStorage.getItem("session_id") || undefined
|