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