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/esm/index.js
CHANGED
|
@@ -4049,6 +4049,123 @@ function v1Bytes(rnds, msecs, nsecs, clockseq, node, buf, offset = 0) {
|
|
|
4049
4049
|
return buf;
|
|
4050
4050
|
}
|
|
4051
4051
|
|
|
4052
|
+
var LogLevel;
|
|
4053
|
+
(function (LogLevel) {
|
|
4054
|
+
LogLevel[LogLevel["NONE"] = 0] = "NONE";
|
|
4055
|
+
LogLevel[LogLevel["ERROR"] = 1] = "ERROR";
|
|
4056
|
+
LogLevel[LogLevel["WARN"] = 2] = "WARN";
|
|
4057
|
+
LogLevel[LogLevel["INFO"] = 3] = "INFO";
|
|
4058
|
+
LogLevel[LogLevel["DEBUG"] = 4] = "DEBUG";
|
|
4059
|
+
})(LogLevel || (LogLevel = {}));
|
|
4060
|
+
class Logger {
|
|
4061
|
+
constructor(config) {
|
|
4062
|
+
this.config = {
|
|
4063
|
+
level: LogLevel.ERROR, // Default to only errors in production
|
|
4064
|
+
enableConsole: true,
|
|
4065
|
+
enableStorage: false
|
|
4066
|
+
};
|
|
4067
|
+
this.isBrowser = typeof window !== 'undefined';
|
|
4068
|
+
if (config) {
|
|
4069
|
+
this.config = Object.assign(Object.assign({}, this.config), config);
|
|
4070
|
+
}
|
|
4071
|
+
}
|
|
4072
|
+
setConfig(config) {
|
|
4073
|
+
this.config = Object.assign(Object.assign({}, this.config), config);
|
|
4074
|
+
}
|
|
4075
|
+
shouldLog(level) {
|
|
4076
|
+
return level <= this.config.level;
|
|
4077
|
+
}
|
|
4078
|
+
formatMessage(level, message, ...args) {
|
|
4079
|
+
const timestamp = new Date().toISOString();
|
|
4080
|
+
return `[HumanBehavior ${level}] ${timestamp}: ${message}`;
|
|
4081
|
+
}
|
|
4082
|
+
error(message, ...args) {
|
|
4083
|
+
if (!this.shouldLog(LogLevel.ERROR))
|
|
4084
|
+
return;
|
|
4085
|
+
const formattedMessage = this.formatMessage('ERROR', message);
|
|
4086
|
+
if (this.config.enableConsole) {
|
|
4087
|
+
console.error(formattedMessage, ...args);
|
|
4088
|
+
}
|
|
4089
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
4090
|
+
this.logToStorage(formattedMessage, args);
|
|
4091
|
+
}
|
|
4092
|
+
}
|
|
4093
|
+
warn(message, ...args) {
|
|
4094
|
+
if (!this.shouldLog(LogLevel.WARN))
|
|
4095
|
+
return;
|
|
4096
|
+
const formattedMessage = this.formatMessage('WARN', message);
|
|
4097
|
+
if (this.config.enableConsole) {
|
|
4098
|
+
console.warn(formattedMessage, ...args);
|
|
4099
|
+
}
|
|
4100
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
4101
|
+
this.logToStorage(formattedMessage, args);
|
|
4102
|
+
}
|
|
4103
|
+
}
|
|
4104
|
+
info(message, ...args) {
|
|
4105
|
+
if (!this.shouldLog(LogLevel.INFO))
|
|
4106
|
+
return;
|
|
4107
|
+
const formattedMessage = this.formatMessage('INFO', message);
|
|
4108
|
+
if (this.config.enableConsole) {
|
|
4109
|
+
console.log(formattedMessage, ...args);
|
|
4110
|
+
}
|
|
4111
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
4112
|
+
this.logToStorage(formattedMessage, args);
|
|
4113
|
+
}
|
|
4114
|
+
}
|
|
4115
|
+
debug(message, ...args) {
|
|
4116
|
+
if (!this.shouldLog(LogLevel.DEBUG))
|
|
4117
|
+
return;
|
|
4118
|
+
const formattedMessage = this.formatMessage('DEBUG', message);
|
|
4119
|
+
if (this.config.enableConsole) {
|
|
4120
|
+
console.log(formattedMessage, ...args);
|
|
4121
|
+
}
|
|
4122
|
+
if (this.config.enableStorage && this.isBrowser) {
|
|
4123
|
+
this.logToStorage(formattedMessage, args);
|
|
4124
|
+
}
|
|
4125
|
+
}
|
|
4126
|
+
logToStorage(message, args) {
|
|
4127
|
+
try {
|
|
4128
|
+
const logs = JSON.parse(localStorage.getItem('human_behavior_logs') || '[]');
|
|
4129
|
+
const logEntry = {
|
|
4130
|
+
message,
|
|
4131
|
+
args: args.length > 0 ? args : undefined,
|
|
4132
|
+
timestamp: Date.now()
|
|
4133
|
+
};
|
|
4134
|
+
logs.push(logEntry);
|
|
4135
|
+
// Keep only last 1000 logs to prevent storage bloat
|
|
4136
|
+
if (logs.length > 1000) {
|
|
4137
|
+
logs.splice(0, logs.length - 1000);
|
|
4138
|
+
}
|
|
4139
|
+
localStorage.setItem('human_behavior_logs', JSON.stringify(logs));
|
|
4140
|
+
}
|
|
4141
|
+
catch (e) {
|
|
4142
|
+
// Silently fail if storage is not available
|
|
4143
|
+
}
|
|
4144
|
+
}
|
|
4145
|
+
getLogs() {
|
|
4146
|
+
if (!this.isBrowser)
|
|
4147
|
+
return [];
|
|
4148
|
+
try {
|
|
4149
|
+
return JSON.parse(localStorage.getItem('human_behavior_logs') || '[]');
|
|
4150
|
+
}
|
|
4151
|
+
catch (e) {
|
|
4152
|
+
return [];
|
|
4153
|
+
}
|
|
4154
|
+
}
|
|
4155
|
+
clearLogs() {
|
|
4156
|
+
if (this.isBrowser) {
|
|
4157
|
+
localStorage.removeItem('human_behavior_logs');
|
|
4158
|
+
}
|
|
4159
|
+
}
|
|
4160
|
+
}
|
|
4161
|
+
// Create singleton instance
|
|
4162
|
+
const logger = new Logger();
|
|
4163
|
+
// Export convenience methods
|
|
4164
|
+
const logError = (message, ...args) => logger.error(message, ...args);
|
|
4165
|
+
const logWarn = (message, ...args) => logger.warn(message, ...args);
|
|
4166
|
+
const logInfo = (message, ...args) => logger.info(message, ...args);
|
|
4167
|
+
const logDebug = (message, ...args) => logger.debug(message, ...args);
|
|
4168
|
+
|
|
4052
4169
|
const MAX_CHUNK_SIZE_BYTES = 1024 * 1024 * 10; // 10MB chunk size
|
|
4053
4170
|
function isChunkSizeExceeded(currentChunk, newEvent, sessionId) {
|
|
4054
4171
|
const nextChunkSize = new TextEncoder().encode(JSON.stringify({
|
|
@@ -4073,15 +4190,25 @@ class HumanBehaviorAPI {
|
|
|
4073
4190
|
}
|
|
4074
4191
|
init(sessionId, userId) {
|
|
4075
4192
|
return __awaiter$1(this, void 0, void 0, function* () {
|
|
4193
|
+
// Get current page URL and referrer if in browser environment
|
|
4194
|
+
let entryURL = null;
|
|
4195
|
+
let referrer = null;
|
|
4196
|
+
if (typeof window !== 'undefined') {
|
|
4197
|
+
entryURL = window.location.href;
|
|
4198
|
+
referrer = document.referrer;
|
|
4199
|
+
}
|
|
4076
4200
|
const response = yield fetch(`${this.baseUrl}/api/ingestion/init`, {
|
|
4077
4201
|
method: 'POST',
|
|
4078
4202
|
headers: {
|
|
4079
4203
|
'Content-Type': 'application/json',
|
|
4080
|
-
'Authorization': `Bearer ${this.apiKey}
|
|
4204
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
4205
|
+
'Referer': referrer || ''
|
|
4081
4206
|
},
|
|
4082
4207
|
body: JSON.stringify({
|
|
4083
4208
|
sessionId: sessionId,
|
|
4084
|
-
endUserId: userId
|
|
4209
|
+
endUserId: userId,
|
|
4210
|
+
entryURL: entryURL,
|
|
4211
|
+
referrer: referrer
|
|
4085
4212
|
})
|
|
4086
4213
|
});
|
|
4087
4214
|
if (!response.ok) {
|
|
@@ -4170,7 +4297,7 @@ class HumanBehaviorAPI {
|
|
|
4170
4297
|
return results.flat();
|
|
4171
4298
|
}
|
|
4172
4299
|
catch (error) {
|
|
4173
|
-
|
|
4300
|
+
logError('Error sending events:', error);
|
|
4174
4301
|
throw error;
|
|
4175
4302
|
}
|
|
4176
4303
|
});
|
|
@@ -4196,7 +4323,7 @@ class HumanBehaviorAPI {
|
|
|
4196
4323
|
return yield response.json();
|
|
4197
4324
|
}
|
|
4198
4325
|
catch (error) {
|
|
4199
|
-
|
|
4326
|
+
logError('Error sending user data:', error);
|
|
4200
4327
|
throw error;
|
|
4201
4328
|
}
|
|
4202
4329
|
});
|
|
@@ -4223,7 +4350,7 @@ class HumanBehaviorAPI {
|
|
|
4223
4350
|
return yield response.json();
|
|
4224
4351
|
}
|
|
4225
4352
|
catch (error) {
|
|
4226
|
-
|
|
4353
|
+
logError('Error authenticating user:', error);
|
|
4227
4354
|
throw error;
|
|
4228
4355
|
}
|
|
4229
4356
|
});
|
|
@@ -4270,7 +4397,7 @@ class HumanBehaviorAPI {
|
|
|
4270
4397
|
catch (error) {
|
|
4271
4398
|
retryCount++;
|
|
4272
4399
|
if (retryCount === maxRetries) {
|
|
4273
|
-
|
|
4400
|
+
logError('Error sending custom event after max retries:', error);
|
|
4274
4401
|
throw error;
|
|
4275
4402
|
}
|
|
4276
4403
|
// Exponential backoff
|
|
@@ -4303,7 +4430,7 @@ class HumanBehaviorAPI {
|
|
|
4303
4430
|
catch (error) {
|
|
4304
4431
|
retryCount++;
|
|
4305
4432
|
if (retryCount === maxRetries) {
|
|
4306
|
-
|
|
4433
|
+
logError('Error sending custom events after max retries:', error);
|
|
4307
4434
|
throw error;
|
|
4308
4435
|
}
|
|
4309
4436
|
// Exponential backoff
|
|
@@ -4319,7 +4446,7 @@ class HumanBehaviorAPI {
|
|
|
4319
4446
|
data.append('timestamp', encodeURIComponent(Date.now().toString()));
|
|
4320
4447
|
data.append('apiKey', encodeURIComponent(this.apiKey));
|
|
4321
4448
|
if (isSessionComplete) {
|
|
4322
|
-
|
|
4449
|
+
logInfo('Session complete beacon sending');
|
|
4323
4450
|
localStorage.setItem('koalaware_session_complete', Date.now().toString());
|
|
4324
4451
|
data.append('sessionComplete', encodeURIComponent('true'));
|
|
4325
4452
|
}
|
|
@@ -4383,18 +4510,18 @@ class RedactionManager {
|
|
|
4383
4510
|
this.userSelectedFields.clear();
|
|
4384
4511
|
fields.forEach(field => this.userSelectedFields.add(field));
|
|
4385
4512
|
if (fields.length > 0) {
|
|
4386
|
-
|
|
4513
|
+
logDebug(`Redaction: Active for ${fields.length} field(s):`, fields);
|
|
4387
4514
|
// Debug: Check if elements exist
|
|
4388
4515
|
fields.forEach(selector => {
|
|
4389
4516
|
const elements = document.querySelectorAll(selector);
|
|
4390
|
-
|
|
4517
|
+
logDebug(`Redaction: Found ${elements.length} element(s) for selector '${selector}'`);
|
|
4391
4518
|
elements.forEach((el, index) => {
|
|
4392
|
-
|
|
4519
|
+
logDebug(`Redaction: Element ${index} for '${selector}':`, el);
|
|
4393
4520
|
});
|
|
4394
4521
|
});
|
|
4395
4522
|
}
|
|
4396
4523
|
else {
|
|
4397
|
-
|
|
4524
|
+
logDebug('Redaction: Disabled - no fields selected');
|
|
4398
4525
|
}
|
|
4399
4526
|
}
|
|
4400
4527
|
/**
|
|
@@ -4424,7 +4551,7 @@ class RedactionManager {
|
|
|
4424
4551
|
if (processedEvent.data.source === 5) { // Input event
|
|
4425
4552
|
const shouldRedact = this.isFieldSelected(processedEvent.data);
|
|
4426
4553
|
if (shouldRedact) {
|
|
4427
|
-
|
|
4554
|
+
logDebug('Redaction: Processing input event for redaction');
|
|
4428
4555
|
this.redactInputEvent(processedEvent.data);
|
|
4429
4556
|
}
|
|
4430
4557
|
}
|
|
@@ -4450,30 +4577,30 @@ class RedactionManager {
|
|
|
4450
4577
|
if (!this.isFieldSelected(inputData)) {
|
|
4451
4578
|
return;
|
|
4452
4579
|
}
|
|
4453
|
-
|
|
4580
|
+
logDebug('Redaction: Redacting input event with text:', inputData.text);
|
|
4454
4581
|
// Redact all text-related properties that could contain input data
|
|
4455
4582
|
const textProperties = ['text', 'value', 'content', 'data', 'input', 'textContent'];
|
|
4456
4583
|
textProperties.forEach(prop => {
|
|
4457
4584
|
if (inputData[prop] !== undefined && typeof inputData[prop] === 'string') {
|
|
4458
4585
|
inputData[prop] = this.redactedText;
|
|
4459
|
-
|
|
4586
|
+
logDebug(`Redaction: Redacted property '${prop}'`);
|
|
4460
4587
|
}
|
|
4461
4588
|
});
|
|
4462
4589
|
// Also check for any other string properties that might contain input data
|
|
4463
4590
|
Object.keys(inputData).forEach(key => {
|
|
4464
4591
|
if (typeof inputData[key] === 'string' && inputData[key].length > 0) {
|
|
4465
4592
|
inputData[key] = this.redactedText;
|
|
4466
|
-
|
|
4593
|
+
logDebug(`Redaction: Redacted additional property '${key}'`);
|
|
4467
4594
|
}
|
|
4468
4595
|
});
|
|
4469
4596
|
// Handle nested objects that might contain text data
|
|
4470
4597
|
if (inputData.attributes && typeof inputData.attributes === 'object') {
|
|
4471
4598
|
if (inputData.attributes.value && typeof inputData.attributes.value === 'string') {
|
|
4472
4599
|
inputData.attributes.value = this.redactedText;
|
|
4473
|
-
|
|
4600
|
+
logDebug('Redaction: Redacted nested value attribute');
|
|
4474
4601
|
}
|
|
4475
4602
|
}
|
|
4476
|
-
|
|
4603
|
+
logDebug('Redaction: Input event redaction complete');
|
|
4477
4604
|
}
|
|
4478
4605
|
/**
|
|
4479
4606
|
* Redact sensitive data in DOM mutation events
|
|
@@ -4535,7 +4662,7 @@ class RedactionManager {
|
|
|
4535
4662
|
return false;
|
|
4536
4663
|
}
|
|
4537
4664
|
catch (e) {
|
|
4538
|
-
|
|
4665
|
+
logWarn('Error checking if DOM change should be redacted:', e);
|
|
4539
4666
|
return false;
|
|
4540
4667
|
}
|
|
4541
4668
|
}
|
|
@@ -4682,7 +4809,7 @@ class RedactionManager {
|
|
|
4682
4809
|
// Check if any of these elements are currently focused
|
|
4683
4810
|
for (const el of matchingElements) {
|
|
4684
4811
|
if (el === document.activeElement) {
|
|
4685
|
-
|
|
4812
|
+
logDebug('Redaction: Found focused element matching selector:', selector);
|
|
4686
4813
|
return true;
|
|
4687
4814
|
}
|
|
4688
4815
|
}
|
|
@@ -4692,7 +4819,7 @@ class RedactionManager {
|
|
|
4692
4819
|
// Look for any input element that might be the active one
|
|
4693
4820
|
const activeElement = document.activeElement;
|
|
4694
4821
|
if (activeElement && this.shouldRedactElement(activeElement)) {
|
|
4695
|
-
|
|
4822
|
+
logDebug('Redaction: Active element should be redacted');
|
|
4696
4823
|
return true;
|
|
4697
4824
|
}
|
|
4698
4825
|
return false;
|
|
@@ -4725,7 +4852,7 @@ class RedactionManager {
|
|
|
4725
4852
|
return false;
|
|
4726
4853
|
}
|
|
4727
4854
|
catch (e) {
|
|
4728
|
-
|
|
4855
|
+
logWarn('Error checking if field should be redacted:', e);
|
|
4729
4856
|
return false;
|
|
4730
4857
|
}
|
|
4731
4858
|
}
|
|
@@ -4781,15 +4908,26 @@ class HumanBehaviorTracker {
|
|
|
4781
4908
|
this.endUserId = null;
|
|
4782
4909
|
this.initialized = false;
|
|
4783
4910
|
this.initializationPromise = null;
|
|
4911
|
+
// Console tracking properties
|
|
4912
|
+
this.originalConsole = null;
|
|
4913
|
+
this.originalLogger = null;
|
|
4914
|
+
this.consoleTrackingEnabled = false;
|
|
4784
4915
|
if (!apiKey) {
|
|
4785
4916
|
throw new Error('Human Behavior API Key is required');
|
|
4786
4917
|
}
|
|
4787
|
-
|
|
4788
|
-
|
|
4789
|
-
|
|
4918
|
+
// ========================================
|
|
4919
|
+
// DEVELOPER: Choose your ingestion server
|
|
4920
|
+
// ========================================
|
|
4921
|
+
// Uncomment ONE of the following lines to select your server:
|
|
4922
|
+
// AWS Development Server
|
|
4923
|
+
const defaultIngestionUrl = 'http://3.137.217.33:3000';
|
|
4924
|
+
// Vercel Production Server
|
|
4925
|
+
// const defaultIngestionUrl = 'https://ingestion-server.vercel.app';
|
|
4926
|
+
// Local Development Server
|
|
4927
|
+
// const defaultIngestionUrl = 'http://localhost:3000';
|
|
4790
4928
|
this.api = new HumanBehaviorAPI({
|
|
4791
4929
|
apiKey: apiKey,
|
|
4792
|
-
ingestionUrl: ingestionUrl
|
|
4930
|
+
ingestionUrl: ingestionUrl || defaultIngestionUrl
|
|
4793
4931
|
});
|
|
4794
4932
|
this.apiKey = apiKey;
|
|
4795
4933
|
this.redactionManager = new RedactionManager();
|
|
@@ -4822,13 +4960,13 @@ class HumanBehaviorTracker {
|
|
|
4822
4960
|
this.processRejectedEvents();
|
|
4823
4961
|
}
|
|
4824
4962
|
else {
|
|
4825
|
-
|
|
4963
|
+
logWarn('HumanBehaviorTracker initialized in a non-browser environment. Session tracking is disabled.');
|
|
4826
4964
|
}
|
|
4827
4965
|
this.initialized = true;
|
|
4828
|
-
|
|
4966
|
+
logInfo('HumanBehaviorTracker initialized');
|
|
4829
4967
|
}
|
|
4830
4968
|
catch (error) {
|
|
4831
|
-
|
|
4969
|
+
logError('Failed to initialize HumanBehaviorTracker:', error);
|
|
4832
4970
|
throw error;
|
|
4833
4971
|
}
|
|
4834
4972
|
});
|
|
@@ -4842,24 +4980,107 @@ class HumanBehaviorTracker {
|
|
|
4842
4980
|
});
|
|
4843
4981
|
}
|
|
4844
4982
|
static logToStorage(message) {
|
|
4845
|
-
|
|
4846
|
-
|
|
4847
|
-
|
|
4848
|
-
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4983
|
+
logInfo(message);
|
|
4984
|
+
}
|
|
4985
|
+
/**
|
|
4986
|
+
* Configure logging behavior for the SDK
|
|
4987
|
+
* @param config Logger configuration options
|
|
4988
|
+
*/
|
|
4989
|
+
static configureLogging(config) {
|
|
4990
|
+
const levelMap = {
|
|
4991
|
+
'none': 0,
|
|
4992
|
+
'error': 1,
|
|
4993
|
+
'warn': 2,
|
|
4994
|
+
'info': 3,
|
|
4995
|
+
'debug': 4
|
|
4996
|
+
};
|
|
4997
|
+
logger.setConfig({
|
|
4998
|
+
level: levelMap[config.level || 'error'],
|
|
4999
|
+
enableConsole: config.enableConsole !== false,
|
|
5000
|
+
enableStorage: config.enableStorage || false
|
|
5001
|
+
});
|
|
5002
|
+
}
|
|
5003
|
+
/**
|
|
5004
|
+
* Enable console event tracking
|
|
5005
|
+
*/
|
|
5006
|
+
enableConsoleTracking() {
|
|
5007
|
+
if (!isBrowser || this.consoleTrackingEnabled)
|
|
5008
|
+
return;
|
|
5009
|
+
// Store original console methods
|
|
5010
|
+
this.originalConsole = {
|
|
5011
|
+
log: console.log,
|
|
5012
|
+
warn: console.warn,
|
|
5013
|
+
error: console.error
|
|
5014
|
+
};
|
|
5015
|
+
// Store original logger methods
|
|
5016
|
+
this.originalLogger = {
|
|
5017
|
+
error: logError,
|
|
5018
|
+
warn: logWarn,
|
|
5019
|
+
info: logInfo,
|
|
5020
|
+
debug: logDebug
|
|
5021
|
+
};
|
|
5022
|
+
// Override console methods to capture ALL console output (including logger output)
|
|
5023
|
+
console.log = (...args) => {
|
|
5024
|
+
this.trackConsoleEvent('log', args);
|
|
5025
|
+
this.originalConsole.log(...args);
|
|
5026
|
+
};
|
|
5027
|
+
console.warn = (...args) => {
|
|
5028
|
+
this.trackConsoleEvent('warn', args);
|
|
5029
|
+
this.originalConsole.warn(...args);
|
|
5030
|
+
};
|
|
5031
|
+
console.error = (...args) => {
|
|
5032
|
+
this.trackConsoleEvent('error', args);
|
|
5033
|
+
this.originalConsole.error(...args);
|
|
5034
|
+
};
|
|
5035
|
+
this.consoleTrackingEnabled = true;
|
|
5036
|
+
this.originalLogger.debug('Console tracking enabled');
|
|
5037
|
+
}
|
|
5038
|
+
/**
|
|
5039
|
+
* Disable console event tracking
|
|
5040
|
+
*/
|
|
5041
|
+
disableConsoleTracking() {
|
|
5042
|
+
if (!isBrowser || !this.consoleTrackingEnabled || !this.originalConsole)
|
|
5043
|
+
return;
|
|
5044
|
+
// Restore original console methods
|
|
5045
|
+
console.log = this.originalConsole.log;
|
|
5046
|
+
console.warn = this.originalConsole.warn;
|
|
5047
|
+
console.error = this.originalConsole.error;
|
|
5048
|
+
this.consoleTrackingEnabled = false;
|
|
5049
|
+
this.originalConsole = null;
|
|
5050
|
+
this.originalLogger = null;
|
|
5051
|
+
}
|
|
5052
|
+
/**
|
|
5053
|
+
* Track console events
|
|
5054
|
+
*/
|
|
5055
|
+
trackConsoleEvent(level, args) {
|
|
5056
|
+
if (!this.initialized)
|
|
5057
|
+
return;
|
|
5058
|
+
const consoleEvent = {
|
|
5059
|
+
type: 5, // Custom event type
|
|
5060
|
+
data: {
|
|
5061
|
+
payload: {
|
|
5062
|
+
type: 'console',
|
|
5063
|
+
level: level,
|
|
5064
|
+
message: args.map(arg => typeof arg === 'string' ? arg :
|
|
5065
|
+
typeof arg === 'object' ? JSON.stringify(arg) :
|
|
5066
|
+
String(arg)).join(' '),
|
|
5067
|
+
timestamp: Date.now(),
|
|
5068
|
+
url: window.location.href
|
|
5069
|
+
}
|
|
5070
|
+
},
|
|
5071
|
+
timestamp: Date.now()
|
|
5072
|
+
};
|
|
5073
|
+
this.addEvent(consoleEvent);
|
|
4853
5074
|
}
|
|
4854
5075
|
setupPageUnloadHandler() {
|
|
4855
5076
|
if (!isBrowser)
|
|
4856
5077
|
return;
|
|
4857
|
-
|
|
5078
|
+
logDebug('Setting up page unload handler');
|
|
4858
5079
|
// Handle visibility changes for sending events
|
|
4859
5080
|
window.addEventListener('visibilitychange', () => {
|
|
4860
5081
|
// Only send events when page becomes hidden
|
|
4861
5082
|
if (document.visibilityState === 'hidden') {
|
|
4862
|
-
|
|
5083
|
+
logDebug('Page hidden - sending pending events');
|
|
4863
5084
|
this.api.sendBeaconEvents(this.eventIngestionQueue, this.sessionId);
|
|
4864
5085
|
}
|
|
4865
5086
|
});
|
|
@@ -4877,12 +5098,12 @@ class HumanBehaviorTracker {
|
|
|
4877
5098
|
}
|
|
4878
5099
|
viewLogs() {
|
|
4879
5100
|
try {
|
|
4880
|
-
const logs =
|
|
5101
|
+
const logs = logger.getLogs();
|
|
4881
5102
|
console.log('HumanBehavior Logs:', logs);
|
|
4882
|
-
|
|
5103
|
+
logger.clearLogs(); // Clear logs after viewing
|
|
4883
5104
|
}
|
|
4884
5105
|
catch (e) {
|
|
4885
|
-
|
|
5106
|
+
logError('Failed to read logs:', e);
|
|
4886
5107
|
}
|
|
4887
5108
|
}
|
|
4888
5109
|
addUserInfo(userProperties) {
|
|
@@ -4926,6 +5147,8 @@ class HumanBehaviorTracker {
|
|
|
4926
5147
|
this.flushInterval = window.setInterval(() => {
|
|
4927
5148
|
this.flush();
|
|
4928
5149
|
}, this.FLUSH_INTERVAL_MS);
|
|
5150
|
+
// Enable console tracking
|
|
5151
|
+
this.enableConsoleTracking();
|
|
4929
5152
|
// Start recording with redaction enabled
|
|
4930
5153
|
record({
|
|
4931
5154
|
emit: (event) => {
|
|
@@ -4949,6 +5172,8 @@ class HumanBehaviorTracker {
|
|
|
4949
5172
|
clearInterval(this.flushInterval);
|
|
4950
5173
|
this.flushInterval = null;
|
|
4951
5174
|
}
|
|
5175
|
+
// Disable console tracking
|
|
5176
|
+
this.disableConsoleTracking();
|
|
4952
5177
|
});
|
|
4953
5178
|
}
|
|
4954
5179
|
addEvent(event) {
|
|
@@ -4982,7 +5207,7 @@ class HumanBehaviorTracker {
|
|
|
4982
5207
|
this.sessionId = newSessionId;
|
|
4983
5208
|
}
|
|
4984
5209
|
catch (error) {
|
|
4985
|
-
|
|
5210
|
+
logError('Failed to process rejected events:', error);
|
|
4986
5211
|
}
|
|
4987
5212
|
finally {
|
|
4988
5213
|
this.isProcessingRejectedEvents = false;
|
|
@@ -5003,14 +5228,14 @@ class HumanBehaviorTracker {
|
|
|
5003
5228
|
this.eventIngestionQueue = [];
|
|
5004
5229
|
this.queueSizeBytes = 0;
|
|
5005
5230
|
if (eventsToProcess.length > 0) {
|
|
5006
|
-
|
|
5231
|
+
logDebug('Flushing events:', eventsToProcess);
|
|
5007
5232
|
try {
|
|
5008
5233
|
yield this.api.sendEvents(eventsToProcess, this.sessionId, this.endUserId);
|
|
5009
5234
|
}
|
|
5010
5235
|
catch (error) {
|
|
5011
5236
|
// If we get a 400 error, store events for retry
|
|
5012
5237
|
if ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('ERROR: Session already completed')) {
|
|
5013
|
-
|
|
5238
|
+
logInfo('Session expired, storing events for retry');
|
|
5014
5239
|
this.rejectedEvents.push(...eventsToProcess);
|
|
5015
5240
|
this.processRejectedEvents();
|
|
5016
5241
|
}
|
|
@@ -5056,7 +5281,7 @@ class HumanBehaviorTracker {
|
|
|
5056
5281
|
return __awaiter$1(this, void 0, void 0, function* () {
|
|
5057
5282
|
yield this.ensureInitialized();
|
|
5058
5283
|
if (!isBrowser) {
|
|
5059
|
-
|
|
5284
|
+
logWarn('Redaction is only available in browser environments');
|
|
5060
5285
|
return;
|
|
5061
5286
|
}
|
|
5062
5287
|
// Create a new redaction manager with the provided options
|
|
@@ -5069,7 +5294,7 @@ class HumanBehaviorTracker {
|
|
|
5069
5294
|
*/
|
|
5070
5295
|
setRedactedFields(fields) {
|
|
5071
5296
|
if (!isBrowser) {
|
|
5072
|
-
|
|
5297
|
+
logWarn('Redaction is only available in browser environments');
|
|
5073
5298
|
return;
|
|
5074
5299
|
}
|
|
5075
5300
|
this.redactionManager.setFieldsToRedact(fields);
|
|
@@ -5100,5 +5325,5 @@ if (typeof window !== 'undefined') {
|
|
|
5100
5325
|
window.HumanBehaviorTracker = HumanBehaviorTracker;
|
|
5101
5326
|
}
|
|
5102
5327
|
|
|
5103
|
-
export { HumanBehaviorAPI, HumanBehaviorTracker, MAX_CHUNK_SIZE_BYTES, RedactionManager, HumanBehaviorTracker as default, isChunkSizeExceeded, redactionManager, validateSingleEventSize };
|
|
5328
|
+
export { HumanBehaviorAPI, HumanBehaviorTracker, LogLevel, MAX_CHUNK_SIZE_BYTES, RedactionManager, HumanBehaviorTracker as default, isChunkSizeExceeded, logDebug, logError, logInfo, logWarn, logger, redactionManager, validateSingleEventSize };
|
|
5104
5329
|
//# sourceMappingURL=index.js.map
|