@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/README.md
CHANGED
|
@@ -62,7 +62,11 @@ export default function App({ children }) {
|
|
|
62
62
|
```tsx
|
|
63
63
|
<TempsAnalyticsProvider
|
|
64
64
|
// Core
|
|
65
|
-
basePath="/api/_temps" // API endpoint (default)
|
|
65
|
+
basePath="/api/_temps" // API endpoint (default). Can be an absolute
|
|
66
|
+
// URL on another origin -- see "Externally
|
|
67
|
+
// Hosted Apps" below
|
|
68
|
+
ingestKey={undefined} // Keyed-ingest credential, only needed when
|
|
69
|
+
// Temps didn't deploy this app
|
|
66
70
|
domain="example.com" // Override detected hostname
|
|
67
71
|
disabled={false} // Kill switch for analytics
|
|
68
72
|
|
|
@@ -86,7 +90,8 @@ export default function App({ children }) {
|
|
|
86
90
|
// Session Recording
|
|
87
91
|
enableSessionRecording={false} // Off by default
|
|
88
92
|
sessionRecordingConfig={{
|
|
89
|
-
excludedPaths: ['/settings/*', '/admin/*'],
|
|
93
|
+
excludedPaths: ['/settings/*', '/admin/*'], // Added to the built-in sensitive paths
|
|
94
|
+
useDefaultExcludedPaths: true, // Keep excluding login, signup, checkout, payment, billing, password reset, MFA, verification (default)
|
|
90
95
|
sessionSampleRate: 1.0, // Record 100% of sessions (default)
|
|
91
96
|
maskAllInputs: true, // Mask all input fields (default)
|
|
92
97
|
maskTextSelector: '[data-mask]', // CSS selector for text masking
|
|
@@ -94,9 +99,12 @@ export default function App({ children }) {
|
|
|
94
99
|
ignoreClass: 'rr-ignore', // CSS class to ignore
|
|
95
100
|
maskTextClass: 'rr-mask', // CSS class to mask text
|
|
96
101
|
recordCanvas: false, // Record canvas elements (default: false)
|
|
97
|
-
collectFonts:
|
|
102
|
+
collectFonts: true, // Capture FontFace-loaded fonts for replay (default: true)
|
|
98
103
|
batchSize: 100, // Events per batch (default)
|
|
99
|
-
flushInterval:
|
|
104
|
+
flushInterval: 10000, // Flush interval in ms (default)
|
|
105
|
+
idleTimeout: 60000, // Pause after 60s without interaction; 0 = never (default)
|
|
106
|
+
pauseOnHidden: true, // Pause while the tab is hidden (default)
|
|
107
|
+
debug: false, // Log every failed request + why a page is not recorded (default: false)
|
|
100
108
|
}}
|
|
101
109
|
>
|
|
102
110
|
{children}
|
|
@@ -236,7 +244,7 @@ useSpeedAnalytics({
|
|
|
236
244
|
| **CLS** | Cumulative Layout Shift | Visual stability |
|
|
237
245
|
| **INP** | Interaction to Next Paint | Overall responsiveness |
|
|
238
246
|
|
|
239
|
-
|
|
247
|
+
Load metrics (TTFB, FCP, LCP) are sent in a single request as soon as all three are known, or when the page is hidden, whichever comes first -- they never wait for an interaction, so visitors who only read the page are measured too. FID is included when it already arrived and sent on its own otherwise. Late metrics (CLS, INP) are sent individually as they stabilize. Every metric is attributed to the path the page loaded on, even when it is reported after a client-side navigation.
|
|
240
248
|
|
|
241
249
|
### `useEngagementTracking(options?)`
|
|
242
250
|
|
|
@@ -401,20 +409,26 @@ sessionRecordingConfig={{
|
|
|
401
409
|
|
|
402
410
|
### Path Exclusions
|
|
403
411
|
|
|
404
|
-
|
|
412
|
+
Sensitive routes are excluded by default: `/login`, `/log-in`, `/signin`, `/sign-in`, `/logout`, `/log-out`, `/signup`, `/sign-up`, `/register`, and everything under `/checkout`, `/payment`, `/billing`, `/reset-password`, `/forgot-password`, `/mfa`, `/2fa` and `/verify`. Add your own routes with `excludedPaths`:
|
|
405
413
|
|
|
406
414
|
```tsx
|
|
407
415
|
sessionRecordingConfig={{
|
|
408
416
|
excludedPaths: [
|
|
409
417
|
'/settings/*', // Wildcard: all settings subpages
|
|
410
418
|
'/admin/*', // Wildcard: entire admin area
|
|
411
|
-
'/
|
|
419
|
+
'/account', // Exact match
|
|
412
420
|
],
|
|
413
421
|
}}
|
|
414
422
|
```
|
|
415
423
|
|
|
424
|
+
Set `useDefaultExcludedPaths: false` to record the built-in paths too, so only `excludedPaths` is skipped.
|
|
425
|
+
|
|
416
426
|
Recording automatically pauses when navigating to excluded paths and resumes when leaving them.
|
|
417
427
|
|
|
428
|
+
### Troubleshooting
|
|
429
|
+
|
|
430
|
+
A failed session-replay request is reported once per page as a console warning (for example a `401` from a wrong `ingestKey`). Set `debug: true` in `sessionRecordingConfig` to log every failure and the reason a page is not recorded (excluded path, sampled out).
|
|
431
|
+
|
|
418
432
|
### How It Works
|
|
419
433
|
|
|
420
434
|
1. `SessionRecorder` initializes a session via `POST /api/_temps/session-replay/init` with device metadata
|
|
@@ -492,6 +506,23 @@ ReactDOM.createRoot(document.getElementById('root')!).render(
|
|
|
492
506
|
);
|
|
493
507
|
```
|
|
494
508
|
|
|
509
|
+
## Externally Hosted Apps (Cross-Origin Ingest)
|
|
510
|
+
|
|
511
|
+
If Temps isn't the platform hosting this app -- e.g. a Next.js app on a separate hosting platform talking to a self-hosted Temps backend on another domain -- point the provider at your Temps backend with an ingest key instead of the default same-origin `basePath`:
|
|
512
|
+
|
|
513
|
+
```tsx
|
|
514
|
+
<TempsAnalyticsProvider
|
|
515
|
+
basePath="https://api.your-domain.example.com/api/_temps"
|
|
516
|
+
ingestKey="pa_..."
|
|
517
|
+
>
|
|
518
|
+
{children}
|
|
519
|
+
</TempsAnalyticsProvider>
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
Mint the key with `bunx @temps-sdk/cli analytics keys create --project <id>` or from the project's Analytics settings in the Console. Session/visitor identity (`localStorage`-backed, generated automatically by `@temps-sdk/analytics-core`) is attached to every event and is what the backend attributes traffic to on this path, since no Temps-issued cookie exists cross-origin.
|
|
523
|
+
|
|
524
|
+
For the full walkthrough -- routing through a same-origin proxy so the key never ships to the browser, `sendBeacon` header limitations, and Next.js-specific gotchas -- see [Externally Hosted Apps](https://temps.sh/docs/sdks#external-hosting) in the SDK docs.
|
|
525
|
+
|
|
495
526
|
## Environment Detection
|
|
496
527
|
|
|
497
528
|
The SDK automatically disables itself in non-production environments when `ignoreLocalhost` is `true` (default):
|
|
@@ -1,55 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
basePath?: string;
|
|
3
|
-
domain?: string;
|
|
4
|
-
heartbeatInterval?: number;
|
|
5
|
-
inactivityTimeout?: number;
|
|
6
|
-
engagementThreshold?: number;
|
|
7
|
-
onHeartbeat?: (data: EngagementData) => void;
|
|
8
|
-
onPageLeave?: (data: EngagementData) => void;
|
|
9
|
-
}
|
|
10
|
-
export interface EngagementData {
|
|
11
|
-
engagement_time_seconds: number;
|
|
12
|
-
total_time_seconds: number;
|
|
13
|
-
heartbeat_count: number;
|
|
14
|
-
is_engaged: boolean;
|
|
15
|
-
is_visible: boolean;
|
|
16
|
-
time_since_last_activity: number;
|
|
17
|
-
}
|
|
18
|
-
export declare class EngagementTracker {
|
|
19
|
-
private startTime;
|
|
20
|
-
private engagementTime;
|
|
21
|
-
private lastActivityTime;
|
|
22
|
-
private lastEngagementStart;
|
|
23
|
-
private heartbeatCount;
|
|
24
|
-
private heartbeatInterval;
|
|
25
|
-
private inactivityTimeout;
|
|
26
|
-
private engagementThreshold;
|
|
27
|
-
private isVisible;
|
|
28
|
-
private isFocused;
|
|
29
|
-
private isActive;
|
|
30
|
-
private heartbeatTimer;
|
|
31
|
-
private inactivityTimer;
|
|
32
|
-
private hasTrackedLeave;
|
|
33
|
-
private readonly basePath;
|
|
34
|
-
private readonly domain;
|
|
35
|
-
private readonly onHeartbeat?;
|
|
36
|
-
private readonly onPageLeave?;
|
|
37
|
-
constructor(options?: EngagementTrackerOptions);
|
|
38
|
-
private setupEventListeners;
|
|
39
|
-
private handleVisibilityChange;
|
|
40
|
-
private handleFocus;
|
|
41
|
-
private handleBlur;
|
|
42
|
-
private pauseEngagement;
|
|
43
|
-
private handleUserActivity;
|
|
44
|
-
private resetInactivityTimer;
|
|
45
|
-
private updateEngagementTime;
|
|
46
|
-
private startHeartbeat;
|
|
47
|
-
private stopHeartbeat;
|
|
48
|
-
private sendHeartbeat;
|
|
49
|
-
private handlePageLeave;
|
|
50
|
-
private handleNavigation;
|
|
51
|
-
private getEngagementData;
|
|
52
|
-
private reset;
|
|
53
|
-
destroy(): void;
|
|
54
|
-
}
|
|
1
|
+
export { EngagementTracker, type EngagementTrackerOptions, type EngagementData, } from "@temps-sdk/analytics-core";
|
|
55
2
|
//# sourceMappingURL=EngagementTracker.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EngagementTracker.d.ts","sourceRoot":"","sources":["../src/EngagementTracker.ts"],"names":[],"mappings":"AAIA,
|
|
1
|
+
{"version":3,"file":"EngagementTracker.d.ts","sourceRoot":"","sources":["../src/EngagementTracker.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,iBAAiB,EACjB,KAAK,wBAAwB,EAC7B,KAAK,cAAc,GACpB,MAAM,2BAA2B,CAAC"}
|
|
@@ -1,223 +1,4 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
1
3
|
"use client";
|
|
2
|
-
|
|
3
|
-
export class EngagementTracker {
|
|
4
|
-
constructor(options = {}) {
|
|
5
|
-
this.engagementTime = 0;
|
|
6
|
-
this.lastEngagementStart = null;
|
|
7
|
-
this.heartbeatCount = 0;
|
|
8
|
-
this.isActive = true;
|
|
9
|
-
this.heartbeatTimer = null;
|
|
10
|
-
this.inactivityTimer = null;
|
|
11
|
-
this.hasTrackedLeave = false;
|
|
12
|
-
this.handleVisibilityChange = () => {
|
|
13
|
-
const wasVisible = this.isVisible;
|
|
14
|
-
this.isVisible = document.visibilityState === "visible";
|
|
15
|
-
if (!wasVisible && this.isVisible) {
|
|
16
|
-
// Tab became visible - but don't start engagement until user interacts
|
|
17
|
-
this.startHeartbeat();
|
|
18
|
-
}
|
|
19
|
-
else if (wasVisible && !this.isVisible) {
|
|
20
|
-
// Tab became hidden - pause engagement
|
|
21
|
-
this.pauseEngagement();
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
this.handleFocus = () => {
|
|
25
|
-
this.isFocused = true;
|
|
26
|
-
};
|
|
27
|
-
this.handleBlur = () => {
|
|
28
|
-
this.isFocused = false;
|
|
29
|
-
// When window loses focus, pause engagement tracking
|
|
30
|
-
this.pauseEngagement();
|
|
31
|
-
};
|
|
32
|
-
this.handleUserActivity = () => {
|
|
33
|
-
// Only track activity if tab is visible AND focused
|
|
34
|
-
if (!this.isVisible || !this.isFocused)
|
|
35
|
-
return;
|
|
36
|
-
const now = Date.now();
|
|
37
|
-
if (!this.isActive) {
|
|
38
|
-
// Resume from inactive state
|
|
39
|
-
this.isActive = true;
|
|
40
|
-
this.lastEngagementStart = now;
|
|
41
|
-
this.startHeartbeat();
|
|
42
|
-
}
|
|
43
|
-
this.lastActivityTime = now;
|
|
44
|
-
this.resetInactivityTimer();
|
|
45
|
-
};
|
|
46
|
-
this.handlePageLeave = () => {
|
|
47
|
-
if (this.hasTrackedLeave)
|
|
48
|
-
return;
|
|
49
|
-
this.hasTrackedLeave = true;
|
|
50
|
-
this.updateEngagementTime();
|
|
51
|
-
const data = this.getEngagementData();
|
|
52
|
-
if (this.onPageLeave) {
|
|
53
|
-
this.onPageLeave(data);
|
|
54
|
-
}
|
|
55
|
-
sendAnalyticsReliable("event", {
|
|
56
|
-
event_name: "page_leave",
|
|
57
|
-
request_path: window.location.pathname,
|
|
58
|
-
request_query: window.location.search,
|
|
59
|
-
domain: this.domain,
|
|
60
|
-
event_data: {
|
|
61
|
-
engagement_time_seconds: data.engagement_time_seconds,
|
|
62
|
-
total_time_seconds: data.total_time_seconds,
|
|
63
|
-
heartbeat_count: this.heartbeatCount,
|
|
64
|
-
was_engaged: data.is_engaged,
|
|
65
|
-
url: window.location.href,
|
|
66
|
-
referrer: document.referrer,
|
|
67
|
-
},
|
|
68
|
-
}, this.basePath);
|
|
69
|
-
};
|
|
70
|
-
this.handleNavigation = () => {
|
|
71
|
-
this.handlePageLeave();
|
|
72
|
-
this.reset();
|
|
73
|
-
};
|
|
74
|
-
this.basePath = options.basePath || "/api/_temps";
|
|
75
|
-
this.domain = options.domain || window.location.hostname;
|
|
76
|
-
this.heartbeatInterval = options.heartbeatInterval || 30000; // 30 seconds
|
|
77
|
-
this.inactivityTimeout = options.inactivityTimeout || 30000; // 30 seconds
|
|
78
|
-
this.engagementThreshold = options.engagementThreshold || 10000; // 10 seconds
|
|
79
|
-
this.onHeartbeat = options.onHeartbeat;
|
|
80
|
-
this.onPageLeave = options.onPageLeave;
|
|
81
|
-
this.startTime = Date.now();
|
|
82
|
-
this.lastActivityTime = Date.now();
|
|
83
|
-
this.isVisible = document.visibilityState === "visible";
|
|
84
|
-
this.isFocused = document.hasFocus();
|
|
85
|
-
// Don't start engagement tracking until first user interaction
|
|
86
|
-
this.lastEngagementStart = null;
|
|
87
|
-
this.isActive = false;
|
|
88
|
-
this.setupEventListeners();
|
|
89
|
-
this.startHeartbeat();
|
|
90
|
-
}
|
|
91
|
-
setupEventListeners() {
|
|
92
|
-
// Visibility change
|
|
93
|
-
document.addEventListener("visibilitychange", this.handleVisibilityChange);
|
|
94
|
-
// Focus/blur for better multi-monitor support
|
|
95
|
-
window.addEventListener("focus", this.handleFocus);
|
|
96
|
-
window.addEventListener("blur", this.handleBlur);
|
|
97
|
-
// User activity tracking - use more specific events to avoid false positives
|
|
98
|
-
const activityEvents = ["mousedown", "keypress", "scroll", "touchstart", "click"];
|
|
99
|
-
activityEvents.forEach(event => {
|
|
100
|
-
document.addEventListener(event, this.handleUserActivity, { passive: true });
|
|
101
|
-
});
|
|
102
|
-
// Page leave events
|
|
103
|
-
window.addEventListener("pagehide", this.handlePageLeave);
|
|
104
|
-
window.addEventListener("beforeunload", this.handlePageLeave);
|
|
105
|
-
// SPA navigation
|
|
106
|
-
const originalPushState = window.history.pushState;
|
|
107
|
-
window.history.pushState = (...args) => {
|
|
108
|
-
this.handlePageLeave();
|
|
109
|
-
originalPushState.apply(window.history, args);
|
|
110
|
-
this.reset();
|
|
111
|
-
};
|
|
112
|
-
window.addEventListener("popstate", this.handleNavigation);
|
|
113
|
-
}
|
|
114
|
-
pauseEngagement() {
|
|
115
|
-
this.updateEngagementTime();
|
|
116
|
-
this.lastEngagementStart = null;
|
|
117
|
-
if (!this.isVisible) {
|
|
118
|
-
this.stopHeartbeat();
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
resetInactivityTimer() {
|
|
122
|
-
if (this.inactivityTimer) {
|
|
123
|
-
clearTimeout(this.inactivityTimer);
|
|
124
|
-
}
|
|
125
|
-
this.inactivityTimer = setTimeout(() => {
|
|
126
|
-
if (this.isVisible) {
|
|
127
|
-
this.updateEngagementTime();
|
|
128
|
-
this.isActive = false;
|
|
129
|
-
this.lastEngagementStart = null;
|
|
130
|
-
}
|
|
131
|
-
}, this.inactivityTimeout);
|
|
132
|
-
}
|
|
133
|
-
updateEngagementTime() {
|
|
134
|
-
if (this.lastEngagementStart !== null) {
|
|
135
|
-
const currentEngagement = Date.now() - this.lastEngagementStart;
|
|
136
|
-
this.engagementTime += currentEngagement;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
startHeartbeat() {
|
|
140
|
-
if (this.heartbeatTimer)
|
|
141
|
-
return;
|
|
142
|
-
this.heartbeatTimer = setInterval(() => {
|
|
143
|
-
// Only send heartbeat if tab is visible AND user has been active
|
|
144
|
-
if (this.isVisible && this.isActive) {
|
|
145
|
-
this.sendHeartbeat();
|
|
146
|
-
}
|
|
147
|
-
}, this.heartbeatInterval);
|
|
148
|
-
}
|
|
149
|
-
stopHeartbeat() {
|
|
150
|
-
if (this.heartbeatTimer) {
|
|
151
|
-
clearInterval(this.heartbeatTimer);
|
|
152
|
-
this.heartbeatTimer = null;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
async sendHeartbeat() {
|
|
156
|
-
this.updateEngagementTime();
|
|
157
|
-
this.heartbeatCount++;
|
|
158
|
-
const data = this.getEngagementData();
|
|
159
|
-
if (this.onHeartbeat) {
|
|
160
|
-
this.onHeartbeat(data);
|
|
161
|
-
}
|
|
162
|
-
await sendAnalytics("event", {
|
|
163
|
-
event_name: "heartbeat",
|
|
164
|
-
request_path: window.location.pathname,
|
|
165
|
-
request_query: window.location.search,
|
|
166
|
-
domain: this.domain,
|
|
167
|
-
event_data: {
|
|
168
|
-
engagement_time: Math.round(this.engagementTime),
|
|
169
|
-
total_time: Math.round(Date.now() - this.startTime),
|
|
170
|
-
is_engaged: data.is_engaged,
|
|
171
|
-
is_visible: this.isVisible,
|
|
172
|
-
time_since_last_activity: data.time_since_last_activity,
|
|
173
|
-
heartbeat_count: this.heartbeatCount,
|
|
174
|
-
},
|
|
175
|
-
}, "POST", this.basePath);
|
|
176
|
-
// Reset engagement start for next period
|
|
177
|
-
if (this.isActive && this.isVisible) {
|
|
178
|
-
this.lastEngagementStart = Date.now();
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
getEngagementData() {
|
|
182
|
-
const totalTime = Date.now() - this.startTime;
|
|
183
|
-
const timeSinceLastActivity = Date.now() - this.lastActivityTime;
|
|
184
|
-
return {
|
|
185
|
-
engagement_time_seconds: Math.round(this.engagementTime / 1000),
|
|
186
|
-
total_time_seconds: Math.round(totalTime / 1000),
|
|
187
|
-
heartbeat_count: this.heartbeatCount,
|
|
188
|
-
is_engaged: this.engagementTime >= this.engagementThreshold,
|
|
189
|
-
is_visible: this.isVisible,
|
|
190
|
-
time_since_last_activity: Math.round(timeSinceLastActivity / 1000),
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
reset() {
|
|
194
|
-
this.startTime = Date.now();
|
|
195
|
-
this.engagementTime = 0;
|
|
196
|
-
this.lastActivityTime = Date.now();
|
|
197
|
-
this.heartbeatCount = 0;
|
|
198
|
-
this.hasTrackedLeave = false;
|
|
199
|
-
// Reset to inactive state - wait for user interaction
|
|
200
|
-
this.isActive = false;
|
|
201
|
-
this.lastEngagementStart = null;
|
|
202
|
-
if (this.isVisible) {
|
|
203
|
-
this.startHeartbeat();
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
destroy() {
|
|
207
|
-
this.handlePageLeave();
|
|
208
|
-
this.stopHeartbeat();
|
|
209
|
-
if (this.inactivityTimer) {
|
|
210
|
-
clearTimeout(this.inactivityTimer);
|
|
211
|
-
}
|
|
212
|
-
document.removeEventListener("visibilitychange", this.handleVisibilityChange);
|
|
213
|
-
window.removeEventListener("focus", this.handleFocus);
|
|
214
|
-
window.removeEventListener("blur", this.handleBlur);
|
|
215
|
-
window.removeEventListener("pagehide", this.handlePageLeave);
|
|
216
|
-
window.removeEventListener("beforeunload", this.handlePageLeave);
|
|
217
|
-
window.removeEventListener("popstate", this.handleNavigation);
|
|
218
|
-
const activityEvents = ["mousedown", "keypress", "scroll", "touchstart", "click"];
|
|
219
|
-
activityEvents.forEach(event => {
|
|
220
|
-
document.removeEventListener(event, this.handleUserActivity);
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
}
|
|
4
|
+
export { EngagementTracker, } from "@temps-sdk/analytics-core";
|
package/dist/Provider.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
1
2
|
import type { AnalyticsContextValue, TempsAnalyticsProviderProps } from "./types";
|
|
2
|
-
export declare function TempsAnalyticsProvider({ basePath, disabled, ignoreLocalhost, autoTrackPageviews, autoTrackPageLeave, pageLeaveEventName, autoTrackSpeedAnalytics, autoTrackEngagement, heartbeatInterval, inactivityTimeout, engagementThreshold, enableSessionRecording, sessionRecordingConfig, domain, children, }: TempsAnalyticsProviderProps):
|
|
3
|
+
export declare function TempsAnalyticsProvider({ basePath, disabled, ignoreLocalhost, autoTrackPageviews, autoTrackPageLeave, pageLeaveEventName, autoTrackSpeedAnalytics, autoTrackEngagement, heartbeatInterval, inactivityTimeout, engagementThreshold, enableSessionRecording, sessionRecordingConfig, domain, ingestKey, children, }: TempsAnalyticsProviderProps): ReactElement;
|
|
3
4
|
export declare function useTempsAnalytics(): AnalyticsContextValue;
|
|
4
5
|
//# sourceMappingURL=Provider.d.ts.map
|
package/dist/Provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../src/Provider.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../src/Provider.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,KAAK,EAAE,qBAAqB,EAAE,2BAA2B,EAAE,MAAM,SAAS,CAAC;AAUlF,wBAAgB,sBAAsB,CAAC,EACrC,QAA4B,EAC5B,QAAgB,EAChB,eAAsB,EACtB,kBAAyB,EACzB,kBAAyB,EACzB,kBAAiC,EACjC,uBAA8B,EAC9B,mBAA0B,EAC1B,iBAAyB,EACzB,iBAAyB,EACzB,mBAA2B,EAC3B,sBAA8B,EAC9B,sBAA2B,EAC3B,MAAM,EACN,SAAS,EACT,QAAQ,GACT,EAAE,2BAA2B,GAAG,YAAY,CAsO5C;AAED,wBAAgB,iBAAiB,IAAI,qBAAqB,CAMzD"}
|
package/dist/Provider.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
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, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
5
|
import { createContext, useCallback, useContext, useEffect, useMemo, useRef } from "react";
|
|
@@ -8,7 +10,7 @@ import { SessionRecorder } from "./SessionRecorder";
|
|
|
8
10
|
import { SessionRecordingProvider, useSessionRecordingControl } from "./useSessionRecording";
|
|
9
11
|
import { DEFAULT_BASE_PATH } from "./constants";
|
|
10
12
|
const AnalyticsContext = createContext(undefined);
|
|
11
|
-
export function TempsAnalyticsProvider({ basePath = DEFAULT_BASE_PATH, disabled = false, ignoreLocalhost = true, autoTrackPageviews = true, autoTrackPageLeave = true, pageLeaveEventName = "page_leave", autoTrackSpeedAnalytics = true, autoTrackEngagement = true, heartbeatInterval = 30000, inactivityTimeout = 30000, engagementThreshold = 10000, enableSessionRecording = false, sessionRecordingConfig = {}, domain, children, }) {
|
|
13
|
+
export function TempsAnalyticsProvider({ basePath = DEFAULT_BASE_PATH, disabled = false, ignoreLocalhost = true, autoTrackPageviews = true, autoTrackPageLeave = true, pageLeaveEventName = "page_leave", autoTrackSpeedAnalytics = true, autoTrackEngagement = true, heartbeatInterval = 30000, inactivityTimeout = 30000, engagementThreshold = 10000, enableSessionRecording = false, sessionRecordingConfig = {}, domain, ingestKey, children, }) {
|
|
12
14
|
const enabled = useMemo(() => {
|
|
13
15
|
if (disabled)
|
|
14
16
|
return false;
|
|
@@ -20,7 +22,9 @@ export function TempsAnalyticsProvider({ basePath = DEFAULT_BASE_PATH, disabled
|
|
|
20
22
|
if (window.localStorage?.temps_ignore === "true")
|
|
21
23
|
return false;
|
|
22
24
|
}
|
|
23
|
-
catch {
|
|
25
|
+
catch {
|
|
26
|
+
// Storage blocked: there is no opt-out flag to honour.
|
|
27
|
+
}
|
|
24
28
|
return true;
|
|
25
29
|
}, [disabled, ignoreLocalhost]);
|
|
26
30
|
const trackEvent = useCallback(async (eventName, data = {}) => {
|
|
@@ -30,10 +34,12 @@ export function TempsAnalyticsProvider({ basePath = DEFAULT_BASE_PATH, disabled
|
|
|
30
34
|
event_name: eventName,
|
|
31
35
|
request_query: window.location.search,
|
|
32
36
|
request_path: window.location.pathname,
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- an empty domain falls back too
|
|
33
38
|
domain: domain || window.location.hostname,
|
|
39
|
+
language: navigator.language,
|
|
34
40
|
event_data: data,
|
|
35
|
-
}, "POST", basePath);
|
|
36
|
-
}, [enabled, basePath, domain]);
|
|
41
|
+
}, "POST", basePath, ingestKey);
|
|
42
|
+
}, [enabled, basePath, domain, ingestKey]);
|
|
37
43
|
const trackPageview = useCallback(() => {
|
|
38
44
|
if (!enabled)
|
|
39
45
|
return;
|
|
@@ -41,14 +47,16 @@ export function TempsAnalyticsProvider({ basePath = DEFAULT_BASE_PATH, disabled
|
|
|
41
47
|
event_name: "page_view",
|
|
42
48
|
request_query: window.location.search,
|
|
43
49
|
request_path: window.location.pathname,
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- an empty domain falls back too
|
|
44
51
|
domain: domain || window.location.hostname,
|
|
52
|
+
language: navigator.language,
|
|
45
53
|
event_data: {
|
|
46
54
|
referrer: document.referrer,
|
|
47
55
|
userAgent: navigator.userAgent,
|
|
48
56
|
timestamp: new Date().toISOString(),
|
|
49
57
|
},
|
|
50
|
-
}, "POST", basePath);
|
|
51
|
-
}, [enabled, basePath, domain]);
|
|
58
|
+
}, "POST", basePath, ingestKey);
|
|
59
|
+
}, [enabled, basePath, domain, ingestKey]);
|
|
52
60
|
const identify = useCallback(async () => {
|
|
53
61
|
// no-op placeholder for SDK parity; implement when identity endpoint is available
|
|
54
62
|
}, []);
|
|
@@ -135,6 +143,7 @@ export function TempsAnalyticsProvider({ basePath = DEFAULT_BASE_PATH, disabled
|
|
|
135
143
|
if (autoTrackEngagement) {
|
|
136
144
|
const tracker = new EngagementTracker({
|
|
137
145
|
basePath,
|
|
146
|
+
ingestKey,
|
|
138
147
|
domain,
|
|
139
148
|
heartbeatInterval,
|
|
140
149
|
inactivityTimeout,
|
|
@@ -147,7 +156,7 @@ export function TempsAnalyticsProvider({ basePath = DEFAULT_BASE_PATH, disabled
|
|
|
147
156
|
// Legacy page leave tracking without engagement metrics
|
|
148
157
|
else if (autoTrackPageLeave) {
|
|
149
158
|
let hasTracked = false;
|
|
150
|
-
|
|
159
|
+
const startTime = Date.now();
|
|
151
160
|
const trackPageLeave = () => {
|
|
152
161
|
if (hasTracked)
|
|
153
162
|
return;
|
|
@@ -157,14 +166,16 @@ export function TempsAnalyticsProvider({ basePath = DEFAULT_BASE_PATH, disabled
|
|
|
157
166
|
event_name: pageLeaveEventName,
|
|
158
167
|
request_query: window.location.search,
|
|
159
168
|
request_path: window.location.pathname,
|
|
169
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- an empty domain falls back too
|
|
160
170
|
domain: domain || window.location.hostname,
|
|
171
|
+
language: navigator.language,
|
|
161
172
|
event_data: {
|
|
162
173
|
time_on_page_ms: timeOnPage,
|
|
163
174
|
timestamp: new Date().toISOString(),
|
|
164
175
|
url: window.location.href,
|
|
165
176
|
referrer: document.referrer,
|
|
166
177
|
},
|
|
167
|
-
}, basePath);
|
|
178
|
+
}, basePath, ingestKey);
|
|
168
179
|
};
|
|
169
180
|
// Use pagehide as primary (most reliable), with beforeunload as fallback
|
|
170
181
|
const handlePageLeave = () => trackPageLeave();
|
|
@@ -177,16 +188,20 @@ export function TempsAnalyticsProvider({ basePath = DEFAULT_BASE_PATH, disabled
|
|
|
177
188
|
window.removeEventListener("beforeunload", handlePageLeave);
|
|
178
189
|
};
|
|
179
190
|
}
|
|
180
|
-
}, [autoTrackPageLeave, autoTrackEngagement, enabled, pageLeaveEventName, domain, basePath, heartbeatInterval, inactivityTimeout, engagementThreshold]);
|
|
191
|
+
}, [autoTrackPageLeave, autoTrackEngagement, enabled, pageLeaveEventName, domain, basePath, ingestKey, heartbeatInterval, inactivityTimeout, engagementThreshold]);
|
|
181
192
|
// Speed analytics tracking
|
|
182
193
|
useSpeedAnalytics({
|
|
183
194
|
basePath,
|
|
195
|
+
ingestKey,
|
|
184
196
|
disabled: !enabled || !autoTrackSpeedAnalytics,
|
|
185
197
|
});
|
|
186
198
|
// Session recording control
|
|
187
199
|
const { isEnabled: isRecordingEnabled } = useSessionRecordingControl(enableSessionRecording);
|
|
188
200
|
const value = useMemo(() => ({ trackEvent, identify, trackPageview, enabled }), [trackEvent, identify, trackPageview, enabled]);
|
|
189
|
-
return (_jsx(AnalyticsContext.Provider, { value: value, children: _jsxs(SessionRecordingProvider, { defaultEnabled: enableSessionRecording, children: [children, enabled && (
|
|
201
|
+
return (_jsx(AnalyticsContext.Provider, { value: value, children: _jsxs(SessionRecordingProvider, { defaultEnabled: enableSessionRecording, children: [children, enabled && (
|
|
202
|
+
// Forward the whole config; the provider-level values go last so the
|
|
203
|
+
// config cannot override them.
|
|
204
|
+
_jsx(SessionRecorder, { ...sessionRecordingConfig, basePath: basePath, ingestKey: ingestKey, domain: domain, enabled: isRecordingEnabled }))] }) }));
|
|
190
205
|
}
|
|
191
206
|
export function useTempsAnalytics() {
|
|
192
207
|
const ctx = useContext(AnalyticsContext);
|
|
@@ -1,57 +1,56 @@
|
|
|
1
|
+
import { type SessionRecorderOptions } from "@temps-sdk/analytics-core";
|
|
1
2
|
export declare const SESSION_RECORDER_ENDPOINT = "session-replay";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
password?: boolean;
|
|
18
|
-
email?: boolean;
|
|
3
|
+
/** rrweb sampling options, typed for React callers. */
|
|
4
|
+
export interface SessionRecordingSampling {
|
|
5
|
+
scroll?: number;
|
|
6
|
+
media?: number;
|
|
7
|
+
mouseInteraction?: boolean | {
|
|
8
|
+
click?: boolean;
|
|
9
|
+
dblclick?: boolean;
|
|
10
|
+
contextmenu?: boolean;
|
|
11
|
+
focus?: boolean;
|
|
12
|
+
blur?: boolean;
|
|
13
|
+
touchstart?: boolean;
|
|
14
|
+
touchend?: boolean;
|
|
15
|
+
touchcancel?: boolean;
|
|
16
|
+
play?: boolean;
|
|
17
|
+
pause?: boolean;
|
|
19
18
|
};
|
|
19
|
+
mousemove?: boolean | number;
|
|
20
|
+
input?: "all" | "last";
|
|
21
|
+
canvas?: number | "all";
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Every recorder option `<SessionRecorder>` and the provider's
|
|
25
|
+
* `sessionRecordingConfig` accept: the shared `SessionRecordingConfig` plus
|
|
26
|
+
* the rrweb-level options of the core recorder (`maskInputOptions`,
|
|
27
|
+
* `blockSelector`, `ignoreSelector`, `slimDOMOptions`, `sampling`).
|
|
28
|
+
*
|
|
29
|
+
* Derived from the core recorder's options rather than re-declared: a
|
|
30
|
+
* hand-copied field list is how the provider came to drop `idleTimeout`,
|
|
31
|
+
* `useDefaultExcludedPaths` and others without an error.
|
|
32
|
+
*/
|
|
33
|
+
export type SessionRecorderConfig = Omit<SessionRecorderOptions, "basePath" | "ingestKey" | "domain" | "enabled" | "sampling"> & {
|
|
34
|
+
sampling?: SessionRecordingSampling;
|
|
35
|
+
};
|
|
36
|
+
export type SessionRecorderProps = SessionRecorderConfig & {
|
|
37
|
+
basePath: string;
|
|
20
38
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* Increased from 200 to reduce request frequency.
|
|
24
|
-
*/
|
|
25
|
-
batchSize?: number;
|
|
26
|
-
/**
|
|
27
|
-
* Interval in milliseconds to flush events. Default: 60000 (60s)
|
|
28
|
-
* Events are sent when EITHER flushInterval elapses OR batchSize is reached (whichever comes first).
|
|
29
|
-
* Increased from 30s to reduce request frequency.
|
|
39
|
+
* Analytics ingest key (`pa_…`). Required only when Temps does not serve or
|
|
40
|
+
* proxy the app. See `AnalyticsClientOptions.ingestKey`.
|
|
30
41
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
touchcancel?: boolean;
|
|
46
|
-
play?: boolean;
|
|
47
|
-
pause?: boolean;
|
|
48
|
-
};
|
|
49
|
-
mousemove?: boolean | number;
|
|
50
|
-
input?: "all" | "last";
|
|
51
|
-
canvas?: number | "all";
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
export declare function SessionRecorder({ basePath, // Required, no default
|
|
55
|
-
domain, enabled, excludedPaths, sessionSampleRate, maskAllInputs, maskTextSelector, blockClass, ignoreClass, maskTextClass, ignoreSelector, blockSelector, recordCanvas, collectFonts, slimDOMOptions, maskInputOptions, batchSize, flushInterval, sampling, }: SessionRecorderProps): null;
|
|
56
|
-
export {};
|
|
42
|
+
ingestKey?: string;
|
|
43
|
+
domain?: string;
|
|
44
|
+
enabled?: boolean;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* React binding for the framework-agnostic recorder in `@temps-sdk/analytics-core`.
|
|
48
|
+
*
|
|
49
|
+
* This used to be a second, independent rrweb implementation. Keeping two
|
|
50
|
+
* copies meant every ingest fix had to be made twice and they drifted instead
|
|
51
|
+
* — batch handling, the start-up race, and idle gating were all fixed in one
|
|
52
|
+
* and not the other. The component now owns only React lifecycle; all
|
|
53
|
+
* recording behaviour lives in the core class.
|
|
54
|
+
*/
|
|
55
|
+
export declare function SessionRecorder(props: SessionRecorderProps): null;
|
|
57
56
|
//# sourceMappingURL=SessionRecorder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SessionRecorder.d.ts","sourceRoot":"","sources":["../src/SessionRecorder.tsx"],"names":[],"mappings":"AAKA,eAAO,MAAM,yBAAyB,mBAAmB,CAAC;AAE1D,
|
|
1
|
+
{"version":3,"file":"SessionRecorder.d.ts","sourceRoot":"","sources":["../src/SessionRecorder.tsx"],"names":[],"mappings":"AAKA,OAAO,EAEL,KAAK,sBAAsB,EAC5B,MAAM,2BAA2B,CAAC;AAEnC,eAAO,MAAM,yBAAyB,mBAAmB,CAAC;AAE1D,uDAAuD;AACvD,MAAM,WAAW,wBAAwB;IACvC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,gBAAgB,CAAC,EAAE,OAAO,GAAG;QAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;QAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;QACnB,WAAW,CAAC,EAAE,OAAO,CAAC;QACtB,KAAK,CAAC,EAAE,OAAO,CAAC;QAChB,IAAI,CAAC,EAAE,OAAO,CAAC;QACf,UAAU,CAAC,EAAE,OAAO,CAAC;QACrB,QAAQ,CAAC,EAAE,OAAO,CAAC;QACnB,WAAW,CAAC,EAAE,OAAO,CAAC;QACtB,IAAI,CAAC,EAAE,OAAO,CAAC;QACf,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB,CAAC;IACF,SAAS,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC7B,KAAK,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CACzB;AAED;;;;;;;;;GASG;AACH,MAAM,MAAM,qBAAqB,GAAG,IAAI,CACtC,sBAAsB,EACtB,UAAU,GAAG,WAAW,GAAG,QAAQ,GAAG,SAAS,GAAG,UAAU,CAC7D,GAAG;IACF,QAAQ,CAAC,EAAE,wBAAwB,CAAC;CACrC,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG,qBAAqB,GAAG;IACzD,QAAQ,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,GAAG,IAAI,CA6BjE"}
|