@temps-sdk/react-analytics 0.0.1-beta.36 → 0.0.1-beta.38
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.
|
@@ -13,6 +13,10 @@ interface SessionRecorderProps {
|
|
|
13
13
|
recordCanvas?: boolean;
|
|
14
14
|
collectFonts?: boolean;
|
|
15
15
|
slimDOMOptions?: Record<string, boolean>;
|
|
16
|
+
maskInputOptions?: {
|
|
17
|
+
password?: boolean;
|
|
18
|
+
email?: boolean;
|
|
19
|
+
};
|
|
16
20
|
/**
|
|
17
21
|
* Number of events to batch before sending. Default: 500
|
|
18
22
|
* Events are sent when EITHER batchSize is reached OR flushInterval elapses (whichever comes first).
|
|
@@ -48,6 +52,6 @@ interface SessionRecorderProps {
|
|
|
48
52
|
};
|
|
49
53
|
}
|
|
50
54
|
export declare function SessionRecorder({ basePath, // Required, no default
|
|
51
|
-
domain, enabled, excludedPaths, sessionSampleRate, maskAllInputs, maskTextSelector, blockClass, ignoreClass, maskTextClass, ignoreSelector, blockSelector, recordCanvas, collectFonts, slimDOMOptions, batchSize, flushInterval, sampling, }: SessionRecorderProps): null;
|
|
55
|
+
domain, enabled, excludedPaths, sessionSampleRate, maskAllInputs, maskTextSelector, blockClass, ignoreClass, maskTextClass, ignoreSelector, blockSelector, recordCanvas, collectFonts, slimDOMOptions, maskInputOptions, batchSize, flushInterval, sampling, }: SessionRecorderProps): null;
|
|
52
56
|
export {};
|
|
53
57
|
//# sourceMappingURL=SessionRecorder.d.ts.map
|
|
@@ -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,EAA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AC;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 || {};
|
|
@@ -42,16 +41,20 @@ function getSessionMetadata(domain) {
|
|
|
42
41
|
};
|
|
43
42
|
}
|
|
44
43
|
export function SessionRecorder({ basePath, // Required, no default
|
|
45
|
-
domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInputs = true, maskTextSelector = "[data-mask]", blockClass = "rr-block", ignoreClass = "rr-ignore", maskTextClass = "rr-mask", ignoreSelector = "[data-ignore]", blockSelector = "[data-private]", recordCanvas = false, collectFonts =
|
|
44
|
+
domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInputs = true, maskTextSelector = "[data-mask]", blockClass = "rr-block", ignoreClass = "rr-ignore", maskTextClass = "rr-mask", ignoreSelector = "[data-ignore]", blockSelector = "[data-private]", recordCanvas = false, collectFonts = true, slimDOMOptions = {
|
|
46
45
|
script: false,
|
|
47
|
-
comment:
|
|
48
|
-
headFavicon:
|
|
49
|
-
headWhitespace:
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
comment: true,
|
|
47
|
+
headFavicon: true,
|
|
48
|
+
headWhitespace: true,
|
|
49
|
+
headMetaDescKeywords: true,
|
|
50
|
+
headMetaSocial: true,
|
|
51
|
+
headMetaRobots: true,
|
|
52
|
+
headMetaHttpEquiv: true,
|
|
53
|
+
headMetaAuthorship: true,
|
|
54
|
+
headMetaVerification: true,
|
|
55
|
+
}, maskInputOptions = {
|
|
56
|
+
password: true,
|
|
57
|
+
email: true,
|
|
55
58
|
}, batchSize = 500, flushInterval = 60000, sampling = {}, }) {
|
|
56
59
|
const stopFnRef = useRef(null);
|
|
57
60
|
const eventsRef = useRef([]);
|
|
@@ -63,28 +66,37 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
63
66
|
const takeSnapshotRef = useRef(null); // Function to force a full snapshot
|
|
64
67
|
const maxInitRetries = 3;
|
|
65
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
|
|
66
74
|
// Deep merge sampling defaults with proper handling of mouseInteraction
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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]);
|
|
88
100
|
const shouldRecord = useCallback(() => {
|
|
89
101
|
if (!enabled)
|
|
90
102
|
return false;
|
|
@@ -181,11 +193,28 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
181
193
|
const events = eventsRef.current;
|
|
182
194
|
if (events.length === 0)
|
|
183
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;
|
|
184
213
|
const eventsToSend = [...events];
|
|
185
214
|
// Don't clear the array immediately - clear only after successful send
|
|
186
|
-
// eventsRef.current = [];
|
|
187
215
|
try {
|
|
188
216
|
// Pack the events for more efficient transmission
|
|
217
|
+
// Type assertion needed because rrweb's pack function expects eventWithTime but we have eventWithTime[]
|
|
189
218
|
const packedEvents = pack(eventsToSend);
|
|
190
219
|
// Base64 encode the packed string for safe JSON transmission
|
|
191
220
|
const encodedEvents = btoa(packedEvents);
|
|
@@ -211,6 +240,7 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
211
240
|
// For reliable sends, we don't clear events as they're sent via beacon
|
|
212
241
|
// which doesn't provide response feedback
|
|
213
242
|
eventsRef.current = [];
|
|
243
|
+
sendRetryCountRef.current = 0; // Reset retry count on success
|
|
214
244
|
}
|
|
215
245
|
else {
|
|
216
246
|
// Fallback to fetch with keepalive
|
|
@@ -222,6 +252,7 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
222
252
|
});
|
|
223
253
|
// Clear events after successful send
|
|
224
254
|
eventsRef.current = [];
|
|
255
|
+
sendRetryCountRef.current = 0; // Reset retry count on success
|
|
225
256
|
}
|
|
226
257
|
}
|
|
227
258
|
else {
|
|
@@ -246,22 +277,45 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
246
277
|
}
|
|
247
278
|
// Don't restore events - they're lost
|
|
248
279
|
eventsRef.current = [];
|
|
280
|
+
sendRetryCountRef.current = 0; // Reset retry count
|
|
249
281
|
}
|
|
250
282
|
else if (!response.ok) {
|
|
251
283
|
console.error("[SessionRecorder] Failed to send events:", response.status);
|
|
252
|
-
//
|
|
253
|
-
|
|
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
|
+
}
|
|
254
295
|
}
|
|
255
296
|
else {
|
|
256
297
|
// Clear events after successful send
|
|
257
298
|
eventsRef.current = [];
|
|
299
|
+
sendRetryCountRef.current = 0; // Reset retry count on success
|
|
258
300
|
}
|
|
259
301
|
}
|
|
260
302
|
}
|
|
261
303
|
catch (error) {
|
|
262
304
|
console.error("[SessionRecorder] Failed to send session events:", error);
|
|
263
|
-
//
|
|
264
|
-
|
|
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;
|
|
265
319
|
}
|
|
266
320
|
}, [basePath]);
|
|
267
321
|
const scheduleFlush = useCallback(() => {
|
|
@@ -315,11 +369,14 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
315
369
|
recordCanvas,
|
|
316
370
|
collectFonts,
|
|
317
371
|
maskAllInputs,
|
|
372
|
+
maskInputOptions,
|
|
318
373
|
maskTextSelector,
|
|
319
374
|
blockClass,
|
|
320
375
|
ignoreClass,
|
|
321
376
|
maskTextClass,
|
|
322
377
|
slimDOMOptions,
|
|
378
|
+
checkoutEveryNms: 30000,
|
|
379
|
+
checkoutEveryNth: 200,
|
|
323
380
|
});
|
|
324
381
|
if (stopFn) {
|
|
325
382
|
stopFnRef.current = stopFn;
|
|
@@ -329,7 +386,7 @@ domain, enabled = false, excludedPaths = [], sessionSampleRate = 1.0, maskAllInp
|
|
|
329
386
|
}
|
|
330
387
|
// Start the flush interval
|
|
331
388
|
scheduleFlush();
|
|
332
|
-
}, [shouldRecord, initializeSession, sendEvents, scheduleFlush, batchSize, recordCanvas, collectFonts, maskAllInputs, maskTextSelector, blockClass, ignoreClass, maskTextClass, slimDOMOptions, samplingConfig]);
|
|
389
|
+
}, [shouldRecord, initializeSession, sendEvents, scheduleFlush, batchSize, blockSelector, ignoreSelector, recordCanvas, collectFonts, maskAllInputs, maskInputOptions, maskTextSelector, blockClass, ignoreClass, maskTextClass, slimDOMOptions, samplingConfig]);
|
|
333
390
|
const stopRecording = useCallback(() => {
|
|
334
391
|
if (stopFnRef.current) {
|
|
335
392
|
stopFnRef.current();
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temps-sdk/react-analytics",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.38",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"private": false,
|
|
5
6
|
"main": "./dist/index.js",
|
|
6
7
|
"types": "./dist/index.d.ts",
|
|
@@ -20,7 +21,9 @@
|
|
|
20
21
|
"clean": "rm -rf dist",
|
|
21
22
|
"prepublishOnly": "bun run clean && bun run build",
|
|
22
23
|
"test": "vitest",
|
|
23
|
-
"test:coverage": "vitest run --coverage"
|
|
24
|
+
"test:coverage": "vitest run --coverage",
|
|
25
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
26
|
+
"lint:fix": "eslint src --ext .ts,.tsx --fix"
|
|
24
27
|
},
|
|
25
28
|
"publishConfig": {
|
|
26
29
|
"access": "public"
|
|
@@ -40,11 +43,17 @@
|
|
|
40
43
|
"web-vitals": "^4.2.4"
|
|
41
44
|
},
|
|
42
45
|
"devDependencies": {
|
|
46
|
+
"@eslint/js": "^9.37.0",
|
|
43
47
|
"@testing-library/react": "^14.2.1",
|
|
44
48
|
"@testing-library/react-hooks": "^8.0.1",
|
|
45
49
|
"@types/react": "^18.2.56",
|
|
46
50
|
"@types/react-dom": "^18.2.19",
|
|
51
|
+
"@typescript-eslint/eslint-plugin": "^8.46.1",
|
|
52
|
+
"@typescript-eslint/parser": "^8.46.1",
|
|
47
53
|
"@vitest/coverage-v8": "^1.3.1",
|
|
54
|
+
"eslint": "^9.37.0",
|
|
55
|
+
"eslint-plugin-react": "^7.37.5",
|
|
56
|
+
"eslint-plugin-react-hooks": "^7.0.0",
|
|
48
57
|
"jsdom": "^24.0.0",
|
|
49
58
|
"react": "^18.2.0",
|
|
50
59
|
"react-dom": "^18.2.0",
|