@devskin/browser-sdk 1.0.25 → 1.0.26

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.
@@ -14066,11 +14066,32 @@
14066
14066
  * Private methods
14067
14067
  */
14068
14068
  startSession() {
14069
- var _a;
14069
+ var _a, _b, _c;
14070
+ // Check if there's an active session (stored in sessionStorage to persist across page navigations)
14071
+ const existingSessionId = sessionStorage.getItem('devskin_session_id');
14072
+ const existingSessionStart = sessionStorage.getItem('devskin_session_start');
14073
+ if (existingSessionId && existingSessionStart) {
14074
+ // Resume existing session
14075
+ this.sessionId = existingSessionId;
14076
+ this.sessionStartTime = parseInt(existingSessionStart, 10);
14077
+ if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.debug) {
14078
+ console.log('[DevSkin] Resuming existing session:', this.sessionId);
14079
+ }
14080
+ // Send page view but DON'T create a new session
14081
+ // The session is already created, just continue it
14082
+ return;
14083
+ }
14084
+ // Create new session
14070
14085
  this.sessionId = this.generateId();
14071
14086
  this.sessionStartTime = Date.now();
14087
+ // Store in sessionStorage (persists across page navigations in same tab)
14088
+ sessionStorage.setItem('devskin_session_id', this.sessionId);
14089
+ sessionStorage.setItem('devskin_session_start', this.sessionStartTime.toString());
14072
14090
  const sessionData = Object.assign({ session_id: this.sessionId, user_id: this.userId || undefined, anonymous_id: this.anonymousId, started_at: new Date().toISOString() }, this.getContextData());
14073
- (_a = this.transport) === null || _a === void 0 ? void 0 : _a.startSession(sessionData);
14091
+ (_b = this.transport) === null || _b === void 0 ? void 0 : _b.startSession(sessionData);
14092
+ if ((_c = this.config) === null || _c === void 0 ? void 0 : _c.debug) {
14093
+ console.log('[DevSkin] New session created:', this.sessionId);
14094
+ }
14074
14095
  }
14075
14096
  getContextData() {
14076
14097
  const context = {};
@@ -14107,16 +14128,26 @@
14107
14128
  });
14108
14129
  }
14109
14130
  setupUnloadTracking() {
14110
- window.addEventListener('beforeunload', () => {
14131
+ // Use pagehide event which is more reliable than beforeunload
14132
+ window.addEventListener('pagehide', (event) => {
14111
14133
  var _a, _b;
14112
- this.track('page_unload');
14113
- // Send session end update with duration AND context data
14114
- if (this.sessionId && this.sessionStartTime) {
14115
- const endedAt = new Date();
14116
- const durationMs = Date.now() - this.sessionStartTime;
14117
- (_a = this.transport) === null || _a === void 0 ? void 0 : _a.startSession(Object.assign({ session_id: this.sessionId, user_id: this.userId || undefined, anonymous_id: this.anonymousId, ended_at: endedAt.toISOString(), duration_ms: durationMs }, this.getContextData()));
14134
+ // Only end session if tab is actually closing (not just navigating)
14135
+ // persisted = false means the page is going into bfcache (navigation)
14136
+ // persisted = true OR event doesn't exist means actual close
14137
+ const isActualClose = !event.persisted;
14138
+ if (isActualClose) {
14139
+ this.track('page_unload');
14140
+ // Only send session end if tab is actually closing
14141
+ if (this.sessionId && this.sessionStartTime) {
14142
+ const endedAt = new Date();
14143
+ const durationMs = Date.now() - this.sessionStartTime;
14144
+ (_a = this.transport) === null || _a === void 0 ? void 0 : _a.startSession(Object.assign({ session_id: this.sessionId, user_id: this.userId || undefined, anonymous_id: this.anonymousId, ended_at: endedAt.toISOString(), duration_ms: durationMs }, this.getContextData()));
14145
+ // Clear session storage since session is ending
14146
+ sessionStorage.removeItem('devskin_session_id');
14147
+ sessionStorage.removeItem('devskin_session_start');
14148
+ }
14118
14149
  }
14119
- // Send any pending data
14150
+ // Always flush pending data (whether navigation or close)
14120
14151
  (_b = this.transport) === null || _b === void 0 ? void 0 : _b.flush(true); // Use beacon for reliability
14121
14152
  });
14122
14153
  }