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