cdp-lite-sdk 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/README.md +846 -0
- package/dist/cdp-lite-sdk.umd.js +407 -0
- package/dist/cdp-lite-sdk.umd.js.map +1 -0
- package/dist/cdp-lite-sdk.umd.min.js +2 -0
- package/dist/cdp-lite-sdk.umd.min.js.map +1 -0
- package/dist/index.cjs.js +401 -0
- package/dist/index.cjs.js.map +1 -0
- package/{src/cdp-lite-sdk.js → dist/index.esm.js} +12 -9
- package/dist/index.esm.js.map +1 -0
- package/package.json +43 -10
package/README.md
ADDED
|
@@ -0,0 +1,846 @@
|
|
|
1
|
+
# CDP Lite SDK 🚀
|
|
2
|
+
|
|
3
|
+
**Customer Data Platform SDK** for tracking events and users. A lightweight, powerful analytics SDK for JavaScript/TypeScript applications.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/cdp-lite-sdk)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## 📋 Table of Contents
|
|
9
|
+
|
|
10
|
+
- [Features](#features)
|
|
11
|
+
- [Installation](#installation)
|
|
12
|
+
- [Quick Start](#quick-start)
|
|
13
|
+
- [Configuration](#configuration)
|
|
14
|
+
- [Core Methods](#core-methods)
|
|
15
|
+
- [Advanced Usage](#advanced-usage)
|
|
16
|
+
- [React Integration](#react-integration)
|
|
17
|
+
- [TypeScript Support](#typescript-support)
|
|
18
|
+
- [API Reference](#api-reference)
|
|
19
|
+
- [Examples](#examples)
|
|
20
|
+
- [Troubleshooting](#troubleshooting)
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## ✨ Features
|
|
25
|
+
|
|
26
|
+
- ✅ **Event Tracking** - Track custom events with properties
|
|
27
|
+
- ✅ **User Identification** - Identify and track users
|
|
28
|
+
- ✅ **Batch Processing** - Auto-batch events for optimal performance
|
|
29
|
+
- ✅ **Device Detection** - Auto-detect device information
|
|
30
|
+
- ✅ **Anonymous Tracking** - Track users before identification
|
|
31
|
+
- ✅ **Page/Screen Views** - Built-in page and screen tracking
|
|
32
|
+
- ✅ **TypeScript Support** - Full TypeScript definitions
|
|
33
|
+
- ✅ **Framework Agnostic** - Works with React, Vue, Angular, vanilla JS
|
|
34
|
+
- ✅ **Browser & Node.js** - Universal JavaScript support
|
|
35
|
+
- ✅ **Queue Management** - Smart event queueing and flushing
|
|
36
|
+
- ✅ **Debug Mode** - Easy debugging with console logs
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 📦 Installation
|
|
41
|
+
|
|
42
|
+
### NPM
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm install cdp-lite-sdk
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Yarn
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
yarn add cdp-lite-sdk
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### CDN (Browser)
|
|
55
|
+
|
|
56
|
+
```html
|
|
57
|
+
<script src="https://cdn.jsdelivr.net/npm/cdp-lite-sdk/dist/cdp-lite-sdk.min.js"></script>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## 🚀 Quick Start
|
|
63
|
+
|
|
64
|
+
### ES6 Import (Recommended)
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import CdpLiteSdk from 'cdp-lite-sdk';
|
|
68
|
+
|
|
69
|
+
// Initialize SDK
|
|
70
|
+
const cdp = new CdpLiteSdk({
|
|
71
|
+
apiKey: 'your-api-key',
|
|
72
|
+
source: 'YourApp',
|
|
73
|
+
serviceName: 'YourService',
|
|
74
|
+
isTest: true,
|
|
75
|
+
debug: true
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Identify user
|
|
79
|
+
cdp.identify('user_123', {
|
|
80
|
+
name: 'John Doe',
|
|
81
|
+
email: 'john@example.com'
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Track event
|
|
85
|
+
cdp.track('button_clicked', {
|
|
86
|
+
button_name: 'Sign Up',
|
|
87
|
+
page: 'Landing'
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### CommonJS (Node.js)
|
|
92
|
+
|
|
93
|
+
```javascript
|
|
94
|
+
const CdpLiteSdk = require('cdp-lite-sdk');
|
|
95
|
+
|
|
96
|
+
const cdp = new CdpLiteSdk({
|
|
97
|
+
apiKey: 'your-api-key',
|
|
98
|
+
source: 'BackendService'
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Browser (Script Tag)
|
|
103
|
+
|
|
104
|
+
```html
|
|
105
|
+
<script src="path/to/cdp-lite-sdk.min.js"></script>
|
|
106
|
+
<script>
|
|
107
|
+
const cdp = new CdpLiteSdk({
|
|
108
|
+
apiKey: 'your-api-key',
|
|
109
|
+
source: 'WebApp'
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
cdp.track('page_view', { page: 'Home' });
|
|
113
|
+
</script>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## ⚙️ Configuration
|
|
119
|
+
|
|
120
|
+
### Configuration Options
|
|
121
|
+
|
|
122
|
+
```javascript
|
|
123
|
+
const cdp = new CdpLiteSdk({
|
|
124
|
+
// Required
|
|
125
|
+
apiKey: 'string', // Your API key
|
|
126
|
+
|
|
127
|
+
// Optional
|
|
128
|
+
source: 'string', // Source name (default: 'Web')
|
|
129
|
+
serviceName: 'string', // Service name (default: 'DefaultService')
|
|
130
|
+
baseUrl: 'string', // API base URL
|
|
131
|
+
isTest: boolean, // Test mode (default: false)
|
|
132
|
+
debug: boolean, // Enable debug logs (default: false)
|
|
133
|
+
batchSize: number, // Events per batch (default: 10)
|
|
134
|
+
batchInterval: number, // Batch interval in ms (default: 5000)
|
|
135
|
+
autoTrackDevice: boolean // Auto detect device (default: true)
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Example Configurations
|
|
140
|
+
|
|
141
|
+
#### Development
|
|
142
|
+
|
|
143
|
+
```javascript
|
|
144
|
+
const cdp = new CdpLiteSdk({
|
|
145
|
+
apiKey: 'dev-api-key',
|
|
146
|
+
source: 'MyApp',
|
|
147
|
+
serviceName: 'MyService',
|
|
148
|
+
baseUrl: 'https://vconnect-dev.vietcredit.com.vn',
|
|
149
|
+
isTest: true,
|
|
150
|
+
debug: true,
|
|
151
|
+
batchSize: 5,
|
|
152
|
+
batchInterval: 3000
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### Production
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
const cdp = new CdpLiteSdk({
|
|
160
|
+
apiKey: process.env.CDP_API_KEY,
|
|
161
|
+
source: 'MyApp',
|
|
162
|
+
serviceName: 'MyService',
|
|
163
|
+
baseUrl: 'https://vconnect.vietcredit.com.vn',
|
|
164
|
+
isTest: false,
|
|
165
|
+
debug: false,
|
|
166
|
+
batchSize: 20,
|
|
167
|
+
batchInterval: 10000
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## 📚 Core Methods
|
|
174
|
+
|
|
175
|
+
### 1. Track Events
|
|
176
|
+
|
|
177
|
+
Track custom events with properties.
|
|
178
|
+
|
|
179
|
+
```javascript
|
|
180
|
+
cdp.track(eventName, properties, options);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**Parameters:**
|
|
184
|
+
- `eventName` (string) - Name of the event
|
|
185
|
+
- `properties` (object) - Event properties (optional)
|
|
186
|
+
- `options` (object) - Additional options (optional)
|
|
187
|
+
|
|
188
|
+
**Example:**
|
|
189
|
+
|
|
190
|
+
```javascript
|
|
191
|
+
// Basic event
|
|
192
|
+
cdp.track('purchase_completed', {
|
|
193
|
+
product_id: 'PROD123',
|
|
194
|
+
amount: 99.99,
|
|
195
|
+
currency: 'USD'
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// With loan code
|
|
199
|
+
cdp.track('loan_application', {
|
|
200
|
+
amount: 50000000,
|
|
201
|
+
term: 12
|
|
202
|
+
}, {
|
|
203
|
+
loanCode: 'LOAN123'
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// With campaign data
|
|
207
|
+
cdp.track('signup', {
|
|
208
|
+
method: 'email'
|
|
209
|
+
}, {
|
|
210
|
+
campaign: {
|
|
211
|
+
source: 'facebook',
|
|
212
|
+
medium: 'cpc',
|
|
213
|
+
campaign: 'summer_sale'
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### 2. Identify Users
|
|
219
|
+
|
|
220
|
+
Identify users and set their attributes.
|
|
221
|
+
|
|
222
|
+
```javascript
|
|
223
|
+
cdp.identify(userId, traits);
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
**Parameters:**
|
|
227
|
+
- `userId` (string) - Unique user identifier
|
|
228
|
+
- `traits` (object) - User attributes (optional)
|
|
229
|
+
|
|
230
|
+
**Example:**
|
|
231
|
+
|
|
232
|
+
```javascript
|
|
233
|
+
cdp.identify('user_123', {
|
|
234
|
+
name: 'John Doe',
|
|
235
|
+
email: 'john@example.com',
|
|
236
|
+
phone: '+84901234567',
|
|
237
|
+
age: 30,
|
|
238
|
+
city: 'Hanoi',
|
|
239
|
+
plan: 'premium'
|
|
240
|
+
});
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### 3. Page Views
|
|
244
|
+
|
|
245
|
+
Track page views (for web applications).
|
|
246
|
+
|
|
247
|
+
```javascript
|
|
248
|
+
cdp.page(pageName, properties);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
**Example:**
|
|
252
|
+
|
|
253
|
+
```javascript
|
|
254
|
+
cdp.page('Home', {
|
|
255
|
+
url: window.location.href,
|
|
256
|
+
title: document.title,
|
|
257
|
+
referrer: document.referrer
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
cdp.page('Product Details', {
|
|
261
|
+
product_id: 'PROD123',
|
|
262
|
+
category: 'Electronics'
|
|
263
|
+
});
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### 4. Screen Views
|
|
267
|
+
|
|
268
|
+
Track screen views (for mobile applications).
|
|
269
|
+
|
|
270
|
+
```javascript
|
|
271
|
+
cdp.screen(screenName, properties);
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
**Example:**
|
|
275
|
+
|
|
276
|
+
```javascript
|
|
277
|
+
cdp.screen('Home Screen', {
|
|
278
|
+
tab: 'featured'
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
cdp.screen('Product Detail', {
|
|
282
|
+
product_id: 'PROD123'
|
|
283
|
+
});
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### 5. Update User Attributes
|
|
287
|
+
|
|
288
|
+
Update user attributes without re-identifying.
|
|
289
|
+
|
|
290
|
+
```javascript
|
|
291
|
+
cdp.setUserAttributes(traits);
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
**Example:**
|
|
295
|
+
|
|
296
|
+
```javascript
|
|
297
|
+
cdp.setUserAttributes({
|
|
298
|
+
subscription: 'premium',
|
|
299
|
+
last_login: new Date().toISOString(),
|
|
300
|
+
total_purchases: 5
|
|
301
|
+
});
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### 6. Set Device Info
|
|
305
|
+
|
|
306
|
+
Manually set device information.
|
|
307
|
+
|
|
308
|
+
```javascript
|
|
309
|
+
cdp.setDeviceInfo(deviceInfo);
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
**Example:**
|
|
313
|
+
|
|
314
|
+
```javascript
|
|
315
|
+
cdp.setDeviceInfo({
|
|
316
|
+
platform: 'ios',
|
|
317
|
+
brand: 'Apple',
|
|
318
|
+
model: 'iPhone 14 Pro',
|
|
319
|
+
app_version: '2.1.0',
|
|
320
|
+
os_version: '16.3'
|
|
321
|
+
});
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
### 7. Flush Events
|
|
325
|
+
|
|
326
|
+
Manually send all queued events immediately.
|
|
327
|
+
|
|
328
|
+
```javascript
|
|
329
|
+
await cdp.flush();
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
**Example:**
|
|
333
|
+
|
|
334
|
+
```javascript
|
|
335
|
+
// Add multiple events
|
|
336
|
+
cdp.track('event_1');
|
|
337
|
+
cdp.track('event_2');
|
|
338
|
+
cdp.track('event_3');
|
|
339
|
+
|
|
340
|
+
// Send all immediately
|
|
341
|
+
await cdp.flush();
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### 8. Reset User
|
|
345
|
+
|
|
346
|
+
Clear user data (useful for logout).
|
|
347
|
+
|
|
348
|
+
```javascript
|
|
349
|
+
cdp.reset();
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**Example:**
|
|
353
|
+
|
|
354
|
+
```javascript
|
|
355
|
+
function handleLogout() {
|
|
356
|
+
cdp.reset();
|
|
357
|
+
// Redirect to login page
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### 9. Cleanup
|
|
362
|
+
|
|
363
|
+
Clean up resources when done.
|
|
364
|
+
|
|
365
|
+
```javascript
|
|
366
|
+
cdp.destroy();
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
---
|
|
370
|
+
|
|
371
|
+
## 🔥 Advanced Usage
|
|
372
|
+
|
|
373
|
+
### Batch Processing
|
|
374
|
+
|
|
375
|
+
SDK automatically batches events for efficiency.
|
|
376
|
+
|
|
377
|
+
```javascript
|
|
378
|
+
const cdp = new CdpLiteSdk({
|
|
379
|
+
apiKey: 'your-api-key',
|
|
380
|
+
batchSize: 20, // Send after 20 events
|
|
381
|
+
batchInterval: 10000 // Or after 10 seconds
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
// These will be batched
|
|
385
|
+
cdp.track('event_1');
|
|
386
|
+
cdp.track('event_2');
|
|
387
|
+
cdp.track('event_3');
|
|
388
|
+
// ... will send when 20 events or 10 seconds
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
### Disable Batching
|
|
392
|
+
|
|
393
|
+
Send events immediately:
|
|
394
|
+
|
|
395
|
+
```javascript
|
|
396
|
+
const cdp = new CdpLiteSdk({
|
|
397
|
+
apiKey: 'your-api-key',
|
|
398
|
+
batchSize: 1 // Send immediately
|
|
399
|
+
});
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### Error Handling
|
|
403
|
+
|
|
404
|
+
```javascript
|
|
405
|
+
try {
|
|
406
|
+
await cdp.track('event_name', { data: 'value' });
|
|
407
|
+
console.log('Event tracked successfully');
|
|
408
|
+
} catch (error) {
|
|
409
|
+
console.error('Failed to track event:', error);
|
|
410
|
+
}
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### Custom Context
|
|
414
|
+
|
|
415
|
+
```javascript
|
|
416
|
+
cdp.track('checkout', {
|
|
417
|
+
total: 299.99
|
|
418
|
+
}, {
|
|
419
|
+
context: {
|
|
420
|
+
ip: '192.168.1.1',
|
|
421
|
+
locale: 'vi_VN',
|
|
422
|
+
timezone: 'Asia/Ho_Chi_Minh',
|
|
423
|
+
user_agent: navigator.userAgent
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
---
|
|
429
|
+
|
|
430
|
+
## ⚛️ React Integration
|
|
431
|
+
|
|
432
|
+
### Setup Analytics Context
|
|
433
|
+
|
|
434
|
+
```javascript
|
|
435
|
+
// analytics.js
|
|
436
|
+
import CdpLiteSdk from 'cdp-lite-sdk';
|
|
437
|
+
|
|
438
|
+
export const cdp = new CdpLiteSdk({
|
|
439
|
+
apiKey: process.env.REACT_APP_CDP_API_KEY,
|
|
440
|
+
source: 'WebApp',
|
|
441
|
+
serviceName: 'MyApp',
|
|
442
|
+
debug: process.env.NODE_ENV === 'development'
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
export default cdp;
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
### Custom Hook
|
|
449
|
+
|
|
450
|
+
```javascript
|
|
451
|
+
// useAnalytics.js
|
|
452
|
+
import { useCallback } from 'react';
|
|
453
|
+
import { cdp } from './analytics';
|
|
454
|
+
|
|
455
|
+
export function useAnalytics() {
|
|
456
|
+
const trackEvent = useCallback((eventName, properties) => {
|
|
457
|
+
cdp.track(eventName, properties);
|
|
458
|
+
}, []);
|
|
459
|
+
|
|
460
|
+
const identifyUser = useCallback((userId, traits) => {
|
|
461
|
+
cdp.identify(userId, traits);
|
|
462
|
+
}, []);
|
|
463
|
+
|
|
464
|
+
const trackPageView = useCallback((pageName, properties) => {
|
|
465
|
+
cdp.page(pageName, properties);
|
|
466
|
+
}, []);
|
|
467
|
+
|
|
468
|
+
return { trackEvent, identifyUser, trackPageView };
|
|
469
|
+
}
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
### Usage in Components
|
|
473
|
+
|
|
474
|
+
```javascript
|
|
475
|
+
// Component.jsx
|
|
476
|
+
import { useAnalytics } from './useAnalytics';
|
|
477
|
+
|
|
478
|
+
function ProductPage({ product }) {
|
|
479
|
+
const { trackEvent, trackPageView } = useAnalytics();
|
|
480
|
+
|
|
481
|
+
useEffect(() => {
|
|
482
|
+
trackPageView('Product Detail', {
|
|
483
|
+
product_id: product.id,
|
|
484
|
+
product_name: product.name
|
|
485
|
+
});
|
|
486
|
+
}, [product]);
|
|
487
|
+
|
|
488
|
+
const handleAddToCart = () => {
|
|
489
|
+
trackEvent('add_to_cart', {
|
|
490
|
+
product_id: product.id,
|
|
491
|
+
price: product.price
|
|
492
|
+
});
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
return (
|
|
496
|
+
<div>
|
|
497
|
+
<h1>{product.name}</h1>
|
|
498
|
+
<button onClick={handleAddToCart}>Add to Cart</button>
|
|
499
|
+
</div>
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
### Auto Page Tracking
|
|
505
|
+
|
|
506
|
+
```javascript
|
|
507
|
+
// App.jsx
|
|
508
|
+
import { useEffect } from 'react';
|
|
509
|
+
import { useLocation } from 'react-router-dom';
|
|
510
|
+
import { cdp } from './analytics';
|
|
511
|
+
|
|
512
|
+
function App() {
|
|
513
|
+
const location = useLocation();
|
|
514
|
+
|
|
515
|
+
useEffect(() => {
|
|
516
|
+
cdp.page(location.pathname, {
|
|
517
|
+
path: location.pathname,
|
|
518
|
+
search: location.search
|
|
519
|
+
});
|
|
520
|
+
}, [location]);
|
|
521
|
+
|
|
522
|
+
return <YourApp />;
|
|
523
|
+
}
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
---
|
|
527
|
+
|
|
528
|
+
## 🎯 TypeScript Support
|
|
529
|
+
|
|
530
|
+
Full TypeScript support with type definitions.
|
|
531
|
+
|
|
532
|
+
```typescript
|
|
533
|
+
import CdpLiteSdk, { CdpConfig, EventProperties, UserTraits } from 'cdp-lite-sdk';
|
|
534
|
+
|
|
535
|
+
const config: CdpConfig = {
|
|
536
|
+
apiKey: 'your-api-key',
|
|
537
|
+
source: 'MyApp',
|
|
538
|
+
debug: true
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
const cdp = new CdpLiteSdk(config);
|
|
542
|
+
|
|
543
|
+
const userTraits: UserTraits = {
|
|
544
|
+
name: 'John Doe',
|
|
545
|
+
email: 'john@example.com',
|
|
546
|
+
age: 30
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
cdp.identify('user_123', userTraits);
|
|
550
|
+
|
|
551
|
+
const eventProps: EventProperties = {
|
|
552
|
+
product_id: '123',
|
|
553
|
+
price: 99.99,
|
|
554
|
+
currency: 'USD'
|
|
555
|
+
};
|
|
556
|
+
|
|
557
|
+
cdp.track('purchase', eventProps);
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
---
|
|
561
|
+
|
|
562
|
+
## 📖 API Reference
|
|
563
|
+
|
|
564
|
+
### Constructor
|
|
565
|
+
|
|
566
|
+
```typescript
|
|
567
|
+
new CdpLiteSdk(config: CdpConfig)
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
### Methods
|
|
571
|
+
|
|
572
|
+
| Method | Parameters | Return | Description |
|
|
573
|
+
|--------|-----------|--------|-------------|
|
|
574
|
+
| `track()` | `eventName, properties?, options?` | `Promise<any> \| void` | Track an event |
|
|
575
|
+
| `identify()` | `userId, traits?` | `Promise<any> \| void` | Identify a user |
|
|
576
|
+
| `page()` | `pageName, properties?` | `Promise<any> \| void` | Track page view |
|
|
577
|
+
| `screen()` | `screenName, properties?` | `Promise<any> \| void` | Track screen view |
|
|
578
|
+
| `setUserAttributes()` | `traits` | `Promise<any> \| void` | Update user attrs |
|
|
579
|
+
| `setDeviceInfo()` | `deviceInfo` | `void` | Set device info |
|
|
580
|
+
| `flush()` | - | `Promise<any>` | Flush event queue |
|
|
581
|
+
| `reset()` | - | `void` | Reset user data |
|
|
582
|
+
| `destroy()` | - | `void` | Cleanup resources |
|
|
583
|
+
|
|
584
|
+
---
|
|
585
|
+
|
|
586
|
+
## 💡 Examples
|
|
587
|
+
|
|
588
|
+
### E-commerce Tracking
|
|
589
|
+
|
|
590
|
+
```javascript
|
|
591
|
+
// Product viewed
|
|
592
|
+
cdp.track('product_viewed', {
|
|
593
|
+
product_id: 'SKU123',
|
|
594
|
+
product_name: 'iPhone 14',
|
|
595
|
+
price: 20000000,
|
|
596
|
+
currency: 'VND',
|
|
597
|
+
category: 'Electronics'
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
// Add to cart
|
|
601
|
+
cdp.track('add_to_cart', {
|
|
602
|
+
product_id: 'SKU123',
|
|
603
|
+
quantity: 1,
|
|
604
|
+
price: 20000000
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
// Checkout
|
|
608
|
+
cdp.track('checkout_started', {
|
|
609
|
+
cart_total: 20000000,
|
|
610
|
+
item_count: 1
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
// Purchase
|
|
614
|
+
cdp.track('purchase_completed', {
|
|
615
|
+
order_id: 'ORDER789',
|
|
616
|
+
total: 20000000,
|
|
617
|
+
currency: 'VND',
|
|
618
|
+
payment_method: 'credit_card'
|
|
619
|
+
});
|
|
620
|
+
```
|
|
621
|
+
|
|
622
|
+
### Loan Application Flow
|
|
623
|
+
|
|
624
|
+
```javascript
|
|
625
|
+
// Start application
|
|
626
|
+
cdp.track('loan_application_started', {
|
|
627
|
+
loan_amount: 50000000,
|
|
628
|
+
loan_term: 12,
|
|
629
|
+
loan_type: 'personal'
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
// Step completed
|
|
633
|
+
cdp.track('application_step_completed', {
|
|
634
|
+
step_name: 'personal_info',
|
|
635
|
+
step_number: 1
|
|
636
|
+
}, {
|
|
637
|
+
loanCode: 'LOAN123'
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
// Submit application
|
|
641
|
+
cdp.track('loan_application_submitted', {
|
|
642
|
+
loan_amount: 50000000,
|
|
643
|
+
loan_term: 12
|
|
644
|
+
}, {
|
|
645
|
+
loanCode: 'LOAN123'
|
|
646
|
+
});
|
|
647
|
+
```
|
|
648
|
+
|
|
649
|
+
### User Authentication
|
|
650
|
+
|
|
651
|
+
```javascript
|
|
652
|
+
// Registration
|
|
653
|
+
cdp.track('user_registered', {
|
|
654
|
+
method: 'email',
|
|
655
|
+
source: 'organic'
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
cdp.identify('user_123', {
|
|
659
|
+
name: 'John Doe',
|
|
660
|
+
email: 'john@example.com',
|
|
661
|
+
created_at: new Date().toISOString()
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
// Login
|
|
665
|
+
cdp.track('user_logged_in', {
|
|
666
|
+
method: 'email'
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
// Logout
|
|
670
|
+
cdp.track('user_logged_out');
|
|
671
|
+
cdp.reset();
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
### Form Tracking
|
|
675
|
+
|
|
676
|
+
```javascript
|
|
677
|
+
// Form started
|
|
678
|
+
cdp.track('form_started', {
|
|
679
|
+
form_name: 'contact_form',
|
|
680
|
+
form_id: 'contact_123'
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
// Field completed
|
|
684
|
+
cdp.track('form_field_completed', {
|
|
685
|
+
form_name: 'contact_form',
|
|
686
|
+
field_name: 'email'
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
// Form submitted
|
|
690
|
+
cdp.track('form_submitted', {
|
|
691
|
+
form_name: 'contact_form',
|
|
692
|
+
fields_count: 5,
|
|
693
|
+
time_spent: 120 // seconds
|
|
694
|
+
});
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
---
|
|
698
|
+
|
|
699
|
+
## 🐛 Troubleshooting
|
|
700
|
+
|
|
701
|
+
### Events Not Sending
|
|
702
|
+
|
|
703
|
+
**Problem:** Events are not being tracked
|
|
704
|
+
|
|
705
|
+
**Solutions:**
|
|
706
|
+
1. Check API key is correct
|
|
707
|
+
2. Enable debug mode: `debug: true`
|
|
708
|
+
3. Check network tab in DevTools
|
|
709
|
+
4. Verify baseUrl is correct
|
|
710
|
+
|
|
711
|
+
```javascript
|
|
712
|
+
const cdp = new CdpLiteSdk({
|
|
713
|
+
apiKey: 'your-api-key',
|
|
714
|
+
debug: true // Enable logging
|
|
715
|
+
});
|
|
716
|
+
```
|
|
717
|
+
|
|
718
|
+
### Import Errors
|
|
719
|
+
|
|
720
|
+
**Problem:** `SyntaxError: The requested module does not provide an export named 'default'`
|
|
721
|
+
|
|
722
|
+
**Solutions:**
|
|
723
|
+
|
|
724
|
+
Use one of these import methods:
|
|
725
|
+
|
|
726
|
+
```javascript
|
|
727
|
+
// Method 1: Default import (recommended)
|
|
728
|
+
import CdpLiteSdk from 'cdp-lite-sdk';
|
|
729
|
+
|
|
730
|
+
// Method 2: Named import
|
|
731
|
+
import { CdpLiteSdk } from 'cdp-lite-sdk';
|
|
732
|
+
|
|
733
|
+
// Method 3: CommonJS
|
|
734
|
+
const CdpLiteSdk = require('cdp-lite-sdk');
|
|
735
|
+
```
|
|
736
|
+
|
|
737
|
+
### CORS Issues
|
|
738
|
+
|
|
739
|
+
**Problem:** CORS error in browser console
|
|
740
|
+
|
|
741
|
+
**Solution:** Ensure your domain is whitelisted on the API server.
|
|
742
|
+
|
|
743
|
+
### Events Delayed
|
|
744
|
+
|
|
745
|
+
**Problem:** Events take time to send
|
|
746
|
+
|
|
747
|
+
**Explanation:** Events are batched by default for performance.
|
|
748
|
+
|
|
749
|
+
**Solutions:**
|
|
750
|
+
|
|
751
|
+
```javascript
|
|
752
|
+
// Reduce batch size
|
|
753
|
+
const cdp = new CdpLiteSdk({
|
|
754
|
+
apiKey: 'your-api-key',
|
|
755
|
+
batchSize: 1 // Send immediately
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
// Or manually flush
|
|
759
|
+
cdp.track('important_event', {...});
|
|
760
|
+
await cdp.flush();
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
### TypeScript Errors
|
|
764
|
+
|
|
765
|
+
**Problem:** Type errors in TypeScript
|
|
766
|
+
|
|
767
|
+
**Solution:** Ensure types are imported:
|
|
768
|
+
|
|
769
|
+
```typescript
|
|
770
|
+
import CdpLiteSdk, { CdpConfig } from 'cdp-lite-sdk';
|
|
771
|
+
|
|
772
|
+
const config: CdpConfig = {
|
|
773
|
+
apiKey: 'your-api-key'
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
const cdp = new CdpLiteSdk(config);
|
|
777
|
+
```
|
|
778
|
+
|
|
779
|
+
---
|
|
780
|
+
|
|
781
|
+
## 📝 Event Naming Best Practices
|
|
782
|
+
|
|
783
|
+
Use `object_action` format:
|
|
784
|
+
|
|
785
|
+
```javascript
|
|
786
|
+
// ✅ Good
|
|
787
|
+
cdp.track('product_viewed');
|
|
788
|
+
cdp.track('cart_item_added');
|
|
789
|
+
cdp.track('checkout_completed');
|
|
790
|
+
cdp.track('user_registered');
|
|
791
|
+
|
|
792
|
+
// ❌ Avoid
|
|
793
|
+
cdp.track('viewProduct');
|
|
794
|
+
cdp.track('AddItemToCart');
|
|
795
|
+
cdp.track('CHECKOUT');
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
---
|
|
799
|
+
|
|
800
|
+
## 🔒 Privacy & Security
|
|
801
|
+
|
|
802
|
+
- SDK automatically generates anonymous IDs
|
|
803
|
+
- User data is only sent after explicit `identify()` call
|
|
804
|
+
- All data transmitted over HTTPS
|
|
805
|
+
- API keys should be kept secure
|
|
806
|
+
- Never commit API keys to version control
|
|
807
|
+
|
|
808
|
+
---
|
|
809
|
+
|
|
810
|
+
## 📄 License
|
|
811
|
+
|
|
812
|
+
MIT License - see LICENSE file for details
|
|
813
|
+
|
|
814
|
+
---
|
|
815
|
+
|
|
816
|
+
## 🤝 Support
|
|
817
|
+
|
|
818
|
+
- **Email:** vinv@vega.com.vn
|
|
819
|
+
|
|
820
|
+
---
|
|
821
|
+
|
|
822
|
+
## 🎉 Quick Reference
|
|
823
|
+
|
|
824
|
+
```javascript
|
|
825
|
+
// Initialize
|
|
826
|
+
const cdp = new CdpLiteSdk({ apiKey: 'key' });
|
|
827
|
+
|
|
828
|
+
// Identify
|
|
829
|
+
cdp.identify('user_id', { name: 'John' });
|
|
830
|
+
|
|
831
|
+
// Track
|
|
832
|
+
cdp.track('event_name', { prop: 'value' });
|
|
833
|
+
|
|
834
|
+
// Page
|
|
835
|
+
cdp.page('Page Name');
|
|
836
|
+
|
|
837
|
+
// Flush
|
|
838
|
+
await cdp.flush();
|
|
839
|
+
|
|
840
|
+
// Reset
|
|
841
|
+
cdp.reset();
|
|
842
|
+
```
|
|
843
|
+
|
|
844
|
+
---
|
|
845
|
+
|
|
846
|
+
Made with ❤️ by Your Team
|