humanbehavior-js 0.2.6 → 0.2.8
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/cjs/index.js +125 -8
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/react/index.js.map +1 -1
- package/dist/esm/index.js +125 -9
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react/index.js.map +1 -1
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +1 -1
- package/dist/types/index.d.ts +36 -4
- package/package.json +1 -1
- package/src/index.ts +3 -0
- package/src/react/index.tsx +5 -0
- package/src/server.ts +51 -0
- package/src/tracker.ts +124 -33
package/dist/cjs/index.js
CHANGED
|
@@ -4960,6 +4960,9 @@ class HumanBehaviorTracker {
|
|
|
4960
4960
|
this.originalReplaceState = null;
|
|
4961
4961
|
this.navigationListeners = [];
|
|
4962
4962
|
this._connectionBlocked = false;
|
|
4963
|
+
this.recordInstance = null;
|
|
4964
|
+
this.frequencyUpdateInterval = null;
|
|
4965
|
+
this.sessionStartTime = Date.now();
|
|
4963
4966
|
if (!apiKey) {
|
|
4964
4967
|
throw new Error('Human Behavior API Key is required');
|
|
4965
4968
|
}
|
|
@@ -5518,8 +5521,33 @@ class HumanBehaviorTracker {
|
|
|
5518
5521
|
}, this.FLUSH_INTERVAL_MS);
|
|
5519
5522
|
// Enable console tracking
|
|
5520
5523
|
this.enableConsoleTracking();
|
|
5521
|
-
//
|
|
5522
|
-
|
|
5524
|
+
// Adaptive snapshot configuration based on session duration
|
|
5525
|
+
const sessionStartTime = Date.now();
|
|
5526
|
+
let snapshotInterval = 5000; // Start with 5 seconds
|
|
5527
|
+
let eventThreshold = 100; // Start with 100 events
|
|
5528
|
+
// Function to update snapshot frequency based on session duration
|
|
5529
|
+
const updateSnapshotFrequency = () => {
|
|
5530
|
+
const sessionDuration = Date.now() - sessionStartTime;
|
|
5531
|
+
const thirtyMinutes = 30 * 60 * 1000;
|
|
5532
|
+
const twoHours = 2 * 60 * 60 * 1000;
|
|
5533
|
+
if (sessionDuration > twoHours) {
|
|
5534
|
+
// After 2 hours, very infrequent snapshots
|
|
5535
|
+
snapshotInterval = 30000; // 30 seconds
|
|
5536
|
+
eventThreshold = 500; // 500 events
|
|
5537
|
+
logDebug('Reduced snapshot frequency: 30s/500 events (2+ hours)');
|
|
5538
|
+
}
|
|
5539
|
+
else if (sessionDuration > thirtyMinutes) {
|
|
5540
|
+
// After 30 minutes, moderate frequency
|
|
5541
|
+
snapshotInterval = 15000; // 15 seconds
|
|
5542
|
+
eventThreshold = 300; // 300 events
|
|
5543
|
+
logDebug('Reduced snapshot frequency: 15s/300 events (30+ minutes)');
|
|
5544
|
+
}
|
|
5545
|
+
// First 30 minutes: 5s/100 events (default)
|
|
5546
|
+
};
|
|
5547
|
+
// Update frequency every 5 minutes
|
|
5548
|
+
const frequencyUpdateInterval = setInterval(updateSnapshotFrequency, 5 * 60 * 1000);
|
|
5549
|
+
// Start recording with adaptive redaction enabled
|
|
5550
|
+
const recordInstance = record({
|
|
5523
5551
|
emit: (event) => {
|
|
5524
5552
|
// Add additional validation for FullSnapshot events
|
|
5525
5553
|
if (event.type === 2) { // FullSnapshot event
|
|
@@ -5546,10 +5574,13 @@ class HumanBehaviorTracker {
|
|
|
5546
5574
|
blockClass: 'rr-block',
|
|
5547
5575
|
ignoreClass: 'rr-ignore',
|
|
5548
5576
|
maskTextClass: 'rr-ignore',
|
|
5549
|
-
//
|
|
5550
|
-
checkoutEveryNms:
|
|
5551
|
-
checkoutEveryNth:
|
|
5577
|
+
// Adaptive configuration
|
|
5578
|
+
checkoutEveryNms: snapshotInterval,
|
|
5579
|
+
checkoutEveryNth: eventThreshold
|
|
5552
5580
|
});
|
|
5581
|
+
// Store the record instance and interval for cleanup
|
|
5582
|
+
this.recordInstance = recordInstance;
|
|
5583
|
+
this.frequencyUpdateInterval = frequencyUpdateInterval;
|
|
5553
5584
|
});
|
|
5554
5585
|
}
|
|
5555
5586
|
stop() {
|
|
@@ -5561,6 +5592,16 @@ class HumanBehaviorTracker {
|
|
|
5561
5592
|
clearInterval(this.flushInterval);
|
|
5562
5593
|
this.flushInterval = null;
|
|
5563
5594
|
}
|
|
5595
|
+
// Cleanup adaptive snapshot intervals
|
|
5596
|
+
if (this.frequencyUpdateInterval) {
|
|
5597
|
+
clearInterval(this.frequencyUpdateInterval);
|
|
5598
|
+
this.frequencyUpdateInterval = null;
|
|
5599
|
+
}
|
|
5600
|
+
// Stop rrweb recording
|
|
5601
|
+
if (this.recordInstance) {
|
|
5602
|
+
this.recordInstance();
|
|
5603
|
+
this.recordInstance = null;
|
|
5604
|
+
}
|
|
5564
5605
|
// Disable console tracking
|
|
5565
5606
|
this.disableConsoleTracking();
|
|
5566
5607
|
// Cleanup navigation tracking
|
|
@@ -5722,8 +5763,9 @@ class HumanBehaviorTracker {
|
|
|
5722
5763
|
}
|
|
5723
5764
|
}
|
|
5724
5765
|
/**
|
|
5725
|
-
*
|
|
5726
|
-
* This
|
|
5766
|
+
* Clear user data and reset session when user signs out of the site
|
|
5767
|
+
* This should be called when a user logs out of your application to prevent
|
|
5768
|
+
* data contamination between different users
|
|
5727
5769
|
*/
|
|
5728
5770
|
logout() {
|
|
5729
5771
|
if (!isBrowser)
|
|
@@ -5738,7 +5780,13 @@ class HumanBehaviorTracker {
|
|
|
5738
5780
|
// Reset user-related properties
|
|
5739
5781
|
this.endUserId = null;
|
|
5740
5782
|
this.userProperties = {};
|
|
5741
|
-
|
|
5783
|
+
// Generate a new session ID for the next user
|
|
5784
|
+
this.sessionId = v1();
|
|
5785
|
+
if (isBrowser) {
|
|
5786
|
+
localStorage.setItem('human_behavior_session_id', this.sessionId);
|
|
5787
|
+
localStorage.setItem('human_behavior_last_activity', Date.now().toString());
|
|
5788
|
+
}
|
|
5789
|
+
logInfo('User logged out - cleared all user data and started fresh session');
|
|
5742
5790
|
}
|
|
5743
5791
|
catch (error) {
|
|
5744
5792
|
logError('Error during logout:', error);
|
|
@@ -5794,6 +5842,33 @@ class HumanBehaviorTracker {
|
|
|
5794
5842
|
getCurrentUrl() {
|
|
5795
5843
|
return this.currentUrl;
|
|
5796
5844
|
}
|
|
5845
|
+
/**
|
|
5846
|
+
* Get current snapshot frequency info
|
|
5847
|
+
*/
|
|
5848
|
+
getSnapshotFrequencyInfo() {
|
|
5849
|
+
const sessionDuration = Date.now() - this.sessionStartTime;
|
|
5850
|
+
const thirtyMinutes = 30 * 60 * 1000;
|
|
5851
|
+
const twoHours = 2 * 60 * 60 * 1000;
|
|
5852
|
+
let phase = 'initial';
|
|
5853
|
+
let interval = 5000;
|
|
5854
|
+
let threshold = 100;
|
|
5855
|
+
if (sessionDuration > twoHours) {
|
|
5856
|
+
phase = 'extended';
|
|
5857
|
+
interval = 30000;
|
|
5858
|
+
threshold = 500;
|
|
5859
|
+
}
|
|
5860
|
+
else if (sessionDuration > thirtyMinutes) {
|
|
5861
|
+
phase = 'moderate';
|
|
5862
|
+
interval = 15000;
|
|
5863
|
+
threshold = 300;
|
|
5864
|
+
}
|
|
5865
|
+
return {
|
|
5866
|
+
sessionDuration,
|
|
5867
|
+
currentInterval: interval,
|
|
5868
|
+
currentThreshold: threshold,
|
|
5869
|
+
phase
|
|
5870
|
+
};
|
|
5871
|
+
}
|
|
5797
5872
|
/**
|
|
5798
5873
|
* Test if the tracker can reach the ingestion server
|
|
5799
5874
|
*/
|
|
@@ -5866,6 +5941,47 @@ if (isBrowser) {
|
|
|
5866
5941
|
window.HumanBehaviorTracker = HumanBehaviorTracker;
|
|
5867
5942
|
}
|
|
5868
5943
|
|
|
5944
|
+
/**
|
|
5945
|
+
* Server-side utilities for HumanBehavior SDK
|
|
5946
|
+
*/
|
|
5947
|
+
/**
|
|
5948
|
+
* Identify user from server-side (NextAuth, Firebase, etc.)
|
|
5949
|
+
* Use this in your auth events to track users immediately on sign-in
|
|
5950
|
+
*/
|
|
5951
|
+
function identifyUser(userData, apiKey) {
|
|
5952
|
+
return __awaiter$1(this, void 0, void 0, function* () {
|
|
5953
|
+
try {
|
|
5954
|
+
const response = yield fetch('https://ingest.humanbehavior.co/api/ingestion/user', {
|
|
5955
|
+
method: 'POST',
|
|
5956
|
+
headers: {
|
|
5957
|
+
'Content-Type': 'application/json',
|
|
5958
|
+
'Authorization': `Bearer ${apiKey}`
|
|
5959
|
+
},
|
|
5960
|
+
body: JSON.stringify({
|
|
5961
|
+
userId: userData.email,
|
|
5962
|
+
userAttributes: userData,
|
|
5963
|
+
sessionId: null, // Will be set by client-side session recording
|
|
5964
|
+
posthogName: userData.email || userData.name || null // Update posthogName with email
|
|
5965
|
+
})
|
|
5966
|
+
});
|
|
5967
|
+
if (!response.ok) {
|
|
5968
|
+
const errorText = yield response.text();
|
|
5969
|
+
return {
|
|
5970
|
+
success: false,
|
|
5971
|
+
error: `Failed to identify user: ${response.status} ${response.statusText} - ${errorText}`
|
|
5972
|
+
};
|
|
5973
|
+
}
|
|
5974
|
+
return { success: true };
|
|
5975
|
+
}
|
|
5976
|
+
catch (error) {
|
|
5977
|
+
return {
|
|
5978
|
+
success: false,
|
|
5979
|
+
error: error instanceof Error ? error.message : 'Unknown error'
|
|
5980
|
+
};
|
|
5981
|
+
}
|
|
5982
|
+
});
|
|
5983
|
+
}
|
|
5984
|
+
|
|
5869
5985
|
/**
|
|
5870
5986
|
* Main entry point for the HumanBehavior SDK
|
|
5871
5987
|
*/
|
|
@@ -5879,6 +5995,7 @@ exports.HumanBehaviorTracker = HumanBehaviorTracker;
|
|
|
5879
5995
|
exports.MAX_CHUNK_SIZE_BYTES = MAX_CHUNK_SIZE_BYTES;
|
|
5880
5996
|
exports.RedactionManager = RedactionManager;
|
|
5881
5997
|
exports.default = HumanBehaviorTracker;
|
|
5998
|
+
exports.identifyUser = identifyUser;
|
|
5882
5999
|
exports.isChunkSizeExceeded = isChunkSizeExceeded;
|
|
5883
6000
|
exports.logDebug = logDebug;
|
|
5884
6001
|
exports.logError = logError;
|