@temps-sdk/react-analytics 0.0.1-beta.1 → 0.0.1-beta.10
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/Provider.d.ts +1 -1
- package/dist/Provider.d.ts.map +1 -1
- package/dist/Provider.js +175 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1262
- package/dist/types.d.ts +22 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/dist/useAnalytics.d.ts.map +1 -1
- package/dist/useAnalytics.js +15 -0
- package/dist/usePageLeave.d.ts +13 -0
- package/dist/usePageLeave.d.ts.map +1 -0
- package/dist/usePageLeave.js +65 -0
- package/dist/useSpeedAnalytics.d.ts +8 -0
- package/dist/useSpeedAnalytics.d.ts.map +1 -0
- package/dist/useSpeedAnalytics.js +60 -0
- package/dist/useTrackEvent.d.ts.map +1 -1
- package/dist/useTrackEvent.js +7 -0
- package/dist/useTrackPageview.d.ts.map +1 -1
- package/dist/useTrackPageview.js +7 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +97 -0
- package/package.json +10 -3
- package/dist/index.js.map +0 -18
package/dist/types.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ export interface AnalyticsClientOptions {
|
|
|
16
16
|
disabled?: boolean;
|
|
17
17
|
/** Ignore localhost/test env automatically. Defaults to true. */
|
|
18
18
|
ignoreLocalhost?: boolean;
|
|
19
|
+
/** Custom domain to use for analytics. Defaults to window.location.hostname. */
|
|
20
|
+
domain?: string;
|
|
19
21
|
}
|
|
20
22
|
export interface AnalyticsContextValue {
|
|
21
23
|
/** Send a custom event. */
|
|
@@ -30,7 +32,27 @@ export interface AnalyticsContextValue {
|
|
|
30
32
|
export interface TempsAnalyticsProviderProps extends AnalyticsClientOptions {
|
|
31
33
|
/** Auto track pageviews on route changes. Defaults to true. */
|
|
32
34
|
autoTrackPageviews?: boolean;
|
|
35
|
+
/** Auto track page leave events. Defaults to true. */
|
|
36
|
+
autoTrackPageLeave?: boolean;
|
|
37
|
+
/** Custom event name for page leave events. Defaults to "page_leave". */
|
|
38
|
+
pageLeaveEventName?: string;
|
|
39
|
+
/** Auto track speed analytics (Web Vitals). Defaults to true. */
|
|
40
|
+
autoTrackSpeedAnalytics?: boolean;
|
|
33
41
|
/** Children to render inside the provider. */
|
|
34
42
|
children: React.ReactNode;
|
|
35
43
|
}
|
|
44
|
+
export interface WebVitalMetric {
|
|
45
|
+
value: number;
|
|
46
|
+
rating: "good" | "needs-improvement" | "poor";
|
|
47
|
+
}
|
|
48
|
+
export interface SpeedMetric {
|
|
49
|
+
ttfb?: number | null;
|
|
50
|
+
lcp?: number | null;
|
|
51
|
+
fid?: number | null;
|
|
52
|
+
fcp?: number | null;
|
|
53
|
+
cls?: number | null;
|
|
54
|
+
inp?: number | null;
|
|
55
|
+
path?: string | null;
|
|
56
|
+
query?: string | null;
|
|
57
|
+
}
|
|
36
58
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAC7D,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG,SAAS,EAAE,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEnF,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;CACxC;AAED,MAAM,WAAW,sBAAsB;IACrC,qDAAqD;IACrD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,0DAA0D;IAC1D,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,iEAAiE;IACjE,eAAe,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAC7D,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG,SAAS,EAAE,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEnF,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;CACxC;AAED,MAAM,WAAW,sBAAsB;IACrC,qDAAqD;IACrD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,0DAA0D;IAC1D,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,iEAAiE;IACjE,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,qBAAqB;IACpC,2BAA2B;IAC3B,UAAU,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACnF,oDAAoD;IACpD,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IACvF,mCAAmC;IACnC,aAAa,EAAE,MAAM,IAAI,CAAC;IAC1B,+CAA+C;IAC/C,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,2BAA4B,SAAQ,sBAAsB;IACzE,+DAA+D;IAC/D,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,sDAAsD;IACtD,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,yEAAyE;IACzE,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,iEAAiE;IACjE,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,8CAA8C;IAC9C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAED,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,GAAG,mBAAmB,GAAG,MAAM,CAAC;CAC/C;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAnalytics.d.ts","sourceRoot":"","sources":["../src/useAnalytics.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useAnalytics.d.ts","sourceRoot":"","sources":["../src/useAnalytics.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE5D,MAAM,WAAW,eAAe;IAC9B,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChF,QAAQ,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACnF;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,eAAe,CAAC;IACxB,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC1C;AAED,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB;uBAIzC,MAAM,YAAY,qBAAqB;uBAQ1C,MAAM,WAAW,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;EAQpD"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useMemo } from "react";
|
|
3
|
+
export function useAnalytics(options) {
|
|
4
|
+
const { client, defaultContext } = options;
|
|
5
|
+
const track = useCallback((eventName, payload) => {
|
|
6
|
+
const finalPayload = defaultContext ? { ...defaultContext, ...payload } : payload;
|
|
7
|
+
return client.track(eventName, finalPayload);
|
|
8
|
+
}, [client, defaultContext]);
|
|
9
|
+
const identify = useCallback((userId, traits) => {
|
|
10
|
+
if (!client.identify)
|
|
11
|
+
return;
|
|
12
|
+
return client.identify(userId, traits);
|
|
13
|
+
}, [client]);
|
|
14
|
+
return useMemo(() => ({ track, identify }), [track, identify]);
|
|
15
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { JsonValue } from "./types";
|
|
2
|
+
export interface UsePageLeaveOptions {
|
|
3
|
+
/** Custom event name. Defaults to "page_leave" */
|
|
4
|
+
eventName?: string;
|
|
5
|
+
/** Additional data to send with the page leave event */
|
|
6
|
+
eventData?: Record<string, JsonValue>;
|
|
7
|
+
/** Whether to enable page leave tracking. Defaults to true */
|
|
8
|
+
enabled?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare function usePageLeave(options?: UsePageLeaveOptions): {
|
|
11
|
+
triggerPageLeave: () => Promise<void> | undefined;
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=usePageLeave.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePageLeave.d.ts","sourceRoot":"","sources":["../src/usePageLeave.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,MAAM,WAAW,mBAAmB;IAClC,kDAAkD;IAClD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wDAAwD;IACxD,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACtC,8DAA8D;IAC9D,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB;;EA2E7D"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { useTempsAnalytics } from "./Provider";
|
|
4
|
+
import { sendAnalyticsReliable } from "./utils";
|
|
5
|
+
export function usePageLeave(options = {}) {
|
|
6
|
+
const { eventName = "page_leave", eventData = {}, enabled = true } = options;
|
|
7
|
+
const { enabled: analyticsEnabled, trackEvent } = useTempsAnalytics();
|
|
8
|
+
const hasTrackedRef = useRef(false);
|
|
9
|
+
const startTimeRef = useRef();
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
startTimeRef.current = Date.now();
|
|
12
|
+
hasTrackedRef.current = false;
|
|
13
|
+
}, []);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (!enabled || !analyticsEnabled)
|
|
16
|
+
return;
|
|
17
|
+
const trackPageLeave = () => {
|
|
18
|
+
if (hasTrackedRef.current)
|
|
19
|
+
return;
|
|
20
|
+
hasTrackedRef.current = true;
|
|
21
|
+
const timeOnPage = startTimeRef.current ? Date.now() - startTimeRef.current : 0;
|
|
22
|
+
const finalEventData = {
|
|
23
|
+
...eventData,
|
|
24
|
+
time_on_page_ms: timeOnPage,
|
|
25
|
+
timestamp: new Date().toISOString(),
|
|
26
|
+
url: window.location.href,
|
|
27
|
+
referrer: document.referrer,
|
|
28
|
+
};
|
|
29
|
+
// Use reliable sending method that tries sendBeacon first
|
|
30
|
+
sendAnalyticsReliable("event", {
|
|
31
|
+
event_name: eventName,
|
|
32
|
+
request_query: window.location.search,
|
|
33
|
+
request_path: window.location.pathname,
|
|
34
|
+
domain: window.location.hostname,
|
|
35
|
+
event_data: finalEventData,
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
// Use pagehide as primary (most reliable), with beforeunload as fallback
|
|
39
|
+
const handlePageLeave = () => trackPageLeave();
|
|
40
|
+
// pagehide is the most reliable for modern browsers
|
|
41
|
+
window.addEventListener("pagehide", handlePageLeave);
|
|
42
|
+
// beforeunload as fallback for older browsers
|
|
43
|
+
window.addEventListener("beforeunload", handlePageLeave);
|
|
44
|
+
return () => {
|
|
45
|
+
window.removeEventListener("pagehide", handlePageLeave);
|
|
46
|
+
window.removeEventListener("beforeunload", handlePageLeave);
|
|
47
|
+
};
|
|
48
|
+
}, [enabled, analyticsEnabled, eventName, eventData]);
|
|
49
|
+
// Manual trigger function
|
|
50
|
+
const triggerPageLeave = () => {
|
|
51
|
+
if (!enabled || !analyticsEnabled || hasTrackedRef.current)
|
|
52
|
+
return;
|
|
53
|
+
hasTrackedRef.current = true;
|
|
54
|
+
const timeOnPage = startTimeRef.current ? Date.now() - startTimeRef.current : 0;
|
|
55
|
+
return trackEvent(eventName, {
|
|
56
|
+
...eventData,
|
|
57
|
+
time_on_page_ms: timeOnPage,
|
|
58
|
+
timestamp: new Date().toISOString(),
|
|
59
|
+
url: window.location.href,
|
|
60
|
+
referrer: document.referrer,
|
|
61
|
+
manual_trigger: true,
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
return { triggerPageLeave };
|
|
65
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface UseSpeedAnalyticsOptions {
|
|
2
|
+
/** Base endpoint path. Defaults to `/api/_temps`. */
|
|
3
|
+
basePath?: string;
|
|
4
|
+
/** Set to true to disable speed analytics. Defaults to false. */
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function useSpeedAnalytics(options?: UseSpeedAnalyticsOptions): void;
|
|
8
|
+
//# sourceMappingURL=useSpeedAnalytics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSpeedAnalytics.d.ts","sourceRoot":"","sources":["../src/useSpeedAnalytics.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,wBAAwB;IACvC,qDAAqD;IACrD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,wBAA6B,QAgEvE"}
|
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
export function useSpeedAnalytics(options = {}) {
|
|
6
|
+
const { basePath = "/api/_temps", disabled = false } = options;
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
if (disabled || typeof window === "undefined")
|
|
9
|
+
return;
|
|
10
|
+
const initialMetrics = {};
|
|
11
|
+
const lateMetrics = {};
|
|
12
|
+
const sendInitialMetrics = () => {
|
|
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);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const sendLateMetric = (metricName, value) => {
|
|
26
|
+
const payload = {
|
|
27
|
+
[metricName.toLowerCase()]: value,
|
|
28
|
+
path: window.location.pathname,
|
|
29
|
+
query: window.location.search,
|
|
30
|
+
};
|
|
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
|
+
});
|
|
59
|
+
}, [basePath, disabled]);
|
|
60
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTrackEvent.d.ts","sourceRoot":"","sources":["../src/useTrackEvent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useTrackEvent.d.ts","sourceRoot":"","sources":["../src/useTrackEvent.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,wBAAgB,aAAa,gBAEI,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,mBACxE"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback } from "react";
|
|
3
|
+
import { useTempsAnalytics } from "./Provider";
|
|
4
|
+
export function useTrackEvent() {
|
|
5
|
+
const { trackEvent } = useTempsAnalytics();
|
|
6
|
+
return useCallback((eventName, data) => trackEvent(eventName, data), [trackEvent]);
|
|
7
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTrackPageview.d.ts","sourceRoot":"","sources":["../src/useTrackPageview.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useTrackPageview.d.ts","sourceRoot":"","sources":["../src/useTrackPageview.ts"],"names":[],"mappings":"AAIA,wBAAgB,gBAAgB,eAG/B"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -3,4 +3,5 @@ export declare function getRequestId(): string | undefined;
|
|
|
3
3
|
export declare function isLocalhostLike(): boolean;
|
|
4
4
|
export declare function isTestEnvironment(): boolean;
|
|
5
5
|
export declare function sendAnalytics(endpoint: string, data: Record<string, JsonValue>, method?: "POST" | "PUT" | "PATCH", basePath?: string): Promise<void>;
|
|
6
|
+
export declare function sendAnalyticsReliable(endpoint: string, data: Record<string, JsonValue>, basePath?: string): boolean;
|
|
6
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":"
|
|
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,GAAE,MAAM,GAAG,KAAK,GAAG,OAAgB,EACzC,QAAQ,SAAgB,GACvB,OAAO,CAAC,IAAI,CAAC,CAoBf;AAED,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,QAAQ,SAAgB,GACvB,OAAO,CAqCT"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
export function getRequestId() {
|
|
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 = "/api/_temps") {
|
|
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 = "/api/_temps") {
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temps-sdk/react-analytics",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.10",
|
|
4
4
|
"private": false,
|
|
5
|
-
"type": "module",
|
|
6
5
|
"main": "./dist/index.js",
|
|
7
6
|
"types": "./dist/index.d.ts",
|
|
8
7
|
"exports": {
|
|
@@ -15,7 +14,7 @@
|
|
|
15
14
|
"dist"
|
|
16
15
|
],
|
|
17
16
|
"scripts": {
|
|
18
|
-
"build:js": "
|
|
17
|
+
"build:js": "tsc -p tsconfig.json",
|
|
19
18
|
"build:types": "tsc -p tsconfig.types.json",
|
|
20
19
|
"build": "bun run build:js && bun run build:types",
|
|
21
20
|
"clean": "rm -rf dist",
|
|
@@ -28,6 +27,14 @@
|
|
|
28
27
|
"react": "^18.0.0 || ^19.0.0",
|
|
29
28
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
30
29
|
},
|
|
30
|
+
"peerDependenciesMeta": {
|
|
31
|
+
"react-dom": {
|
|
32
|
+
"optional": true
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"web-vitals": "^4.2.4"
|
|
37
|
+
},
|
|
31
38
|
"devDependencies": {
|
|
32
39
|
"@types/react": "^18.2.56",
|
|
33
40
|
"@types/react-dom": "^18.2.19",
|