humanbehavior-js 0.5.66 → 0.5.67
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/README.md +493 -102
- package/package.json +1 -1
- package/packages/browser/dist/cjs/index.js +2 -2
- package/packages/browser/dist/cjs/index.js.map +1 -1
- package/packages/browser/dist/esm/index.js +2 -2
- package/packages/browser/dist/esm/index.js.map +1 -1
- package/packages/browser/dist/index.min.js +1 -1
- package/packages/browser/dist/index.min.js.map +1 -1
- package/packages/core/dist/api.d.ts +1 -1
- package/packages/core/dist/api.d.ts.map +1 -1
- package/packages/core/dist/index.js +1 -1
- package/packages/core/dist/index.js.map +1 -1
- package/packages/core/dist/index.mjs +1 -1
- package/packages/core/dist/index.mjs.map +1 -1
- package/packages/core/dist/tracker.d.ts.map +1 -1
- package/packages/react/dist/index.js +1 -1
- package/packages/react/dist/index.js.map +1 -1
- package/packages/react/dist/index.mjs +1 -1
- package/packages/react/dist/index.mjs.map +1 -1
package/README.md
CHANGED
|
@@ -1,25 +1,89 @@
|
|
|
1
|
-
# HumanBehavior JS - Modular
|
|
1
|
+
# HumanBehavior JS - Modular SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A modular JavaScript SDK for recording user sessions and tracking events with high-fidelity session replay, automatic event tracking, and comprehensive analytics capabilities.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Architecture
|
|
6
|
+
|
|
7
|
+
This SDK is built as a **monorepo** using npm workspaces and Turbo, organized into separate packages for optimal tree-shaking, maintainability, and framework-specific optimizations.
|
|
6
8
|
|
|
7
|
-
###
|
|
9
|
+
### Package Structure
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
```
|
|
12
|
+
humanbehavior-js/
|
|
13
|
+
├── packages/
|
|
14
|
+
│ ├── browser/ # Main entry point (re-exports from core)
|
|
15
|
+
│ ├── core/ # Core SDK functionality
|
|
16
|
+
│ ├── react/ # React hooks and context provider
|
|
17
|
+
│ └── wizard/ # AI-enhanced installation wizard
|
|
18
|
+
└── tooling/ # Shared build tools and configs
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Core Packages
|
|
22
|
+
|
|
23
|
+
#### `humanbehavior-js` (Main Package)
|
|
24
|
+
The primary package that provides everything you need out of the box. Re-exports from `@humanbehavior/core` and provides UMD builds for direct browser usage.
|
|
25
|
+
|
|
26
|
+
**Exports:**
|
|
27
|
+
- `HumanBehaviorTracker` - Main tracker class
|
|
28
|
+
- `init()` - Initialization helper
|
|
29
|
+
- Framework integrations via subpaths: `/react`, `/core`, `/wizard`
|
|
30
|
+
|
|
31
|
+
#### `@humanbehavior/core`
|
|
32
|
+
Core SDK functionality including:
|
|
33
|
+
- **Session Recording**: High-fidelity session replay using rrweb
|
|
34
|
+
- **Event Tracking**: Automatic and manual event tracking
|
|
35
|
+
- **API Client**: Communication with HumanBehavior ingestion servers
|
|
36
|
+
- **Redaction Manager**: Privacy-first data redaction
|
|
37
|
+
- **Persistence Layer**: Session persistence across page reloads
|
|
38
|
+
- **Retry Queue**: Reliable event delivery with retry logic
|
|
39
|
+
- **Utilities**: Logger, property manager, global tracker
|
|
40
|
+
|
|
41
|
+
#### `@humanbehavior/react`
|
|
42
|
+
React-specific integrations:
|
|
43
|
+
- `HumanBehaviorProvider` - Context provider for React apps
|
|
44
|
+
- `useHumanBehavior()` - Hook to access tracker instance
|
|
45
|
+
- `useRedaction()` - Hook for managing redaction fields
|
|
46
|
+
- `useUserTracking()` - Hook for user identification
|
|
47
|
+
- Automatic page view tracking for SPAs
|
|
48
|
+
|
|
49
|
+
#### `@humanbehavior/wizard`
|
|
50
|
+
AI-enhanced installation wizard:
|
|
51
|
+
- Framework detection and auto-installation
|
|
52
|
+
- Code modification suggestions
|
|
53
|
+
- CLI tools for setup automation
|
|
54
|
+
- Centralized AI service integration
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
### Quick Start (Recommended)
|
|
10
59
|
|
|
11
60
|
```bash
|
|
12
61
|
npm install humanbehavior-js
|
|
13
62
|
```
|
|
14
63
|
|
|
15
|
-
This
|
|
16
|
-
- Core session recording
|
|
64
|
+
This single package includes everything you need:
|
|
65
|
+
- Core session recording
|
|
17
66
|
- Automatic event tracking
|
|
18
67
|
- User identification
|
|
19
68
|
- Data redaction
|
|
20
69
|
- Session persistence
|
|
70
|
+
- Framework integrations
|
|
71
|
+
|
|
72
|
+
### Framework-Specific Packages (Optional)
|
|
73
|
+
|
|
74
|
+
For better tree-shaking, you can install framework-specific packages:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
# React
|
|
78
|
+
npm install @humanbehavior/react
|
|
79
|
+
|
|
80
|
+
# Or use subpath imports
|
|
81
|
+
import { useHumanBehavior } from 'humanbehavior-js/react';
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Quick Start
|
|
21
85
|
|
|
22
|
-
###
|
|
86
|
+
### Vanilla JavaScript
|
|
23
87
|
|
|
24
88
|
```javascript
|
|
25
89
|
import { HumanBehaviorTracker } from 'humanbehavior-js';
|
|
@@ -28,165 +92,491 @@ const tracker = HumanBehaviorTracker.init('your-api-key');
|
|
|
28
92
|
tracker.start();
|
|
29
93
|
```
|
|
30
94
|
|
|
31
|
-
|
|
95
|
+
### React
|
|
32
96
|
|
|
33
|
-
|
|
97
|
+
```jsx
|
|
98
|
+
import { HumanBehaviorProvider, useHumanBehavior } from 'humanbehavior-js/react';
|
|
99
|
+
|
|
100
|
+
function App() {
|
|
101
|
+
return (
|
|
102
|
+
<HumanBehaviorProvider apiKey="your-api-key">
|
|
103
|
+
<YourApp />
|
|
104
|
+
</HumanBehaviorProvider>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
34
107
|
|
|
35
|
-
|
|
108
|
+
function MyComponent() {
|
|
109
|
+
const tracker = useHumanBehavior();
|
|
110
|
+
|
|
111
|
+
const handleClick = () => {
|
|
112
|
+
tracker.customEvent('button_clicked', { buttonId: 'signup' });
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
return <button onClick={handleClick}>Sign Up</button>;
|
|
116
|
+
}
|
|
117
|
+
```
|
|
36
118
|
|
|
37
|
-
###
|
|
119
|
+
### UMD Build (Browser)
|
|
38
120
|
|
|
39
|
-
|
|
121
|
+
```html
|
|
122
|
+
<script src="https://unpkg.com/humanbehavior-js"></script>
|
|
123
|
+
<script>
|
|
124
|
+
const tracker = HumanBehaviorTracker.init('your-api-key');
|
|
125
|
+
tracker.start();
|
|
126
|
+
</script>
|
|
127
|
+
```
|
|
40
128
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
-
|
|
45
|
-
-
|
|
129
|
+
## Key Features
|
|
130
|
+
|
|
131
|
+
### 🎥 Session Recording
|
|
132
|
+
- **High-fidelity replay** using rrweb
|
|
133
|
+
- Captures mouse movements, clicks, scrolls, keyboard input
|
|
134
|
+
- Canvas recording support (optional)
|
|
135
|
+
- Multi-window tracking
|
|
136
|
+
- Session persistence across page reloads (30-minute window)
|
|
137
|
+
|
|
138
|
+
### 📊 Event Tracking
|
|
139
|
+
- **Automatic tracking** of buttons, links, and forms
|
|
140
|
+
- Custom event tracking
|
|
141
|
+
- Console and network error tracking
|
|
142
|
+
- Navigation tracking for SPAs
|
|
143
|
+
- Rage click and dead click detection
|
|
144
|
+
|
|
145
|
+
### 🔒 Privacy & Security
|
|
146
|
+
- **Privacy-first redaction** by default
|
|
147
|
+
- Configurable redaction strategies
|
|
148
|
+
- Field-level data masking
|
|
149
|
+
- Unredaction for specific fields when needed
|
|
150
|
+
|
|
151
|
+
### 👤 User Identification
|
|
152
|
+
- User property tracking
|
|
153
|
+
- Session-to-user association
|
|
154
|
+
- Global user identification across sessions
|
|
155
|
+
|
|
156
|
+
### 🚀 Performance
|
|
157
|
+
- Event batching and queueing
|
|
158
|
+
- Automatic retry on failures
|
|
159
|
+
- Configurable queue sizes
|
|
160
|
+
- Tree-shakeable modules
|
|
161
|
+
|
|
162
|
+
## Configuration Options
|
|
46
163
|
|
|
47
|
-
|
|
164
|
+
```javascript
|
|
165
|
+
const tracker = HumanBehaviorTracker.init('your-api-key', {
|
|
166
|
+
ingestionUrl: 'https://your-ingestion-url.com',
|
|
167
|
+
redactionStrategy: {
|
|
168
|
+
mode: 'privacy-first', // or 'visibility-first'
|
|
169
|
+
unredactFields: ['email', 'name'] // Fields to keep visible
|
|
170
|
+
},
|
|
171
|
+
enableAutomaticTracking: true,
|
|
172
|
+
automaticTrackingOptions: {
|
|
173
|
+
trackButtons: true,
|
|
174
|
+
trackLinks: true,
|
|
175
|
+
trackForms: true,
|
|
176
|
+
includeText: true,
|
|
177
|
+
includeClasses: true
|
|
178
|
+
},
|
|
179
|
+
recordCanvas: false, // Enable canvas recording
|
|
180
|
+
maxQueueSize: 1000, // Maximum events in queue
|
|
181
|
+
enableConsoleTracking: true, // Track console errors
|
|
182
|
+
enableNetworkTracking: true // Track network errors
|
|
183
|
+
});
|
|
184
|
+
```
|
|
48
185
|
|
|
49
|
-
|
|
50
|
-
- **`@humanbehavior/wizard`** - Installation wizard and CLI tools
|
|
186
|
+
## API Reference
|
|
51
187
|
|
|
52
|
-
|
|
188
|
+
### Core Methods
|
|
53
189
|
|
|
54
|
-
|
|
190
|
+
```javascript
|
|
191
|
+
// Initialize
|
|
192
|
+
const tracker = HumanBehaviorTracker.init(apiKey, options);
|
|
55
193
|
|
|
56
|
-
|
|
57
|
-
|
|
194
|
+
// Start/Stop recording
|
|
195
|
+
tracker.start();
|
|
196
|
+
tracker.stop();
|
|
197
|
+
|
|
198
|
+
// User identification
|
|
199
|
+
tracker.identifyUser({ userProperties: { email: 'user@example.com' } });
|
|
200
|
+
|
|
201
|
+
// Custom events
|
|
202
|
+
tracker.customEvent('event_name', { property: 'value' });
|
|
203
|
+
|
|
204
|
+
// Redaction
|
|
205
|
+
tracker.setUnredactedFields(['email', 'name']);
|
|
206
|
+
tracker.getUnredactedFields();
|
|
207
|
+
|
|
208
|
+
// Session info
|
|
209
|
+
tracker.getSessionId();
|
|
210
|
+
tracker.getCurrentUrl();
|
|
58
211
|
```
|
|
59
212
|
|
|
60
|
-
|
|
213
|
+
## API Endpoints
|
|
214
|
+
|
|
215
|
+
The SDK communicates with HumanBehavior ingestion servers through the following endpoints. All requests include authentication via `Authorization: Bearer {apiKey}` header.
|
|
216
|
+
|
|
217
|
+
### `POST /api/ingestion/events`
|
|
218
|
+
|
|
219
|
+
**When:** Called automatically when events are batched (every 3 seconds) or when queue reaches maximum size
|
|
220
|
+
|
|
221
|
+
**Request Body:**
|
|
222
|
+
```json
|
|
223
|
+
{
|
|
224
|
+
"sessionId": "string",
|
|
225
|
+
"events": [
|
|
226
|
+
{
|
|
227
|
+
"type": "string",
|
|
228
|
+
"timestamp": number,
|
|
229
|
+
"data": {...},
|
|
230
|
+
...
|
|
231
|
+
}
|
|
232
|
+
],
|
|
233
|
+
"endUserId": "string | null",
|
|
234
|
+
"windowId": "string (optional)",
|
|
235
|
+
"automaticProperties": {...} (optional),
|
|
236
|
+
"sdkVersion": "string"
|
|
237
|
+
}
|
|
238
|
+
```
|
|
61
239
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
240
|
+
**Headers:**
|
|
241
|
+
- `Authorization: Bearer {apiKey}`
|
|
242
|
+
- `Content-Type: application/json`
|
|
243
|
+
|
|
244
|
+
**Response:**
|
|
245
|
+
```json
|
|
246
|
+
{
|
|
247
|
+
"success": true,
|
|
248
|
+
"appended": number,
|
|
249
|
+
"sessionCreated": boolean,
|
|
250
|
+
"monthlyLimitReached": boolean (optional)
|
|
251
|
+
}
|
|
252
|
+
```
|
|
65
253
|
|
|
66
|
-
|
|
67
|
-
|
|
254
|
+
**Purpose:** Sends session replay events (rrweb events) and custom events to the server. Supports chunking for large payloads (max 1MB per chunk). Events are automatically batched and sent every 3 seconds. **Sessions are created automatically on the server when the first event is received** (PostHog-style, no separate init call needed).
|
|
255
|
+
|
|
256
|
+
**Special Features:**
|
|
257
|
+
- Automatic chunking for payloads > 1MB
|
|
258
|
+
- Retry logic with exponential backoff
|
|
259
|
+
- Persistence to localStorage for offline scenarios
|
|
260
|
+
- Falls back to `sendBeacon` on page unload or CSP violations
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
### `POST /api/ingestion/user`
|
|
265
|
+
|
|
266
|
+
**When:** Called when `tracker.identifyUser()` is invoked
|
|
68
267
|
|
|
69
|
-
|
|
70
|
-
|
|
268
|
+
**Request Body:**
|
|
269
|
+
```json
|
|
270
|
+
{
|
|
271
|
+
"userId": "string",
|
|
272
|
+
"userAttributes": {
|
|
273
|
+
"email": "string",
|
|
274
|
+
"name": "string",
|
|
275
|
+
...
|
|
276
|
+
},
|
|
277
|
+
"sessionId": "string",
|
|
278
|
+
"posthogName": "string | null"
|
|
279
|
+
}
|
|
71
280
|
```
|
|
72
281
|
|
|
73
|
-
|
|
282
|
+
**Headers:**
|
|
283
|
+
- `Authorization: Bearer {apiKey}`
|
|
284
|
+
- `Content-Type: application/json`
|
|
74
285
|
|
|
75
|
-
|
|
286
|
+
**Response:**
|
|
287
|
+
```json
|
|
288
|
+
{
|
|
289
|
+
"success": true,
|
|
290
|
+
"userId": "string",
|
|
291
|
+
"sessionId": "string"
|
|
292
|
+
}
|
|
293
|
+
```
|
|
76
294
|
|
|
77
|
-
|
|
78
|
-
# React
|
|
79
|
-
npm install @humanbehavior/react
|
|
295
|
+
**Purpose:** Identifies and associates user properties with a session. Creates or updates user profile on the server.
|
|
80
296
|
|
|
81
|
-
|
|
82
|
-
npm install @humanbehavior/vue
|
|
297
|
+
---
|
|
83
298
|
|
|
84
|
-
|
|
85
|
-
npm install @humanbehavior/svelte
|
|
299
|
+
### `POST /api/ingestion/user/auth`
|
|
86
300
|
|
|
87
|
-
|
|
88
|
-
|
|
301
|
+
**When:** Called when `tracker.auth()` is invoked
|
|
302
|
+
|
|
303
|
+
**Request Body:**
|
|
304
|
+
```json
|
|
305
|
+
{
|
|
306
|
+
"userId": "string",
|
|
307
|
+
"userAttributes": {
|
|
308
|
+
"email": "string",
|
|
309
|
+
"name": "string",
|
|
310
|
+
...
|
|
311
|
+
},
|
|
312
|
+
"sessionId": "string",
|
|
313
|
+
"authFields": ["email", "password", ...]
|
|
314
|
+
}
|
|
315
|
+
```
|
|
89
316
|
|
|
90
|
-
|
|
91
|
-
|
|
317
|
+
**Headers:**
|
|
318
|
+
- `Authorization: Bearer {apiKey}`
|
|
319
|
+
- `Content-Type: application/json`
|
|
320
|
+
|
|
321
|
+
**Response:**
|
|
322
|
+
```json
|
|
323
|
+
{
|
|
324
|
+
"success": true,
|
|
325
|
+
"message": "User authenticated",
|
|
326
|
+
"userId": "string",
|
|
327
|
+
"sessionId": "string"
|
|
328
|
+
}
|
|
92
329
|
```
|
|
93
330
|
|
|
94
|
-
|
|
331
|
+
**Purpose:** Authenticates a user and marks specific fields as authentication-related for enhanced security handling.
|
|
95
332
|
|
|
96
|
-
|
|
333
|
+
---
|
|
97
334
|
|
|
98
|
-
|
|
99
|
-
import { HumanBehaviorTracker } from 'humanbehavior-js';
|
|
335
|
+
### `POST /api/ingestion/customEvent`
|
|
100
336
|
|
|
101
|
-
|
|
102
|
-
|
|
337
|
+
**When:** Called when `tracker.customEvent()` is invoked
|
|
338
|
+
|
|
339
|
+
**Request Body:**
|
|
340
|
+
```json
|
|
341
|
+
{
|
|
342
|
+
"sessionId": "string",
|
|
343
|
+
"eventName": "string",
|
|
344
|
+
"eventProperties": {
|
|
345
|
+
"property1": "value1",
|
|
346
|
+
...
|
|
347
|
+
},
|
|
348
|
+
"endUserId": "string | null"
|
|
349
|
+
}
|
|
103
350
|
```
|
|
104
351
|
|
|
105
|
-
|
|
352
|
+
**Headers:**
|
|
353
|
+
- `Authorization: Bearer {apiKey}`
|
|
354
|
+
- `Content-Type: application/json`
|
|
106
355
|
|
|
107
|
-
|
|
108
|
-
|
|
356
|
+
**Response:**
|
|
357
|
+
```json
|
|
358
|
+
{
|
|
359
|
+
"success": true,
|
|
360
|
+
"eventId": "string"
|
|
361
|
+
}
|
|
362
|
+
```
|
|
109
363
|
|
|
110
|
-
|
|
111
|
-
const tracker = useHumanBehavior();
|
|
112
|
-
|
|
113
|
-
const handleClick = () => {
|
|
114
|
-
tracker.customEvent('button_clicked');
|
|
115
|
-
};
|
|
364
|
+
**Purpose:** Sends a single custom event to the server for analytics tracking.
|
|
116
365
|
|
|
117
|
-
|
|
366
|
+
---
|
|
367
|
+
|
|
368
|
+
### `POST /api/ingestion/customEvent/batch`
|
|
369
|
+
|
|
370
|
+
**When:** Called when multiple custom events need to be sent together
|
|
371
|
+
|
|
372
|
+
**Request Body:**
|
|
373
|
+
```json
|
|
374
|
+
{
|
|
375
|
+
"sessionId": "string",
|
|
376
|
+
"events": [
|
|
377
|
+
{
|
|
378
|
+
"eventName": "string",
|
|
379
|
+
"eventProperties": {...}
|
|
380
|
+
},
|
|
381
|
+
...
|
|
382
|
+
],
|
|
383
|
+
"endUserId": "string | null"
|
|
118
384
|
}
|
|
119
385
|
```
|
|
120
386
|
|
|
121
|
-
|
|
387
|
+
**Headers:**
|
|
388
|
+
- `Authorization: Bearer {apiKey}`
|
|
389
|
+
- `Content-Type: application/json`
|
|
122
390
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
391
|
+
**Response:**
|
|
392
|
+
```json
|
|
393
|
+
{
|
|
394
|
+
"success": true,
|
|
395
|
+
"eventIds": ["string", ...]
|
|
396
|
+
}
|
|
397
|
+
```
|
|
126
398
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
399
|
+
**Purpose:** Sends multiple custom events in a single request for better efficiency.
|
|
400
|
+
|
|
401
|
+
---
|
|
402
|
+
|
|
403
|
+
### `POST /api/ingestion/logs`
|
|
404
|
+
|
|
405
|
+
**When:** Called automatically when console warnings or errors occur (if `enableConsoleTracking` is true)
|
|
406
|
+
|
|
407
|
+
**Request Body:**
|
|
408
|
+
```json
|
|
409
|
+
{
|
|
410
|
+
"level": "warn" | "error",
|
|
411
|
+
"message": "string",
|
|
412
|
+
"stack": "string (optional)",
|
|
413
|
+
"url": "string",
|
|
414
|
+
"timestampMs": number,
|
|
415
|
+
"sessionId": "string",
|
|
416
|
+
"endUserId": "string | null"
|
|
417
|
+
}
|
|
131
418
|
```
|
|
132
419
|
|
|
133
|
-
|
|
420
|
+
**Headers:**
|
|
421
|
+
- `Authorization: Bearer {apiKey}`
|
|
422
|
+
- `Content-Type: application/json`
|
|
134
423
|
|
|
135
|
-
|
|
136
|
-
|
|
424
|
+
**Response:**
|
|
425
|
+
```json
|
|
426
|
+
{
|
|
427
|
+
"success": true
|
|
428
|
+
}
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
**Purpose:** Tracks console warnings and errors for debugging and monitoring. Only sends `warn` and `error` level logs, not `log` or `info`.
|
|
432
|
+
|
|
433
|
+
---
|
|
434
|
+
|
|
435
|
+
### `POST /api/ingestion/network`
|
|
436
|
+
|
|
437
|
+
**When:** Called automatically when network requests fail (4xx, 5xx errors) or encounter network errors (if `enableNetworkTracking` is true)
|
|
438
|
+
|
|
439
|
+
**Request Body:**
|
|
440
|
+
```json
|
|
441
|
+
{
|
|
442
|
+
"requestId": "string",
|
|
443
|
+
"url": "string",
|
|
444
|
+
"method": "string",
|
|
445
|
+
"status": number | null,
|
|
446
|
+
"statusText": "string | null",
|
|
447
|
+
"duration": number,
|
|
448
|
+
"timestampMs": number,
|
|
449
|
+
"sessionId": "string",
|
|
450
|
+
"endUserId": "string | null",
|
|
451
|
+
"errorType": "client_error" | "server_error" | "network_error" | "timeout_error" | "cors_error" | "csp_violation" | "blocked_by_client" | "unknown_error",
|
|
452
|
+
"errorMessage": "string | null",
|
|
453
|
+
"errorName": "string | null",
|
|
454
|
+
"startTimeMs": number (optional),
|
|
455
|
+
"spanName": "string (optional)",
|
|
456
|
+
"spanStatus": "error" | "success" | "slow" (optional),
|
|
457
|
+
"attributes": {
|
|
458
|
+
"http.status_code": number,
|
|
459
|
+
"http.status_text": "string",
|
|
460
|
+
...
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
```
|
|
137
464
|
|
|
138
|
-
|
|
465
|
+
**Headers:**
|
|
466
|
+
- `Authorization: Bearer {apiKey}`
|
|
467
|
+
- `Content-Type: application/json`
|
|
468
|
+
|
|
469
|
+
**Response:**
|
|
470
|
+
```json
|
|
471
|
+
{
|
|
472
|
+
"success": true
|
|
473
|
+
}
|
|
139
474
|
```
|
|
140
475
|
|
|
476
|
+
**Purpose:** Tracks network errors and failed HTTP requests for monitoring and debugging. Automatically classifies error types (CORS, timeout, CSP violations, etc.). **Note:** SDK's own requests to ingestion endpoints are excluded from tracking to avoid recursion.
|
|
477
|
+
|
|
478
|
+
---
|
|
479
|
+
|
|
480
|
+
## Request Flow & Timing
|
|
481
|
+
|
|
482
|
+
1. **Initialization**: SDK creates session ID locally (no server call)
|
|
483
|
+
2. **Event Batching**: `POST /api/ingestion/events` is called:
|
|
484
|
+
- Every 3 seconds (automatic flush interval)
|
|
485
|
+
- When event queue reaches maximum size
|
|
486
|
+
- On page unload (via `sendBeacon` if available)
|
|
487
|
+
3. **User Actions**: `POST /api/ingestion/user` or `/user/auth` when user identification occurs
|
|
488
|
+
4. **Custom Events**: `POST /api/ingestion/customEvent` immediately when `customEvent()` is called
|
|
489
|
+
5. **Error Tracking**: `POST /api/ingestion/logs` and `/network` are called asynchronously when errors occur
|
|
490
|
+
|
|
491
|
+
## Error Handling
|
|
492
|
+
|
|
493
|
+
- **429 (Rate Limit)**: SDK sets `monthlyLimitReached` flag and stops sending events
|
|
494
|
+
- **413 (Payload Too Large)**: SDK automatically reduces batch size and retries
|
|
495
|
+
- **Network Errors**: Events are persisted to localStorage and retried with exponential backoff
|
|
496
|
+
- **CSP Violations**: SDK automatically falls back to `sendBeacon` API
|
|
497
|
+
- **Offline**: Events are queued and sent when connection is restored
|
|
498
|
+
|
|
141
499
|
## Development
|
|
142
500
|
|
|
501
|
+
### Prerequisites
|
|
502
|
+
- Node.js 18+
|
|
503
|
+
- npm 10+
|
|
504
|
+
|
|
143
505
|
### Setup
|
|
506
|
+
|
|
144
507
|
```bash
|
|
508
|
+
# Install dependencies
|
|
145
509
|
npm install
|
|
146
|
-
```
|
|
147
510
|
|
|
148
|
-
|
|
149
|
-
```bash
|
|
511
|
+
# Build all packages
|
|
150
512
|
npm run build
|
|
151
|
-
```
|
|
152
513
|
|
|
153
|
-
|
|
154
|
-
```bash
|
|
514
|
+
# Build specific package
|
|
155
515
|
npm run build --workspace=@humanbehavior/core
|
|
156
|
-
```
|
|
157
516
|
|
|
158
|
-
|
|
159
|
-
```bash
|
|
517
|
+
# Development mode (watch)
|
|
160
518
|
npm run dev
|
|
161
|
-
```
|
|
162
519
|
|
|
163
|
-
|
|
520
|
+
# Linting
|
|
521
|
+
npm run lint
|
|
522
|
+
npm run lint:fix
|
|
523
|
+
|
|
524
|
+
# Testing
|
|
525
|
+
npm run test
|
|
526
|
+
```
|
|
164
527
|
|
|
165
|
-
|
|
528
|
+
### Project Structure
|
|
166
529
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
5. **Better maintainability**: Clear separation of concerns
|
|
172
|
-
6. **Reduced bundle size**: Smaller packages for specific use cases
|
|
173
|
-
7. **Easier testing**: Isolated packages are easier to test
|
|
530
|
+
- **Monorepo**: Uses npm workspaces for package management
|
|
531
|
+
- **Build System**: Turbo for fast, cached builds
|
|
532
|
+
- **Bundling**: Rollup for ESM, CJS, and UMD outputs
|
|
533
|
+
- **TypeScript**: Full TypeScript support with type definitions
|
|
174
534
|
|
|
175
|
-
|
|
535
|
+
### Package Exports
|
|
176
536
|
|
|
177
|
-
The
|
|
537
|
+
The main package uses modern package.json exports:
|
|
178
538
|
|
|
179
539
|
```javascript
|
|
180
|
-
//
|
|
181
|
-
import { HumanBehaviorTracker } from 'humanbehavior-js';
|
|
182
|
-
|
|
183
|
-
// New - Same as before!
|
|
540
|
+
// Main entry
|
|
184
541
|
import { HumanBehaviorTracker } from 'humanbehavior-js';
|
|
185
542
|
|
|
186
|
-
//
|
|
543
|
+
// React integration
|
|
187
544
|
import { useHumanBehavior } from 'humanbehavior-js/react';
|
|
545
|
+
|
|
546
|
+
// Core package (advanced)
|
|
547
|
+
import { HumanBehaviorTracker } from 'humanbehavior-js/core';
|
|
548
|
+
|
|
549
|
+
// Wizard (CLI)
|
|
550
|
+
import { AIAutoInstallCLI } from 'humanbehavior-js/wizard';
|
|
188
551
|
```
|
|
189
552
|
|
|
553
|
+
## Architecture Benefits
|
|
554
|
+
|
|
555
|
+
1. **Simple Start**: Install one package and everything works
|
|
556
|
+
2. **Progressive Enhancement**: Add framework-specific features as needed
|
|
557
|
+
3. **Tree-shaking**: Only bundle what you use
|
|
558
|
+
4. **Framework Optimizations**: Each framework package is optimized for its target
|
|
559
|
+
5. **Better Maintainability**: Clear separation of concerns
|
|
560
|
+
6. **Reduced Bundle Size**: Smaller packages for specific use cases
|
|
561
|
+
7. **Easier Testing**: Isolated packages are easier to test
|
|
562
|
+
|
|
563
|
+
## Browser Support
|
|
564
|
+
|
|
565
|
+
- Chrome/Edge (latest)
|
|
566
|
+
- Firefox (latest)
|
|
567
|
+
- Safari (latest)
|
|
568
|
+
- Mobile browsers (iOS Safari, Chrome Mobile)
|
|
569
|
+
|
|
570
|
+
## License
|
|
571
|
+
|
|
572
|
+
ISC
|
|
573
|
+
|
|
574
|
+
## Links
|
|
575
|
+
|
|
576
|
+
- **Documentation**: https://documentation.humanbehavior.co
|
|
577
|
+
- **Repository**: https://github.com/humanbehavior-gh/humanbehavior-js
|
|
578
|
+
- **Homepage**: https://documentation.humanbehavior.co
|
|
579
|
+
|
|
190
580
|
## Contributing
|
|
191
581
|
|
|
192
582
|
1. Fork the repository
|
|
@@ -195,6 +585,7 @@ import { useHumanBehavior } from 'humanbehavior-js/react';
|
|
|
195
585
|
4. Add tests
|
|
196
586
|
5. Submit a pull request
|
|
197
587
|
|
|
198
|
-
|
|
588
|
+
---
|
|
589
|
+
|
|
590
|
+
Built with ❤️ by the HumanBehavior team
|
|
199
591
|
|
|
200
|
-
ISC
|