@temps-sdk/react-analytics 0.0.3 → 0.0.7
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/README.md +38 -7
- package/dist/EngagementTracker.d.ts +1 -54
- package/dist/EngagementTracker.d.ts.map +1 -1
- package/dist/EngagementTracker.js +3 -222
- package/dist/Provider.d.ts +2 -1
- package/dist/Provider.d.ts.map +1 -1
- package/dist/Provider.js +25 -10
- package/dist/SessionRecorder.d.ts +51 -52
- package/dist/SessionRecorder.d.ts.map +1 -1
- package/dist/SessionRecorder.js +32 -527
- package/dist/constants.d.ts +1 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +3 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/types.d.ts +9 -59
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -0
- package/dist/useAnalytics.d.ts +3 -2
- package/dist/useAnalytics.d.ts.map +1 -1
- package/dist/useAnalytics.js +2 -0
- package/dist/useEngagementTracking.d.ts +3 -2
- package/dist/useEngagementTracking.d.ts.map +1 -1
- package/dist/useEngagementTracking.js +12 -23
- package/dist/usePageLeave.d.ts +4 -2
- package/dist/usePageLeave.d.ts.map +1 -1
- package/dist/usePageLeave.js +2 -0
- package/dist/useScrollVisibility.d.ts.map +1 -1
- package/dist/useScrollVisibility.js +2 -0
- package/dist/useSessionRecording.d.ts +5 -3
- package/dist/useSessionRecording.d.ts.map +1 -1
- package/dist/useSessionRecording.js +42 -42
- package/dist/useSpeedAnalytics.d.ts +11 -0
- package/dist/useSpeedAnalytics.d.ts.map +1 -1
- package/dist/useSpeedAnalytics.js +13 -53
- package/dist/useTrackEvent.d.ts.map +1 -1
- package/dist/useTrackEvent.js +2 -0
- package/dist/useTrackPageview.d.ts.map +1 -1
- package/dist/useTrackPageview.js +2 -0
- package/dist/utils.d.ts +1 -6
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +3 -96
- package/package.json +4 -3
package/dist/utils.js
CHANGED
|
@@ -1,97 +1,4 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
1
3
|
"use client";
|
|
2
|
-
export
|
|
3
|
-
if (typeof document === "undefined")
|
|
4
|
-
return undefined;
|
|
5
|
-
const metaElement = document.querySelector('meta[name="temps-metadata"]');
|
|
6
|
-
if (metaElement) {
|
|
7
|
-
try {
|
|
8
|
-
const content = metaElement.getAttribute("content") || "{}";
|
|
9
|
-
const metadata = JSON.parse(content);
|
|
10
|
-
return metadata.request_id;
|
|
11
|
-
}
|
|
12
|
-
catch (error) {
|
|
13
|
-
// eslint-disable-next-line no-console
|
|
14
|
-
console.error("Failed to parse metadata:", error);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return undefined;
|
|
18
|
-
}
|
|
19
|
-
export function isLocalhostLike() {
|
|
20
|
-
try {
|
|
21
|
-
const host = window.location.hostname;
|
|
22
|
-
const isFile = window.location.protocol === "file:";
|
|
23
|
-
const isLocalhost = /^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(host);
|
|
24
|
-
return isFile || isLocalhost;
|
|
25
|
-
}
|
|
26
|
-
catch {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
export function isTestEnvironment() {
|
|
31
|
-
if (typeof window === "undefined")
|
|
32
|
-
return false;
|
|
33
|
-
// @ts-expect-error - non-standard globals from test frameworks
|
|
34
|
-
const isPhantom = Boolean(window._phantom);
|
|
35
|
-
// @ts-expect-error - non-standard globals from test frameworks
|
|
36
|
-
const isNightmare = Boolean(window.__nightmare);
|
|
37
|
-
const isWebdriver = Boolean(window.navigator?.webdriver);
|
|
38
|
-
// @ts-expect-error - non-standard globals from test frameworks
|
|
39
|
-
const isCypress = Boolean(window.Cypress);
|
|
40
|
-
// @ts-expect-error - allow explicit override via window.__temps
|
|
41
|
-
const allowTemps = Boolean(window.__temps);
|
|
42
|
-
return (isPhantom || isNightmare || isWebdriver || isCypress) && !allowTemps;
|
|
43
|
-
}
|
|
44
|
-
export async function sendAnalytics(endpoint, data, method = "POST", basePath) {
|
|
45
|
-
try {
|
|
46
|
-
const enrichedData = {
|
|
47
|
-
...data,
|
|
48
|
-
request_id: getRequestId(),
|
|
49
|
-
session_id: typeof localStorage !== "undefined"
|
|
50
|
-
? localStorage.getItem("session_id") || undefined
|
|
51
|
-
: undefined,
|
|
52
|
-
};
|
|
53
|
-
await fetch(`${basePath}/${endpoint}`, {
|
|
54
|
-
method,
|
|
55
|
-
headers: { "Content-Type": "application/json" },
|
|
56
|
-
body: JSON.stringify(enrichedData),
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
// eslint-disable-next-line no-console
|
|
61
|
-
console.error("Failed to send analytics:", error);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
export function sendAnalyticsReliable(endpoint, data, basePath) {
|
|
65
|
-
try {
|
|
66
|
-
const enrichedData = {
|
|
67
|
-
...data,
|
|
68
|
-
request_id: getRequestId(),
|
|
69
|
-
session_id: typeof localStorage !== "undefined"
|
|
70
|
-
? localStorage.getItem("session_id") || undefined
|
|
71
|
-
: undefined,
|
|
72
|
-
};
|
|
73
|
-
const url = `${basePath}/${endpoint}`;
|
|
74
|
-
const payload = JSON.stringify(enrichedData);
|
|
75
|
-
// Try sendBeacon first (most reliable for page unload)
|
|
76
|
-
if (navigator.sendBeacon && typeof navigator.sendBeacon === "function") {
|
|
77
|
-
const blob = new Blob([payload], { type: "application/json" });
|
|
78
|
-
return navigator.sendBeacon(url, blob);
|
|
79
|
-
}
|
|
80
|
-
// Fallback to synchronous fetch with keepalive
|
|
81
|
-
fetch(url, {
|
|
82
|
-
method: "POST",
|
|
83
|
-
headers: { "Content-Type": "application/json" },
|
|
84
|
-
body: payload,
|
|
85
|
-
keepalive: true,
|
|
86
|
-
}).catch((error) => {
|
|
87
|
-
// eslint-disable-next-line no-console
|
|
88
|
-
console.error("Failed to send analytics (reliable):", error);
|
|
89
|
-
});
|
|
90
|
-
return true;
|
|
91
|
-
}
|
|
92
|
-
catch (error) {
|
|
93
|
-
// eslint-disable-next-line no-console
|
|
94
|
-
console.error("Failed to send analytics (reliable):", error);
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
4
|
+
export { getRequestId, isLocalhostLike, isTestEnvironment, sendAnalytics, sendAnalyticsReliable, ingestKeyHeaders, withIngestKey, } from "@temps-sdk/analytics-core";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temps-sdk/react-analytics",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@rrweb/packer": "^2.0.0-alpha.17",
|
|
41
|
+
"@temps-sdk/analytics-core": "0.0.5",
|
|
41
42
|
"rrweb": "^2.0.0-alpha.17",
|
|
42
43
|
"web-vitals": "^4.2.4"
|
|
43
44
|
},
|
|
@@ -49,7 +50,7 @@
|
|
|
49
50
|
"@types/react-dom": "^18.2.19",
|
|
50
51
|
"@typescript-eslint/eslint-plugin": "^8.46.1",
|
|
51
52
|
"@typescript-eslint/parser": "^8.46.1",
|
|
52
|
-
"@vitest/coverage-v8": "^1.
|
|
53
|
+
"@vitest/coverage-v8": "^4.1.0",
|
|
53
54
|
"eslint": "^9.37.0",
|
|
54
55
|
"eslint-plugin-react": "^7.37.5",
|
|
55
56
|
"eslint-plugin-react-hooks": "^7.0.0",
|
|
@@ -57,7 +58,7 @@
|
|
|
57
58
|
"react": "^18.2.0",
|
|
58
59
|
"react-dom": "^18.2.0",
|
|
59
60
|
"typescript": "^5.5.4",
|
|
60
|
-
"vitest": "^1.
|
|
61
|
+
"vitest": "^4.1.0"
|
|
61
62
|
},
|
|
62
63
|
"sideEffects": false
|
|
63
64
|
}
|