@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/types.d.ts
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
1
|
import type React from "react";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
};
|
|
6
|
-
export interface AnalyticsEventBase {
|
|
7
|
-
event_name: string;
|
|
8
|
-
request_query?: string;
|
|
9
|
-
request_path?: string;
|
|
10
|
-
event_data?: Record<string, JsonValue>;
|
|
11
|
-
}
|
|
12
|
-
export interface AnalyticsClientOptions {
|
|
13
|
-
/** Base endpoint path. Defaults to `/_temps`. */
|
|
14
|
-
basePath?: string;
|
|
15
|
-
/** Set to true to disable analytics (e.g., for tests). */
|
|
16
|
-
disabled?: boolean;
|
|
17
|
-
/** Ignore localhost/test env automatically. Defaults to true. */
|
|
18
|
-
ignoreLocalhost?: boolean;
|
|
19
|
-
/** Custom domain to use for analytics. Defaults to window.location.hostname. */
|
|
20
|
-
domain?: string;
|
|
21
|
-
}
|
|
2
|
+
import type { JsonValue, AnalyticsClientOptions } from "@temps-sdk/analytics-core";
|
|
3
|
+
import type { SessionRecorderConfig } from "./SessionRecorder";
|
|
4
|
+
export type { JsonPrimitive, JsonValue, AnalyticsEventBase, AnalyticsClientOptions, WebVitalMetric, SpeedMetric, } from "@temps-sdk/analytics-core";
|
|
22
5
|
export interface AnalyticsContextValue {
|
|
23
6
|
/** Send a custom event. */
|
|
24
7
|
trackEvent: (eventName: string, data?: Record<string, JsonValue>) => Promise<void>;
|
|
@@ -48,46 +31,13 @@ export interface TempsAnalyticsProviderProps extends AnalyticsClientOptions {
|
|
|
48
31
|
engagementThreshold?: number;
|
|
49
32
|
/** Enable session recording. Defaults to false. */
|
|
50
33
|
enableSessionRecording?: boolean;
|
|
51
|
-
/**
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
/** Mask all inputs. Defaults to true. */
|
|
58
|
-
maskAllInputs?: boolean;
|
|
59
|
-
/** CSS selector for masking text. Defaults to "[data-mask]". */
|
|
60
|
-
maskTextSelector?: string;
|
|
61
|
-
/** CSS class to block from recording. Defaults to "rr-block". */
|
|
62
|
-
blockClass?: string;
|
|
63
|
-
/** CSS class to ignore from recording. Defaults to "rr-ignore". */
|
|
64
|
-
ignoreClass?: string;
|
|
65
|
-
/** CSS class to mask text. Defaults to "rr-mask". */
|
|
66
|
-
maskTextClass?: string;
|
|
67
|
-
/** Record canvas elements. Defaults to false. */
|
|
68
|
-
recordCanvas?: boolean;
|
|
69
|
-
/** Collect fonts. Defaults to false. */
|
|
70
|
-
collectFonts?: boolean;
|
|
71
|
-
/** Number of events to batch before sending. Defaults to 100. */
|
|
72
|
-
batchSize?: number;
|
|
73
|
-
/** Interval in ms to flush events. Defaults to 5000. */
|
|
74
|
-
flushInterval?: number;
|
|
75
|
-
};
|
|
34
|
+
/**
|
|
35
|
+
* Session recording configuration, forwarded to the recorder in full
|
|
36
|
+
* (including `idleTimeout`, `useDefaultExcludedPaths`, `debug` and the
|
|
37
|
+
* rrweb-level options).
|
|
38
|
+
*/
|
|
39
|
+
sessionRecordingConfig?: SessionRecorderConfig;
|
|
76
40
|
/** Children to render inside the provider. */
|
|
77
41
|
children: React.ReactNode;
|
|
78
42
|
}
|
|
79
|
-
export interface WebVitalMetric {
|
|
80
|
-
value: number;
|
|
81
|
-
rating: "good" | "needs-improvement" | "poor";
|
|
82
|
-
}
|
|
83
|
-
export interface SpeedMetric {
|
|
84
|
-
ttfb?: number | null;
|
|
85
|
-
lcp?: number | null;
|
|
86
|
-
fid?: number | null;
|
|
87
|
-
fcp?: number | null;
|
|
88
|
-
cls?: number | null;
|
|
89
|
-
inp?: number | null;
|
|
90
|
-
path?: string | null;
|
|
91
|
-
query?: string | null;
|
|
92
|
-
}
|
|
93
43
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,KAAK,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AACnF,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAE/D,YAAY,EACV,aAAa,EACb,SAAS,EACT,kBAAkB,EAClB,sBAAsB,EACtB,cAAc,EACd,WAAW,GACZ,MAAM,2BAA2B,CAAC;AAEnC,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,+DAA+D;IAC/D,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,0EAA0E;IAC1E,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,0EAA0E;IAC1E,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,wGAAwG;IACxG,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,mDAAmD;IACnD,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,qBAAqB,CAAC;IAC/C,8CAA8C;IAC9C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B"}
|
package/dist/types.js
CHANGED
package/dist/useAnalytics.d.ts
CHANGED
|
@@ -7,8 +7,9 @@ export interface UseAnalyticsOptions {
|
|
|
7
7
|
client: AnalyticsClient;
|
|
8
8
|
defaultContext?: Record<string, unknown>;
|
|
9
9
|
}
|
|
10
|
-
export
|
|
10
|
+
export interface UseAnalyticsResult {
|
|
11
11
|
track: (eventName: string, payload?: AnalyticsEventPayload) => void | Promise<void>;
|
|
12
12
|
identify: (userId: string, traits?: Record<string, unknown>) => void | Promise<void>;
|
|
13
|
-
}
|
|
13
|
+
}
|
|
14
|
+
export declare function useAnalytics(options: UseAnalyticsOptions): UseAnalyticsResult;
|
|
14
15
|
//# sourceMappingURL=useAnalytics.d.ts.map
|
|
@@ -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":"AAMA,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,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpF,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACtF;AAED,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,kBAAkB,CAoB7E"}
|
package/dist/useAnalytics.js
CHANGED
|
@@ -27,8 +27,9 @@ export interface UseEngagementTrackingOptions extends Omit<EngagementTrackerOpti
|
|
|
27
27
|
* }
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
|
-
export
|
|
30
|
+
export interface UseEngagementTrackingResult {
|
|
31
31
|
engagementData: EngagementData;
|
|
32
32
|
isTracking: boolean;
|
|
33
|
-
}
|
|
33
|
+
}
|
|
34
|
+
export declare function useEngagementTracking(options?: UseEngagementTrackingOptions): UseEngagementTrackingResult;
|
|
34
35
|
//# sourceMappingURL=useEngagementTracking.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useEngagementTracking.d.ts","sourceRoot":"","sources":["../src/useEngagementTracking.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useEngagementTracking.d.ts","sourceRoot":"","sources":["../src/useEngagementTracking.ts"],"names":[],"mappings":"AAKA,OAAO,EAAqB,KAAK,wBAAwB,EAAE,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAG5G,MAAM,WAAW,4BAA6B,SAAQ,IAAI,CAAC,wBAAwB,EAAE,UAAU,GAAG,QAAQ,CAAC;IACzG,+DAA+D;IAC/D,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,6DAA6D;IAC7D,kBAAkB,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;IACpD,4CAA4C;IAC5C,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;CAC9C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,2BAA2B;IAC1C,cAAc,EAAE,cAAc,CAAC;IAC/B,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,wBAAgB,qBAAqB,CACnC,OAAO,GAAE,4BAAiC,GACzC,2BAA2B,CA2D7B"}
|
|
@@ -1,27 +1,9 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
1
3
|
"use client";
|
|
2
4
|
import { useEffect, useRef } from "react";
|
|
3
5
|
import { EngagementTracker } from "./EngagementTracker";
|
|
4
6
|
import { useTempsAnalytics } from "./Provider";
|
|
5
|
-
/**
|
|
6
|
-
* Hook to manually control engagement tracking for specific components or pages.
|
|
7
|
-
* This is useful when you want fine-grained control over engagement tracking
|
|
8
|
-
* or need to track engagement for specific sections of your app.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```tsx
|
|
12
|
-
* function ArticlePage() {
|
|
13
|
-
* const { engagementData } = useEngagementTracking({
|
|
14
|
-
* heartbeatInterval: 15000, // Send heartbeat every 15 seconds
|
|
15
|
-
* engagementThreshold: 5000, // Consider engaged after 5 seconds
|
|
16
|
-
* onEngagementUpdate: (data) => {
|
|
17
|
-
* console.log('Engagement updated:', data);
|
|
18
|
-
* }
|
|
19
|
-
* });
|
|
20
|
-
*
|
|
21
|
-
* return <article>...</article>;
|
|
22
|
-
* }
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
7
|
export function useEngagementTracking(options = {}) {
|
|
26
8
|
const analytics = useTempsAnalytics();
|
|
27
9
|
const trackerRef = useRef(null);
|
|
@@ -33,21 +15,28 @@ export function useEngagementTracking(options = {}) {
|
|
|
33
15
|
is_visible: true,
|
|
34
16
|
time_since_last_activity: 0,
|
|
35
17
|
});
|
|
36
|
-
const { enabled = true
|
|
18
|
+
const { enabled = true } = options;
|
|
19
|
+
// Callers usually pass an inline options object, so it is a new value on
|
|
20
|
+
// every render. Keeping it in a ref lets the callbacks always see the latest
|
|
21
|
+
// closure without tearing the tracker down and recreating it each render.
|
|
22
|
+
// Tracker settings (intervals, thresholds) are read when tracking starts.
|
|
23
|
+
const optionsRef = useRef(options);
|
|
24
|
+
optionsRef.current = options;
|
|
37
25
|
useEffect(() => {
|
|
38
26
|
if (!enabled || !analytics.enabled) {
|
|
39
27
|
return;
|
|
40
28
|
}
|
|
29
|
+
const { enabled: _enabled, onEngagementUpdate: _onEngagementUpdate, onPageLeave: _onPageLeave, ...trackerOptions } = optionsRef.current;
|
|
41
30
|
// Create tracker instance
|
|
42
31
|
trackerRef.current = new EngagementTracker({
|
|
43
32
|
...trackerOptions,
|
|
44
33
|
onHeartbeat: (data) => {
|
|
45
34
|
engagementDataRef.current = data;
|
|
46
|
-
onEngagementUpdate?.(data);
|
|
35
|
+
optionsRef.current.onEngagementUpdate?.(data);
|
|
47
36
|
},
|
|
48
37
|
onPageLeave: (data) => {
|
|
49
38
|
engagementDataRef.current = data;
|
|
50
|
-
onPageLeave?.(data);
|
|
39
|
+
optionsRef.current.onPageLeave?.(data);
|
|
51
40
|
},
|
|
52
41
|
});
|
|
53
42
|
// Cleanup on unmount
|
package/dist/usePageLeave.d.ts
CHANGED
|
@@ -7,7 +7,9 @@ export interface UsePageLeaveOptions {
|
|
|
7
7
|
/** Whether to enable page leave tracking. Defaults to true */
|
|
8
8
|
enabled?: boolean;
|
|
9
9
|
}
|
|
10
|
-
export
|
|
10
|
+
export interface UsePageLeaveResult {
|
|
11
|
+
/** Send the page-leave event now; resolves once sent, `undefined` if skipped. */
|
|
11
12
|
triggerPageLeave: () => Promise<void> | undefined;
|
|
12
|
-
}
|
|
13
|
+
}
|
|
14
|
+
export declare function usePageLeave(options?: UsePageLeaveOptions): UsePageLeaveResult;
|
|
13
15
|
//# sourceMappingURL=usePageLeave.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePageLeave.d.ts","sourceRoot":"","sources":["../src/usePageLeave.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePageLeave.d.ts","sourceRoot":"","sources":["../src/usePageLeave.ts"],"names":[],"mappings":"AAQA,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,MAAM,WAAW,kBAAkB;IACjC,iFAAiF;IACjF,gBAAgB,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;CACnD;AAED,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG,kBAAkB,CA2ElF"}
|
package/dist/usePageLeave.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useScrollVisibility.d.ts","sourceRoot":"","sources":["../src/useScrollVisibility.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useScrollVisibility.d.ts","sourceRoot":"","sources":["../src/useScrollVisibility.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,MAAM,WAAW,0BAA0B;IACzC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAEtC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAEtB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,GAAE,0BAA+B,GACvC,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI,KAAK,IAAI,CAiEpC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type React from "react";
|
|
2
|
+
import type { ReactElement } from "react";
|
|
2
3
|
interface SessionRecordingContextValue {
|
|
3
4
|
isRecordingEnabled: boolean;
|
|
4
5
|
enableRecording: () => void;
|
|
@@ -11,13 +12,14 @@ interface SessionRecordingProviderProps {
|
|
|
11
12
|
defaultEnabled?: boolean;
|
|
12
13
|
persistPreference?: boolean;
|
|
13
14
|
}
|
|
14
|
-
export declare function SessionRecordingProvider({ children, defaultEnabled, persistPreference }: SessionRecordingProviderProps):
|
|
15
|
+
export declare function SessionRecordingProvider({ children, defaultEnabled, persistPreference }: SessionRecordingProviderProps): ReactElement;
|
|
15
16
|
export declare function useSessionRecording(): SessionRecordingContextValue;
|
|
16
|
-
export
|
|
17
|
+
export interface SessionRecordingControl {
|
|
17
18
|
isEnabled: boolean;
|
|
18
19
|
enable: () => void;
|
|
19
20
|
disable: () => void;
|
|
20
21
|
toggle: () => void;
|
|
21
|
-
}
|
|
22
|
+
}
|
|
23
|
+
export declare function useSessionRecordingControl(defaultEnabled?: boolean): SessionRecordingControl;
|
|
22
24
|
export {};
|
|
23
25
|
//# sourceMappingURL=useSessionRecording.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSessionRecording.d.ts","sourceRoot":"","sources":["../src/useSessionRecording.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useSessionRecording.d.ts","sourceRoot":"","sources":["../src/useSessionRecording.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAE1C,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;AAiCD,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,GAAG,YAAY,CA6C9C;AAED,wBAAgB,mBAAmB,IAAI,4BAA4B,CAMlE;AAED,MAAM,WAAW,uBAAuB;IACtC,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB;AAGD,wBAAgB,0BAA0B,CAAC,cAAc,UAAQ,GAAG,uBAAuB,CA2B1F"}
|
|
@@ -1,44 +1,58 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
1
3
|
"use client";
|
|
2
4
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
5
|
import { useState, useEffect, useCallback, createContext, useContext, useMemo } from "react";
|
|
4
6
|
const SessionRecordingContext = createContext(undefined);
|
|
7
|
+
const PREFERENCE_KEY = "temps_session_recording_enabled";
|
|
8
|
+
/**
|
|
9
|
+
* `localStorage` access that cannot throw. Storage may be blocked (site data
|
|
10
|
+
* disabled, sandboxed iframe: merely reading `localStorage` throws) or full;
|
|
11
|
+
* the recording preference is a convenience and must never take the host
|
|
12
|
+
* app's render or event handler down with it.
|
|
13
|
+
*/
|
|
14
|
+
function readStorage(key) {
|
|
15
|
+
try {
|
|
16
|
+
return typeof localStorage === "undefined" ? null : localStorage.getItem(key);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function writeStorage(key, value) {
|
|
23
|
+
try {
|
|
24
|
+
if (typeof localStorage !== "undefined")
|
|
25
|
+
localStorage.setItem(key, value);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
console.warn("[SessionRecording] could not persist the recording preference:", error);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function readPreference(defaultEnabled) {
|
|
32
|
+
const stored = readStorage(PREFERENCE_KEY);
|
|
33
|
+
return stored === null ? defaultEnabled : stored === "true";
|
|
34
|
+
}
|
|
5
35
|
export function SessionRecordingProvider({ children, defaultEnabled = false, persistPreference = true }) {
|
|
6
|
-
const [isRecordingEnabled, setIsRecordingEnabled] = useState(() =>
|
|
7
|
-
if (!persistPreference)
|
|
8
|
-
return defaultEnabled;
|
|
9
|
-
if (typeof window !== "undefined" && typeof localStorage !== "undefined") {
|
|
10
|
-
const stored = localStorage.getItem("temps_session_recording_enabled");
|
|
11
|
-
if (stored !== null) {
|
|
12
|
-
return stored === "true";
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return defaultEnabled;
|
|
16
|
-
});
|
|
36
|
+
const [isRecordingEnabled, setIsRecordingEnabled] = useState(() => persistPreference ? readPreference(defaultEnabled) : defaultEnabled);
|
|
17
37
|
const [sessionId, setSessionId] = useState(null);
|
|
18
38
|
useEffect(() => {
|
|
19
|
-
|
|
20
|
-
const storedSessionId = localStorage.getItem("currentRecordingSessionId");
|
|
21
|
-
setSessionId(storedSessionId);
|
|
22
|
-
}
|
|
39
|
+
setSessionId(readStorage("currentRecordingSessionId"));
|
|
23
40
|
}, [isRecordingEnabled]);
|
|
24
41
|
const enableRecording = useCallback(() => {
|
|
25
42
|
setIsRecordingEnabled(true);
|
|
26
|
-
if (persistPreference
|
|
27
|
-
|
|
28
|
-
}
|
|
43
|
+
if (persistPreference)
|
|
44
|
+
writeStorage(PREFERENCE_KEY, "true");
|
|
29
45
|
}, [persistPreference]);
|
|
30
46
|
const disableRecording = useCallback(() => {
|
|
31
47
|
setIsRecordingEnabled(false);
|
|
32
|
-
if (persistPreference
|
|
33
|
-
|
|
34
|
-
}
|
|
48
|
+
if (persistPreference)
|
|
49
|
+
writeStorage(PREFERENCE_KEY, "false");
|
|
35
50
|
}, [persistPreference]);
|
|
36
51
|
const toggleRecording = useCallback(() => {
|
|
37
52
|
setIsRecordingEnabled(prev => {
|
|
38
53
|
const newValue = !prev;
|
|
39
|
-
if (persistPreference
|
|
40
|
-
|
|
41
|
-
}
|
|
54
|
+
if (persistPreference)
|
|
55
|
+
writeStorage(PREFERENCE_KEY, String(newValue));
|
|
42
56
|
return newValue;
|
|
43
57
|
});
|
|
44
58
|
}, [persistPreference]);
|
|
@@ -60,33 +74,19 @@ export function useSessionRecording() {
|
|
|
60
74
|
}
|
|
61
75
|
// Standalone hook for controlling session recording without provider
|
|
62
76
|
export function useSessionRecordingControl(defaultEnabled = false) {
|
|
63
|
-
const [isEnabled, setIsEnabled] = useState(() =>
|
|
64
|
-
if (typeof window !== "undefined" && typeof localStorage !== "undefined") {
|
|
65
|
-
const stored = localStorage.getItem("temps_session_recording_enabled");
|
|
66
|
-
if (stored !== null) {
|
|
67
|
-
return stored === "true";
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return defaultEnabled;
|
|
71
|
-
});
|
|
77
|
+
const [isEnabled, setIsEnabled] = useState(() => readPreference(defaultEnabled));
|
|
72
78
|
const enable = useCallback(() => {
|
|
73
79
|
setIsEnabled(true);
|
|
74
|
-
|
|
75
|
-
localStorage.setItem("temps_session_recording_enabled", "true");
|
|
76
|
-
}
|
|
80
|
+
writeStorage(PREFERENCE_KEY, "true");
|
|
77
81
|
}, []);
|
|
78
82
|
const disable = useCallback(() => {
|
|
79
83
|
setIsEnabled(false);
|
|
80
|
-
|
|
81
|
-
localStorage.setItem("temps_session_recording_enabled", "false");
|
|
82
|
-
}
|
|
84
|
+
writeStorage(PREFERENCE_KEY, "false");
|
|
83
85
|
}, []);
|
|
84
86
|
const toggle = useCallback(() => {
|
|
85
87
|
setIsEnabled(prev => {
|
|
86
88
|
const newValue = !prev;
|
|
87
|
-
|
|
88
|
-
localStorage.setItem("temps_session_recording_enabled", String(newValue));
|
|
89
|
-
}
|
|
89
|
+
writeStorage(PREFERENCE_KEY, String(newValue));
|
|
90
90
|
return newValue;
|
|
91
91
|
});
|
|
92
92
|
}, []);
|
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
export interface UseSpeedAnalyticsOptions {
|
|
2
2
|
/** Base endpoint path. Defaults to `/_temps`. */
|
|
3
3
|
basePath?: string;
|
|
4
|
+
/**
|
|
5
|
+
* Analytics ingest key (`pa_…`). Required only when Temps does not serve or
|
|
6
|
+
* proxy the app. See `AnalyticsClientOptions.ingestKey`.
|
|
7
|
+
*/
|
|
8
|
+
ingestKey?: string;
|
|
4
9
|
/** Set to true to disable speed analytics. Defaults to false. */
|
|
5
10
|
disabled?: boolean;
|
|
6
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Reports Web Vitals for the page that mounted the hook. See `SpeedTracker`
|
|
14
|
+
* for what is sent and when: load metrics never wait for an interaction, and
|
|
15
|
+
* every beacon is attributed to the path captured at mount, not the path
|
|
16
|
+
* current when a late CLS/INP value is reported.
|
|
17
|
+
*/
|
|
7
18
|
export declare function useSpeedAnalytics(options?: UseSpeedAnalyticsOptions): void;
|
|
8
19
|
//# sourceMappingURL=useSpeedAnalytics.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSpeedAnalytics.d.ts","sourceRoot":"","sources":["../src/useSpeedAnalytics.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useSpeedAnalytics.d.ts","sourceRoot":"","sources":["../src/useSpeedAnalytics.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,wBAAwB;IACvC,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,wBAA6B,GAAG,IAAI,CAQ9E"}
|
|
@@ -1,60 +1,20 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
1
3
|
"use client";
|
|
2
4
|
import { useEffect } from "react";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
+
import { SpeedTracker } from "@temps-sdk/analytics-core";
|
|
6
|
+
/**
|
|
7
|
+
* Reports Web Vitals for the page that mounted the hook. See `SpeedTracker`
|
|
8
|
+
* for what is sent and when: load metrics never wait for an interaction, and
|
|
9
|
+
* every beacon is attributed to the path captured at mount, not the path
|
|
10
|
+
* current when a late CLS/INP value is reported.
|
|
11
|
+
*/
|
|
5
12
|
export function useSpeedAnalytics(options = {}) {
|
|
6
|
-
const { basePath = "/_temps", disabled = false } = options;
|
|
13
|
+
const { basePath = "/_temps", ingestKey, disabled = false } = options;
|
|
7
14
|
useEffect(() => {
|
|
8
15
|
if (disabled || typeof window === "undefined")
|
|
9
16
|
return;
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
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]);
|
|
17
|
+
const tracker = new SpeedTracker({ basePath, ingestKey });
|
|
18
|
+
return () => tracker.destroy();
|
|
19
|
+
}, [basePath, ingestKey, disabled]);
|
|
60
20
|
}
|
|
@@ -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":"AAMA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,wBAAgB,aAAa,IAAI,CAC/B,SAAS,EAAE,MAAM,EACjB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,KAC7B,OAAO,CAAC,IAAI,CAAC,CAGjB"}
|
package/dist/useTrackEvent.js
CHANGED
|
@@ -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":"AAOA,wBAAgB,gBAAgB,IAAI,MAAM,IAAI,CAG7C"}
|
package/dist/useTrackPageview.js
CHANGED
package/dist/utils.d.ts
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
|
|
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;
|
|
1
|
+
export { getRequestId, isLocalhostLike, isTestEnvironment, sendAnalytics, sendAnalyticsReliable, ingestKeyHeaders, withIngestKey, } from "@temps-sdk/analytics-core";
|
|
7
2
|
//# 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":"AAIA,OAAO,EACL,YAAY,EACZ,eAAe,EACf,iBAAiB,EACjB,aAAa,EACb,qBAAqB,EACrB,gBAAgB,EAChB,aAAa,GACd,MAAM,2BAA2B,CAAC"}
|