@devskin/browser-sdk 1.0.4 → 1.0.6

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.
@@ -5280,6 +5280,7 @@ class DevSkinSDK {
5280
5280
  this.sessionId = null;
5281
5281
  this.userId = null;
5282
5282
  this.anonymousId = null;
5283
+ this.sessionStartTime = 0;
5283
5284
  this.initialized = false;
5284
5285
  // Collectors
5285
5286
  this.deviceCollector = null;
@@ -5309,12 +5310,12 @@ class DevSkinSDK {
5309
5310
  this.transport = new Transport(this.config);
5310
5311
  // Generate anonymous ID if not exists
5311
5312
  this.anonymousId = this.getOrCreateAnonymousId();
5312
- // Start session
5313
- this.startSession();
5314
- // Initialize collectors
5313
+ // Initialize collectors BEFORE starting session (so getContextData works)
5315
5314
  this.deviceCollector = new DeviceCollector(this.config);
5316
5315
  this.locationCollector = new LocationCollector(this.config);
5317
5316
  this.browserCollector = new BrowserCollector(this.config);
5317
+ // Start session (will now include device/browser/location data)
5318
+ this.startSession();
5318
5319
  if (this.config.captureWebVitals) {
5319
5320
  this.performanceCollector = new PerformanceCollector(this.config, this.transport);
5320
5321
  this.performanceCollector.start();
@@ -5474,6 +5475,7 @@ class DevSkinSDK {
5474
5475
  startSession() {
5475
5476
  var _a;
5476
5477
  this.sessionId = this.generateId();
5478
+ this.sessionStartTime = Date.now();
5477
5479
  const sessionData = Object.assign({ session_id: this.sessionId, user_id: this.userId || undefined, anonymous_id: this.anonymousId, started_at: new Date().toISOString() }, this.getContextData());
5478
5480
  (_a = this.transport) === null || _a === void 0 ? void 0 : _a.startSession(sessionData);
5479
5481
  }
@@ -5513,10 +5515,16 @@ class DevSkinSDK {
5513
5515
  }
5514
5516
  setupUnloadTracking() {
5515
5517
  window.addEventListener('beforeunload', () => {
5516
- var _a;
5518
+ var _a, _b;
5517
5519
  this.track('page_unload');
5520
+ // Send session end update with duration AND context data
5521
+ if (this.sessionId && this.sessionStartTime) {
5522
+ const endedAt = new Date();
5523
+ const durationMs = Date.now() - this.sessionStartTime;
5524
+ (_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()));
5525
+ }
5518
5526
  // Send any pending data
5519
- (_a = this.transport) === null || _a === void 0 ? void 0 : _a.flush();
5527
+ (_b = this.transport) === null || _b === void 0 ? void 0 : _b.flush(true); // Use beacon for reliability
5520
5528
  });
5521
5529
  }
5522
5530
  }