@temps-sdk/react-analytics 0.0.1-beta.3 → 0.0.1-beta.31
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/EngagementTracker.d.ts +55 -0
- package/dist/EngagementTracker.d.ts.map +1 -0
- package/dist/EngagementTracker.js +223 -0
- package/dist/Provider.d.ts +1 -1
- package/dist/Provider.d.ts.map +1 -1
- package/dist/Provider.js +73 -8
- package/dist/SessionRecorder.d.ts +43 -0
- package/dist/SessionRecorder.d.ts.map +1 -0
- package/dist/SessionRecorder.js +452 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/types.d.ts +58 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/useEngagementTracking.d.ts +34 -0
- package/dist/useEngagementTracking.d.ts.map +1 -0
- package/dist/useEngagementTracking.js +65 -0
- package/dist/usePageLeave.d.ts +13 -0
- package/dist/usePageLeave.d.ts.map +1 -0
- package/dist/usePageLeave.js +66 -0
- package/dist/useSessionRecording.d.ts +23 -0
- package/dist/useSessionRecording.d.ts.map +1 -0
- package/dist/useSessionRecording.js +99 -0
- package/dist/useSpeedAnalytics.d.ts +8 -0
- package/dist/useSpeedAnalytics.d.ts.map +1 -0
- package/dist/useSpeedAnalytics.js +79 -0
- package/dist/utils.d.ts +2 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +35 -1
- package/package.json +19 -6
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export interface EngagementTrackerOptions {
|
|
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
|
+
}
|
|
55
|
+
//# sourceMappingURL=EngagementTracker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EngagementTracker.d.ts","sourceRoot":"","sources":["../src/EngagementTracker.ts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AAW,CAAC,CAAiC;gBAElD,OAAO,GAAE,wBAA6B;IAsBlD,OAAO,CAAC,mBAAmB;IA6B3B,OAAO,CAAC,sBAAsB,CAW5B;IAEF,OAAO,CAAC,WAAW,CAEjB;IAEF,OAAO,CAAC,UAAU,CAIhB;IAEF,OAAO,CAAC,eAAe;IAQvB,OAAO,CAAC,kBAAkB,CAexB;IAEF,OAAO,CAAC,oBAAoB;IAc5B,OAAO,CAAC,oBAAoB;IAO5B,OAAO,CAAC,cAAc;IAWtB,OAAO,CAAC,aAAa;YAOP,aAAa;IA+B3B,OAAO,CAAC,eAAe,CAyBrB;IAEF,OAAO,CAAC,gBAAgB,CAGtB;IAEF,OAAO,CAAC,iBAAiB;IAczB,OAAO,CAAC,KAAK;IAeN,OAAO,IAAI,IAAI;CAoBvB"}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { sendAnalytics, sendAnalyticsReliable } from "./utils";
|
|
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
|
+
}
|
package/dist/Provider.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { AnalyticsContextValue, TempsAnalyticsProviderProps } from "./types";
|
|
2
|
-
export declare function TempsAnalyticsProvider({ basePath, disabled, ignoreLocalhost, autoTrackPageviews, children, }: TempsAnalyticsProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
export declare function TempsAnalyticsProvider({ basePath, disabled, ignoreLocalhost, autoTrackPageviews, autoTrackPageLeave, pageLeaveEventName, autoTrackSpeedAnalytics, autoTrackEngagement, heartbeatInterval, inactivityTimeout, engagementThreshold, enableSessionRecording, sessionRecordingConfig, domain, children, }: TempsAnalyticsProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
3
3
|
export declare function useTempsAnalytics(): AnalyticsContextValue;
|
|
4
4
|
//# 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":"AAEA,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,QAAQ,GACT,EAAE,2BAA2B,2CAmO7B;AAED,wBAAgB,iBAAiB,IAAI,qBAAqB,CAMzD"}
|
package/dist/Provider.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
import
|
|
4
|
-
import { isLocalhostLike, isTestEnvironment, sendAnalytics } from "./utils";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef } from "react";
|
|
4
|
+
import { isLocalhostLike, isTestEnvironment, sendAnalytics, sendAnalyticsReliable } from "./utils";
|
|
5
|
+
import { useSpeedAnalytics } from "./useSpeedAnalytics";
|
|
6
|
+
import { EngagementTracker } from "./EngagementTracker";
|
|
7
|
+
import { SessionRecorder } from "./SessionRecorder";
|
|
8
|
+
import { SessionRecordingProvider, useSessionRecordingControl } from "./useSessionRecording";
|
|
9
|
+
import { DEFAULT_BASE_PATH } from "./constants";
|
|
5
10
|
const AnalyticsContext = createContext(undefined);
|
|
6
|
-
export function TempsAnalyticsProvider({ basePath =
|
|
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, }) {
|
|
7
12
|
const enabled = useMemo(() => {
|
|
8
13
|
if (disabled)
|
|
9
14
|
return false;
|
|
@@ -25,23 +30,25 @@ export function TempsAnalyticsProvider({ basePath = "/api/_temps", disabled = fa
|
|
|
25
30
|
event_name: eventName,
|
|
26
31
|
request_query: window.location.search,
|
|
27
32
|
request_path: window.location.pathname,
|
|
33
|
+
domain: domain || window.location.hostname,
|
|
28
34
|
event_data: data,
|
|
29
35
|
}, "POST", basePath);
|
|
30
|
-
}, [enabled, basePath]);
|
|
36
|
+
}, [enabled, basePath, domain]);
|
|
31
37
|
const trackPageview = useCallback(() => {
|
|
32
38
|
if (!enabled)
|
|
33
39
|
return;
|
|
34
40
|
void sendAnalytics("event", {
|
|
35
|
-
event_name: "
|
|
41
|
+
event_name: "page_view",
|
|
36
42
|
request_query: window.location.search,
|
|
37
43
|
request_path: window.location.pathname,
|
|
44
|
+
domain: domain || window.location.hostname,
|
|
38
45
|
event_data: {
|
|
39
46
|
referrer: document.referrer,
|
|
40
47
|
userAgent: navigator.userAgent,
|
|
41
48
|
timestamp: new Date().toISOString(),
|
|
42
49
|
},
|
|
43
50
|
}, "POST", basePath);
|
|
44
|
-
}, [enabled, basePath]);
|
|
51
|
+
}, [enabled, basePath, domain]);
|
|
45
52
|
const identify = useCallback(async () => {
|
|
46
53
|
// no-op placeholder for SDK parity; implement when identity endpoint is available
|
|
47
54
|
}, []);
|
|
@@ -120,8 +127,66 @@ export function TempsAnalyticsProvider({ basePath = "/api/_temps", disabled = fa
|
|
|
120
127
|
document.addEventListener("click", onClick);
|
|
121
128
|
return () => document.removeEventListener("click", onClick);
|
|
122
129
|
}, [enabled, trackEvent]);
|
|
130
|
+
// Engagement tracking with heartbeats and page leave
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
if (!enabled)
|
|
133
|
+
return;
|
|
134
|
+
// If engagement tracking is enabled, it handles both heartbeats and page leave
|
|
135
|
+
if (autoTrackEngagement) {
|
|
136
|
+
const tracker = new EngagementTracker({
|
|
137
|
+
basePath,
|
|
138
|
+
domain,
|
|
139
|
+
heartbeatInterval,
|
|
140
|
+
inactivityTimeout,
|
|
141
|
+
engagementThreshold,
|
|
142
|
+
});
|
|
143
|
+
return () => {
|
|
144
|
+
tracker.destroy();
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
// Legacy page leave tracking without engagement metrics
|
|
148
|
+
else if (autoTrackPageLeave) {
|
|
149
|
+
let hasTracked = false;
|
|
150
|
+
let startTime = Date.now();
|
|
151
|
+
const trackPageLeave = () => {
|
|
152
|
+
if (hasTracked)
|
|
153
|
+
return;
|
|
154
|
+
hasTracked = true;
|
|
155
|
+
const timeOnPage = Date.now() - startTime;
|
|
156
|
+
sendAnalyticsReliable("event", {
|
|
157
|
+
event_name: pageLeaveEventName,
|
|
158
|
+
request_query: window.location.search,
|
|
159
|
+
request_path: window.location.pathname,
|
|
160
|
+
domain: domain || window.location.hostname,
|
|
161
|
+
event_data: {
|
|
162
|
+
time_on_page_ms: timeOnPage,
|
|
163
|
+
timestamp: new Date().toISOString(),
|
|
164
|
+
url: window.location.href,
|
|
165
|
+
referrer: document.referrer,
|
|
166
|
+
},
|
|
167
|
+
}, basePath);
|
|
168
|
+
};
|
|
169
|
+
// Use pagehide as primary (most reliable), with beforeunload as fallback
|
|
170
|
+
const handlePageLeave = () => trackPageLeave();
|
|
171
|
+
// pagehide is the most reliable for modern browsers
|
|
172
|
+
window.addEventListener("pagehide", handlePageLeave);
|
|
173
|
+
// beforeunload as fallback for older browsers
|
|
174
|
+
window.addEventListener("beforeunload", handlePageLeave);
|
|
175
|
+
return () => {
|
|
176
|
+
window.removeEventListener("pagehide", handlePageLeave);
|
|
177
|
+
window.removeEventListener("beforeunload", handlePageLeave);
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
}, [autoTrackPageLeave, autoTrackEngagement, enabled, pageLeaveEventName, domain, basePath, heartbeatInterval, inactivityTimeout, engagementThreshold]);
|
|
181
|
+
// Speed analytics tracking
|
|
182
|
+
useSpeedAnalytics({
|
|
183
|
+
basePath,
|
|
184
|
+
disabled: !enabled || !autoTrackSpeedAnalytics,
|
|
185
|
+
});
|
|
186
|
+
// Session recording control
|
|
187
|
+
const { isEnabled: isRecordingEnabled } = useSessionRecordingControl(enableSessionRecording);
|
|
123
188
|
const value = useMemo(() => ({ trackEvent, identify, trackPageview, enabled }), [trackEvent, identify, trackPageview, enabled]);
|
|
124
|
-
return _jsx(AnalyticsContext.Provider, { value: value, children: children });
|
|
189
|
+
return (_jsx(AnalyticsContext.Provider, { value: value, children: _jsxs(SessionRecordingProvider, { defaultEnabled: enableSessionRecording, children: [children, enabled && (_jsx(SessionRecorder, { basePath: basePath, domain: domain, enabled: isRecordingEnabled, excludedPaths: sessionRecordingConfig.excludedPaths, sessionSampleRate: sessionRecordingConfig.sessionSampleRate, maskAllInputs: sessionRecordingConfig.maskAllInputs, maskTextSelector: sessionRecordingConfig.maskTextSelector, blockClass: sessionRecordingConfig.blockClass, ignoreClass: sessionRecordingConfig.ignoreClass, maskTextClass: sessionRecordingConfig.maskTextClass, recordCanvas: sessionRecordingConfig.recordCanvas, collectFonts: sessionRecordingConfig.collectFonts, batchSize: sessionRecordingConfig.batchSize, flushInterval: sessionRecordingConfig.flushInterval }))] }) }));
|
|
125
190
|
}
|
|
126
191
|
export function useTempsAnalytics() {
|
|
127
192
|
const ctx = useContext(AnalyticsContext);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export declare const SESSION_RECORDER_ENDPOINT = "session-replay";
|
|
2
|
+
interface SessionRecorderProps {
|
|
3
|
+
basePath: string;
|
|
4
|
+
domain?: string;
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
excludedPaths?: string[];
|
|
7
|
+
sessionSampleRate?: number;
|
|
8
|
+
maskAllInputs?: boolean;
|
|
9
|
+
maskTextSelector?: string;
|
|
10
|
+
blockClass?: string;
|
|
11
|
+
ignoreClass?: string;
|
|
12
|
+
maskTextClass?: string;
|
|
13
|
+
recordCanvas?: boolean;
|
|
14
|
+
collectFonts?: boolean;
|
|
15
|
+
slimDOMOptions?: Record<string, boolean>;
|
|
16
|
+
batchSize?: number;
|
|
17
|
+
flushInterval?: number;
|
|
18
|
+
ignoreSelector?: string;
|
|
19
|
+
blockSelector?: string;
|
|
20
|
+
sampling?: {
|
|
21
|
+
scroll?: number;
|
|
22
|
+
media?: number;
|
|
23
|
+
mouseInteraction?: boolean | {
|
|
24
|
+
click?: boolean;
|
|
25
|
+
dblclick?: boolean;
|
|
26
|
+
contextmenu?: boolean;
|
|
27
|
+
focus?: boolean;
|
|
28
|
+
blur?: boolean;
|
|
29
|
+
touchstart?: boolean;
|
|
30
|
+
touchend?: boolean;
|
|
31
|
+
touchcancel?: boolean;
|
|
32
|
+
play?: boolean;
|
|
33
|
+
pause?: boolean;
|
|
34
|
+
};
|
|
35
|
+
mousemove?: boolean | number;
|
|
36
|
+
input?: "all" | "last";
|
|
37
|
+
canvas?: number | "all";
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export declare function SessionRecorder({ basePath, // Required, no default
|
|
41
|
+
domain, enabled, excludedPaths, sessionSampleRate, maskAllInputs, maskTextSelector, blockClass, ignoreClass, maskTextClass, ignoreSelector, blockSelector, recordCanvas, collectFonts, slimDOMOptions, batchSize, flushInterval, sampling, }: SessionRecorderProps): null;
|
|
42
|
+
export {};
|
|
43
|
+
//# sourceMappingURL=SessionRecorder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SessionRecorder.d.ts","sourceRoot":"","sources":["../src/SessionRecorder.tsx"],"names":[],"mappings":"AAMA,eAAO,MAAM,yBAAyB,mBAAmB,CAAC;AAE1D,UAAU,oBAAoB;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,C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