humanbehavior-js 0.0.7 → 0.0.9
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 +277 -47
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/react/index.js +188 -51
- package/dist/cjs/react/index.js.map +1 -1
- package/dist/esm/index.js +273 -48
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react/index.js +189 -52
- 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 +60 -3
- package/package.json +2 -2
- package/readme.md +116 -28
- package/src/api.ts +22 -8
- package/src/index.ts +3 -0
- package/src/react/index.tsx +85 -62
- package/src/redact.ts +19 -17
- package/src/tracker.ts +157 -26
- package/src/utils/logger.ts +144 -0
package/dist/cjs/index.js
CHANGED
|
@@ -4053,6 +4053,123 @@ function v1Bytes(rnds, msecs, nsecs, clockseq, node, buf, offset = 0) {
|
|
|
4053
4053
|
return buf;
|
|
4054
4054
|
}
|
|
4055
4055
|
|
|
4056
|
+
exports.LogLevel = void 0;
|
|
4057
|
+
(function (LogLevel) {
|
|
4058
|
+
LogLevel[LogLevel["NONE"] = 0] = "NONE";
|
|
4059
|
+
LogLevel[LogLevel["ERROR"] = 1] = "ERROR";
|
|
4060
|
+
LogLevel[LogLevel["WARN"] = 2] = "WARN";
|
|
4061
|
+
LogLevel[LogLevel["INFO"] = 3] = "INFO";
|
|
4062
|
+
LogLevel[LogLevel["DEBUG"] = 4] = "DEBUG";
|
|
4063
|
+
})(exports.LogLevel || (exports.LogLevel = {}));
|
|
4064
|
+
class Logger {
|
|
4065
|
+
constructor(config) {
|
|
4066
|
+
this.config = {
|
|
4067
|
+
level: exports.LogLevel.ERROR, // Default to only errors in production
|
|
4068
|
+
enableConsole: true,
|
|
4069
|
+
enableStorage: false
|
|
4070
|
+
};
|
|
4071
|
+
this.isBrowser = typeof window !== 'undefined';
|
|
4072
|
+
if (config) {
|
|
4073
|
+
this.config = Object.assign(Object.assign({}, this.config), config);
|
|
4074
|
+
}
|
|
4075
|
+
}
|
|
4076
|
+
setConfig(config) {
|
|
4077
|
+
this.config = Object.assign(Object.assign({}, this.config), config);
|
|
4078
|
+
}
|
|
4079
|
+
shouldLog(level) {
|
|
4080
|
+
return level <= this.config.level;
|
|
4081
|
+
}
|
|
4082
|
+
formatMessage(level, message, ...args) {
|
|
4083
|
+
const timestamp = new Date().toISOString();
|
|
4084
|
+
return `[HumanBehavior ${level}] ${timestamp}: ${message}`;
|
|
4085
|
+
}
|
|
4086
|
+
error(message, ...args) {
|
|
4087
|
+
if (!this.shouldLog(exports.LogLevel.ERROR))
|
|
4088
|
+
return;
|
|
4089
|
+
const formattedMessage = this.formatMessage('ERROR', message);
|
|
4090
|
+
if (this.config.enableConsole) {
|
|
4091
|
+
console.error(formattedMessage, ...args);
|
|
4092
|
+
}
|
|
4093
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
4094
|
+
this.logToStorage(formattedMessage, args);
|
|
4095
|
+
}
|
|
4096
|
+
}
|
|
4097
|
+
warn(message, ...args) {
|
|
4098
|
+
if (!this.shouldLog(exports.LogLevel.WARN))
|
|
4099
|
+
return;
|
|
4100
|
+
const formattedMessage = this.formatMessage('WARN', message);
|
|
4101
|
+
if (this.config.enableConsole) {
|
|
4102
|
+
console.warn(formattedMessage, ...args);
|
|
4103
|
+
}
|
|
4104
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
4105
|
+
this.logToStorage(formattedMessage, args);
|
|
4106
|
+
}
|
|
4107
|
+
}
|
|
4108
|
+
info(message, ...args) {
|
|
4109
|
+
if (!this.shouldLog(exports.LogLevel.INFO))
|
|
4110
|
+
return;
|
|
4111
|
+
const formattedMessage = this.formatMessage('INFO', message);
|
|
4112
|
+
if (this.config.enableConsole) {
|
|
4113
|
+
console.log(formattedMessage, ...args);
|
|
4114
|
+
}
|
|
4115
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
4116
|
+
this.logToStorage(formattedMessage, args);
|
|
4117
|
+
}
|
|
4118
|
+
}
|
|
4119
|
+
debug(message, ...args) {
|
|
4120
|
+
if (!this.shouldLog(exports.LogLevel.DEBUG))
|
|
4121
|
+
return;
|
|
4122
|
+
const formattedMessage = this.formatMessage('DEBUG', message);
|
|
4123
|
+
if (this.config.enableConsole) {
|
|
4124
|
+
console.log(formattedMessage, ...args);
|
|
4125
|
+
}
|
|
4126
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
4127
|
+
this.logToStorage(formattedMessage, args);
|
|
4128
|
+
}
|
|
4129
|
+
}
|
|
4130
|
+
logToStorage(message, args) {
|
|
4131
|
+
try {
|
|
4132
|
+
const logs = JSON.parse(localStorage.getItem('human_behavior_logs') || '[]');
|
|
4133
|
+
const logEntry = {
|
|
4134
|
+
message,
|
|
4135
|
+
args: args.length > 0 ? args : undefined,
|
|
4136
|
+
timestamp: Date.now()
|
|
4137
|
+
};
|
|
4138
|
+
logs.push(logEntry);
|
|
4139
|
+
// Keep only last 1000 logs to prevent storage bloat
|
|
4140
|
+
if (logs.length > 1000) {
|
|
4141
|
+
logs.splice(0, logs.length - 1000);
|
|
4142
|
+
}
|
|
4143
|
+
localStorage.setItem('human_behavior_logs', JSON.stringify(logs));
|
|
4144
|
+
}
|
|
4145
|
+
catch (e) {
|
|
4146
|
+
// Silently fail if storage is not available
|
|
4147
|
+
}
|
|
4148
|
+
}
|
|
4149
|
+
getLogs() {
|
|
4150
|
+
if (!this.isBrowser)
|
|
4151
|
+
return [];
|
|
4152
|
+
try {
|
|
4153
|
+
return JSON.parse(localStorage.getItem('human_behavior_logs') || '[]');
|
|
4154
|
+
}
|
|
4155
|
+
catch (e) {
|
|
4156
|
+
return [];
|
|
4157
|
+
}
|
|
4158
|
+
}
|
|
4159
|
+
clearLogs() {
|
|
4160
|
+
if (this.isBrowser) {
|
|
4161
|
+
localStorage.removeItem('human_behavior_logs');
|
|
4162
|
+
}
|
|
4163
|
+
}
|
|
4164
|
+
}
|
|
4165
|
+
// Create singleton instance
|
|
4166
|
+
const logger = new Logger();
|
|
4167
|
+
// Export convenience methods
|
|
4168
|
+
const logError = (message, ...args) => logger.error(message, ...args);
|
|
4169
|
+
const logWarn = (message, ...args) => logger.warn(message, ...args);
|
|
4170
|
+
const logInfo = (message, ...args) => logger.info(message, ...args);
|
|
4171
|
+
const logDebug = (message, ...args) => logger.debug(message, ...args);
|
|
4172
|
+
|
|
4056
4173
|
const MAX_CHUNK_SIZE_BYTES = 1024 * 1024 * 10; // 10MB chunk size
|
|
4057
4174
|
function isChunkSizeExceeded(currentChunk, newEvent, sessionId) {
|
|
4058
4175
|
const nextChunkSize = new TextEncoder().encode(JSON.stringify({
|
|
@@ -4077,15 +4194,25 @@ class HumanBehaviorAPI {
|
|
|
4077
4194
|
}
|
|
4078
4195
|
init(sessionId, userId) {
|
|
4079
4196
|
return __awaiter$1(this, void 0, void 0, function* () {
|
|
4197
|
+
// Get current page URL and referrer if in browser environment
|
|
4198
|
+
let entryURL = null;
|
|
4199
|
+
let referrer = null;
|
|
4200
|
+
if (typeof window !== 'undefined') {
|
|
4201
|
+
entryURL = window.location.href;
|
|
4202
|
+
referrer = document.referrer;
|
|
4203
|
+
}
|
|
4080
4204
|
const response = yield fetch(`${this.baseUrl}/api/ingestion/init`, {
|
|
4081
4205
|
method: 'POST',
|
|
4082
4206
|
headers: {
|
|
4083
4207
|
'Content-Type': 'application/json',
|
|
4084
|
-
'Authorization': `Bearer ${this.apiKey}
|
|
4208
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
4209
|
+
'Referer': referrer || ''
|
|
4085
4210
|
},
|
|
4086
4211
|
body: JSON.stringify({
|
|
4087
4212
|
sessionId: sessionId,
|
|
4088
|
-
endUserId: userId
|
|
4213
|
+
endUserId: userId,
|
|
4214
|
+
entryURL: entryURL,
|
|
4215
|
+
referrer: referrer
|
|
4089
4216
|
})
|
|
4090
4217
|
});
|
|
4091
4218
|
if (!response.ok) {
|
|
@@ -4174,7 +4301,7 @@ class HumanBehaviorAPI {
|
|
|
4174
4301
|
return results.flat();
|
|
4175
4302
|
}
|
|
4176
4303
|
catch (error) {
|
|
4177
|
-
|
|
4304
|
+
logError('Error sending events:', error);
|
|
4178
4305
|
throw error;
|
|
4179
4306
|
}
|
|
4180
4307
|
});
|
|
@@ -4200,7 +4327,7 @@ class HumanBehaviorAPI {
|
|
|
4200
4327
|
return yield response.json();
|
|
4201
4328
|
}
|
|
4202
4329
|
catch (error) {
|
|
4203
|
-
|
|
4330
|
+
logError('Error sending user data:', error);
|
|
4204
4331
|
throw error;
|
|
4205
4332
|
}
|
|
4206
4333
|
});
|
|
@@ -4227,7 +4354,7 @@ class HumanBehaviorAPI {
|
|
|
4227
4354
|
return yield response.json();
|
|
4228
4355
|
}
|
|
4229
4356
|
catch (error) {
|
|
4230
|
-
|
|
4357
|
+
logError('Error authenticating user:', error);
|
|
4231
4358
|
throw error;
|
|
4232
4359
|
}
|
|
4233
4360
|
});
|
|
@@ -4274,7 +4401,7 @@ class HumanBehaviorAPI {
|
|
|
4274
4401
|
catch (error) {
|
|
4275
4402
|
retryCount++;
|
|
4276
4403
|
if (retryCount === maxRetries) {
|
|
4277
|
-
|
|
4404
|
+
logError('Error sending custom event after max retries:', error);
|
|
4278
4405
|
throw error;
|
|
4279
4406
|
}
|
|
4280
4407
|
// Exponential backoff
|
|
@@ -4307,7 +4434,7 @@ class HumanBehaviorAPI {
|
|
|
4307
4434
|
catch (error) {
|
|
4308
4435
|
retryCount++;
|
|
4309
4436
|
if (retryCount === maxRetries) {
|
|
4310
|
-
|
|
4437
|
+
logError('Error sending custom events after max retries:', error);
|
|
4311
4438
|
throw error;
|
|
4312
4439
|
}
|
|
4313
4440
|
// Exponential backoff
|
|
@@ -4323,7 +4450,7 @@ class HumanBehaviorAPI {
|
|
|
4323
4450
|
data.append('timestamp', encodeURIComponent(Date.now().toString()));
|
|
4324
4451
|
data.append('apiKey', encodeURIComponent(this.apiKey));
|
|
4325
4452
|
if (isSessionComplete) {
|
|
4326
|
-
|
|
4453
|
+
logInfo('Session complete beacon sending');
|
|
4327
4454
|
localStorage.setItem('koalaware_session_complete', Date.now().toString());
|
|
4328
4455
|
data.append('sessionComplete', encodeURIComponent('true'));
|
|
4329
4456
|
}
|
|
@@ -4387,18 +4514,18 @@ class RedactionManager {
|
|
|
4387
4514
|
this.userSelectedFields.clear();
|
|
4388
4515
|
fields.forEach(field => this.userSelectedFields.add(field));
|
|
4389
4516
|
if (fields.length > 0) {
|
|
4390
|
-
|
|
4517
|
+
logDebug(`Redaction: Active for ${fields.length} field(s):`, fields);
|
|
4391
4518
|
// Debug: Check if elements exist
|
|
4392
4519
|
fields.forEach(selector => {
|
|
4393
4520
|
const elements = document.querySelectorAll(selector);
|
|
4394
|
-
|
|
4521
|
+
logDebug(`Redaction: Found ${elements.length} element(s) for selector '${selector}'`);
|
|
4395
4522
|
elements.forEach((el, index) => {
|
|
4396
|
-
|
|
4523
|
+
logDebug(`Redaction: Element ${index} for '${selector}':`, el);
|
|
4397
4524
|
});
|
|
4398
4525
|
});
|
|
4399
4526
|
}
|
|
4400
4527
|
else {
|
|
4401
|
-
|
|
4528
|
+
logDebug('Redaction: Disabled - no fields selected');
|
|
4402
4529
|
}
|
|
4403
4530
|
}
|
|
4404
4531
|
/**
|
|
@@ -4428,7 +4555,7 @@ class RedactionManager {
|
|
|
4428
4555
|
if (processedEvent.data.source === 5) { // Input event
|
|
4429
4556
|
const shouldRedact = this.isFieldSelected(processedEvent.data);
|
|
4430
4557
|
if (shouldRedact) {
|
|
4431
|
-
|
|
4558
|
+
logDebug('Redaction: Processing input event for redaction');
|
|
4432
4559
|
this.redactInputEvent(processedEvent.data);
|
|
4433
4560
|
}
|
|
4434
4561
|
}
|
|
@@ -4454,30 +4581,30 @@ class RedactionManager {
|
|
|
4454
4581
|
if (!this.isFieldSelected(inputData)) {
|
|
4455
4582
|
return;
|
|
4456
4583
|
}
|
|
4457
|
-
|
|
4584
|
+
logDebug('Redaction: Redacting input event with text:', inputData.text);
|
|
4458
4585
|
// Redact all text-related properties that could contain input data
|
|
4459
4586
|
const textProperties = ['text', 'value', 'content', 'data', 'input', 'textContent'];
|
|
4460
4587
|
textProperties.forEach(prop => {
|
|
4461
4588
|
if (inputData[prop] !== undefined && typeof inputData[prop] === 'string') {
|
|
4462
4589
|
inputData[prop] = this.redactedText;
|
|
4463
|
-
|
|
4590
|
+
logDebug(`Redaction: Redacted property '${prop}'`);
|
|
4464
4591
|
}
|
|
4465
4592
|
});
|
|
4466
4593
|
// Also check for any other string properties that might contain input data
|
|
4467
4594
|
Object.keys(inputData).forEach(key => {
|
|
4468
4595
|
if (typeof inputData[key] === 'string' && inputData[key].length > 0) {
|
|
4469
4596
|
inputData[key] = this.redactedText;
|
|
4470
|
-
|
|
4597
|
+
logDebug(`Redaction: Redacted additional property '${key}'`);
|
|
4471
4598
|
}
|
|
4472
4599
|
});
|
|
4473
4600
|
// Handle nested objects that might contain text data
|
|
4474
4601
|
if (inputData.attributes && typeof inputData.attributes === 'object') {
|
|
4475
4602
|
if (inputData.attributes.value && typeof inputData.attributes.value === 'string') {
|
|
4476
4603
|
inputData.attributes.value = this.redactedText;
|
|
4477
|
-
|
|
4604
|
+
logDebug('Redaction: Redacted nested value attribute');
|
|
4478
4605
|
}
|
|
4479
4606
|
}
|
|
4480
|
-
|
|
4607
|
+
logDebug('Redaction: Input event redaction complete');
|
|
4481
4608
|
}
|
|
4482
4609
|
/**
|
|
4483
4610
|
* Redact sensitive data in DOM mutation events
|
|
@@ -4539,7 +4666,7 @@ class RedactionManager {
|
|
|
4539
4666
|
return false;
|
|
4540
4667
|
}
|
|
4541
4668
|
catch (e) {
|
|
4542
|
-
|
|
4669
|
+
logWarn('Error checking if DOM change should be redacted:', e);
|
|
4543
4670
|
return false;
|
|
4544
4671
|
}
|
|
4545
4672
|
}
|
|
@@ -4686,7 +4813,7 @@ class RedactionManager {
|
|
|
4686
4813
|
// Check if any of these elements are currently focused
|
|
4687
4814
|
for (const el of matchingElements) {
|
|
4688
4815
|
if (el === document.activeElement) {
|
|
4689
|
-
|
|
4816
|
+
logDebug('Redaction: Found focused element matching selector:', selector);
|
|
4690
4817
|
return true;
|
|
4691
4818
|
}
|
|
4692
4819
|
}
|
|
@@ -4696,7 +4823,7 @@ class RedactionManager {
|
|
|
4696
4823
|
// Look for any input element that might be the active one
|
|
4697
4824
|
const activeElement = document.activeElement;
|
|
4698
4825
|
if (activeElement && this.shouldRedactElement(activeElement)) {
|
|
4699
|
-
|
|
4826
|
+
logDebug('Redaction: Active element should be redacted');
|
|
4700
4827
|
return true;
|
|
4701
4828
|
}
|
|
4702
4829
|
return false;
|
|
@@ -4729,7 +4856,7 @@ class RedactionManager {
|
|
|
4729
4856
|
return false;
|
|
4730
4857
|
}
|
|
4731
4858
|
catch (e) {
|
|
4732
|
-
|
|
4859
|
+
logWarn('Error checking if field should be redacted:', e);
|
|
4733
4860
|
return false;
|
|
4734
4861
|
}
|
|
4735
4862
|
}
|
|
@@ -4785,15 +4912,26 @@ class HumanBehaviorTracker {
|
|
|
4785
4912
|
this.endUserId = null;
|
|
4786
4913
|
this.initialized = false;
|
|
4787
4914
|
this.initializationPromise = null;
|
|
4915
|
+
// Console tracking properties
|
|
4916
|
+
this.originalConsole = null;
|
|
4917
|
+
this.originalLogger = null;
|
|
4918
|
+
this.consoleTrackingEnabled = false;
|
|
4788
4919
|
if (!apiKey) {
|
|
4789
4920
|
throw new Error('Human Behavior API Key is required');
|
|
4790
4921
|
}
|
|
4791
|
-
|
|
4792
|
-
|
|
4793
|
-
|
|
4922
|
+
// ========================================
|
|
4923
|
+
// DEVELOPER: Choose your ingestion server
|
|
4924
|
+
// ========================================
|
|
4925
|
+
// Uncomment ONE of the following lines to select your server:
|
|
4926
|
+
// AWS Development Server
|
|
4927
|
+
const defaultIngestionUrl = 'http://3.137.217.33:3000';
|
|
4928
|
+
// Vercel Production Server
|
|
4929
|
+
// const defaultIngestionUrl = 'https://ingestion-server.vercel.app';
|
|
4930
|
+
// Local Development Server
|
|
4931
|
+
// const defaultIngestionUrl = 'http://localhost:3000';
|
|
4794
4932
|
this.api = new HumanBehaviorAPI({
|
|
4795
4933
|
apiKey: apiKey,
|
|
4796
|
-
ingestionUrl: ingestionUrl
|
|
4934
|
+
ingestionUrl: ingestionUrl || defaultIngestionUrl
|
|
4797
4935
|
});
|
|
4798
4936
|
this.apiKey = apiKey;
|
|
4799
4937
|
this.redactionManager = new RedactionManager();
|
|
@@ -4826,13 +4964,13 @@ class HumanBehaviorTracker {
|
|
|
4826
4964
|
this.processRejectedEvents();
|
|
4827
4965
|
}
|
|
4828
4966
|
else {
|
|
4829
|
-
|
|
4967
|
+
logWarn('HumanBehaviorTracker initialized in a non-browser environment. Session tracking is disabled.');
|
|
4830
4968
|
}
|
|
4831
4969
|
this.initialized = true;
|
|
4832
|
-
|
|
4970
|
+
logInfo('HumanBehaviorTracker initialized');
|
|
4833
4971
|
}
|
|
4834
4972
|
catch (error) {
|
|
4835
|
-
|
|
4973
|
+
logError('Failed to initialize HumanBehaviorTracker:', error);
|
|
4836
4974
|
throw error;
|
|
4837
4975
|
}
|
|
4838
4976
|
});
|
|
@@ -4846,24 +4984,107 @@ class HumanBehaviorTracker {
|
|
|
4846
4984
|
});
|
|
4847
4985
|
}
|
|
4848
4986
|
static logToStorage(message) {
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4987
|
+
logInfo(message);
|
|
4988
|
+
}
|
|
4989
|
+
/**
|
|
4990
|
+
* Configure logging behavior for the SDK
|
|
4991
|
+
* @param config Logger configuration options
|
|
4992
|
+
*/
|
|
4993
|
+
static configureLogging(config) {
|
|
4994
|
+
const levelMap = {
|
|
4995
|
+
'none': 0,
|
|
4996
|
+
'error': 1,
|
|
4997
|
+
'warn': 2,
|
|
4998
|
+
'info': 3,
|
|
4999
|
+
'debug': 4
|
|
5000
|
+
};
|
|
5001
|
+
logger.setConfig({
|
|
5002
|
+
level: levelMap[config.level || 'error'],
|
|
5003
|
+
enableConsole: config.enableConsole !== false,
|
|
5004
|
+
enableStorage: config.enableStorage || false
|
|
5005
|
+
});
|
|
5006
|
+
}
|
|
5007
|
+
/**
|
|
5008
|
+
* Enable console event tracking
|
|
5009
|
+
*/
|
|
5010
|
+
enableConsoleTracking() {
|
|
5011
|
+
if (!isBrowser || this.consoleTrackingEnabled)
|
|
5012
|
+
return;
|
|
5013
|
+
// Store original console methods
|
|
5014
|
+
this.originalConsole = {
|
|
5015
|
+
log: console.log,
|
|
5016
|
+
warn: console.warn,
|
|
5017
|
+
error: console.error
|
|
5018
|
+
};
|
|
5019
|
+
// Store original logger methods
|
|
5020
|
+
this.originalLogger = {
|
|
5021
|
+
error: logError,
|
|
5022
|
+
warn: logWarn,
|
|
5023
|
+
info: logInfo,
|
|
5024
|
+
debug: logDebug
|
|
5025
|
+
};
|
|
5026
|
+
// Override console methods to capture ALL console output (including logger output)
|
|
5027
|
+
console.log = (...args) => {
|
|
5028
|
+
this.trackConsoleEvent('log', args);
|
|
5029
|
+
this.originalConsole.log(...args);
|
|
5030
|
+
};
|
|
5031
|
+
console.warn = (...args) => {
|
|
5032
|
+
this.trackConsoleEvent('warn', args);
|
|
5033
|
+
this.originalConsole.warn(...args);
|
|
5034
|
+
};
|
|
5035
|
+
console.error = (...args) => {
|
|
5036
|
+
this.trackConsoleEvent('error', args);
|
|
5037
|
+
this.originalConsole.error(...args);
|
|
5038
|
+
};
|
|
5039
|
+
this.consoleTrackingEnabled = true;
|
|
5040
|
+
this.originalLogger.debug('Console tracking enabled');
|
|
5041
|
+
}
|
|
5042
|
+
/**
|
|
5043
|
+
* Disable console event tracking
|
|
5044
|
+
*/
|
|
5045
|
+
disableConsoleTracking() {
|
|
5046
|
+
if (!isBrowser || !this.consoleTrackingEnabled || !this.originalConsole)
|
|
5047
|
+
return;
|
|
5048
|
+
// Restore original console methods
|
|
5049
|
+
console.log = this.originalConsole.log;
|
|
5050
|
+
console.warn = this.originalConsole.warn;
|
|
5051
|
+
console.error = this.originalConsole.error;
|
|
5052
|
+
this.consoleTrackingEnabled = false;
|
|
5053
|
+
this.originalConsole = null;
|
|
5054
|
+
this.originalLogger = null;
|
|
5055
|
+
}
|
|
5056
|
+
/**
|
|
5057
|
+
* Track console events
|
|
5058
|
+
*/
|
|
5059
|
+
trackConsoleEvent(level, args) {
|
|
5060
|
+
if (!this.initialized)
|
|
5061
|
+
return;
|
|
5062
|
+
const consoleEvent = {
|
|
5063
|
+
type: 5, // Custom event type
|
|
5064
|
+
data: {
|
|
5065
|
+
payload: {
|
|
5066
|
+
type: 'console',
|
|
5067
|
+
level: level,
|
|
5068
|
+
message: args.map(arg => typeof arg === 'string' ? arg :
|
|
5069
|
+
typeof arg === 'object' ? JSON.stringify(arg) :
|
|
5070
|
+
String(arg)).join(' '),
|
|
5071
|
+
timestamp: Date.now(),
|
|
5072
|
+
url: window.location.href
|
|
5073
|
+
}
|
|
5074
|
+
},
|
|
5075
|
+
timestamp: Date.now()
|
|
5076
|
+
};
|
|
5077
|
+
this.addEvent(consoleEvent);
|
|
4857
5078
|
}
|
|
4858
5079
|
setupPageUnloadHandler() {
|
|
4859
5080
|
if (!isBrowser)
|
|
4860
5081
|
return;
|
|
4861
|
-
|
|
5082
|
+
logDebug('Setting up page unload handler');
|
|
4862
5083
|
// Handle visibility changes for sending events
|
|
4863
5084
|
window.addEventListener('visibilitychange', () => {
|
|
4864
5085
|
// Only send events when page becomes hidden
|
|
4865
5086
|
if (document.visibilityState === 'hidden') {
|
|
4866
|
-
|
|
5087
|
+
logDebug('Page hidden - sending pending events');
|
|
4867
5088
|
this.api.sendBeaconEvents(this.eventIngestionQueue, this.sessionId);
|
|
4868
5089
|
}
|
|
4869
5090
|
});
|
|
@@ -4881,12 +5102,12 @@ class HumanBehaviorTracker {
|
|
|
4881
5102
|
}
|
|
4882
5103
|
viewLogs() {
|
|
4883
5104
|
try {
|
|
4884
|
-
const logs =
|
|
5105
|
+
const logs = logger.getLogs();
|
|
4885
5106
|
console.log('HumanBehavior Logs:', logs);
|
|
4886
|
-
|
|
5107
|
+
logger.clearLogs(); // Clear logs after viewing
|
|
4887
5108
|
}
|
|
4888
5109
|
catch (e) {
|
|
4889
|
-
|
|
5110
|
+
logError('Failed to read logs:', e);
|
|
4890
5111
|
}
|
|
4891
5112
|
}
|
|
4892
5113
|
addUserInfo(userProperties) {
|
|
@@ -4930,6 +5151,8 @@ class HumanBehaviorTracker {
|
|
|
4930
5151
|
this.flushInterval = window.setInterval(() => {
|
|
4931
5152
|
this.flush();
|
|
4932
5153
|
}, this.FLUSH_INTERVAL_MS);
|
|
5154
|
+
// Enable console tracking
|
|
5155
|
+
this.enableConsoleTracking();
|
|
4933
5156
|
// Start recording with redaction enabled
|
|
4934
5157
|
record({
|
|
4935
5158
|
emit: (event) => {
|
|
@@ -4953,6 +5176,8 @@ class HumanBehaviorTracker {
|
|
|
4953
5176
|
clearInterval(this.flushInterval);
|
|
4954
5177
|
this.flushInterval = null;
|
|
4955
5178
|
}
|
|
5179
|
+
// Disable console tracking
|
|
5180
|
+
this.disableConsoleTracking();
|
|
4956
5181
|
});
|
|
4957
5182
|
}
|
|
4958
5183
|
addEvent(event) {
|
|
@@ -4986,7 +5211,7 @@ class HumanBehaviorTracker {
|
|
|
4986
5211
|
this.sessionId = newSessionId;
|
|
4987
5212
|
}
|
|
4988
5213
|
catch (error) {
|
|
4989
|
-
|
|
5214
|
+
logError('Failed to process rejected events:', error);
|
|
4990
5215
|
}
|
|
4991
5216
|
finally {
|
|
4992
5217
|
this.isProcessingRejectedEvents = false;
|
|
@@ -5007,14 +5232,14 @@ class HumanBehaviorTracker {
|
|
|
5007
5232
|
this.eventIngestionQueue = [];
|
|
5008
5233
|
this.queueSizeBytes = 0;
|
|
5009
5234
|
if (eventsToProcess.length > 0) {
|
|
5010
|
-
|
|
5235
|
+
logDebug('Flushing events:', eventsToProcess);
|
|
5011
5236
|
try {
|
|
5012
5237
|
yield this.api.sendEvents(eventsToProcess, this.sessionId, this.endUserId);
|
|
5013
5238
|
}
|
|
5014
5239
|
catch (error) {
|
|
5015
5240
|
// If we get a 400 error, store events for retry
|
|
5016
5241
|
if ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('ERROR: Session already completed')) {
|
|
5017
|
-
|
|
5242
|
+
logInfo('Session expired, storing events for retry');
|
|
5018
5243
|
this.rejectedEvents.push(...eventsToProcess);
|
|
5019
5244
|
this.processRejectedEvents();
|
|
5020
5245
|
}
|
|
@@ -5060,7 +5285,7 @@ class HumanBehaviorTracker {
|
|
|
5060
5285
|
return __awaiter$1(this, void 0, void 0, function* () {
|
|
5061
5286
|
yield this.ensureInitialized();
|
|
5062
5287
|
if (!isBrowser) {
|
|
5063
|
-
|
|
5288
|
+
logWarn('Redaction is only available in browser environments');
|
|
5064
5289
|
return;
|
|
5065
5290
|
}
|
|
5066
5291
|
// Create a new redaction manager with the provided options
|
|
@@ -5073,7 +5298,7 @@ class HumanBehaviorTracker {
|
|
|
5073
5298
|
*/
|
|
5074
5299
|
setRedactedFields(fields) {
|
|
5075
5300
|
if (!isBrowser) {
|
|
5076
|
-
|
|
5301
|
+
logWarn('Redaction is only available in browser environments');
|
|
5077
5302
|
return;
|
|
5078
5303
|
}
|
|
5079
5304
|
this.redactionManager.setFieldsToRedact(fields);
|
|
@@ -5110,6 +5335,11 @@ exports.MAX_CHUNK_SIZE_BYTES = MAX_CHUNK_SIZE_BYTES;
|
|
|
5110
5335
|
exports.RedactionManager = RedactionManager;
|
|
5111
5336
|
exports.default = HumanBehaviorTracker;
|
|
5112
5337
|
exports.isChunkSizeExceeded = isChunkSizeExceeded;
|
|
5338
|
+
exports.logDebug = logDebug;
|
|
5339
|
+
exports.logError = logError;
|
|
5340
|
+
exports.logInfo = logInfo;
|
|
5341
|
+
exports.logWarn = logWarn;
|
|
5342
|
+
exports.logger = logger;
|
|
5113
5343
|
exports.redactionManager = redactionManager;
|
|
5114
5344
|
exports.validateSingleEventSize = validateSingleEventSize;
|
|
5115
5345
|
//# sourceMappingURL=index.js.map
|