@ooneex/analytics 0.0.1 → 0.0.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 +153 -249
- package/dist/index.d.ts +10 -6
- package/dist/index.js +2 -2
- package/dist/index.js.map +5 -4
- package/package.json +15 -9
- package/dist/ooneex-analytics-0.0.1.tgz +0 -0
package/README.md
CHANGED
|
@@ -1,30 +1,26 @@
|
|
|
1
1
|
# @ooneex/analytics
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An analytics and event tracking integration library for TypeScript applications. This package provides seamless integration with PostHog for user behavior insights, product analytics, and event tracking across your applications.
|
|
4
4
|
|
|
5
5
|

|
|
6
|
+

|
|
7
|
+

|
|
6
8
|

|
|
7
9
|

|
|
8
10
|
|
|
9
11
|
## Features
|
|
10
12
|
|
|
11
|
-
✅ **PostHog Integration** -
|
|
13
|
+
✅ **PostHog Integration** - Native support for PostHog analytics platform
|
|
12
14
|
|
|
13
|
-
✅ **
|
|
14
|
-
|
|
15
|
-
✅ **Environment Configuration** - Flexible API key and host configuration
|
|
16
|
-
|
|
17
|
-
✅ **Event Tracking** - Track user events with custom properties and groups
|
|
18
|
-
|
|
19
|
-
✅ **User Properties** - Attach custom properties to user events
|
|
15
|
+
✅ **Event Tracking** - Capture user events with custom properties
|
|
20
16
|
|
|
21
|
-
✅ **
|
|
17
|
+
✅ **User Identification** - Track events with distinct user IDs
|
|
22
18
|
|
|
23
|
-
✅ **
|
|
19
|
+
✅ **Group Analytics** - Support for group-based event tracking
|
|
24
20
|
|
|
25
|
-
✅ **
|
|
21
|
+
✅ **Type-Safe** - Full TypeScript support with proper type definitions
|
|
26
22
|
|
|
27
|
-
✅ **
|
|
23
|
+
✅ **Container Integration** - Works seamlessly with dependency injection
|
|
28
24
|
|
|
29
25
|
## Installation
|
|
30
26
|
|
|
@@ -33,35 +29,19 @@ A comprehensive TypeScript/JavaScript analytics library with PostHog integration
|
|
|
33
29
|
bun add @ooneex/analytics
|
|
34
30
|
```
|
|
35
31
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
### Environment Variables
|
|
39
|
-
|
|
40
|
-
Set the following environment variables in your project:
|
|
41
|
-
|
|
32
|
+
### pnpm
|
|
42
33
|
```bash
|
|
43
|
-
|
|
44
|
-
ANALYTICS_POSTHOG_API_KEY=your_posthog_api_key_here
|
|
45
|
-
|
|
46
|
-
# Optional: PostHog host (defaults to https://eu.i.posthog.com)
|
|
47
|
-
ANALYTICS_POSTHOG_HOST=https://eu.i.posthog.com
|
|
34
|
+
pnpm add @ooneex/analytics
|
|
48
35
|
```
|
|
49
36
|
|
|
50
|
-
###
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
```typescript
|
|
55
|
-
import { PostHogAdapter } from '@ooneex/analytics';
|
|
56
|
-
|
|
57
|
-
// Using constructor options (overrides environment variables)
|
|
58
|
-
const analytics = new PostHogAdapter({
|
|
59
|
-
apiKey: 'your_api_key',
|
|
60
|
-
host: 'https://your-posthog-instance.com'
|
|
61
|
-
});
|
|
37
|
+
### Yarn
|
|
38
|
+
```bash
|
|
39
|
+
yarn add @ooneex/analytics
|
|
40
|
+
```
|
|
62
41
|
|
|
63
|
-
|
|
64
|
-
|
|
42
|
+
### npm
|
|
43
|
+
```bash
|
|
44
|
+
npm install @ooneex/analytics
|
|
65
45
|
```
|
|
66
46
|
|
|
67
47
|
## Usage
|
|
@@ -69,317 +49,252 @@ const analytics = new PostHogAdapter();
|
|
|
69
49
|
### Basic Event Tracking
|
|
70
50
|
|
|
71
51
|
```typescript
|
|
72
|
-
import {
|
|
52
|
+
import { PostHogAnalytics } from '@ooneex/analytics';
|
|
73
53
|
|
|
74
|
-
const analytics = new
|
|
54
|
+
const analytics = new PostHogAnalytics({
|
|
55
|
+
apiKey: 'your-posthog-api-key'
|
|
56
|
+
});
|
|
75
57
|
|
|
76
58
|
// Track a simple event
|
|
77
59
|
analytics.capture({
|
|
78
|
-
id: '
|
|
79
|
-
event: 'button_clicked'
|
|
80
|
-
properties: {
|
|
81
|
-
buttonId: 'signup-btn',
|
|
82
|
-
page: '/landing'
|
|
83
|
-
}
|
|
60
|
+
id: 'user-123',
|
|
61
|
+
event: 'button_clicked'
|
|
84
62
|
});
|
|
85
63
|
```
|
|
86
64
|
|
|
87
|
-
###
|
|
65
|
+
### With Event Properties
|
|
88
66
|
|
|
89
67
|
```typescript
|
|
90
|
-
import {
|
|
91
|
-
|
|
92
|
-
const analytics = new PostHogAdapter();
|
|
93
|
-
|
|
94
|
-
// Track event with user properties and groups
|
|
95
|
-
analytics.capture({
|
|
96
|
-
id: 'user_123',
|
|
97
|
-
event: 'feature_used',
|
|
98
|
-
properties: {
|
|
99
|
-
feature: 'advanced_search',
|
|
100
|
-
sessionDuration: 1200,
|
|
101
|
-
actionsCount: 15,
|
|
102
|
-
source: 'web_app'
|
|
103
|
-
},
|
|
104
|
-
groups: {
|
|
105
|
-
company: 'acme_corp',
|
|
106
|
-
plan: 'enterprise'
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
```
|
|
68
|
+
import { PostHogAnalytics } from '@ooneex/analytics';
|
|
110
69
|
|
|
111
|
-
|
|
70
|
+
const analytics = new PostHogAnalytics();
|
|
112
71
|
|
|
113
|
-
```typescript
|
|
114
|
-
// Track purchase events
|
|
115
72
|
analytics.capture({
|
|
116
|
-
id: '
|
|
73
|
+
id: 'user-123',
|
|
117
74
|
event: 'purchase_completed',
|
|
118
75
|
properties: {
|
|
119
|
-
|
|
120
|
-
|
|
76
|
+
product_id: 'prod-456',
|
|
77
|
+
price: 99.99,
|
|
121
78
|
currency: 'USD',
|
|
122
|
-
|
|
123
|
-
paymentMethod: 'credit_card',
|
|
124
|
-
discount: 10.00
|
|
125
|
-
},
|
|
126
|
-
groups: {
|
|
127
|
-
store: 'online',
|
|
128
|
-
region: 'us_west'
|
|
79
|
+
quantity: 1
|
|
129
80
|
}
|
|
130
81
|
});
|
|
131
82
|
```
|
|
132
83
|
|
|
133
|
-
###
|
|
84
|
+
### Group Analytics
|
|
134
85
|
|
|
135
86
|
```typescript
|
|
136
|
-
|
|
87
|
+
import { PostHogAnalytics } from '@ooneex/analytics';
|
|
88
|
+
|
|
89
|
+
const analytics = new PostHogAnalytics();
|
|
90
|
+
|
|
137
91
|
analytics.capture({
|
|
138
|
-
id: '
|
|
139
|
-
event: '
|
|
92
|
+
id: 'user-123',
|
|
93
|
+
event: 'project_created',
|
|
140
94
|
properties: {
|
|
141
|
-
|
|
142
|
-
source: 'google_ads',
|
|
143
|
-
plan: 'free',
|
|
144
|
-
referrer: 'https://example.com'
|
|
95
|
+
project_name: 'My Project'
|
|
145
96
|
},
|
|
146
97
|
groups: {
|
|
147
|
-
company: '
|
|
98
|
+
company: 'company-789',
|
|
99
|
+
team: 'engineering'
|
|
148
100
|
}
|
|
149
101
|
});
|
|
150
102
|
```
|
|
151
103
|
|
|
152
|
-
###
|
|
104
|
+
### Using Environment Variables
|
|
153
105
|
|
|
154
106
|
```typescript
|
|
155
|
-
import {
|
|
107
|
+
import { PostHogAnalytics } from '@ooneex/analytics';
|
|
156
108
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
// Custom analytics implementation
|
|
160
|
-
console.log('Tracking event:', options.event, 'for user:', options.id);
|
|
109
|
+
// API key and host are read from environment variables
|
|
110
|
+
const analytics = new PostHogAnalytics();
|
|
161
111
|
|
|
162
|
-
|
|
112
|
+
analytics.capture({
|
|
113
|
+
id: 'user-123',
|
|
114
|
+
event: 'page_viewed',
|
|
115
|
+
properties: {
|
|
116
|
+
page: '/dashboard'
|
|
163
117
|
}
|
|
164
|
-
}
|
|
118
|
+
});
|
|
165
119
|
```
|
|
166
120
|
|
|
121
|
+
**Environment Variables:**
|
|
122
|
+
- `ANALYTICS_POSTHOG_API_KEY` - Your PostHog API key
|
|
123
|
+
- `ANALYTICS_POSTHOG_HOST` - PostHog host URL (default: `https://eu.i.posthog.com`)
|
|
124
|
+
|
|
167
125
|
## API Reference
|
|
168
126
|
|
|
169
|
-
###
|
|
127
|
+
### Classes
|
|
170
128
|
|
|
171
|
-
|
|
129
|
+
#### `PostHogAnalytics`
|
|
172
130
|
|
|
173
|
-
|
|
131
|
+
Main analytics class for PostHog integration.
|
|
174
132
|
|
|
133
|
+
**Constructor:**
|
|
175
134
|
```typescript
|
|
176
|
-
new
|
|
135
|
+
new PostHogAnalytics(options?: { apiKey?: string; host?: string })
|
|
177
136
|
```
|
|
178
137
|
|
|
179
138
|
**Parameters:**
|
|
180
|
-
- `options.apiKey` - PostHog API key (optional
|
|
181
|
-
- `options.host` - PostHog host URL (optional, defaults to
|
|
139
|
+
- `options.apiKey` - PostHog API key (optional if set via environment variable)
|
|
140
|
+
- `options.host` - PostHog host URL (optional, defaults to EU region)
|
|
182
141
|
|
|
183
|
-
**
|
|
142
|
+
**Methods:**
|
|
184
143
|
|
|
185
|
-
|
|
186
|
-
```typescript
|
|
187
|
-
// Using environment variables
|
|
188
|
-
const analytics = new PostHogAdapter();
|
|
144
|
+
##### `capture(options: PostHogCaptureOptionsType): void`
|
|
189
145
|
|
|
190
|
-
|
|
191
|
-
const analytics = new PostHogAdapter({
|
|
192
|
-
apiKey: 'phc_your_key_here',
|
|
193
|
-
host: 'https://app.posthog.com'
|
|
194
|
-
});
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
#### Methods
|
|
198
|
-
|
|
199
|
-
##### `capture(options: PostHogAdapterCaptureType): void`
|
|
200
|
-
|
|
201
|
-
Captures an analytics event with user properties and groups.
|
|
146
|
+
Captures an analytics event.
|
|
202
147
|
|
|
203
148
|
**Parameters:**
|
|
204
|
-
- `options.id` - Unique user
|
|
205
|
-
- `options.event` -
|
|
206
|
-
- `options.properties` -
|
|
207
|
-
- `options.groups` -
|
|
149
|
+
- `options.id` - Unique identifier for the user (distinctId)
|
|
150
|
+
- `options.event` - Name of the event to track
|
|
151
|
+
- `options.properties` - Optional custom properties for the event
|
|
152
|
+
- `options.groups` - Optional group identifiers for group analytics
|
|
208
153
|
|
|
209
154
|
**Example:**
|
|
210
155
|
```typescript
|
|
211
156
|
analytics.capture({
|
|
212
|
-
id: '
|
|
213
|
-
event: '
|
|
157
|
+
id: 'user-123',
|
|
158
|
+
event: 'signup_completed',
|
|
214
159
|
properties: {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
userAgent: 'Chrome/91.0'
|
|
218
|
-
},
|
|
219
|
-
groups: {
|
|
220
|
-
company: 'acme_corp',
|
|
221
|
-
team: 'marketing'
|
|
160
|
+
plan: 'premium',
|
|
161
|
+
source: 'landing_page'
|
|
222
162
|
}
|
|
223
163
|
});
|
|
224
164
|
```
|
|
225
165
|
|
|
226
|
-
###
|
|
166
|
+
### Interfaces
|
|
227
167
|
|
|
228
168
|
#### `IAnalytics`
|
|
229
169
|
|
|
230
|
-
Interface defining the analytics contract.
|
|
231
|
-
|
|
232
170
|
```typescript
|
|
233
|
-
interface IAnalytics {
|
|
234
|
-
capture: (options:
|
|
171
|
+
interface IAnalytics<T = any> {
|
|
172
|
+
capture: (options: T) => void;
|
|
235
173
|
}
|
|
236
174
|
```
|
|
237
175
|
|
|
238
|
-
|
|
176
|
+
### Types
|
|
239
177
|
|
|
240
|
-
|
|
178
|
+
#### `PostHogCaptureOptionsType`
|
|
241
179
|
|
|
242
180
|
```typescript
|
|
243
|
-
type
|
|
244
|
-
id: string;
|
|
245
|
-
event: string;
|
|
246
|
-
properties?: Record<string, unknown>;
|
|
247
|
-
groups?: Record<string, string | number>;
|
|
181
|
+
type PostHogCaptureOptionsType = {
|
|
182
|
+
id: string;
|
|
183
|
+
event: string;
|
|
184
|
+
properties?: Record<string, unknown>;
|
|
185
|
+
groups?: Record<string, string | number>;
|
|
248
186
|
};
|
|
249
187
|
```
|
|
250
188
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
#### `AnalyticsException`
|
|
254
|
-
|
|
255
|
-
Custom exception class for analytics-related errors.
|
|
189
|
+
#### `AnalyticsClassType`
|
|
256
190
|
|
|
257
191
|
```typescript
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
try {
|
|
261
|
-
const analytics = new PostHogAdapter(); // Missing API key
|
|
262
|
-
} catch (error) {
|
|
263
|
-
if (error instanceof AnalyticsException) {
|
|
264
|
-
console.error('Analytics Error:', error.message);
|
|
265
|
-
// Handle analytics-specific error
|
|
266
|
-
}
|
|
267
|
-
}
|
|
192
|
+
type AnalyticsClassType = new (...args: any[]) => IAnalytics;
|
|
268
193
|
```
|
|
269
194
|
|
|
270
|
-
|
|
271
|
-
- Missing PostHog API key
|
|
272
|
-
- Invalid configuration options
|
|
273
|
-
- Network connectivity issues
|
|
195
|
+
## Advanced Usage
|
|
274
196
|
|
|
275
|
-
|
|
197
|
+
### Integration with Ooneex App
|
|
276
198
|
|
|
277
|
-
|
|
199
|
+
```typescript
|
|
200
|
+
import { App } from '@ooneex/app';
|
|
201
|
+
import { PostHogAnalytics } from '@ooneex/analytics';
|
|
278
202
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
203
|
+
const app = new App({
|
|
204
|
+
analytics: PostHogAnalytics,
|
|
205
|
+
// ... other config
|
|
206
|
+
});
|
|
282
207
|
```
|
|
283
208
|
|
|
284
|
-
###
|
|
285
|
-
|
|
286
|
-
```bash
|
|
287
|
-
# PostHog Host (optional, defaults to EU instance)
|
|
288
|
-
ANALYTICS_POSTHOG_HOST=https://app.posthog.com
|
|
209
|
+
### Custom Analytics Implementation
|
|
289
210
|
|
|
290
|
-
|
|
291
|
-
|
|
211
|
+
```typescript
|
|
212
|
+
import { type IAnalytics, type PostHogCaptureOptionsType } from '@ooneex/analytics';
|
|
292
213
|
|
|
293
|
-
|
|
294
|
-
|
|
214
|
+
class CustomAnalytics implements IAnalytics<PostHogCaptureOptionsType> {
|
|
215
|
+
public capture(options: PostHogCaptureOptionsType): void {
|
|
216
|
+
// Custom implementation
|
|
217
|
+
console.log(`Event: ${options.event}`, options.properties);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
295
220
|
```
|
|
296
221
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
### Event Naming
|
|
300
|
-
|
|
301
|
-
Use consistent, descriptive event names:
|
|
222
|
+
### Integration with Dependency Injection
|
|
302
223
|
|
|
303
224
|
```typescript
|
|
304
|
-
|
|
305
|
-
analytics
|
|
306
|
-
id: 'user_123',
|
|
307
|
-
event: 'button_clicked',
|
|
308
|
-
properties: { button: 'signup' }
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
// ❌ Avoid
|
|
312
|
-
analytics.capture({
|
|
313
|
-
id: 'user_123',
|
|
314
|
-
event: 'click',
|
|
315
|
-
properties: { what: 'something' }
|
|
316
|
-
});
|
|
317
|
-
```
|
|
225
|
+
import { container, EContainerScope } from '@ooneex/container';
|
|
226
|
+
import { PostHogAnalytics, decorator } from '@ooneex/analytics';
|
|
318
227
|
|
|
319
|
-
|
|
228
|
+
// Register analytics service
|
|
229
|
+
container.add(PostHogAnalytics, EContainerScope.Singleton);
|
|
230
|
+
container.addAlias('analytics', PostHogAnalytics);
|
|
320
231
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
```typescript
|
|
324
|
-
// ✅ Good
|
|
325
|
-
analytics.capture({
|
|
326
|
-
id: 'user_123',
|
|
327
|
-
event: 'purchase_completed',
|
|
328
|
-
properties: {
|
|
329
|
-
orderId: 'order_456',
|
|
330
|
-
revenue: 99.99,
|
|
331
|
-
currency: 'USD',
|
|
332
|
-
itemCount: 3
|
|
333
|
-
}
|
|
334
|
-
});
|
|
232
|
+
// Resolve from container
|
|
233
|
+
const analytics = container.get<PostHogAnalytics>('analytics');
|
|
335
234
|
|
|
336
|
-
// ❌ Avoid deeply nested objects
|
|
337
235
|
analytics.capture({
|
|
338
|
-
id: '
|
|
339
|
-
event: '
|
|
340
|
-
properties: {
|
|
341
|
-
order: {
|
|
342
|
-
details: {
|
|
343
|
-
nested: {
|
|
344
|
-
data: 'value'
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}
|
|
236
|
+
id: 'user-123',
|
|
237
|
+
event: 'app_started'
|
|
349
238
|
});
|
|
350
239
|
```
|
|
351
240
|
|
|
352
241
|
### Error Handling
|
|
353
242
|
|
|
354
|
-
Always handle potential configuration errors:
|
|
355
|
-
|
|
356
243
|
```typescript
|
|
357
|
-
import {
|
|
244
|
+
import { PostHogAnalytics, AnalyticsException } from '@ooneex/analytics';
|
|
358
245
|
|
|
359
246
|
try {
|
|
360
|
-
const analytics = new
|
|
361
|
-
|
|
247
|
+
const analytics = new PostHogAnalytics();
|
|
362
248
|
analytics.capture({
|
|
363
|
-
id: '
|
|
364
|
-
event: '
|
|
249
|
+
id: 'user-123',
|
|
250
|
+
event: 'test_event'
|
|
365
251
|
});
|
|
366
252
|
} catch (error) {
|
|
367
253
|
if (error instanceof AnalyticsException) {
|
|
368
|
-
console.error('Analytics
|
|
369
|
-
// Fallback analytics or silent failure
|
|
254
|
+
console.error('Analytics Error:', error.message);
|
|
370
255
|
}
|
|
371
256
|
}
|
|
372
257
|
```
|
|
373
258
|
|
|
374
|
-
|
|
259
|
+
### Tracking Page Views
|
|
375
260
|
|
|
376
|
-
|
|
261
|
+
```typescript
|
|
262
|
+
import { PostHogAnalytics } from '@ooneex/analytics';
|
|
377
263
|
|
|
378
|
-
|
|
264
|
+
const analytics = new PostHogAnalytics();
|
|
265
|
+
|
|
266
|
+
function trackPageView(userId: string, path: string): void {
|
|
267
|
+
analytics.capture({
|
|
268
|
+
id: userId,
|
|
269
|
+
event: '$pageview',
|
|
270
|
+
properties: {
|
|
271
|
+
$current_url: path,
|
|
272
|
+
$host: 'example.com',
|
|
273
|
+
$pathname: path
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
trackPageView('user-123', '/dashboard');
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Tracking User Properties
|
|
379
282
|
|
|
380
283
|
```typescript
|
|
381
|
-
|
|
382
|
-
|
|
284
|
+
import { PostHogAnalytics } from '@ooneex/analytics';
|
|
285
|
+
|
|
286
|
+
const analytics = new PostHogAnalytics();
|
|
287
|
+
|
|
288
|
+
// Set user properties using $set
|
|
289
|
+
analytics.capture({
|
|
290
|
+
id: 'user-123',
|
|
291
|
+
event: 'user_updated',
|
|
292
|
+
properties: {
|
|
293
|
+
email: 'user@example.com',
|
|
294
|
+
plan: 'enterprise',
|
|
295
|
+
signup_date: '2024-01-01'
|
|
296
|
+
}
|
|
297
|
+
});
|
|
383
298
|
```
|
|
384
299
|
|
|
385
300
|
## License
|
|
@@ -403,17 +318,6 @@ Contributions are welcome! Please feel free to submit a Pull Request. For major
|
|
|
403
318
|
- Follow the existing code style
|
|
404
319
|
- Update documentation for API changes
|
|
405
320
|
- Ensure all tests pass before submitting PR
|
|
406
|
-
- Test with Bun runtime environment
|
|
407
|
-
|
|
408
|
-
### Running Tests
|
|
409
|
-
|
|
410
|
-
```bash
|
|
411
|
-
# Run all tests
|
|
412
|
-
bun run test
|
|
413
|
-
|
|
414
|
-
# Run tests in watch mode
|
|
415
|
-
bun run test:watch
|
|
416
|
-
```
|
|
417
321
|
|
|
418
322
|
---
|
|
419
323
|
|
package/dist/index.d.ts
CHANGED
|
@@ -2,22 +2,26 @@ import { Exception } from "@ooneex/exception";
|
|
|
2
2
|
declare class AnalyticsException extends Exception {
|
|
3
3
|
constructor(message: string, data?: Record<string, unknown>);
|
|
4
4
|
}
|
|
5
|
+
import { EContainerScope } from "@ooneex/container";
|
|
5
6
|
type AnalyticsClassType = new (...args: any[]) => IAnalytics;
|
|
6
|
-
interface IAnalytics {
|
|
7
|
-
capture: (options:
|
|
7
|
+
interface IAnalytics<T = any> {
|
|
8
|
+
capture: (options: T) => void;
|
|
8
9
|
}
|
|
9
|
-
type
|
|
10
|
+
type PostHogCaptureOptionsType = {
|
|
10
11
|
id: string;
|
|
11
12
|
event: string;
|
|
12
13
|
properties?: Record<string, unknown>;
|
|
13
14
|
groups?: Record<string, string | number>;
|
|
14
15
|
};
|
|
15
|
-
declare
|
|
16
|
+
declare const decorator: {
|
|
17
|
+
analytics: (scope?: EContainerScope) => (target: AnalyticsClassType) => void;
|
|
18
|
+
};
|
|
19
|
+
declare class PostHogAnalytics<T extends PostHogCaptureOptionsType = PostHogCaptureOptionsType> implements IAnalytics<T> {
|
|
16
20
|
private client;
|
|
17
21
|
constructor(options?: {
|
|
18
22
|
apiKey?: string;
|
|
19
23
|
host?: string;
|
|
20
24
|
});
|
|
21
|
-
capture
|
|
25
|
+
capture(options: T): void;
|
|
22
26
|
}
|
|
23
|
-
export {
|
|
27
|
+
export { decorator, PostHogCaptureOptionsType, PostHogAnalytics, IAnalytics, AnalyticsException, AnalyticsClassType };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import{Exception as
|
|
2
|
+
import{Exception as s}from"@ooneex/exception";import{HttpStatus as n}from"@ooneex/http-status";class o extends s{constructor(t,e={}){super(t,{status:n.Code.InternalServerError,data:e});this.name="AnalyticsException"}}import{container as i,EContainerScope as p}from"@ooneex/container";var a={analytics:(t=p.Singleton)=>{return(e)=>{i.add(e,t)}}};import{PostHog as c}from"posthog-node";class r{client=null;constructor(t){let e=t?.apiKey||Bun.env.ANALYTICS_POSTHOG_API_KEY;if(!e)throw new o("PostHog API key is required. Please provide an API key either through the constructor options or set the ANALYTICS_POSTHOG_API_KEY environment variable.");if(t?.apiKey)this.client=new c(e,{host:t.host||Bun.env.ANALYTICS_POSTHOG_HOST||"https://eu.i.posthog.com"})}capture(t){this.client?.capture({distinctId:t.id,event:t.event,properties:{$set:t.properties},timestamp:new Date,...t.groups&&{groups:t.groups}}),this.client?.shutdown()}}export{a as decorator,r as PostHogAnalytics,o as AnalyticsException};
|
|
3
3
|
|
|
4
|
-
//# debugId=
|
|
4
|
+
//# debugId=3D9127EA61038C0F64756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["src/AnalyticsException.ts", "src/
|
|
3
|
+
"sources": ["src/AnalyticsException.ts", "src/decorators.ts", "src/PostHogAnalytics.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"import { Exception } from \"@ooneex/exception\";\nimport { HttpStatus } from \"@ooneex/http-status\";\n\nexport class AnalyticsException extends Exception {\n constructor(message: string, data: Record<string, unknown> = {}) {\n super(message, {\n status: HttpStatus.Code.InternalServerError,\n data,\n });\n\n this.name = \"AnalyticsException\";\n }\n}\n",
|
|
6
|
-
"import {
|
|
6
|
+
"import { container, EContainerScope } from \"@ooneex/container\";\nimport type { AnalyticsClassType } from \"./types\";\n\nexport const decorator = {\n analytics: (scope: EContainerScope = EContainerScope.Singleton) => {\n return (target: AnalyticsClassType): void => {\n container.add(target, scope);\n };\n },\n};\n",
|
|
7
|
+
"import { PostHog } from \"posthog-node\";\nimport { AnalyticsException } from \"./AnalyticsException\";\nimport type { IAnalytics, PostHogCaptureOptionsType } from \"./types\";\n\nexport class PostHogAnalytics<T extends PostHogCaptureOptionsType = PostHogCaptureOptionsType>\n implements IAnalytics<T>\n{\n private client: PostHog | null = null;\n\n constructor(options?: { apiKey?: string; host?: string }) {\n const apiKey = options?.apiKey || Bun.env.ANALYTICS_POSTHOG_API_KEY;\n\n if (!apiKey) {\n throw new AnalyticsException(\n \"PostHog API key is required. Please provide an API key either through the constructor options or set the ANALYTICS_POSTHOG_API_KEY environment variable.\",\n );\n }\n\n if (options?.apiKey) {\n this.client = new PostHog(apiKey, {\n host: options.host || Bun.env.ANALYTICS_POSTHOG_HOST || \"https://eu.i.posthog.com\",\n });\n }\n }\n\n public capture(options: T): void {\n this.client?.capture({\n distinctId: options.id,\n event: options.event,\n properties: {\n $set: options.properties,\n },\n timestamp: new Date(),\n ...(options.groups && { groups: options.groups }),\n });\n this.client?.shutdown();\n }\n}\n"
|
|
7
8
|
],
|
|
8
|
-
"mappings": ";AAAA,oBAAS,0BACT,qBAAS,4BAEF,MAAM,UAA2B,CAAU,CAChD,WAAW,CAAC,EAAiB,EAAgC,CAAC,EAAG,CAC/D,MAAM,EAAS,CACb,OAAQ,EAAW,KAAK,oBACxB,MACF,CAAC,EAED,KAAK,KAAO,qBAEhB,CCZA,kBAAS,qBAIF,MAAM,
|
|
9
|
-
"debugId": "
|
|
9
|
+
"mappings": ";AAAA,oBAAS,0BACT,qBAAS,4BAEF,MAAM,UAA2B,CAAU,CAChD,WAAW,CAAC,EAAiB,EAAgC,CAAC,EAAG,CAC/D,MAAM,EAAS,CACb,OAAQ,EAAW,KAAK,oBACxB,MACF,CAAC,EAED,KAAK,KAAO,qBAEhB,CCZA,oBAAS,qBAAW,0BAGb,IAAM,EAAY,CACvB,UAAW,CAAC,EAAyB,EAAgB,YAAc,CACjE,MAAO,CAAC,IAAqC,CAC3C,EAAU,IAAI,EAAQ,CAAK,GAGjC,ECTA,kBAAS,qBAIF,MAAM,CAEb,CACU,OAAyB,KAEjC,WAAW,CAAC,EAA8C,CACxD,IAAM,EAAS,GAAS,QAAU,IAAI,IAAI,0BAE1C,GAAI,CAAC,EACH,MAAM,IAAI,EACR,0JACF,EAGF,GAAI,GAAS,OACX,KAAK,OAAS,IAAI,EAAQ,EAAQ,CAChC,KAAM,EAAQ,MAAQ,IAAI,IAAI,wBAA0B,0BAC1D,CAAC,EAIE,OAAO,CAAC,EAAkB,CAC/B,KAAK,QAAQ,QAAQ,CACnB,WAAY,EAAQ,GACpB,MAAO,EAAQ,MACf,WAAY,CACV,KAAM,EAAQ,UAChB,EACA,UAAW,IAAI,QACX,EAAQ,QAAU,CAAE,OAAQ,EAAQ,MAAO,CACjD,CAAC,EACD,KAAK,QAAQ,SAAS,EAE1B",
|
|
10
|
+
"debugId": "3D9127EA61038C0F64756E2164756E21",
|
|
10
11
|
"names": []
|
|
11
12
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/analytics",
|
|
3
|
-
"description": "",
|
|
4
|
-
"version": "0.0.
|
|
3
|
+
"description": "Analytics and event tracking integration with PostHog for user behavior insights and product analytics",
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"private": false,
|
|
7
6
|
"files": [
|
|
8
7
|
"dist",
|
|
9
8
|
"LICENSE",
|
|
@@ -26,15 +25,22 @@
|
|
|
26
25
|
"test": "bun test tests",
|
|
27
26
|
"build": "bunup",
|
|
28
27
|
"lint": "tsgo --noEmit && bunx biome lint",
|
|
29
|
-
"publish
|
|
30
|
-
"publish:pack": "bun pm pack --destination ./dist",
|
|
31
|
-
"publish:dry": "bun publish --dry-run"
|
|
28
|
+
"publish": "bun publish --access public || true"
|
|
32
29
|
},
|
|
33
|
-
"devDependencies": {},
|
|
34
|
-
"peerDependencies": {},
|
|
35
30
|
"dependencies": {
|
|
31
|
+
"@ooneex/container": "0.0.2",
|
|
36
32
|
"@ooneex/exception": "0.0.1",
|
|
37
33
|
"@ooneex/http-status": "0.0.1",
|
|
38
34
|
"posthog-node": "^5.11.0"
|
|
39
|
-
}
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"analytics",
|
|
38
|
+
"bun",
|
|
39
|
+
"metrics",
|
|
40
|
+
"ooneex",
|
|
41
|
+
"posthog",
|
|
42
|
+
"statistics",
|
|
43
|
+
"tracking",
|
|
44
|
+
"typescript"
|
|
45
|
+
]
|
|
40
46
|
}
|
|
Binary file
|