humanbehavior-js 0.1.0 → 0.1.1

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 CHANGED
@@ -4351,6 +4351,7 @@ class HumanBehaviorAPI {
4351
4351
  if (!response.ok) {
4352
4352
  throw new Error(`Failed to authenticate user: ${response.statusText} with API key: ${this.apiKey}`);
4353
4353
  }
4354
+ // Returns: { success: true, message: '...', userId: '...' }
4354
4355
  return yield response.json();
4355
4356
  }
4356
4357
  catch (error) {
@@ -4366,8 +4367,6 @@ class HumanBehaviorAPI {
4366
4367
  data.append('timestamp', encodeURIComponent(Date.now().toString()));
4367
4368
  data.append('apiKey', encodeURIComponent(this.apiKey));
4368
4369
  navigator.sendBeacon(`${this.baseUrl}/api/ingestion/events`, data);
4369
- // KoalawareTracker.logToStorage(`Sending events beacon: ${this.baseUrl}/api/ingestion/events`);
4370
- // KoalawareTracker.logToStorage(`Events beacon success: ${success}`);
4371
4370
  }
4372
4371
  }
4373
4372
 
@@ -4809,7 +4808,7 @@ class HumanBehaviorTracker {
4809
4808
  }
4810
4809
  // Test connection (non-blocking)
