@mushi-mushi/core 1.41.0 → 1.42.0-next.20261205140616

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.cjs CHANGED
@@ -2019,6 +2019,9 @@ function getSessionId() {
2019
2019
  return existing;
2020
2020
  }
2021
2021
  }
2022
+ return rotateSessionId();
2023
+ }
2024
+ function rotateSessionId() {
2022
2025
  const id = generateSessionId();
2023
2026
  cachedSessionId = id;
2024
2027
  if (typeof sessionStorage !== "undefined") {
@@ -2112,6 +2115,12 @@ function onAnalyticsConsentChange(listener) {
2112
2115
 
2113
2116
  // src/session-tracker.ts
2114
2117
  var HEARTBEAT_INTERVAL_MS = 6e4;
2118
+ var MIN_IDLE_TIMEOUT_MS = 6e4;
2119
+ function normalizeIdleTimeout(value) {
2120
+ if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) return 0;
2121
+ return Math.max(MIN_IDLE_TIMEOUT_MS, value);
2122
+ }
2123
+ var INPUT_EVENTS = ["pointerdown", "pointermove", "keydown", "wheel", "scroll", "touchstart"];
2115
2124
  var _client = null;
2116
2125
  var _sdkVersion;
2117
2126
  var _userIdHash = null;
@@ -2124,6 +2133,10 @@ var _initialized = false;
2124
2133
  var _active = false;
2125
2134
  var _started = false;
2126
2135
  var _unsubscribeConsent = null;
2136
+ var _lastInputAt = 0;
2137
+ var _lastSentAt = 0;
2138
+ var _idleStopped = false;
2139
+ var _idleTimeoutMs = 0;
2127
2140
  function now() {
2128
2141
  return (/* @__PURE__ */ new Date()).toISOString();
2129
2142
  }
@@ -2161,6 +2174,7 @@ function send(payload) {
2161
2174
  if (!_client || !_active) return;
2162
2175
  _client.postSessionEvent(payload).catch(() => {
2163
2176
  });
2177
+ _lastSentAt = Date.now();
2164
2178
  scheduleHeartbeat();
2165
2179
  }
2166
2180
  function clearHeartbeat() {
@@ -2168,13 +2182,41 @@ function clearHeartbeat() {
2168
2182
  }
2169
2183
  function scheduleHeartbeat() {
2170
2184
  clearHeartbeat();
2171
- if (!_active || globalThis.document?.visibilityState === "hidden") return;
2185
+ if (!_active || _idleStopped || globalThis.document?.visibilityState === "hidden") return;
2172
2186
  _heartbeatTimer = setTimeout(() => {
2187
+ if (_idleTimeoutMs > 0 && Date.now() - _lastInputAt >= _idleTimeoutMs) {
2188
+ _idleStopped = true;
2189
+ return;
2190
+ }
2173
2191
  send(buildPayload("session_heartbeat", { route: currentRoute() }));
2174
2192
  }, HEARTBEAT_INTERVAL_MS);
2175
2193
  }
2194
+ function resumeSession() {
2195
+ _idleStopped = false;
2196
+ _endSent = false;
2197
+ if (!_active) return;
2198
+ if (Date.now() - _lastSentAt < _idleTimeoutMs) {
2199
+ scheduleHeartbeat();
2200
+ return;
2201
+ }
2202
+ rotateSessionId();
2203
+ _pageViewCount = 1;
2204
+ _lastRoute = currentRoute();
2205
+ _endSent = false;
2206
+ send(buildPayload("session_start", { route: _lastRoute }));
2207
+ }
2208
+ function onUserInput() {
2209
+ if (_idleTimeoutMs === 0) return;
2210
+ _lastInputAt = Date.now();
2211
+ if (!_idleStopped || !_active || globalThis.document?.visibilityState === "hidden") return;
2212
+ resumeSession();
2213
+ }
2176
2214
  function recordPageView(route) {
2177
2215
  if (!_active) return;
2216
+ if (_idleStopped) {
2217
+ _lastInputAt = Date.now();
2218
+ resumeSession();
2219
+ }
2178
2220
  if (route === _lastRoute) return;
2179
2221
  _lastRoute = route;
2180
2222
  _pageViewCount += 1;
@@ -2182,6 +2224,7 @@ function recordPageView(route) {
2182
2224
  }
2183
2225
  function sendSessionEnd() {
2184
2226
  if (_endSent || !_active) return;
2227
+ if (_idleStopped) return;
2185
2228
  _endSent = true;
2186
2229
  send(buildPayload("session_end", { route: currentRoute() }));
2187
2230
  clearHeartbeat();
@@ -2189,6 +2232,8 @@ function sendSessionEnd() {
2189
2232
  function activate() {
2190
2233
  if (_active || !_initialized) return;
2191
2234
  _active = true;
2235
+ _lastInputAt = Date.now();
2236
+ _idleStopped = false;
2192
2237
  if (!_started) {
2193
2238
  _started = true;
2194
2239
  _pageViewCount = 1;
@@ -2201,9 +2246,12 @@ function activate() {
2201
2246
  function onHide() {
2202
2247
  if (document.visibilityState === "hidden") {
2203
2248
  sendSessionEnd();
2204
- } else {
2249
+ } else if (_idleTimeoutMs === 0) {
2205
2250
  _endSent = false;
2206
2251
  scheduleHeartbeat();
2252
+ } else {
2253
+ _lastInputAt = Date.now();
2254
+ resumeSession();
2207
2255
  }
2208
2256
  }
2209
2257
  function onPagehide() {
@@ -2211,8 +2259,13 @@ function onPagehide() {
2211
2259
  }
2212
2260
  function onPageshow(event) {
2213
2261
  if (!event.persisted) return;
2214
- _endSent = false;
2215
- scheduleHeartbeat();
2262
+ if (_idleTimeoutMs === 0) {
2263
+ _endSent = false;
2264
+ scheduleHeartbeat();
2265
+ return;
2266
+ }
2267
+ _lastInputAt = Date.now();
2268
+ resumeSession();
2216
2269
  }
2217
2270
  function deactivate() {
2218
2271
  _active = false;
@@ -2226,11 +2279,17 @@ function initSessionTracker(opts) {
2226
2279
  _sdkVersion = opts.sdkVersion;
2227
2280
  _reporterToken = opts.reporterToken ?? opts.reporterTokenHash ?? null;
2228
2281
  _userIdHash = opts.userIdHash ?? null;
2282
+ _idleTimeoutMs = normalizeIdleTimeout(opts.analytics?.idleTimeoutMs);
2229
2283
  if (typeof document !== "undefined") {
2230
2284
  document.addEventListener("visibilitychange", onHide, { passive: true });
2231
2285
  }
2232
2286
  window.addEventListener("pagehide", onPagehide, { passive: true });
2233
2287
  window.addEventListener("pageshow", onPageshow, { passive: true });
2288
+ if (_idleTimeoutMs > 0) {
2289
+ for (const type of INPUT_EVENTS) {
2290
+ window.addEventListener(type, onUserInput, { passive: true, capture: true });
2291
+ }
2292
+ }
2234
2293
  if (opts.patchHistory !== false) {
2235
2294
  patchHistoryForPageViews();
2236
2295
  }
@@ -2256,6 +2315,10 @@ function destroySessionTracker() {
2256
2315
  _started = false;
2257
2316
  _lastRoute = null;
2258
2317
  _endSent = false;
2318
+ _idleStopped = false;
2319
+ _lastInputAt = 0;
2320
+ _lastSentAt = 0;
2321
+ _idleTimeoutMs = 0;
2259
2322
  _client = null;
2260
2323
  }
2261
2324
  var _historyPatched = false;