@temps-sdk/react-analytics 0.0.1-beta.20 → 0.0.1-beta.21

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.
@@ -1 +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,CAAC,EAAE,MAAM,CAAC;IAClB,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,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AA+CD,wBAAgB,eAAe,CAAC,EAC9B,QAAoB,EACpB,MAAM,EACN,OAAe,EACf,aAAkB,EAClB,iBAAuB,EACvB,aAAoB,EACpB,gBAAgC,EAChC,UAAuB,EACvB,WAAyB,EACzB,aAAyB,EACzB,YAAoB,EACpB,YAAoB,EACpB,cAUC,EACD,SAAe,EACf,aAAoB,GACrB,EAAE,oBAAoB,QAuUtB"}
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,CAAC,EAAE,MAAM,CAAC;IAClB,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,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AA+CD,wBAAgB,eAAe,CAAC,EAC9B,QAAoB,EACpB,MAAM,EACN,OAAe,EACf,aAAkB,EAClB,iBAAuB,EACvB,aAAoB,EACpB,gBAAgC,EAChC,UAAuB,EACvB,WAAyB,EACzB,aAAyB,EACzB,YAAoB,EACpB,YAAoB,EACpB,cAUC,EACD,SAAe,EACf,aAAoB,GACrB,EAAE,oBAAoB,QA4XtB"}
@@ -62,6 +62,7 @@ export function SessionRecorder({ basePath = "/_temps", domain, enabled = false,
62
62
  const sessionInitializedRef = useRef(false);
63
63
  const flushTimeoutRef = useRef(null);
64
64
  const initRetryCountRef = useRef(0);
65
+ const initFailedRef = useRef(false); // Track if init has permanently failed
65
66
  const maxInitRetries = 3;
66
67
  const [isInitializing, setIsInitializing] = useState(false);
67
68
  const shouldRecord = useCallback(() => {
@@ -87,15 +88,23 @@ export function SessionRecorder({ basePath = "/_temps", domain, enabled = false,
87
88
  return true;
88
89
  }, [enabled, excludedPaths, sessionSampleRate]);
89
90
  const initializeSession = useCallback(async () => {
91
+ // If already initialized or currently initializing, return current state
90
92
  if (sessionInitializedRef.current || isInitializing)
91
93
  return sessionInitializedRef.current;
94
+ // If we've permanently failed, don't try again
95
+ if (initFailedRef.current) {
96
+ console.warn("[SessionRecorder] Initialization has permanently failed, not retrying");
97
+ return false;
98
+ }
92
99
  // Check if we've exceeded retry limit
93
100
  if (initRetryCountRef.current >= maxInitRetries) {
94
- console.error("[SessionRecorder] Exceeded maximum initialization retries");
101
+ console.error(`[SessionRecorder] Exceeded maximum initialization retries (${maxInitRetries})`);
102
+ initFailedRef.current = true; // Mark as permanently failed
95
103
  return false;
96
104
  }
97
105
  setIsInitializing(true);
98
106
  initRetryCountRef.current++;
107
+ console.log(`[SessionRecorder] Attempting to initialize session (attempt ${initRetryCountRef.current}/${maxInitRetries})`);
99
108
  const sessionId = generateSessionId();
100
109
  sessionIdRef.current = sessionId;
101
110
  try {
@@ -113,6 +122,7 @@ export function SessionRecorder({ basePath = "/_temps", domain, enabled = false,
113
122
  if (response.status === 201) {
114
123
  sessionInitializedRef.current = true;
115
124
  initRetryCountRef.current = 0; // Reset retry count on success
125
+ initFailedRef.current = false; // Reset failed flag on success
116
126
  // Store session ID in localStorage for reference
117
127
  if (typeof localStorage !== "undefined") {
118
128
  localStorage.setItem("currentRecordingSessionId", sessionId);
@@ -121,14 +131,22 @@ export function SessionRecorder({ basePath = "/_temps", domain, enabled = false,
121
131
  return true;
122
132
  }
123
133
  else {
124
- console.error("[SessionRecorder] Failed to initialize session:", response.status);
134
+ console.error(`[SessionRecorder] Failed to initialize session: ${response.status} (attempt ${initRetryCountRef.current}/${maxInitRetries})`);
125
135
  sessionIdRef.current = ""; // Clear invalid session ID
136
+ // Mark as permanently failed if we've hit the retry limit
137
+ if (initRetryCountRef.current >= maxInitRetries) {
138
+ initFailedRef.current = true;
139
+ }
126
140
  return false;
127
141
  }
128
142
  }
129
143
  catch (error) {
130
- console.error("[SessionRecorder] Failed to initialize session:", error);
144
+ console.error(`[SessionRecorder] Failed to initialize session: ${error} (attempt ${initRetryCountRef.current}/${maxInitRetries})`);
131
145
  sessionIdRef.current = ""; // Clear invalid session ID
146
+ // Mark as permanently failed if we've hit the retry limit
147
+ if (initRetryCountRef.current >= maxInitRetries) {
148
+ initFailedRef.current = true;
149
+ }
132
150
  return false;
133
151
  }
134
152
  finally {
@@ -228,9 +246,10 @@ export function SessionRecorder({ basePath = "/_temps", domain, enabled = false,
228
246
  return; // Already recording
229
247
  if (!shouldRecord())
230
248
  return;
231
- // Reset retry counter for new recording session
232
- if (!sessionInitializedRef.current) {
233
- initRetryCountRef.current = 0;
249
+ // If initialization has permanently failed, don't try to start
250
+ if (initFailedRef.current) {
251
+ console.warn("[SessionRecorder] Not starting recording - initialization has permanently failed");
252
+ return;
234
253
  }
235
254
  // Initialize session first
236
255
  const initialized = await initializeSession();
@@ -287,31 +306,55 @@ export function SessionRecorder({ basePath = "/_temps", domain, enabled = false,
287
306
  localStorage.removeItem("currentRecordingSessionId");
288
307
  }
289
308
  }
309
+ // Reset retry counters when explicitly stopping
310
+ initRetryCountRef.current = 0;
311
+ initFailedRef.current = false;
290
312
  }, [sendEvents]);
291
313
  // Effect to handle recording lifecycle
292
314
  useEffect(() => {
293
- if (!shouldRecord()) {
294
- stopRecording();
315
+ // Early return if we shouldn't record or have permanently failed
316
+ if (!enabled || initFailedRef.current || stopFnRef.current || sessionInitializedRef.current) {
317
+ return;
318
+ }
319
+ // Check if current path should be excluded
320
+ const currentPath = window.location.pathname;
321
+ const isExcluded = excludedPaths.some(path => {
322
+ const regex = new RegExp(`^${path.replace(/\*/g, '.*')}$`);
323
+ return regex.test(currentPath);
324
+ });
325
+ if (isExcluded) {
295
326
  return;
296
327
  }
328
+ // Apply sampling rate
329
+ if (sessionSampleRate < 1.0) {
330
+ const random = Math.random();
331
+ if (random > sessionSampleRate) {
332
+ return;
333
+ }
334
+ }
335
+ // Now we can start recording
297
336
  startRecording();
298
337
  // Handle page unload
299
338
  const handleUnload = () => {
300
- sendEvents(true);
339
+ if (sessionInitializedRef.current && eventsRef.current.length > 0) {
340
+ sendEvents(true);
341
+ }
301
342
  };
302
343
  window.addEventListener("beforeunload", handleUnload);
303
344
  window.addEventListener("pagehide", handleUnload);
304
345
  return () => {
305
- stopRecording();
306
346
  window.removeEventListener("beforeunload", handleUnload);
307
347
  window.removeEventListener("pagehide", handleUnload);
308
348
  };
309
- }, [shouldRecord, startRecording, stopRecording, sendEvents]);
349
+ // Dependencies that should trigger re-evaluation
350
+ }, [enabled, excludedPaths, sessionSampleRate, startRecording, sendEvents]);
310
351
  // Monitor location changes to stop/start recording based on excluded paths
311
352
  useEffect(() => {
312
353
  const checkPathAndToggleRecording = () => {
313
- if (!enabled) {
314
- stopRecording();
354
+ if (!enabled || initFailedRef.current) {
355
+ if (!enabled) {
356
+ stopRecording();
357
+ }
315
358
  return;
316
359
  }
317
360
  const currentPath = window.location.pathname;
@@ -322,7 +365,7 @@ export function SessionRecorder({ basePath = "/_temps", domain, enabled = false,
322
365
  if (isExcluded && stopFnRef.current) {
323
366
  stopRecording();
324
367
  }
325
- else if (!isExcluded && !stopFnRef.current) {
368
+ else if (!isExcluded && !stopFnRef.current && !initFailedRef.current) {
326
369
  startRecording();
327
370
  }
328
371
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temps-sdk/react-analytics",
3
- "version": "0.0.1-beta.20",
3
+ "version": "0.0.1-beta.21",
4
4
  "private": false,
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",