4811
4810
  if (isBrowser) {
4812
- const testUrl = tracker.api['baseUrl'] + '/api/ingestion/health';
4811
+ const testUrl = tracker.api['baseUrl'] + '/api/health';
4813
4812
  fetch(testUrl, { method: 'HEAD' })
4814
4813
  .then(() => logDebug('Connection test successful'))
4815
4814
  .catch((error) => {
@@ -4855,19 +4854,29 @@ class HumanBehaviorTracker {
4855
4854
  });
4856
4855
  this.apiKey = apiKey;
4857
4856
  this.redactionManager = new RedactionManager();
4858
- // Handle session restoration
4857
+ // Handle session restoration with improved continuity
4859
4858
  if (isBrowser) {
4860
4859
  const existingSessionId = localStorage.getItem('human_behavior_session_id');
4861
4860
  const lastActivity = localStorage.getItem('human_behavior_last_activity');
4862
4861
  const thirtyMinutesAgo = Date.now() - (30 * 60 * 1000);
4862
+ // Check if we have an existing session that's still within the activity window
4863
4863
  if (existingSessionId && lastActivity && parseInt(lastActivity) > thirtyMinutesAgo) {
4864
4864
  this.sessionId = existingSessionId;
4865
4865
  logDebug(`Reusing existing session: ${this.sessionId}`);
4866
+ // Update activity timestamp to extend the session window
4867
+ localStorage.setItem('human_behavior_last_activity', Date.now().toString());
4866
4868
  }
4867
4869
  else {
4870
+ // Clear old session data if it's expired
4871
+ if (existingSessionId) {
4872
+ logDebug(`Session expired, clearing old session: ${existingSessionId}`);
4873
+ localStorage.removeItem('human_behavior_session_id');
4874
+ localStorage.removeItem('human_behavior_last_activity');
4875
+ }
4868
4876
  this.sessionId = v1();
4869
4877
  logDebug(`Creating new session: ${this.sessionId}`);
4870
4878
  localStorage.setItem('human_behavior_session_id', this.sessionId);
4879
+ localStorage.setItem('human_behavior_last_activity', Date.now().toString());
4871
4880
  }
4872
4881
  this.currentUrl = window.location.href;
4873
4882
  window.__humanBehaviorGlobalTracker = this;
@@ -4884,11 +4893,11 @@ class HumanBehaviorTracker {
4884
4893
  const userId = this.getCookie(`human_behavior_end_user_id_${this.apiKey}`);
4885
4894
  logDebug(`Initializing with sessionId: ${this.sessionId}, userId: ${userId}`);
4886
4895
  const { sessionId, endUserId } = yield this.api.init(this.sessionId, userId);
4887
- // Check if server returned a different session ID
4896
+ // Check if server returned a different session ID (for session continuity)
4888
4897
  if (sessionId !== this.sessionId) {
4889
4898
  logDebug(`Server returned different sessionId: ${sessionId} (client had: ${this.sessionId})`);
4890
4899
  this.sessionId = sessionId;
4891
- // Update localStorage with server's session ID
4900
+ // Update localStorage with server's session ID for continuity
4892
4901
  if (isBrowser) {
4893
4902
  localStorage.setItem('human_behavior_session_id', this.sessionId);
4894
4903
  }
@@ -5235,7 +5244,12 @@ class HumanBehaviorTracker {
5235
5244
  if (!this.userProperties || Object.keys(this.userProperties).length === 0) {
5236
5245
  throw new Error('No user info available. Call addUserInfo() first.');
5237
5246
  }
5238
- yield this.api.sendUserAuth(this.endUserId, this.userProperties, this.sessionId, authFields);
5247
+ const response = yield this.api.sendUserAuth(this.endUserId, this.userProperties, this.sessionId, authFields);
5248
+ if (response && response.userId && response.userId !== this.endUserId) {
5249
+ // Update endUserId and cookie if backend returns a new userId
5250
+ this.endUserId = response.userId;
5251
+ this.setCookie(`human_behavior_end_user_id_${this.apiKey}`, response.userId, 365);
5252
+ }
5239
5253
  });
5240
5254
  }
5241
5255
  start() {
@@ -5360,28 +5374,70 @@ class HumanBehaviorTracker {
5360
5374
  }
5361
5375
  });
5362
5376
  }
5363
- // Add helper methods for cookie management
5377
+ // Add helper methods for cookie management with localStorage fallback
5364
5378
  setCookie(name, value, daysToExpire) {
5365
5379
  if (!isBrowser)
5366
5380
  return;
5367
- const date = new Date();
5368
- date.setTime(date.getTime() + (daysToExpire * 24 * 60 * 60 * 1000));
5369
- const expires = `expires=${date.toUTCString()}`;
5370
- document.cookie = `${name}=${value};${expires};path=/;SameSite=Lax`;
5381
+ try {
5382
+ // Try to set cookie first
5383
+ const date = new Date();
5384
+ date.setTime(date.getTime() + (daysToExpire * 24 * 60 * 60 * 1000));
5385
+ const expires = `expires=${date.toUTCString()}`;
5386
+ document.cookie = `${name}=${value};${expires};path=/;SameSite=Lax`;
5387
+ // Also store in localStorage as backup
5388
+ localStorage.setItem(name, value);
5389
+ logDebug(`Set cookie and localStorage: ${name}`);
5390
+ }
5391
+ catch (error) {
5392
+ // If cookie fails, use localStorage only
5393
+ try {
5394
+ localStorage.setItem(name, value);
5395
+ logDebug(`Cookie blocked, using localStorage: ${name}`);
5396
+ }
5397
+ catch (localStorageError) {
5398
+ logError('Failed to store user ID in both cookie and localStorage:', localStorageError);
5399
+ }
5400
+ }
5371
5401
  }
5372
5402
  getCookie(name) {
5373
5403
  if (!isBrowser)
5374
5404
  return null;
5375
- const nameEQ = name + "=";
5376
- const ca = document.cookie.split(';');
5377
- for (let i = 0; i < ca.length; i++) {
5378
- let c = ca[i];
5379
- while (c.charAt(0) === ' ')
5380
- c = c.substring(1, c.length);
5381
- if (c.indexOf(nameEQ) === 0)
5382
- return c.substring(nameEQ.length, c.length);
5405
+ try {
5406
+ // Try to get from cookie first
5407
+ const nameEQ = name + "=";
5408
+ const ca = document.cookie.split(';');
5409
+ for (let i = 0; i < ca.length; i++) {
5410
+ let c = ca[i];
5411
+ while (c.charAt(0) === ' ')
5412
+ c = c.substring(1, c.length);
5413
+ if (c.indexOf(nameEQ) === 0) {
5414
+ const cookieValue = c.substring(nameEQ.length, c.length);
5415
+ logDebug(`Found cookie: ${name}`);
5416
+ return cookieValue;
5417
+ }
5418
+ }
5419
+ // If cookie not found, try localStorage
5420
+ const localStorageValue = localStorage.getItem(name);
5421
+ if (localStorageValue) {
5422
+ logDebug(`Cookie not found, using localStorage: ${name}`);
5423
+ return localStorageValue;
5424
+ }
5425
+ return null;
5426
+ }
5427
+ catch (error) {
5428
+ // If cookie access fails, try localStorage
5429
+ try {
5430
+ const localStorageValue = localStorage.getItem(name);
5431
+ if (localStorageValue) {
5432
+ logDebug(`Cookie access failed, using localStorage: ${name}`);
5433
+ return localStorageValue;
5434
+ }
5435
+ }
5436
+ catch (localStorageError) {
5437
+ logError('Failed to access both cookie and localStorage:', localStorageError);
5438
+ }
5439
+ return null;
5383
5440
  }
5384
- return null;
5385
5441
  }
5386
5442
  /**
5387
5443
  * Start redaction functionality for sensitive input fields
@@ -5476,6 +5532,29 @@ class HumanBehaviorTracker {
5476
5532
  }
5477
5533
  return { blocked, recommendations };
5478
5534
  }
5535
+ /**
5536
+ * Check if the current user is a preexisting user
5537
+ * Returns true if the user has an existing endUserId cookie from a previous session
5538
+ */
5539
+ isPreexistingUser() {
5540
+ if (!isBrowser) {
5541
+ return false;
5542
+ }
5543
+ // Check if there's an existing endUserId cookie for this API key
5544
+ const existingEndUserId = this.getCookie(`human_behavior_end_user_id_${this.apiKey}`);
5545
+ return existingEndUserId !== null && existingEndUserId !== this.endUserId;
5546
+ }
5547
+ /**
5548
+ * Get user information including whether they are preexisting
5549
+ */
5550
+ getUserInfo() {
5551
+ return {
5552
+ endUserId: this.endUserId,
5553
+ sessionId: this.sessionId,
5554
+ isPreexistingUser: this.isPreexistingUser(),
5555
+ initialized: this.initialized
5556
+ };
5557
+ }
5479
5558
  }
5480
5559
  // Only expose to window object in browser environments
5481
5560
  if (isBrowser) {