@temps-sdk/analytics-core 0.0.2

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.
@@ -0,0 +1,7 @@
1
+ import type { JsonValue } from "./types";
2
+ export declare function getRequestId(): string | undefined;
3
+ export declare function isLocalhostLike(): boolean;
4
+ export declare function isTestEnvironment(): boolean;
5
+ export declare function sendAnalytics(endpoint: string, data: Record<string, JsonValue>, method: "POST" | "PUT" | "PATCH" | undefined, basePath: string): Promise<void>;
6
+ export declare function sendAnalyticsReliable(endpoint: string, data: Record<string, JsonValue>, basePath: string): boolean;
7
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,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,CAS3C;AAmBD,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,CAWf;AAED,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,QAAQ,EAAE,MAAM,GACf,OAAO,CAgCT"}
package/dist/utils.js ADDED
@@ -0,0 +1,96 @@
1
+ export function getRequestId() {
2
+ if (typeof document === "undefined")
3
+ return undefined;
4
+ const metaElement = document.querySelector('meta[name="temps-metadata"]');
5
+ if (metaElement) {
6
+ try {
7
+ const content = metaElement.getAttribute("content") || "{}";
8
+ const metadata = JSON.parse(content);
9
+ return metadata.request_id;
10
+ }
11
+ catch (error) {
12
+ // eslint-disable-next-line no-console
13
+ console.error("Failed to parse metadata:", error);
14
+ }
15
+ }
16
+ return undefined;
17
+ }
18
+ export function isLocalhostLike() {
19
+ try {
20
+ const host = window.location.hostname;
21
+ const isFile = window.location.protocol === "file:";
22
+ const isLocalhost = /^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(host);
23
+ return isFile || isLocalhost;
24
+ }
25
+ catch {
26
+ return false;
27
+ }
28
+ }
29
+ export function isTestEnvironment() {
30
+ if (typeof window === "undefined")
31
+ return false;
32
+ const w = window;
33
+ const isPhantom = Boolean(w._phantom);
34
+ const isNightmare = Boolean(w.__nightmare);
35
+ const isWebdriver = Boolean(window.navigator?.webdriver);
36
+ const isCypress = Boolean(w.Cypress);
37
+ const allowTemps = Boolean(w.__temps);
38
+ return (isPhantom || isNightmare || isWebdriver || isCypress) && !allowTemps;
39
+ }
40
+ /**
41
+ * Returns a new object with request_id and session_id attached if available.
42
+ * When they are unavailable, the keys are set to `undefined` so `JSON.stringify`
43
+ * omits them entirely (matching legacy @temps-sdk/react-analytics behavior).
44
+ */
45
+ function enrich(data) {
46
+ const enriched = {
47
+ ...data,
48
+ request_id: getRequestId(),
49
+ session_id: typeof localStorage !== "undefined"
50
+ ? localStorage.getItem("session_id") || undefined
51
+ : undefined,
52
+ };
53
+ return enriched;
54
+ }
55
+ export async function sendAnalytics(endpoint, data, method = "POST", basePath) {
56
+ try {
57
+ await fetch(`${basePath}/${endpoint}`, {
58
+ method,
59
+ headers: { "Content-Type": "application/json" },
60
+ body: JSON.stringify(enrich(data)),
61
+ });
62
+ }
63
+ catch (error) {
64
+ // eslint-disable-next-line no-console
65
+ console.error("Failed to send analytics:", error);
66
+ }
67
+ }
68
+ export function sendAnalyticsReliable(endpoint, data, basePath) {
69
+ try {
70
+ const url = `${basePath}/${endpoint}`;
71
+ const payload = JSON.stringify(enrich(data));
72
+ // Try sendBeacon first (most reliable for page unload)
73
+ if (typeof navigator !== "undefined" &&
74
+ navigator.sendBeacon &&
75
+ typeof navigator.sendBeacon === "function") {
76
+ const blob = new Blob([payload], { type: "application/json" });
77
+ return navigator.sendBeacon(url, blob);
78
+ }
79
+ // Fallback to fetch with keepalive
80
+ fetch(url, {
81
+ method: "POST",
82
+ headers: { "Content-Type": "application/json" },
83
+ body: payload,
84
+ keepalive: true,
85
+ }).catch((error) => {
86
+ // eslint-disable-next-line no-console
87
+ console.error("Failed to send analytics (reliable):", error);
88
+ });
89
+ return true;
90
+ }
91
+ catch (error) {
92
+ // eslint-disable-next-line no-console
93
+ console.error("Failed to send analytics (reliable):", error);
94
+ return false;
95
+ }
96
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@temps-sdk/analytics-core",
3
+ "version": "0.0.2",
4
+ "description": "Framework-agnostic core for Temps analytics, engagement, Web Vitals, and session replay.",
5
+ "private": false,
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "scripts": {
20
+ "build:js": "tsc -p tsconfig.json",
21
+ "build:types": "tsc -p tsconfig.types.json",
22
+ "build": "bun run build:js && bun run build:types",
23
+ "clean": "rm -rf dist",
24
+ "prepublishOnly": "bun run clean && bun run build",
25
+ "test": "vitest",
26
+ "test:run": "vitest run",
27
+ "test:coverage": "vitest run --coverage"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "dependencies": {
33
+ "@rrweb/packer": "^2.0.0-alpha.17",
34
+ "rrweb": "^2.0.0-alpha.17",
35
+ "web-vitals": "^4.2.4"
36
+ },
37
+ "devDependencies": {
38
+ "@vitest/coverage-v8": "^4.1.0",
39
+ "jsdom": "^24.0.0",
40
+ "typescript": "^5.5.4",
41
+ "vitest": "^4.1.0"
42
+ },
43
+ "sideEffects": false
44
+ }