@temps-sdk/react-analytics 0.0.1-beta.30 → 0.0.1-beta.32
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":"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,2CA0E/B;AAED,wBAAgB,mBAAmB,IAAI,4BAA4B,CAMlE;AAGD,wBAAgB,0BAA0B,CAAC,cAAc,UAAQ;;;;;EAyChE"}
|
|
@@ -24,20 +24,35 @@ export function SessionRecordingProvider({ children, defaultEnabled = false, per
|
|
|
24
24
|
const enableRecording = useCallback(() => {
|
|
25
25
|
setIsRecordingEnabled(true);
|
|
26
26
|
if (persistPreference && typeof localStorage !== "undefined") {
|
|
27
|
-
|
|
27
|
+
try {
|
|
28
|
+
localStorage.setItem("temps_session_recording_enabled", "true");
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
// Silently fail if localStorage is not available
|
|
32
|
+
}
|
|
28
33
|
}
|
|
29
34
|
}, [persistPreference]);
|
|
30
35
|
const disableRecording = useCallback(() => {
|
|
31
36
|
setIsRecordingEnabled(false);
|
|
32
37
|
if (persistPreference && typeof localStorage !== "undefined") {
|
|
33
|
-
|
|
38
|
+
try {
|
|
39
|
+
localStorage.setItem("temps_session_recording_enabled", "false");
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
// Silently fail if localStorage is not available
|
|
43
|
+
}
|
|
34
44
|
}
|
|
35
45
|
}, [persistPreference]);
|
|
36
46
|
const toggleRecording = useCallback(() => {
|
|
37
47
|
setIsRecordingEnabled(prev => {
|
|
38
48
|
const newValue = !prev;
|
|
39
49
|
if (persistPreference && typeof localStorage !== "undefined") {
|
|
40
|
-
|
|
50
|
+
try {
|
|
51
|
+
localStorage.setItem("temps_session_recording_enabled", String(newValue));
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
// Silently fail if localStorage is not available
|
|
55
|
+
}
|
|
41
56
|
}
|
|
42
57
|
return newValue;
|
|
43
58
|
});
|
|
@@ -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,QAoFvE"}
|
|
@@ -7,64 +7,73 @@ export function useSpeedAnalytics(options = {}) {
|
|
|
7
7
|
useEffect(() => {
|
|
8
8
|
if (disabled || typeof window === 'undefined')
|
|
9
9
|
return;
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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',
|
|
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/dist/utils.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ 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>;
|
|
5
7
|
export declare function sendAnalytics(endpoint: string, data: Record<string, JsonValue>, method: "POST" | "PUT" | "PATCH" | undefined, basePath: string): Promise<void>;
|
|
6
8
|
export declare function sendAnalyticsReliable(endpoint: string, data: Record<string, JsonValue>, basePath: string): boolean;
|
|
7
9
|
//# 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,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,
|
|
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,wBAAgB,gBAAgB,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAgBzD;AAED,wBAAgB,qBAAqB,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAwB/D;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,CAyBf;AAED,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,QAAQ,EAAE,MAAM,GACf,OAAO,CA0CT"}
|
package/dist/utils.js
CHANGED
|
@@ -41,10 +41,51 @@ 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
|
+
}
|
|
44
81
|
export async function sendAnalytics(endpoint, data, method = "POST", basePath) {
|
|
45
82
|
try {
|
|
83
|
+
const utmParams = extractUtmParams();
|
|
84
|
+
const browserContext = extractBrowserContext();
|
|
46
85
|
const enrichedData = {
|
|
86
|
+
...browserContext,
|
|
47
87
|
...data,
|
|
88
|
+
...utmParams,
|
|
48
89
|
request_id: getRequestId(),
|
|
49
90
|
session_id: typeof localStorage !== "undefined"
|
|
50
91
|
? localStorage.getItem("session_id") || undefined
|
|
@@ -63,8 +104,12 @@ export async function sendAnalytics(endpoint, data, method = "POST", basePath) {
|
|
|
63
104
|
}
|
|
64
105
|
export function sendAnalyticsReliable(endpoint, data, basePath) {
|
|
65
106
|
try {
|
|
107
|
+
const utmParams = extractUtmParams();
|
|
108
|
+
const browserContext = extractBrowserContext();
|
|
66
109
|
const enrichedData = {
|
|
110
|
+
...browserContext,
|
|
67
111
|
...data,
|
|
112
|
+
...utmParams,
|
|
68
113
|
request_id: getRequestId(),
|
|
69
114
|
session_id: typeof localStorage !== "undefined"
|
|
70
115
|
? localStorage.getItem("session_id") || undefined
|