humanbehavior-js 0.0.9 → 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.
@@ -0,0 +1,594 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>HumanBehavior SDK - Simple SPA Demo</title>
7
+ <style>
8
+ body {
9
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
10
+ margin: 0;
11
+ padding: 20px;
12
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
13
+ min-height: 100vh;
14
+ color: white;
15
+ }
16
+
17
+ .container {
18
+ max-width: 800px;
19
+ margin: 0 auto;
20
+ background: rgba(255, 255, 255, 0.1);
21
+ backdrop-filter: blur(10px);
22
+ border-radius: 20px;
23
+ padding: 40px;
24
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
25
+ }
26
+
27
+ h1 {
28
+ text-align: center;
29
+ margin-bottom: 30px;
30
+ font-size: 2.5em;
31
+ text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
32
+ }
33
+
34
+ .nav {
35
+ display: flex;
36
+ justify-content: center;
37
+ gap: 20px;
38
+ margin-bottom: 40px;
39
+ }
40
+
41
+ .nav button {
42
+ background: rgba(255, 255, 255, 0.2);
43
+ border: 2px solid rgba(255, 255, 255, 0.3);
44
+ color: white;
45
+ padding: 12px 24px;
46
+ border-radius: 25px;
47
+ cursor: pointer;
48
+ font-size: 16px;
49
+ transition: all 0.3s ease;
50
+ backdrop-filter: blur(5px);
51
+ }
52
+
53
+ .nav button:hover {
54
+ background: rgba(255, 255, 255, 0.3);
55
+ transform: translateY(-2px);
56
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
57
+ }
58
+
59
+ .nav button.active {
60
+ background: rgba(255, 255, 255, 0.4);
61
+ border-color: rgba(255, 255, 255, 0.6);
62
+ }
63
+
64
+ .content {
65
+ background: rgba(255, 255, 255, 0.1);
66
+ border-radius: 15px;
67
+ padding: 30px;
68
+ min-height: 400px;
69
+ backdrop-filter: blur(5px);
70
+ }
71
+
72
+ .button {
73
+ background: linear-gradient(45deg, #ff6b6b, #ee5a24);
74
+ color: white;
75
+ border: none;
76
+ padding: 12px 24px;
77
+ border-radius: 25px;
78
+ cursor: pointer;
79
+ font-size: 16px;
80
+ margin: 10px;
81
+ transition: all 0.3s ease;
82
+ }
83
+
84
+ .button:hover {
85
+ transform: translateY(-2px);
86
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
87
+ }
88
+
89
+ .button.secondary {
90
+ background: linear-gradient(45deg, #4834d4, #686de0);
91
+ }
92
+
93
+ .button.success {
94
+ background: linear-gradient(45deg, #00b894, #00cec9);
95
+ }
96
+
97
+ .status {
98
+ background: rgba(0, 0, 0, 0.2);
99
+ border-radius: 10px;
100
+ padding: 15px;
101
+ margin: 20px 0;
102
+ font-family: monospace;
103
+ font-size: 14px;
104
+ }
105
+
106
+ .logs {
107
+ background: rgba(0, 0, 0, 0.3);
108
+ border-radius: 10px;
109
+ padding: 15px;
110
+ margin: 20px 0;
111
+ max-height: 200px;
112
+ overflow-y: auto;
113
+ font-family: monospace;
114
+ font-size: 12px;
115
+ }
116
+
117
+ .fade-in {
118
+ animation: fadeIn 0.5s ease-in;
119
+ }
120
+
121
+ @keyframes fadeIn {
122
+ from { opacity: 0; transform: translateY(20px); }
123
+ to { opacity: 1; transform: translateY(0); }
124
+ }
125
+ </style>
126
+ </head>
127
+ <body>
128
+ <div class="container">
129
+ <h1>HumanBehavior SDK - Simple SPA Demo</h1>
130
+
131
+ <div class="nav">
132
+ <button class="nav-btn active" data-page="home">🏠 Home</button>
133
+ <button class="nav-btn" data-page="about">ℹ️ About</button>
134
+ <button class="nav-btn" data-page="demo">🎯 Demo</button>
135
+ <button class="nav-btn" data-page="status">📊 Status</button>
136
+ </div>
137
+
138
+ <div id="content" class="content fade-in">
139
+ <!-- Content will be loaded here -->
140
+ </div>
141
+ </div>
142
+
143
+ <!-- Load the HumanBehavior SDK -->
144
+ <script src="dist/index.min.js"></script>
145
+
146
+ <script>
147
+ // Global tracker instance
148
+ let tracker = null;
149
+ let currentPage = 'home';
150
+
151
+ // Page content definitions
152
+ const pages = {
153
+ home: {
154
+ title: '🏠 Home',
155
+ content: `
156
+ <h2>Welcome to HumanBehavior SDK Demo</h2>
157
+ <p>This is a single-page application that demonstrates session continuity across page navigations.</p>
158
+
159
+ <div class="status">
160
+ <strong>Session Info:</strong><br>
161
+ Session ID: <span id="sessionId">Loading...</span><br>
162
+ End User ID: <span id="endUserId">Loading...</span><br>
163
+ User Type: <span id="userType">Loading...</span><br>
164
+ Current URL: <span id="currentUrl">Loading...</span>
165
+ </div>
166
+
167
+ <h3>Test Actions</h3>
168
+ <button class="button" onclick="trackCustomEvent('home_button_click', {page: 'home'})">
169
+ Track Custom Event
170
+ </button>
171
+ <button class="button secondary" onclick="testConsoleLog()">
172
+ Test Console Log
173
+ </button>
174
+ <button class="button success" onclick="testConsoleError()">
175
+ Test Console Error
176
+ </button>
177
+
178
+ <div class="logs" id="logs">
179
+ <div>Logs will appear here...</div>
180
+ </div>
181
+ `
182
+ },
183
+
184
+ about: {
185
+ title: 'ℹ️ About',
186
+ content: `
187
+ <h2>About This Demo</h2>
188
+ <p>This single-page application demonstrates how the HumanBehavior SDK maintains session continuity across page navigations.</p>
189
+
190
+ <h3>Key Features</h3>
191
+ <ul>
192
+ <li><strong>Session Continuity:</strong> The tracker never reinitializes when navigating between pages</li>
193
+ <li><strong>URL Tracking:</strong> All navigation events are captured and sent to the server</li>
194
+ <li><strong>Event Recording:</strong> User interactions, console logs, and custom events are all tracked</li>
195
+ <li><strong>Real-time Updates:</strong> Session information updates in real-time</li>
196
+ </ul>
197
+
198
+ <div class="status">
199
+ <strong>Current Session:</strong><br>
200
+ Session ID: <span id="sessionId-about">Loading...</span><br>
201
+ User Type: <span id="userType-about">Loading...</span><br>
202
+ Page Load Time: <span id="pageLoadTime">Loading...</span>
203
+ </div>
204
+
205
+ <button class="button" onclick="trackCustomEvent('about_page_viewed', {section: 'about'})">
206
+ Track About Page View
207
+ </button>
208
+ <button class="button secondary" onclick="showUserInfo()">
209
+ Show Detailed User Info
210
+ </button>
211
+ `
212
+ },
213
+
214
+ demo: {
215
+ title: '🎯 Interactive Demo',
216
+ content: `
217
+ <h2>Interactive Demo</h2>
218
+ <p>Test various tracking features and see them in action.</p>
219
+
220
+ <h3>Form Interaction</h3>
221
+ <form onsubmit="handleFormSubmit(event)">
222
+ <input type="text" id="demoInput" placeholder="Type something..." style="padding: 10px; border-radius: 5px; border: none; margin: 10px; width: 200px;">
223
+ <button type="submit" class="button">Submit Form</button>
224
+ </form>
225
+
226
+ <h3>Custom Events</h3>
227
+ <button class="button" onclick="trackCustomEvent('demo_button_1', {button: 'demo1'})">Demo Button 1</button>
228
+ <button class="button secondary" onclick="trackCustomEvent('demo_button_2', {button: 'demo2'})">Demo Button 2</button>
229
+ <button class="button success" onclick="trackCustomEvent('demo_button_3', {button: 'demo3'})">Demo Button 3</button>
230
+
231
+ <div class="logs" id="demoLogs">
232
+ <div>Demo events will appear here...</div>
233
+ </div>
234
+ `
235
+ },
236
+
237
+ status: {
238
+ title: '📊 Session Status',
239
+ content: `
240
+ <h2>Session Status & Debug Info</h2>
241
+
242
+ <div class="status">
243
+ <strong>Tracker Status:</strong><br>
244
+ Initialized: <span id="trackerInitialized">Loading...</span><br>
245
+ Session ID: <span id="sessionId-status">Loading...</span><br>
246
+ End User ID: <span id="endUserId-status">Loading...</span><br>
247
+ User Type: <span id="userType-status">Loading...</span><br>
248
+ Current URL: <span id="currentUrl-status">Loading...</span><br>
249
+ Connection Status: <span id="connectionStatus">Loading...</span>
250
+ </div>
251
+
252
+ <h3>Actions</h3>
253
+ <button class="button" onclick="viewLogs()">View Logs</button>
254
+ <button class="button secondary" onclick="testConnection()">Test Connection</button>
255
+ <button class="button success" onclick="clearLogs()">Clear Logs</button>
256
+
257
+ <div class="logs" id="statusLogs">
258
+ <div>Status logs will appear here...</div>
259
+ </div>
260
+ `
261
+ }
262
+ };
263
+
264
+ // Initialize the application
265
+ async function initApp() {
266
+ try {
267
+ console.log('Starting SPA initialization...');
268
+
269
+ // Check if SDK is loaded
270
+ if (typeof HumanBehaviorTracker === 'undefined') {
271
+ throw new Error('HumanBehaviorTracker SDK not loaded. Check if dist/index.js exists.');
272
+ }
273
+
274
+ // Initialize HumanBehavior tracker
275
+ console.log('Initializing tracker...');
276
+ tracker = HumanBehaviorTracker.init('13c3e029-ca45-4a3c-a33b-f5dcb297e31c', {
277
+ logLevel: 'debug',
278
+ redactFields: ['password', 'credit_card']
279
+ });
280
+
281
+ console.log('Tracker created, waiting for initialization...');
282
+
283
+ // Wait for tracker to initialize
284
+ if (tracker.initializationPromise) {
285
+ await tracker.initializationPromise;
286
+ console.log('Tracker initialized successfully');
287
+ } else {
288
+ console.log('No initialization promise, continuing...');
289
+ }
290
+
291
+ // Set up navigation
292
+ setupNavigation();
293
+
294
+ // Load initial page
295
+ loadPage('home');
296
+
297
+ // Update status periodically
298
+ setInterval(updateStatus, 2000);
299
+
300
+ console.log('SPA App initialized successfully');
301
+ addLog('SPA App initialized successfully');
302
+
303
+ } catch (error) {
304
+ console.error('Failed to initialize SPA app:', error);
305
+ addLog('Error initializing app: ' + error.message);
306
+
307
+ // Show error in content
308
+ document.getElementById('content').innerHTML = `
309
+ <h2>Initialization Error</h2>
310
+ <p>Failed to initialize the HumanBehavior SDK:</p>
311
+ <div class="status">
312
+ <strong>Error:</strong> ${error.message}<br>
313
+ <strong>Stack:</strong> ${error.stack}
314
+ </div>
315
+ <button class="button" onclick="location.reload()">Reload Page</button>
316
+ `;
317
+ }
318
+ }
319
+
320
+ // Set up client-side navigation
321
+ function setupNavigation() {
322
+ const navButtons = document.querySelectorAll('.nav-btn');
323
+
324
+ navButtons.forEach(button => {
325
+ button.addEventListener('click', () => {
326
+ const page = button.dataset.page;
327
+ navigateToPage(page);
328
+ });
329
+ });
330
+
331
+ // Handle browser back/forward buttons
332
+ window.addEventListener('popstate', (event) => {
333
+ const page = event.state?.page || 'home';
334
+ loadPage(page);
335
+ updateActiveNavButton(page);
336
+ });
337
+ }
338
+
339
+ // Navigate to a page
340
+ function navigateToPage(page) {
341
+ if (page === currentPage) return;
342
+
343
+ console.log(`Navigating from ${currentPage} to ${page}`);
344
+
345
+ // Update URL without page reload
346
+ history.pushState({ page }, pages[page].title, `#${page}`);
347
+
348
+ // Load the page content
349
+ loadPage(page);
350
+
351
+ // Update active nav button
352
+ updateActiveNavButton(page);
353
+
354
+ // Track navigation event
355
+ if (tracker && tracker.trackNavigationEvent) {
356
+ try {
357
+ tracker.trackNavigationEvent('spa_navigation', currentPage, page);
358
+ } catch (error) {
359
+ console.error('Failed to track navigation:', error);
360
+ }
361
+ }
362
+
363
+ currentPage = page;
364
+ }
365
+
366
+ // Load page content
367
+ function loadPage(page) {
368
+ const contentDiv = document.getElementById('content');
369
+ const pageData = pages[page];
370
+
371
+ if (!pageData) {
372
+ contentDiv.innerHTML = '<h2>Page not found</h2>';
373
+ return;
374
+ }
375
+
376
+ // Add fade-in animation
377
+ contentDiv.classList.remove('fade-in');
378
+ void contentDiv.offsetWidth; // Trigger reflow
379
+ contentDiv.classList.add('fade-in');
380
+
381
+ contentDiv.innerHTML = pageData.content;
382
+
383
+ // Update page title
384
+ document.title = `HumanBehavior SDK - ${pageData.title}`;
385
+
386
+ // Track page view
387
+ if (tracker && tracker.trackPageView) {
388
+ try {
389
+ tracker.trackPageView();
390
+ } catch (error) {
391
+ console.error('Failed to track pageview:', error);
392
+ }
393
+ }
394
+
395
+ addLog(`Navigated to ${page} page`);
396
+ }
397
+
398
+ // Update active navigation button
399
+ function updateActiveNavButton(page) {
400
+ document.querySelectorAll('.nav-btn').forEach(btn => {
401
+ btn.classList.remove('active');
402
+ });
403
+ const activeButton = document.querySelector(`[data-page="${page}"]`);
404
+ if (activeButton) {
405
+ activeButton.classList.add('active');
406
+ }
407
+ }
408
+
409
+ // Update status information
410
+ function updateStatus() {
411
+ if (!tracker) return;
412
+
413
+ try {
414
+ // Update session info
415
+ const sessionIdElements = document.querySelectorAll('#sessionId, #sessionId-about, #sessionId-status');
416
+ const sessionId = tracker.getSessionId ? tracker.getSessionId() : 'Not available';
417
+ sessionIdElements.forEach(el => {
418
+ el.textContent = sessionId;
419
+ });
420
+
421
+ const endUserIdElements = document.querySelectorAll('#endUserId, #endUserId-status');
422
+ const endUserId = tracker.endUserId || 'Not set';
423
+ endUserIdElements.forEach(el => {
424
+ el.textContent = endUserId;
425
+ });
426
+
427
+ // Update user type (preexisting vs new)
428
+ const userTypeElements = document.querySelectorAll('#userType, #userType-about, #userType-status');
429
+ let userType = 'Unknown';
430
+ if (tracker.isPreexistingUser) {
431
+ const isPreexisting = tracker.isPreexistingUser();
432
+ userType = isPreexisting ? '🔄 Preexisting User' : '🆕 New User';
433
+ }
434
+ userTypeElements.forEach(el => {
435
+ el.textContent = userType;
436
+ });
437
+
438
+ const currentUrlElements = document.querySelectorAll('#currentUrl, #currentUrl-status');
439
+ const currentUrl = window.location.href;
440
+ currentUrlElements.forEach(el => {
441
+ el.textContent = currentUrl;
442
+ });
443
+
444
+ const trackerInitialized = document.getElementById('trackerInitialized');
445
+ if (trackerInitialized) {
446
+ trackerInitialized.textContent = tracker.initialized ? 'Yes' : 'No';
447
+ }
448
+
449
+ const connectionStatus = document.getElementById('connectionStatus');
450
+ if (connectionStatus && tracker.getConnectionStatus) {
451
+ const status = tracker.getConnectionStatus();
452
+ connectionStatus.textContent = status.blocked ? 'Blocked' : 'Connected';
453
+ }
454
+
455
+ const pageLoadTime = document.getElementById('pageLoadTime');
456
+ if (pageLoadTime) {
457
+ pageLoadTime.textContent = new Date().toLocaleTimeString();
458
+ }
459
+ } catch (error) {
460
+ console.error('Error updating status:', error);
461
+ }
462
+ }
463
+
464
+ // Track custom events
465
+ async function trackCustomEvent(eventName, properties = {}) {
466
+ if (!tracker) {
467
+ addLog('Tracker not available');
468
+ return;
469
+ }
470
+
471
+ try {
472
+ if (tracker.customEvent) {
473
+ await tracker.customEvent(eventName, properties);
474
+ addLog(`Custom event tracked: ${eventName}`, properties);
475
+ } else {
476
+ addLog('customEvent method not available');
477
+ }
478
+ } catch (error) {
479
+ addLog(`Error tracking event: ${error.message}`);
480
+ }
481
+ }
482
+
483
+ // Test console logging
484
+ function testConsoleLog() {
485
+ console.log('This is a test console log message');
486
+ addLog('Console log test executed');
487
+ }
488
+
489
+ function testConsoleError() {
490
+ console.error('This is a test console error message');
491
+ addLog('Console error test executed');
492
+ }
493
+
494
+ // Handle form submission
495
+ function handleFormSubmit(event) {
496
+ event.preventDefault();
497
+ const input = document.getElementById('demoInput');
498
+ const value = input.value;
499
+
500
+ trackCustomEvent('form_submitted', { value: value });
501
+ addLog(`Form submitted with value: ${value}`);
502
+
503
+ input.value = '';
504
+ }
505
+
506
+ // View logs
507
+ function viewLogs() {
508
+ if (tracker && tracker.viewLogs) {
509
+ tracker.viewLogs();
510
+ } else {
511
+ addLog('viewLogs method not available');
512
+ }
513
+ }
514
+
515
+ // Test connection
516
+ async function testConnection() {
517
+ if (!tracker) {
518
+ addLog('Tracker not available');
519
+ return;
520
+ }
521
+
522
+ try {
523
+ if (tracker.testConnection) {
524
+ const result = await tracker.testConnection();
525
+ addLog(`Connection test: ${result.success ? 'Success' : 'Failed'}`, result);
526
+ } else {
527
+ addLog('testConnection method not available');
528
+ }
529
+ } catch (error) {
530
+ addLog(`Connection test error: ${error.message}`);
531
+ }
532
+ }
533
+
534
+ // Clear logs
535
+ function clearLogs() {
536
+ const logElements = document.querySelectorAll('.logs');
537
+ logElements.forEach(el => {
538
+ el.innerHTML = '<div>Logs cleared...</div>';
539
+ });
540
+ }
541
+
542
+ // Show detailed user information
543
+ function showUserInfo() {
544
+ if (!tracker) {
545
+ addLog('Tracker not available');
546
+ return;
547
+ }
548
+
549
+ try {
550
+ const userInfo = tracker.getUserInfo ? tracker.getUserInfo() : null;
551
+ if (userInfo) {
552
+ const infoText = `
553
+ <strong>Detailed User Information:</strong><br>
554
+ End User ID: ${userInfo.endUserId || 'Not set'}<br>
555
+ Session ID: ${userInfo.sessionId}<br>
556
+ Is Preexisting User: ${userInfo.isPreexistingUser ? 'Yes' : 'No'}<br>
557
+ Initialized: ${userInfo.initialized ? 'Yes' : 'No'}<br>
558
+ <br>
559
+ <strong>Cookie Check:</strong><br>
560
+ End User Cookie: ${tracker.getCookie ? tracker.getCookie(`human_behavior_end_user_id_13c3e029-ca45-4a3c-a33b-f5dcb297e31c`) : 'Method not available'}<br>
561
+ Session Cookie: ${tracker.getCookie ? tracker.getCookie('human_behavior_session_id') : 'Method not available'}
562
+ `;
563
+
564
+ // Show in an alert or add to logs
565
+ addLog('Detailed user info displayed', userInfo);
566
+ alert(infoText);
567
+ } else {
568
+ addLog('getUserInfo method not available');
569
+ }
570
+ } catch (error) {
571
+ addLog(`Error getting user info: ${error.message}`);
572
+ }
573
+ }
574
+
575
+ // Add log message
576
+ function addLog(message, data = null) {
577
+ const logElements = document.querySelectorAll('.logs');
578
+ const timestamp = new Date().toLocaleTimeString();
579
+ const logEntry = `<div>[${timestamp}] ${message}${data ? ' - ' + JSON.stringify(data) : ''}</div>`;
580
+
581
+ logElements.forEach(el => {
582
+ el.innerHTML += logEntry;
583
+ el.scrollTop = el.scrollHeight;
584
+ });
585
+
586
+ // Also log to console
587
+ console.log(`[${timestamp}] ${message}`, data);
588
+ }
589
+
590
+ // Initialize the app when the page loads
591
+ document.addEventListener('DOMContentLoaded', initApp);
592
+ </script>
593
+ </body>
594
+ </html>