acsi-core 0.1.79 → 0.1.80

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.
@@ -2503,7 +2503,33 @@ var useAmplitude = function useAmplitude() {
2503
2503
  currentSessionStart.current = null;
2504
2504
  savedUserProperties.current = {};
2505
2505
  hasTrackedInitialSession.current = false;
2506
+ localStorage.removeItem('sessionData');
2506
2507
  }, []);
2508
+ var saveSessionToLocalStorage = useCallback(function (session) {
2509
+ var existingSessions = localStorage.getItem('sessionData');
2510
+ var sessions = existingSessions ? JSON.parse(existingSessions) : [];
2511
+ sessions.push(session);
2512
+ localStorage.setItem('sessionData', JSON.stringify(sessions));
2513
+ }, []);
2514
+ var sendSessionsToAmplitude = useCallback(function () {
2515
+ if (!userId) return;
2516
+ var existingSessions = localStorage.getItem('sessionData');
2517
+ if (!existingSessions) return;
2518
+ var sessions = JSON.parse(existingSessions);
2519
+ if (sessions.length === 0) return;
2520
+ sessions.forEach(function (session) {
2521
+ trackEvent({
2522
+ eventName: AmplitudeEvent.SESSION_END,
2523
+ eventProperties: {
2524
+ start_time: session.start_time,
2525
+ end_time: session.end_time,
2526
+ session_duration_seconds: session.session_duration_seconds,
2527
+ session_id: session.session_id
2528
+ }
2529
+ });
2530
+ });
2531
+ localStorage.removeItem('sessionData');
2532
+ }, [userId, trackEvent]);
2507
2533
  var trackTabActivity = useCallback(function (isActive) {
2508
2534
  if (!userId) return;
2509
2535
  var now = new Date();
@@ -2518,32 +2544,39 @@ var useAmplitude = function useAmplitude() {
2518
2544
  } else {
2519
2545
  if (currentSessionStart.current) {
2520
2546
  var sessionDuration = now.getTime() - currentSessionStart.current.getTime();
2547
+ var sessionData = {
2548
+ start_time: currentSessionStart.current.toISOString(),
2549
+ end_time: now.toISOString(),
2550
+ session_duration_seconds: Math.round(sessionDuration / 1000),
2551
+ session_id: currentSessionStart.current.getTime()
2552
+ };
2553
+ saveSessionToLocalStorage(sessionData);
2521
2554
  trackEvent({
2522
2555
  eventName: AmplitudeEvent.SESSION_END,
2523
2556
  eventProperties: {
2524
- session_duration_seconds: Math.round(sessionDuration / 1000),
2525
- start_time: currentSessionStart.current.toISOString(),
2526
- end_time: now.toISOString()
2557
+ session_duration_seconds: sessionData.session_duration_seconds,
2558
+ start_time: sessionData.start_time,
2559
+ end_time: sessionData.end_time,
2560
+ session_id: sessionData.session_id
2527
2561
  }
2528
2562
  });
2529
2563
  currentSessionStart.current = null;
2530
2564
  }
2531
2565
  }
2532
- }, [userId, trackEvent]);
2566
+ }, [userId, trackEvent, saveSessionToLocalStorage]);
2533
2567
  var trackPageUnload = useCallback(function () {
2534
2568
  if (!userId || !currentSessionStart.current) return;
2535
2569
  var now = new Date();
2536
2570
  var sessionDuration = now.getTime() - currentSessionStart.current.getTime();
2537
- trackEvent({
2538
- eventName: AmplitudeEvent.SESSION_END,
2539
- eventProperties: {
2540
- session_duration_seconds: Math.round(sessionDuration / 1000),
2541
- start_time: currentSessionStart.current.toISOString(),
2542
- end_time: now.toISOString(),
2543
- end_reason: 'page_unload'
2544
- }
2545
- });
2546
- }, [userId, trackEvent]);
2571
+ var sessionData = {
2572
+ start_time: currentSessionStart.current.toISOString(),
2573
+ end_time: now.toISOString(),
2574
+ session_duration_seconds: Math.round(sessionDuration / 1000),
2575
+ session_id: currentSessionStart.current.getTime()
2576
+ };
2577
+ saveSessionToLocalStorage(sessionData);
2578
+ sendSessionsToAmplitude();
2579
+ }, [userId, saveSessionToLocalStorage, sendSessionsToAmplitude]);
2547
2580
  useEffect(function () {
2548
2581
  var handleVisibilityChange = function handleVisibilityChange() {
2549
2582
  var isVisible = !document.hidden;