@kitbase/events 0.1.2 → 0.1.4
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 +332 -0
- package/dist/index.cjs +473 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +232 -1
- package/dist/index.d.ts +232 -1
- package/dist/index.js +468 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
# @kitbase/events
|
|
2
|
+
|
|
3
|
+
Official Kitbase SDK for event tracking and web analytics in TypeScript and JavaScript applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @kitbase/events
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @kitbase/events
|
|
11
|
+
# or
|
|
12
|
+
yarn add @kitbase/events
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { Kitbase } from '@kitbase/events';
|
|
19
|
+
|
|
20
|
+
const kitbase = new Kitbase({
|
|
21
|
+
token: 'your-api-key',
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Track a custom event
|
|
25
|
+
await kitbase.track({
|
|
26
|
+
channel: 'payments',
|
|
27
|
+
event: 'Purchase Completed',
|
|
28
|
+
tags: {
|
|
29
|
+
product_id: 'prod_123',
|
|
30
|
+
amount: 4999,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- **Event Tracking** - Track custom events with metadata
|
|
38
|
+
- **Web Analytics** - Automatic session tracking, pageviews, and bounce rate
|
|
39
|
+
- **User Identification** - Link anonymous users to authenticated users
|
|
40
|
+
- **Offline Support** - Queue events locally when offline, sync when back online
|
|
41
|
+
- **Super Properties** - Set properties that are included with every event
|
|
42
|
+
- **Duration Tracking** - Measure how long actions take
|
|
43
|
+
|
|
44
|
+
## Configuration
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
const kitbase = new Kitbase({
|
|
48
|
+
// Required: Your API key
|
|
49
|
+
token: 'your-api-key',
|
|
50
|
+
|
|
51
|
+
// Optional: Custom API endpoint
|
|
52
|
+
baseUrl: 'https://api.kitbase.dev',
|
|
53
|
+
|
|
54
|
+
// Optional: Enable debug logging
|
|
55
|
+
debug: true,
|
|
56
|
+
|
|
57
|
+
// Optional: Offline queue configuration
|
|
58
|
+
offline: {
|
|
59
|
+
enabled: true,
|
|
60
|
+
maxSize: 1000, // Max events to queue
|
|
61
|
+
flushInterval: 30000, // Flush every 30 seconds
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
// Optional: Analytics configuration
|
|
65
|
+
analytics: {
|
|
66
|
+
autoTrackSessions: true, // Track sessions automatically (default: true)
|
|
67
|
+
autoTrackPageViews: false, // Track pageviews on route changes
|
|
68
|
+
sessionTimeout: 1800000, // 30 minutes in ms
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Event Tracking
|
|
74
|
+
|
|
75
|
+
### Basic Event
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
await kitbase.track({
|
|
79
|
+
channel: 'engagement',
|
|
80
|
+
event: 'Button Clicked',
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Event with Metadata
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
await kitbase.track({
|
|
88
|
+
channel: 'payments',
|
|
89
|
+
event: 'Subscription Started',
|
|
90
|
+
user_id: 'user_123',
|
|
91
|
+
icon: '💰',
|
|
92
|
+
notify: true,
|
|
93
|
+
description: 'New premium subscription',
|
|
94
|
+
tags: {
|
|
95
|
+
plan: 'premium',
|
|
96
|
+
interval: 'monthly',
|
|
97
|
+
amount: 1999,
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Duration Tracking
|
|
103
|
+
|
|
104
|
+
Measure how long an action takes:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
// Start timing
|
|
108
|
+
kitbase.timeEvent('Video Watched');
|
|
109
|
+
|
|
110
|
+
// ... user watches video ...
|
|
111
|
+
|
|
112
|
+
// Track with automatic duration
|
|
113
|
+
await kitbase.track({
|
|
114
|
+
channel: 'engagement',
|
|
115
|
+
event: 'Video Watched',
|
|
116
|
+
tags: { video_id: '123' },
|
|
117
|
+
});
|
|
118
|
+
// Event includes $duration property in seconds
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Web Analytics
|
|
122
|
+
|
|
123
|
+
### Automatic Session Tracking
|
|
124
|
+
|
|
125
|
+
Sessions are tracked automatically by default. A new session starts when:
|
|
126
|
+
- User visits for the first time
|
|
127
|
+
- User returns after 30 minutes of inactivity
|
|
128
|
+
|
|
129
|
+
### Track Page Views
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
// Manual pageview tracking
|
|
133
|
+
await kitbase.trackPageView();
|
|
134
|
+
|
|
135
|
+
// With custom path/title
|
|
136
|
+
await kitbase.trackPageView({
|
|
137
|
+
path: '/products/123',
|
|
138
|
+
title: 'Product Details',
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Auto-track Page Views (SPAs)
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
// Enable automatic tracking on route changes
|
|
146
|
+
kitbase.enableAutoPageViews();
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Track Revenue
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
await kitbase.trackRevenue({
|
|
153
|
+
amount: 4999, // Amount in cents ($49.99)
|
|
154
|
+
currency: 'USD',
|
|
155
|
+
tags: {
|
|
156
|
+
product_id: 'prod_123',
|
|
157
|
+
coupon: 'SAVE20',
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## User Identification
|
|
163
|
+
|
|
164
|
+
### Identify a User
|
|
165
|
+
|
|
166
|
+
Link anonymous activity to a known user:
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
kitbase.identify({
|
|
170
|
+
userId: 'user_123',
|
|
171
|
+
traits: {
|
|
172
|
+
email: 'user@example.com',
|
|
173
|
+
plan: 'premium',
|
|
174
|
+
company: 'Acme Inc',
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Reset on Logout
|
|
180
|
+
|
|
181
|
+
Clear user identity and start fresh:
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
kitbase.reset();
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Super Properties
|
|
188
|
+
|
|
189
|
+
Properties included with every event:
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
// Set super properties
|
|
193
|
+
kitbase.register({
|
|
194
|
+
app_version: '2.1.0',
|
|
195
|
+
platform: 'web',
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Set only if not already set
|
|
199
|
+
kitbase.registerOnce({
|
|
200
|
+
first_visit: new Date().toISOString(),
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Remove a super property
|
|
204
|
+
kitbase.unregister('platform');
|
|
205
|
+
|
|
206
|
+
// Clear all
|
|
207
|
+
kitbase.clearSuperProperties();
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Anonymous ID
|
|
211
|
+
|
|
212
|
+
Every user gets a persistent anonymous ID:
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
const anonymousId = kitbase.getAnonymousId();
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Session Management
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
// Get current session ID
|
|
222
|
+
const sessionId = kitbase.getSessionId();
|
|
223
|
+
|
|
224
|
+
// Get full session data
|
|
225
|
+
const session = kitbase.getSession();
|
|
226
|
+
// { id, startedAt, lastActivityAt, screenViewCount, entryPath, entryReferrer }
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Offline Support
|
|
230
|
+
|
|
231
|
+
Events are queued locally when offline and synced when back online:
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
const kitbase = new Kitbase({
|
|
235
|
+
token: 'your-api-key',
|
|
236
|
+
offline: {
|
|
237
|
+
enabled: true,
|
|
238
|
+
maxSize: 1000, // Maximum events to store
|
|
239
|
+
flushInterval: 30000, // Sync interval in ms
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// Check queue status
|
|
244
|
+
const stats = await kitbase.getQueueStats();
|
|
245
|
+
// { size: 5, isFlushing: false }
|
|
246
|
+
|
|
247
|
+
// Manual flush
|
|
248
|
+
await kitbase.flushQueue();
|
|
249
|
+
|
|
250
|
+
// Clear queue
|
|
251
|
+
await kitbase.clearQueue();
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Debug Mode
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
// Enable at initialization
|
|
258
|
+
const kitbase = new Kitbase({
|
|
259
|
+
token: 'your-api-key',
|
|
260
|
+
debug: true,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// Or toggle at runtime
|
|
264
|
+
kitbase.setDebugMode(true);
|
|
265
|
+
kitbase.setDebugMode(false);
|
|
266
|
+
|
|
267
|
+
// Check status
|
|
268
|
+
const isDebug = kitbase.isDebugMode();
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Cleanup
|
|
272
|
+
|
|
273
|
+
Properly shutdown when done:
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
await kitbase.shutdown();
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
## API Reference
|
|
280
|
+
|
|
281
|
+
### Kitbase Class
|
|
282
|
+
|
|
283
|
+
| Method | Description |
|
|
284
|
+
|--------|-------------|
|
|
285
|
+
| `track(options)` | Track a custom event |
|
|
286
|
+
| `trackPageView(options?)` | Track a page view |
|
|
287
|
+
| `trackRevenue(options)` | Track a revenue event |
|
|
288
|
+
| `identify(options)` | Identify the current user |
|
|
289
|
+
| `reset()` | Reset user identity and session |
|
|
290
|
+
| `register(props)` | Set super properties |
|
|
291
|
+
| `registerOnce(props)` | Set super properties if not already set |
|
|
292
|
+
| `unregister(key)` | Remove a super property |
|
|
293
|
+
| `clearSuperProperties()` | Clear all super properties |
|
|
294
|
+
| `timeEvent(name)` | Start timing an event |
|
|
295
|
+
| `cancelTimeEvent(name)` | Cancel timing an event |
|
|
296
|
+
| `getAnonymousId()` | Get the anonymous ID |
|
|
297
|
+
| `getSessionId()` | Get current session ID |
|
|
298
|
+
| `getSession()` | Get current session data |
|
|
299
|
+
| `getUserId()` | Get identified user ID |
|
|
300
|
+
| `enableAutoPageViews()` | Enable automatic pageview tracking |
|
|
301
|
+
| `setDebugMode(enabled)` | Enable/disable debug logging |
|
|
302
|
+
| `getQueueStats()` | Get offline queue statistics |
|
|
303
|
+
| `flushQueue()` | Manually flush the offline queue |
|
|
304
|
+
| `clearQueue()` | Clear the offline queue |
|
|
305
|
+
| `shutdown()` | Cleanup and shutdown the client |
|
|
306
|
+
|
|
307
|
+
## TypeScript Support
|
|
308
|
+
|
|
309
|
+
Full TypeScript support with exported types:
|
|
310
|
+
|
|
311
|
+
```typescript
|
|
312
|
+
import {
|
|
313
|
+
Kitbase,
|
|
314
|
+
KitbaseConfig,
|
|
315
|
+
TrackOptions,
|
|
316
|
+
TrackResponse,
|
|
317
|
+
PageViewOptions,
|
|
318
|
+
RevenueOptions,
|
|
319
|
+
IdentifyOptions,
|
|
320
|
+
Session,
|
|
321
|
+
Tags,
|
|
322
|
+
} from '@kitbase/events';
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## Browser Support
|
|
326
|
+
|
|
327
|
+
- Chrome, Firefox, Safari, Edge (latest versions)
|
|
328
|
+
- Works in Node.js 18+
|
|
329
|
+
|
|
330
|
+
## License
|
|
331
|
+
|
|
332
|
+
MIT
|