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.
package/dist/esm/index.js CHANGED
@@ -13236,6 +13236,8 @@ class HumanBehaviorTracker {
13236
13236
  this._connectionBlocked = false;
13237
13237
  this.recordInstance = null;
13238
13238
  this.sessionStartTime = Date.now();
13239
+ this.rrwebRecord = null;
13240
+ this.fullSnapshotTimeout = null;
13239
13241
  if (!apiKey) {
13240
13242
  throw new Error('Human Behavior API Key is required');
13241
13243
  }
@@ -13343,6 +13345,8 @@ class HumanBehaviorTracker {
13343
13345
  this.originalPushState.apply(history, args);
13344
13346
  // Track navigation event
13345
13347
  this.trackNavigationEvent('pushState', this.previousUrl, this.currentUrl);
13348
+ // Take FullSnapshot on navigation (like PostHog does)
13349
+ this.takeFullSnapshot();
13346
13350
  };
13347
13351
  // Override replaceState to capture programmatic navigation
13348
13352
  history.replaceState = (...args) => {
@@ -13352,12 +13356,16 @@ class HumanBehaviorTracker {
13352
13356
  this.originalReplaceState.apply(history, args);
13353
13357
  // Track navigation event
13354
13358
  this.trackNavigationEvent('replaceState', this.previousUrl, this.currentUrl);
13359
+ // Take FullSnapshot on navigation (like PostHog does)
13360
+ this.takeFullSnapshot();
13355
13361
  };
13356
13362
  // Listen for popstate events (back/forward navigation)
13357
13363
  const popstateListener = () => {
13358
13364
  this.previousUrl = this.currentUrl;
13359
13365
  this.currentUrl = window.location.href;
13360
13366
  this.trackNavigationEvent('popstate', this.previousUrl, this.currentUrl);
13367
+ // Take FullSnapshot on navigation (like PostHog does)
13368
+ this.takeFullSnapshot();
13361
13369
  };
13362
13370
  window.addEventListener('popstate', popstateListener);
13363
13371
  this.navigationListeners.push(() => {
@@ -13785,13 +13793,14 @@ class HumanBehaviorTracker {
13785
13793
  this.flushInterval = window.setInterval(() => {
13786
13794
  this.flush();
13787
13795
  }, this.FLUSH_INTERVAL_MS);
13788
- // Enable console tracking
13789
- this.enableConsoleTracking();
13796
+ // Disable console tracking to reduce event pollution
13797
+ // this.enableConsoleTracking();
13790
13798
  // ✅ DOM READY DETECTION
13791
13799
  // Wait for DOM to be ready before starting recording
13792
13800
  const startRecording = () => {
13793
13801
  logDebug('🎯 DOM ready, starting session recording');
13794
13802
  // ✅ HUMANBEHAVIOR RRWEB CONFIGURATION
13803
+ this.rrwebRecord = record;
13795
13804
  const recordInstance = record({
13796
13805
  emit: (event) => {
13797
13806
  // ✅ DIRECT EVENT HANDLING - Let rrweb handle events natively
@@ -13813,12 +13822,12 @@ class HumanBehaviorTracker {
13813
13822
  recordCrossOriginIframes: false, // HumanBehavior default
13814
13823
  // ✅ CANVAS RECORDING - Disabled to prevent large data URIs
13815
13824
  recordCanvas: false, // Disabled to prevent large data URIs
13816
- // ✅ FULLSNAPSHOT GENERATION - Use reasonable intervals
13817
- // checkoutEveryNms: 300000, // Take FullSnapshot every 5 minutes
13818
- // checkoutEveryNth: 1000, // Take FullSnapshot every 1000 events
13825
+ // ✅ FULLSNAPSHOT GENERATION - Following PostHog's approach (no periodic snapshots)
13826
+ // PostHog doesn't use checkoutEveryNms or checkoutEveryNth to avoid animation issues
13827
+ // They rely on initial FullSnapshot + navigation-triggered ones only
13819
13828
  });
13820
- // Store the record instance for cleanup
13821
- this.recordInstance = recordInstance;
13829
+ // Store the record instance for cleanup
13830
+ this.recordInstance = recordInstance || null;
13822
13831
  };
13823
13832
  // ✅ DOM READY DETECTION
13824
13833
  logDebug(`🎯 DOM ready state: ${document.readyState}`);
@@ -13837,6 +13846,37 @@ class HumanBehaviorTracker {
13837
13846
  }
13838
13847
  });
13839
13848
  }
13849
+ /**
13850
+ * Manually trigger a FullSnapshot (for navigation events)
13851
+ * Delays snapshot to avoid capturing mid-animation states
13852
+ */
13853
+ takeFullSnapshot() {
13854
+ // Clear any existing timeout to avoid multiple snapshots
13855
+ if (this.fullSnapshotTimeout) {
13856
+ clearTimeout(this.fullSnapshotTimeout);
13857
+ }
13858
+ // Delay FullSnapshot to let animations settle
13859
+ this.fullSnapshotTimeout = window.setTimeout(() => {
13860
+ try {
13861
+ // Wait for any pending animations/transitions to complete
13862
+ requestAnimationFrame(() => {
13863
+ requestAnimationFrame(() => {
13864
+ // Access takeFullSnapshot from the rrweb record function
13865
+ if (this.rrwebRecord && typeof this.rrwebRecord.takeFullSnapshot === 'function') {
13866
+ this.rrwebRecord.takeFullSnapshot();
13867
+ logDebug('✅ FullSnapshot taken for navigation (delayed for animations)');
13868
+ }
13869
+ else {
13870
+ logWarn('⚠️ takeFullSnapshot not available on record function');
13871
+ }
13872
+ });
13873
+ });
13874
+ }
13875
+ catch (error) {
13876
+ logError('❌ Failed to take FullSnapshot for navigation:', error);
13877
+ }
13878
+ }, 1000); // Wait 1 second for animations to settle
13879
+ }
13840
13880
  stop() {
13841
13881
  return __awaiter(this, void 0, void 0, function* () {
13842
13882
  yield this.ensureInitialized();
@@ -13851,6 +13891,12 @@ class HumanBehaviorTracker {
13851
13891
  this.recordInstance();
13852
13892
  this.recordInstance = null;
13853
13893
  }
13894
+ // Clear any pending FullSnapshot timeouts
13895
+ if (this.fullSnapshotTimeout) {
13896
+ clearTimeout(this.fullSnapshotTimeout);
13897
+ this.fullSnapshotTimeout = null;
13898
+ }
13899
+ this.rrwebRecord = null;
13854
13900
  // Disable console tracking
13855
13901
  this.disableConsoleTracking();
13856
13902
  // Cleanup navigation tracking