@temps-sdk/react-analytics 0.0.1-beta.37 → 0.0.1-beta.39
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/SessionRecorder.d.ts.map +1 -1
- package/dist/SessionRecorder.js +80 -31
- package/package.json +10 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SessionRecorder.d.ts","sourceRoot":"","sources":["../src/SessionRecorder.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"SessionRecorder.d.ts","sourceRoot":"","sources":["../src/SessionRecorder.tsx"],"names":[],"mappings":"AAKA,eAAO,MAAM,yBAAyB,mBAAmB,CAAC;AAE1D,UAAU,oBAAoB;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACzC,gBAAgB,CAAC,EAAE;QACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;QACnB,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB,CAAC;IACF;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE;QACT,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,gBAAgB,CAAC,EAAE,OAAO,GAAG;YAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;YAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;YACnB,WAAW,CAAC,EAAE,OAAO,CAAC;YACtB,KAAK,CAAC,EAAE,OAAO,CAAC;YAChB,IAAI,CAAC,EAAE,OAAO,CAAC;YACf,UAAU,CAAC,EAAE,OAAO,CAAC;YACrB,QAAQ,CAAC,EAAE,OAAO,CAAC;YACnB,WAAW,CAAC,EAAE,OAAO,CAAC;YACtB,IAAI,CAAC,EAAE,OAAO,CAAC;YACf,KAAK,CAAC,EAAE,OAAO,CAAC;SACjB,CAAC;QACF,SAAS,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;QAC7B,KAAK,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;QACvB,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;KACzB,CAAC;CACH;AA2CD,wBAAgB,eAAe,CAAC,EAC9B,QAAQ,EAAG,uBAAuB;AAClC,MAAM,EACN,OAAe,EACf,aAAkB,EAClB,iBAAuB,EACvB,aAAoB,EACpB,gBAAgC,EAChC,UAAuB,EACvB,WAAyB,EACzB,aAAyB,EACzB,cAAgC,EAChC,aAAgC,EAChC,YAAoB,EACpB,YAAmB,EACnB,cAWC,EACD,gBAGC,EACD,SAAe,EACf,aAAqB,EACrB,QAAa,GACd,EAAE,oBAAoB,GAAG,IAAI,CAshB7B"}
|
package/dist/SessionRecorder.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useEffect, useRef, useCallback, useState } from "react";
|
|
2
|
+
import { useEffect, useRef, useCallback, useState, useMemo } from "react";
|
|
3
3
|
import { record } from "rrweb";
|
|
4
4
|
import { pack } from "@rrweb/packer";
|
|
5
|
-
import { sendAnalytics, sendAnalyticsReliable, getRequestId } from "./utils";
|
|
6
5
|
export const SESSION_RECORDER_ENDPOINT = "session-replay";
|
|
7
6
|
function generateSessionId() {
|
|
8
7
|
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
@@ -22,7 +21,7 @@ function generateVisitorId() {
|
|
|
22
21
|
}
|
|
23
22
|
return `visitor_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
24
23
|
}
|
|
25
|
-
function getSessionMetadata(
|
|
24
|
+
function getSessionMetadata(_domain) {
|
|
26
25
|
if (typeof window === "undefined")
|
|
27
26
|
return {};
|
|
28
27
|
const screen = window.screen || {};
|
|
@@ -67,29 +66,37 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
67
66
|
const takeSnapshotRef = useRef(null); // Function to force a full snapshot
|
|
68
67
|
const maxInitRetries = 3;
|
|
69
68
|
const [isInitializing, setIsInitializing] = useState(false);
|
|
69
|
+
// Exponential backoff state for event sending
|
|
70
|
+
const sendRetryCountRef = useRef(0);
|
|
71
|
+
const maxSendRetries = 5;
|
|
72
|
+
const lastSendAttemptRef = useRef(0);
|
|
73
|
+
const isSendingRef = useRef(false); // Prevent concurrent sends
|
|
70
74
|
// Deep merge sampling defaults with proper handling of mouseInteraction
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
75
|
+
// Use useMemo to avoid recreating this object on every render
|
|
76
|
+
const samplingConfig = useMemo(() => {
|
|
77
|
+
const defaultSampling = {
|
|
78
|
+
scroll: 500,
|
|
79
|
+
media: 800,
|
|
80
|
+
mouseInteraction: {
|
|
81
|
+
MouseUp: false,
|
|
82
|
+
MouseDown: false,
|
|
83
|
+
Click: true,
|
|
84
|
+
ContextMenu: false,
|
|
85
|
+
DblClick: true,
|
|
86
|
+
Focus: true,
|
|
87
|
+
Blur: true,
|
|
88
|
+
TouchStart: false,
|
|
89
|
+
TouchEnd: false,
|
|
90
|
+
},
|
|
91
|
+
mousemove: false,
|
|
92
|
+
input: "last",
|
|
93
|
+
};
|
|
94
|
+
// Merge sampling configs, handling mouseInteraction specially
|
|
95
|
+
return {
|
|
96
|
+
...defaultSampling,
|
|
97
|
+
...sampling,
|
|
98
|
+
};
|
|
99
|
+
}, [sampling]);
|
|
93
100
|
const shouldRecord = useCallback(() => {
|
|
94
101
|
if (!enabled)
|
|
95
102
|
return false;
|
|
@@ -186,11 +193,28 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
186
193
|
const events = eventsRef.current;
|
|
187
194
|
if (events.length === 0)
|
|
188
195
|
return;
|
|
196
|
+
// Prevent concurrent sends
|
|
197
|
+
if (isSendingRef.current && !isReliable) {
|
|
198
|
+
console.log("[SessionRecorder] Already sending events, skipping this attempt");
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
// Check if we should apply backoff
|
|
202
|
+
const now = Date.now();
|
|
203
|
+
if (sendRetryCountRef.current > 0 && !isReliable) {
|
|
204
|
+
const backoffMs = Math.min(1000 * Math.pow(2, sendRetryCountRef.current), 30000);
|
|
205
|
+
const timeSinceLastAttempt = now - lastSendAttemptRef.current;
|
|
206
|
+
if (timeSinceLastAttempt < backoffMs) {
|
|
207
|
+
console.log(`[SessionRecorder] Backing off, waiting ${backoffMs - timeSinceLastAttempt}ms before retry`);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
isSendingRef.current = true;
|
|
212
|
+
lastSendAttemptRef.current = now;
|
|
189
213
|
const eventsToSend = [...events];
|
|
190
214
|
// Don't clear the array immediately - clear only after successful send
|
|
191
|
-
// eventsRef.current = [];
|
|
192
215
|
try {
|
|
193
216
|
// Pack the events for more efficient transmission
|
|
217
|
+
// Type assertion needed because rrweb's pack function expects eventWithTime but we have eventWithTime[]
|
|
194
218
|
const packedEvents = pack(eventsToSend);
|
|
195
219
|
// Base64 encode the packed string for safe JSON transmission
|
|
196
220
|
const encodedEvents = btoa(packedEvents);
|
|
@@ -216,6 +240,7 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
216
240
|
// For reliable sends, we don't clear events as they're sent via beacon
|
|
217
241
|
// which doesn't provide response feedback
|
|
218
242
|
eventsRef.current = [];
|
|
243
|
+
sendRetryCountRef.current = 0; // Reset retry count on success
|
|
219
244
|
}
|
|
220
245
|
else {
|
|
221
246
|
// Fallback to fetch with keepalive
|
|
@@ -227,6 +252,7 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
227
252
|
});
|
|
228
253
|
// Clear events after successful send
|
|
229
254
|
eventsRef.current = [];
|
|
255
|
+
sendRetryCountRef.current = 0; // Reset retry count on success
|
|
230
256
|
}
|
|
231
257
|
}
|
|
232
258
|
else {
|
|
@@ -251,22 +277,45 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
251
277
|
}
|
|
252
278
|
// Don't restore events - they're lost
|
|
253
279
|
eventsRef.current = [];
|
|
280
|
+
sendRetryCountRef.current = 0; // Reset retry count
|
|
254
281
|
}
|
|
255
282
|
else if (!response.ok) {
|
|
256
283
|
console.error("[SessionRecorder] Failed to send events:", response.status);
|
|
257
|
-
//
|
|
258
|
-
|
|
284
|
+
// Increment retry count with exponential backoff
|
|
285
|
+
sendRetryCountRef.current++;
|
|
286
|
+
if (sendRetryCountRef.current >= maxSendRetries) {
|
|
287
|
+
console.error(`[SessionRecorder] Exceeded max send retries (${maxSendRetries}), dropping ${events.length} events`);
|
|
288
|
+
eventsRef.current = []; // Drop events after too many failures
|
|
289
|
+
sendRetryCountRef.current = 0; // Reset for next batch
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
console.log(`[SessionRecorder] Will retry (${sendRetryCountRef.current}/${maxSendRetries}) with exponential backoff`);
|
|
293
|
+
// Keep events in the buffer for retry
|
|
294
|
+
}
|
|
259
295
|
}
|
|
260
296
|
else {
|
|
261
297
|
// Clear events after successful send
|
|
262
298
|
eventsRef.current = [];
|
|
299
|
+
sendRetryCountRef.current = 0; // Reset retry count on success
|
|
263
300
|
}
|
|
264
301
|
}
|
|
265
302
|
}
|
|
266
303
|
catch (error) {
|
|
267
304
|
console.error("[SessionRecorder] Failed to send session events:", error);
|
|
268
|
-
//
|
|
269
|
-
|
|
305
|
+
// Increment retry count with exponential backoff
|
|
306
|
+
sendRetryCountRef.current++;
|
|
307
|
+
if (sendRetryCountRef.current >= maxSendRetries) {
|
|
308
|
+
console.error(`[SessionRecorder] Exceeded max send retries (${maxSendRetries}) after error, dropping ${events.length} events`);
|
|
309
|
+
eventsRef.current = []; // Drop events after too many failures
|
|
310
|
+
sendRetryCountRef.current = 0; // Reset for next batch
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
console.log(`[SessionRecorder] Will retry (${sendRetryCountRef.current}/${maxSendRetries}) with exponential backoff`);
|
|
314
|
+
// Keep events in the buffer for retry
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
finally {
|
|
318
|
+
isSendingRef.current = false;
|
|
270
319
|
}
|
|
271
320
|
}, [basePath]);
|
|
272
321
|
const scheduleFlush = useCallback(() => {
|
|
@@ -337,7 +386,7 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
337
386
|
}
|
|
338
387
|
// Start the flush interval
|
|
339
388
|
scheduleFlush();
|
|
340
|
-
}, [shouldRecord, initializeSession, sendEvents, scheduleFlush, batchSize, recordCanvas, collectFonts, maskAllInputs, maskInputOptions, maskTextSelector, blockClass, ignoreClass, maskTextClass, slimDOMOptions, samplingConfig]);
|
|
389
|
+
}, [shouldRecord, initializeSession, sendEvents, scheduleFlush, batchSize, blockSelector, ignoreSelector, recordCanvas, collectFonts, maskAllInputs, maskInputOptions, maskTextSelector, blockClass, ignoreClass, maskTextClass, slimDOMOptions, samplingConfig]);
|
|
341
390
|
const stopRecording = useCallback(() => {
|
|
342
391
|
if (stopFnRef.current) {
|
|
343
392
|
stopFnRef.current();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temps-sdk/react-analytics",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.39",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
"clean": "rm -rf dist",
|
|
21
21
|
"prepublishOnly": "bun run clean && bun run build",
|
|
22
22
|
"test": "vitest",
|
|
23
|
-
"test:coverage": "vitest run --coverage"
|
|
23
|
+
"test:coverage": "vitest run --coverage",
|
|
24
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
25
|
+
"lint:fix": "eslint src --ext .ts,.tsx --fix"
|
|
24
26
|
},
|
|
25
27
|
"publishConfig": {
|
|
26
28
|
"access": "public"
|
|
@@ -40,11 +42,17 @@
|
|
|
40
42
|
"web-vitals": "^4.2.4"
|
|
41
43
|
},
|
|
42
44
|
"devDependencies": {
|
|
45
|
+
"@eslint/js": "^9.37.0",
|
|
43
46
|
"@testing-library/react": "^14.2.1",
|
|
44
47
|
"@testing-library/react-hooks": "^8.0.1",
|
|
45
48
|
"@types/react": "^18.2.56",
|
|
46
49
|
"@types/react-dom": "^18.2.19",
|
|
50
|
+
"@typescript-eslint/eslint-plugin": "^8.46.1",
|
|
51
|
+
"@typescript-eslint/parser": "^8.46.1",
|
|
47
52
|
"@vitest/coverage-v8": "^1.3.1",
|
|
53
|
+
"eslint": "^9.37.0",
|
|
54
|
+
"eslint-plugin-react": "^7.37.5",
|
|
55
|
+
"eslint-plugin-react-hooks": "^7.0.0",
|
|
48
56
|
"jsdom": "^24.0.0",
|
|
49
57
|
"react": "^18.2.0",
|
|
50
58
|
"react-dom": "^18.2.0",
|