@temps-sdk/analytics-core 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Analytics.d.ts +7 -0
- package/dist/Analytics.d.ts.map +1 -0
- package/dist/Analytics.js +218 -0
- package/dist/EngagementTracker.d.ts +56 -0
- package/dist/EngagementTracker.d.ts.map +1 -0
- package/dist/EngagementTracker.js +211 -0
- package/dist/SessionRecorder.d.ts +69 -0
- package/dist/SessionRecorder.d.ts.map +1 -0
- package/dist/SessionRecorder.js +407 -0
- package/dist/SpeedTracker.d.ts +18 -0
- package/dist/SpeedTracker.d.ts.map +1 -0
- package/dist/SpeedTracker.js +61 -0
- package/dist/constants.d.ts +13 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +30 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/types.d.ts +108 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +96 -0
- package/package.json +44 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AnalyticsApi, AnalyticsOptions } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Framework-agnostic factory. Call once at app startup in the browser to get an
|
|
4
|
+
* analytics instance. All framework adapters wrap this factory.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createAnalytics(options?: AnalyticsOptions): AnalyticsApi;
|
|
7
|
+
//# sourceMappingURL=Analytics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Analytics.d.ts","sourceRoot":"","sources":["../src/Analytics.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAa,MAAM,SAAS,CAAC;AAezE;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,GAAE,gBAAqB,GAAG,YAAY,CAwI5E"}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { DEFAULT_BASE_PATH } from "./constants";
|
|
2
|
+
import { isLocalhostLike, isTestEnvironment, sendAnalytics, sendAnalyticsReliable, } from "./utils";
|
|
3
|
+
import { EngagementTracker } from "./EngagementTracker";
|
|
4
|
+
import { SpeedTracker } from "./SpeedTracker";
|
|
5
|
+
import { SessionRecorder } from "./SessionRecorder";
|
|
6
|
+
/**
|
|
7
|
+
* Framework-agnostic factory. Call once at app startup in the browser to get an
|
|
8
|
+
* analytics instance. All framework adapters wrap this factory.
|
|
9
|
+
*/
|
|
10
|
+
export function createAnalytics(options = {}) {
|
|
11
|
+
const { basePath = DEFAULT_BASE_PATH, disabled = false, ignoreLocalhost = true, domain, autoTrackPageviews = true, autoTrackPageLeave = true, pageLeaveEventName = "page_leave", autoTrackSpeedAnalytics = true, autoTrackEngagement = true, heartbeatInterval = 30000, inactivityTimeout = 30000, engagementThreshold = 10000, enableSessionRecording = false, sessionRecordingConfig = {}, } = options;
|
|
12
|
+
const cleanups = [];
|
|
13
|
+
const enabled = computeEnabled(disabled, ignoreLocalhost);
|
|
14
|
+
const resolveDomain = () => domain || (typeof window !== "undefined" ? window.location.hostname : "");
|
|
15
|
+
const trackEvent = async (eventName, data = {}) => {
|
|
16
|
+
if (!enabled)
|
|
17
|
+
return;
|
|
18
|
+
await sendAnalytics("event", {
|
|
19
|
+
event_name: eventName,
|
|
20
|
+
request_query: window.location.search,
|
|
21
|
+
request_path: window.location.pathname,
|
|
22
|
+
domain: resolveDomain(),
|
|
23
|
+
event_data: data,
|
|
24
|
+
}, "POST", basePath);
|
|
25
|
+
};
|
|
26
|
+
const trackPageview = () => {
|
|
27
|
+
if (!enabled)
|
|
28
|
+
return;
|
|
29
|
+
void sendAnalytics("event", {
|
|
30
|
+
event_name: "page_view",
|
|
31
|
+
request_query: window.location.search,
|
|
32
|
+
request_path: window.location.pathname,
|
|
33
|
+
domain: resolveDomain(),
|
|
34
|
+
event_data: {
|
|
35
|
+
referrer: document.referrer,
|
|
36
|
+
userAgent: navigator.userAgent,
|
|
37
|
+
timestamp: new Date().toISOString(),
|
|
38
|
+
},
|
|
39
|
+
}, "POST", basePath);
|
|
40
|
+
};
|
|
41
|
+
const identify = async () => {
|
|
42
|
+
// Placeholder for SDK parity; implement when identity endpoint is available.
|
|
43
|
+
};
|
|
44
|
+
if (enabled) {
|
|
45
|
+
if (autoTrackPageviews) {
|
|
46
|
+
cleanups.push(setupPageviewTracking(trackPageview));
|
|
47
|
+
}
|
|
48
|
+
if (autoTrackEngagement) {
|
|
49
|
+
const tracker = new EngagementTracker({
|
|
50
|
+
basePath,
|
|
51
|
+
domain: resolveDomain(),
|
|
52
|
+
heartbeatInterval,
|
|
53
|
+
inactivityTimeout,
|
|
54
|
+
engagementThreshold,
|
|
55
|
+
});
|
|
56
|
+
cleanups.push(() => tracker.destroy());
|
|
57
|
+
}
|
|
58
|
+
else if (autoTrackPageLeave) {
|
|
59
|
+
cleanups.push(setupLegacyPageLeave(pageLeaveEventName, resolveDomain, basePath));
|
|
60
|
+
}
|
|
61
|
+
if (autoTrackSpeedAnalytics) {
|
|
62
|
+
const speed = new SpeedTracker({ basePath });
|
|
63
|
+
cleanups.push(() => speed.destroy());
|
|
64
|
+
}
|
|
65
|
+
cleanups.push(setupClickDelegation(trackEvent));
|
|
66
|
+
}
|
|
67
|
+
const recorder = enabled && enableSessionRecording
|
|
68
|
+
? new SessionRecorder({ basePath, domain: resolveDomain(), enabled: true, ...sessionRecordingConfig })
|
|
69
|
+
: null;
|
|
70
|
+
if (recorder)
|
|
71
|
+
cleanups.push(() => recorder.destroy());
|
|
72
|
+
// Runtime-controlled recorder handle for enable/disable after init
|
|
73
|
+
let runtimeRecorder = recorder;
|
|
74
|
+
return {
|
|
75
|
+
get enabled() {
|
|
76
|
+
return enabled;
|
|
77
|
+
},
|
|
78
|
+
trackEvent,
|
|
79
|
+
trackPageview,
|
|
80
|
+
identify,
|
|
81
|
+
enableSessionRecording() {
|
|
82
|
+
if (!enabled)
|
|
83
|
+
return;
|
|
84
|
+
if (runtimeRecorder) {
|
|
85
|
+
runtimeRecorder.start();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
runtimeRecorder = new SessionRecorder({
|
|
89
|
+
basePath,
|
|
90
|
+
domain: resolveDomain(),
|
|
91
|
+
enabled: true,
|
|
92
|
+
...sessionRecordingConfig,
|
|
93
|
+
});
|
|
94
|
+
cleanups.push(() => runtimeRecorder?.destroy());
|
|
95
|
+
},
|
|
96
|
+
disableSessionRecording() {
|
|
97
|
+
runtimeRecorder?.stop();
|
|
98
|
+
},
|
|
99
|
+
destroy() {
|
|
100
|
+
while (cleanups.length > 0) {
|
|
101
|
+
const fn = cleanups.pop();
|
|
102
|
+
try {
|
|
103
|
+
fn?.();
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
// eslint-disable-next-line no-console
|
|
107
|
+
console.error("Analytics cleanup error:", error);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function computeEnabled(disabled, ignoreLocalhost) {
|
|
114
|
+
if (disabled)
|
|
115
|
+
return false;
|
|
116
|
+
if (typeof window === "undefined")
|
|
117
|
+
return false;
|
|
118
|
+
if (ignoreLocalhost && (isLocalhostLike() || isTestEnvironment()))
|
|
119
|
+
return false;
|
|
120
|
+
try {
|
|
121
|
+
if (window.localStorage?.getItem("temps_ignore") === "true")
|
|
122
|
+
return false;
|
|
123
|
+
// Legacy lookup via property (matches old React implementation)
|
|
124
|
+
const legacy = window.localStorage?.temps_ignore;
|
|
125
|
+
if (legacy === "true")
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
// ignore
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
function setupPageviewTracking(trackPageview) {
|
|
134
|
+
let currentPath = window.location.pathname;
|
|
135
|
+
let initialLoad = true;
|
|
136
|
+
const originalPushState = window.history.pushState.bind(window.history);
|
|
137
|
+
const maybeTrack = () => {
|
|
138
|
+
const nextPath = window.location.pathname;
|
|
139
|
+
if (currentPath !== nextPath) {
|
|
140
|
+
currentPath = nextPath;
|
|
141
|
+
trackPageview();
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
window.history.pushState = ((data, unused, url) => {
|
|
145
|
+
originalPushState(data, unused, url);
|
|
146
|
+
maybeTrack();
|
|
147
|
+
});
|
|
148
|
+
const onPop = () => maybeTrack();
|
|
149
|
+
window.addEventListener("popstate", onPop);
|
|
150
|
+
const handleVisibility = () => {
|
|
151
|
+
if (document.visibilityState === "visible" && initialLoad) {
|
|
152
|
+
initialLoad = false;
|
|
153
|
+
trackPageview();
|
|
154
|
+
document.removeEventListener("visibilitychange", handleVisibility);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
if (document.visibilityState === "prerender") {
|
|
158
|
+
document.addEventListener("visibilitychange", handleVisibility);
|
|
159
|
+
}
|
|
160
|
+
else if (initialLoad) {
|
|
161
|
+
initialLoad = false;
|
|
162
|
+
trackPageview();
|
|
163
|
+
}
|
|
164
|
+
return () => {
|
|
165
|
+
window.removeEventListener("popstate", onPop);
|
|
166
|
+
document.removeEventListener("visibilitychange", handleVisibility);
|
|
167
|
+
window.history.pushState = originalPushState;
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function setupClickDelegation(trackEvent) {
|
|
171
|
+
const onClick = (event) => {
|
|
172
|
+
const target = event.target;
|
|
173
|
+
const eventElement = target?.closest?.("[temps-event-name]");
|
|
174
|
+
if (!(eventElement instanceof HTMLElement))
|
|
175
|
+
return;
|
|
176
|
+
const eventName = eventElement.getAttribute("temps-event-name");
|
|
177
|
+
if (!eventName)
|
|
178
|
+
return;
|
|
179
|
+
const eventData = {};
|
|
180
|
+
for (const attr of eventElement.getAttributeNames()) {
|
|
181
|
+
if (attr.startsWith("temps-data-")) {
|
|
182
|
+
const dataKey = attr.replace("temps-data-", "");
|
|
183
|
+
eventData[dataKey] = eventElement.getAttribute(attr);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
void trackEvent(eventName, eventData);
|
|
187
|
+
};
|
|
188
|
+
document.addEventListener("click", onClick);
|
|
189
|
+
return () => document.removeEventListener("click", onClick);
|
|
190
|
+
}
|
|
191
|
+
function setupLegacyPageLeave(pageLeaveEventName, resolveDomain, basePath) {
|
|
192
|
+
let hasTracked = false;
|
|
193
|
+
const startTime = Date.now();
|
|
194
|
+
const trackPageLeave = () => {
|
|
195
|
+
if (hasTracked)
|
|
196
|
+
return;
|
|
197
|
+
hasTracked = true;
|
|
198
|
+
const timeOnPage = Date.now() - startTime;
|
|
199
|
+
sendAnalyticsReliable("event", {
|
|
200
|
+
event_name: pageLeaveEventName,
|
|
201
|
+
request_query: window.location.search,
|
|
202
|
+
request_path: window.location.pathname,
|
|
203
|
+
domain: resolveDomain(),
|
|
204
|
+
event_data: {
|
|
205
|
+
time_on_page_ms: timeOnPage,
|
|
206
|
+
timestamp: new Date().toISOString(),
|
|
207
|
+
url: window.location.href,
|
|
208
|
+
referrer: document.referrer,
|
|
209
|
+
},
|
|
210
|
+
}, basePath);
|
|
211
|
+
};
|
|
212
|
+
window.addEventListener("pagehide", trackPageLeave);
|
|
213
|
+
window.addEventListener("beforeunload", trackPageLeave);
|
|
214
|
+
return () => {
|
|
215
|
+
window.removeEventListener("pagehide", trackPageLeave);
|
|
216
|
+
window.removeEventListener("beforeunload", trackPageLeave);
|
|
217
|
+
};
|
|
218
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
private readonly originalPushState;
|
|
38
|
+
constructor(options?: EngagementTrackerOptions);
|
|
39
|
+
private setupEventListeners;
|
|
40
|
+
private handleVisibilityChange;
|
|
41
|
+
private handleFocus;
|
|
42
|
+
private handleBlur;
|
|
43
|
+
private pauseEngagement;
|
|
44
|
+
private handleUserActivity;
|
|
45
|
+
private resetInactivityTimer;
|
|
46
|
+
private updateEngagementTime;
|
|
47
|
+
private startHeartbeat;
|
|
48
|
+
private stopHeartbeat;
|
|
49
|
+
private sendHeartbeat;
|
|
50
|
+
private handlePageLeave;
|
|
51
|
+
private handleNavigation;
|
|
52
|
+
private getEngagementData;
|
|
53
|
+
private reset;
|
|
54
|
+
destroy(): void;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=EngagementTracker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EngagementTracker.d.ts","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C9D,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAiC;IAC9D,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAuB;gBAE7C,OAAO,GAAE,wBAA6B;IAsBlD,OAAO,CAAC,mBAAmB;IAuB3B,OAAO,CAAC,sBAAsB,CAS5B;IAEF,OAAO,CAAC,WAAW,CAEjB;IAEF,OAAO,CAAC,UAAU,CAGhB;IAEF,OAAO,CAAC,eAAe;IAQvB,OAAO,CAAC,kBAAkB,CAaxB;IAEF,OAAO,CAAC,oBAAoB;IAc5B,OAAO,CAAC,oBAAoB;IAO5B,OAAO,CAAC,cAAc;IAUtB,OAAO,CAAC,aAAa;YAOP,aAAa;IAmC3B,OAAO,CAAC,eAAe,CA6BrB;IAEF,OAAO,CAAC,gBAAgB,CAGtB;IAEF,OAAO,CAAC,iBAAiB;IAczB,OAAO,CAAC,KAAK;IAcN,OAAO,IAAI,IAAI;CAsBvB"}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { sendAnalytics, sendAnalyticsReliable } from "./utils";
|
|
2
|
+
import { DEFAULT_BASE_PATH } from "./constants";
|
|
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
|
+
this.startHeartbeat();
|
|
17
|
+
}
|
|
18
|
+
else if (wasVisible && !this.isVisible) {
|
|
19
|
+
this.pauseEngagement();
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
this.handleFocus = () => {
|
|
23
|
+
this.isFocused = true;
|
|
24
|
+
};
|
|
25
|
+
this.handleBlur = () => {
|
|
26
|
+
this.isFocused = false;
|
|
27
|
+
this.pauseEngagement();
|
|
28
|
+
};
|
|
29
|
+
this.handleUserActivity = () => {
|
|
30
|
+
if (!this.isVisible || !this.isFocused)
|
|
31
|
+
return;
|
|
32
|
+
const now = Date.now();
|
|
33
|
+
if (!this.isActive) {
|
|
34
|
+
this.isActive = true;
|
|
35
|
+
this.lastEngagementStart = now;
|
|
36
|
+
this.startHeartbeat();
|
|
37
|
+
}
|
|
38
|
+
this.lastActivityTime = now;
|
|
39
|
+
this.resetInactivityTimer();
|
|
40
|
+
};
|
|
41
|
+
this.handlePageLeave = () => {
|
|
42
|
+
if (this.hasTrackedLeave)
|
|
43
|
+
return;
|
|
44
|
+
this.hasTrackedLeave = true;
|
|
45
|
+
this.updateEngagementTime();
|
|
46
|
+
const data = this.getEngagementData();
|
|
47
|
+
if (this.onPageLeave) {
|
|
48
|
+
this.onPageLeave(data);
|
|
49
|
+
}
|
|
50
|
+
sendAnalyticsReliable("event", {
|
|
51
|
+
event_name: "page_leave",
|
|
52
|
+
request_path: window.location.pathname,
|
|
53
|
+
request_query: window.location.search,
|
|
54
|
+
domain: this.domain,
|
|
55
|
+
event_data: {
|
|
56
|
+
engagement_time_seconds: data.engagement_time_seconds,
|
|
57
|
+
total_time_seconds: data.total_time_seconds,
|
|
58
|
+
heartbeat_count: this.heartbeatCount,
|
|
59
|
+
was_engaged: data.is_engaged,
|
|
60
|
+
url: window.location.href,
|
|
61
|
+
referrer: document.referrer,
|
|
62
|
+
},
|
|
63
|
+
}, this.basePath);
|
|
64
|
+
};
|
|
65
|
+
this.handleNavigation = () => {
|
|
66
|
+
this.handlePageLeave();
|
|
67
|
+
this.reset();
|
|
68
|
+
};
|
|
69
|
+
this.basePath = options.basePath || DEFAULT_BASE_PATH;
|
|
70
|
+
this.domain = options.domain || window.location.hostname;
|
|
71
|
+
this.heartbeatInterval = options.heartbeatInterval || 30000;
|
|
72
|
+
this.inactivityTimeout = options.inactivityTimeout || 30000;
|
|
73
|
+
this.engagementThreshold = options.engagementThreshold || 10000;
|
|
74
|
+
this.onHeartbeat = options.onHeartbeat;
|
|
75
|
+
this.onPageLeave = options.onPageLeave;
|
|
76
|
+
this.startTime = Date.now();
|
|
77
|
+
this.lastActivityTime = Date.now();
|
|
78
|
+
this.isVisible = document.visibilityState === "visible";
|
|
79
|
+
this.isFocused = document.hasFocus();
|
|
80
|
+
this.lastEngagementStart = null;
|
|
81
|
+
this.isActive = false;
|
|
82
|
+
this.originalPushState = window.history.pushState.bind(window.history);
|
|
83
|
+
this.setupEventListeners();
|
|
84
|
+
this.startHeartbeat();
|
|
85
|
+
}
|
|
86
|
+
setupEventListeners() {
|
|
87
|
+
document.addEventListener("visibilitychange", this.handleVisibilityChange);
|
|
88
|
+
window.addEventListener("focus", this.handleFocus);
|
|
89
|
+
window.addEventListener("blur", this.handleBlur);
|
|
90
|
+
const activityEvents = ["mousedown", "keypress", "scroll", "touchstart", "click"];
|
|
91
|
+
activityEvents.forEach((event) => {
|
|
92
|
+
document.addEventListener(event, this.handleUserActivity, { passive: true });
|
|
93
|
+
});
|
|
94
|
+
window.addEventListener("pagehide", this.handlePageLeave);
|
|
95
|
+
window.addEventListener("beforeunload", this.handlePageLeave);
|
|
96
|
+
const original = this.originalPushState;
|
|
97
|
+
window.history.pushState = ((...args) => {
|
|
98
|
+
this.handlePageLeave();
|
|
99
|
+
original(...args);
|
|
100
|
+
this.reset();
|
|
101
|
+
});
|
|
102
|
+
window.addEventListener("popstate", this.handleNavigation);
|
|
103
|
+
}
|
|
104
|
+
pauseEngagement() {
|
|
105
|
+
this.updateEngagementTime();
|
|
106
|
+
this.lastEngagementStart = null;
|
|
107
|
+
if (!this.isVisible) {
|
|
108
|
+
this.stopHeartbeat();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
resetInactivityTimer() {
|
|
112
|
+
if (this.inactivityTimer) {
|
|
113
|
+
clearTimeout(this.inactivityTimer);
|
|
114
|
+
}
|
|
115
|
+
this.inactivityTimer = setTimeout(() => {
|
|
116
|
+
if (this.isVisible) {
|
|
117
|
+
this.updateEngagementTime();
|
|
118
|
+
this.isActive = false;
|
|
119
|
+
this.lastEngagementStart = null;
|
|
120
|
+
}
|
|
121
|
+
}, this.inactivityTimeout);
|
|
122
|
+
}
|
|
123
|
+
updateEngagementTime() {
|
|
124
|
+
if (this.lastEngagementStart !== null) {
|
|
125
|
+
const currentEngagement = Date.now() - this.lastEngagementStart;
|
|
126
|
+
this.engagementTime += currentEngagement;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
startHeartbeat() {
|
|
130
|
+
if (this.heartbeatTimer)
|
|
131
|
+
return;
|
|
132
|
+
this.heartbeatTimer = setInterval(() => {
|
|
133
|
+
if (this.isVisible && this.isActive) {
|
|
134
|
+
this.sendHeartbeat();
|
|
135
|
+
}
|
|
136
|
+
}, this.heartbeatInterval);
|
|
137
|
+
}
|
|
138
|
+
stopHeartbeat() {
|
|
139
|
+
if (this.heartbeatTimer) {
|
|
140
|
+
clearInterval(this.heartbeatTimer);
|
|
141
|
+
this.heartbeatTimer = null;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async sendHeartbeat() {
|
|
145
|
+
this.updateEngagementTime();
|
|
146
|
+
this.heartbeatCount++;
|
|
147
|
+
const data = this.getEngagementData();
|
|
148
|
+
if (this.onHeartbeat) {
|
|
149
|
+
this.onHeartbeat(data);
|
|
150
|
+
}
|
|
151
|
+
await sendAnalytics("event", {
|
|
152
|
+
event_name: "heartbeat",
|
|
153
|
+
request_path: window.location.pathname,
|
|
154
|
+
request_query: window.location.search,
|
|
155
|
+
domain: this.domain,
|
|
156
|
+
event_data: {
|
|
157
|
+
engagement_time: Math.round(this.engagementTime),
|
|
158
|
+
total_time: Math.round(Date.now() - this.startTime),
|
|
159
|
+
is_engaged: data.is_engaged,
|
|
160
|
+
is_visible: this.isVisible,
|
|
161
|
+
time_since_last_activity: data.time_since_last_activity,
|
|
162
|
+
heartbeat_count: this.heartbeatCount,
|
|
163
|
+
},
|
|
164
|
+
}, "POST", this.basePath);
|
|
165
|
+
if (this.isActive && this.isVisible) {
|
|
166
|
+
this.lastEngagementStart = Date.now();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
getEngagementData() {
|
|
170
|
+
const totalTime = Date.now() - this.startTime;
|
|
171
|
+
const timeSinceLastActivity = Date.now() - this.lastActivityTime;
|
|
172
|
+
return {
|
|
173
|
+
engagement_time_seconds: Math.round(this.engagementTime / 1000),
|
|
174
|
+
total_time_seconds: Math.round(totalTime / 1000),
|
|
175
|
+
heartbeat_count: this.heartbeatCount,
|
|
176
|
+
is_engaged: this.engagementTime >= this.engagementThreshold,
|
|
177
|
+
is_visible: this.isVisible,
|
|
178
|
+
time_since_last_activity: Math.round(timeSinceLastActivity / 1000),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
reset() {
|
|
182
|
+
this.startTime = Date.now();
|
|
183
|
+
this.engagementTime = 0;
|
|
184
|
+
this.lastActivityTime = Date.now();
|
|
185
|
+
this.heartbeatCount = 0;
|
|
186
|
+
this.hasTrackedLeave = false;
|
|
187
|
+
this.isActive = false;
|
|
188
|
+
this.lastEngagementStart = null;
|
|
189
|
+
if (this.isVisible) {
|
|
190
|
+
this.startHeartbeat();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
destroy() {
|
|
194
|
+
this.handlePageLeave();
|
|
195
|
+
this.stopHeartbeat();
|
|
196
|
+
if (this.inactivityTimer) {
|
|
197
|
+
clearTimeout(this.inactivityTimer);
|
|
198
|
+
}
|
|
199
|
+
document.removeEventListener("visibilitychange", this.handleVisibilityChange);
|
|
200
|
+
window.removeEventListener("focus", this.handleFocus);
|
|
201
|
+
window.removeEventListener("blur", this.handleBlur);
|
|
202
|
+
window.removeEventListener("pagehide", this.handlePageLeave);
|
|
203
|
+
window.removeEventListener("beforeunload", this.handlePageLeave);
|
|
204
|
+
window.removeEventListener("popstate", this.handleNavigation);
|
|
205
|
+
window.history.pushState = this.originalPushState;
|
|
206
|
+
const activityEvents = ["mousedown", "keypress", "scroll", "touchstart", "click"];
|
|
207
|
+
activityEvents.forEach((event) => {
|
|
208
|
+
document.removeEventListener(event, this.handleUserActivity);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { SessionRecordingConfig } from "./types";
|
|
2
|
+
export interface SessionRecorderOptions extends SessionRecordingConfig {
|
|
3
|
+
basePath?: string;
|
|
4
|
+
domain?: string;
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
ignoreSelector?: string;
|
|
7
|
+
blockSelector?: string;
|
|
8
|
+
sampling?: Record<string, unknown>;
|
|
9
|
+
slimDOMOptions?: Record<string, boolean>;
|
|
10
|
+
maskInputOptions?: {
|
|
11
|
+
password?: boolean;
|
|
12
|
+
email?: boolean;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Framework-agnostic rrweb wrapper. Call `start()` to begin recording and
|
|
17
|
+
* `stop()` to end. `destroy()` removes all listeners and flushes pending events.
|
|
18
|
+
*/
|
|
19
|
+
export declare class SessionRecorder {
|
|
20
|
+
private readonly basePath;
|
|
21
|
+
private readonly excludedPaths;
|
|
22
|
+
private readonly sessionSampleRate;
|
|
23
|
+
private readonly maskAllInputs;
|
|
24
|
+
private readonly maskTextSelector;
|
|
25
|
+
private readonly blockClass;
|
|
26
|
+
private readonly ignoreClass;
|
|
27
|
+
private readonly maskTextClass;
|
|
28
|
+
private readonly ignoreSelector;
|
|
29
|
+
private readonly blockSelector;
|
|
30
|
+
private readonly recordCanvas;
|
|
31
|
+
private readonly collectFonts;
|
|
32
|
+
private readonly batchSize;
|
|
33
|
+
private readonly flushInterval;
|
|
34
|
+
private readonly slimDOMOptions;
|
|
35
|
+
private readonly maskInputOptions;
|
|
36
|
+
private readonly samplingConfig;
|
|
37
|
+
private stopFn;
|
|
38
|
+
private takeSnapshot;
|
|
39
|
+
private events;
|
|
40
|
+
private sessionId;
|
|
41
|
+
private sessionInitialized;
|
|
42
|
+
private flushTimer;
|
|
43
|
+
private initRetryCount;
|
|
44
|
+
private initFailed;
|
|
45
|
+
private readonly maxInitRetries;
|
|
46
|
+
private sendRetryCount;
|
|
47
|
+
private readonly maxSendRetries;
|
|
48
|
+
private lastSendAttempt;
|
|
49
|
+
private isSending;
|
|
50
|
+
private originalPushState;
|
|
51
|
+
private originalReplaceState;
|
|
52
|
+
private enabled;
|
|
53
|
+
constructor(options?: SessionRecorderOptions);
|
|
54
|
+
private shouldRecord;
|
|
55
|
+
private initializeSession;
|
|
56
|
+
private sendEvents;
|
|
57
|
+
private scheduleFlush;
|
|
58
|
+
private startRecording;
|
|
59
|
+
private stopRecording;
|
|
60
|
+
private handleUnload;
|
|
61
|
+
private wrapHistory;
|
|
62
|
+
private handlePopState;
|
|
63
|
+
private checkPath;
|
|
64
|
+
start(): void;
|
|
65
|
+
stop(): void;
|
|
66
|
+
destroy(): void;
|
|
67
|
+
getSessionId(): string | null;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=SessionRecorder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SessionRecorder.d.ts","sourceRoot":"","sources":["../src/SessionRecorder.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAEtD,MAAM,WAAW,sBAAuB,SAAQ,sBAAsB;IACpE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC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