humanbehavior-js 0.4.6 → 0.4.7

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.
@@ -13228,6 +13228,8 @@ class HumanBehaviorTracker {
13228
13228
  this._connectionBlocked = false;
13229
13229
  this.recordInstance = null;
13230
13230
  this.sessionStartTime = Date.now();
13231
+ this.rrwebRecord = null;
13232
+ this.fullSnapshotTimeout = null;
13231
13233
  if (!apiKey) {
13232
13234
  throw new Error('Human Behavior API Key is required');
13233
13235
  }
@@ -13335,6 +13337,8 @@ class HumanBehaviorTracker {
13335
13337
  this.originalPushState.apply(history, args);
13336
13338
  // Track navigation event
13337
13339
  this.trackNavigationEvent('pushState', this.previousUrl, this.currentUrl);
13340
+ // Take FullSnapshot on navigation (like PostHog does)
13341
+ this.takeFullSnapshot();
13338
13342
  };
13339
13343
  // Override replaceState to capture programmatic navigation
13340
13344
  history.replaceState = (...args) => {
@@ -13344,12 +13348,16 @@ class HumanBehaviorTracker {
13344
13348
  this.originalReplaceState.apply(history, args);
13345
13349
  // Track navigation event
13346
13350
  this.trackNavigationEvent('replaceState', this.previousUrl, this.currentUrl);
13351
+ // Take FullSnapshot on navigation (like PostHog does)
13352
+ this.takeFullSnapshot();
13347
13353
  };
13348
13354
  // Listen for popstate events (back/forward navigation)
13349
13355
  const popstateListener = () => {
13350
13356
  this.previousUrl = this.currentUrl;
13351
13357
  this.currentUrl = window.location.href;
13352
13358
  this.trackNavigationEvent('popstate', this.previousUrl, this.currentUrl);
13359
+ // Take FullSnapshot on navigation (like PostHog does)
13360
+ this.takeFullSnapshot();
13353
13361
  };
13354
13362
  window.addEventListener('popstate', popstateListener);
13355
13363
  this.navigationListeners.push(() => {
@@ -13777,13 +13785,14 @@ class HumanBehaviorTracker {
13777
13785
  this.flushInterval = window.setInterval(() => {
13778
13786
  this.flush();
13779
13787
  }, this.FLUSH_INTERVAL_MS);
13780
- // Enable console tracking
13781
- this.enableConsoleTracking();
13788
+ // Disable console tracking to reduce event pollution
13789
+ // this.enableConsoleTracking();
13782
13790
  // ✅ DOM READY DETECTION
13783
13791
  // Wait for DOM to be ready before starting recording
13784
13792
  const startRecording = () => {
13785
13793
  logDebug('🎯 DOM ready, starting session recording');
13786
13794
  // ✅ HUMANBEHAVIOR RRWEB CONFIGURATION
13795
+ this.rrwebRecord = record;
13787
13796
  const recordInstance = record({
13788
13797
  emit: (event) => {
13789
13798
  // ✅ DIRECT EVENT HANDLING - Let rrweb handle events natively
@@ -13805,12 +13814,12 @@ class HumanBehaviorTracker {
13805
13814
  recordCrossOriginIframes: false, // HumanBehavior default
13806
13815
  // ✅ CANVAS RECORDING - Disabled to prevent large data URIs
13807
13816
  recordCanvas: false, // Disabled to prevent large data URIs
13808
- // ✅ FULLSNAPSHOT GENERATION - Use reasonable intervals
13809
- // checkoutEveryNms: 300000, // Take FullSnapshot every 5 minutes
13810
- // checkoutEveryNth: 1000, // Take FullSnapshot every 1000 events
13817
+ // ✅ FULLSNAPSHOT GENERATION - Following PostHog's approach (no periodic snapshots)
13818
+ // PostHog doesn't use checkoutEveryNms or checkoutEveryNth to avoid animation issues
13819
+ // They rely on initial FullSnapshot + navigation-triggered ones only
13811
13820
  });
13812
- // Store the record instance for cleanup
13813
- this.recordInstance = recordInstance;
13821
+ // Store the record instance for cleanup
13822
+ this.recordInstance = recordInstance || null;
13814
13823
  };
13815
13824
  // ✅ DOM READY DETECTION
13816
13825
  logDebug(`🎯 DOM ready state: ${document.readyState}`);
@@ -13829,6 +13838,37 @@ class HumanBehaviorTracker {
13829
13838
  }
13830
13839
  });
13831
13840
  }
13841
+ /**
13842
+ * Manually trigger a FullSnapshot (for navigation events)
13843
+ * Delays snapshot to avoid capturing mid-animation states
13844
+ */
13845
+ takeFullSnapshot() {
13846
+ // Clear any existing timeout to avoid multiple snapshots
13847
+ if (this.fullSnapshotTimeout) {
13848
+ clearTimeout(this.fullSnapshotTimeout);
13849
+ }
13850
+ // Delay FullSnapshot to let animations settle
13851
+ this.fullSnapshotTimeout = window.setTimeout(() => {
13852
+ try {
13853
+ // Wait for any pending animations/transitions to complete
13854
+ requestAnimationFrame(() => {
13855
+ requestAnimationFrame(() => {
13856
+ // Access takeFullSnapshot from the rrweb record function
13857
+ if (this.rrwebRecord && typeof this.rrwebRecord.takeFullSnapshot === 'function') {
13858
+ this.rrwebRecord.takeFullSnapshot();
13859
+ logDebug('✅ FullSnapshot taken for navigation (delayed for animations)');
13860
+ }
13861
+ else {
13862
+ logWarn('⚠️ takeFullSnapshot not available on record function');
13863
+ }
13864
+ });
13865
+ });
13866
+ }
13867
+ catch (error) {
13868
+ logError('❌ Failed to take FullSnapshot for navigation:', error);
13869
+ }
13870
+ }, 1000); // Wait 1 second for animations to settle
13871
+ }
13832
13872
  stop() {
13833
13873
  return __awaiter(this, void 0, void 0, function* () {
13834
13874
  yield this.ensureInitialized();
@@ -13843,6 +13883,12 @@ class HumanBehaviorTracker {
13843
13883
  this.recordInstance();
13844
13884
  this.recordInstance = null;
13845
13885
  }
13886
+ // Clear any pending FullSnapshot timeouts
13887
+ if (this.fullSnapshotTimeout) {
13888
+ clearTimeout(this.fullSnapshotTimeout);
13889
+ this.fullSnapshotTimeout = null;
13890
+ }
13891
+ this.rrwebRecord = null;
13846
13892
  // Disable console tracking
13847
13893
  this.disableConsoleTracking();
13848
13894
  // Cleanup navigation tracking