autotel-subscribers 4.0.0
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/LICENSE +21 -0
- package/README.md +669 -0
- package/dist/amplitude.cjs +2486 -0
- package/dist/amplitude.cjs.map +1 -0
- package/dist/amplitude.d.cts +49 -0
- package/dist/amplitude.d.ts +49 -0
- package/dist/amplitude.js +2463 -0
- package/dist/amplitude.js.map +1 -0
- package/dist/event-subscriber-base-CnF3V56W.d.cts +182 -0
- package/dist/event-subscriber-base-CnF3V56W.d.ts +182 -0
- package/dist/factories.cjs +16660 -0
- package/dist/factories.cjs.map +1 -0
- package/dist/factories.d.cts +304 -0
- package/dist/factories.d.ts +304 -0
- package/dist/factories.js +16624 -0
- package/dist/factories.js.map +1 -0
- package/dist/index.cjs +16575 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +179 -0
- package/dist/index.d.ts +179 -0
- package/dist/index.js +16539 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.cjs +220 -0
- package/dist/middleware.cjs.map +1 -0
- package/dist/middleware.d.cts +227 -0
- package/dist/middleware.d.ts +227 -0
- package/dist/middleware.js +208 -0
- package/dist/middleware.js.map +1 -0
- package/dist/mixpanel.cjs +2940 -0
- package/dist/mixpanel.cjs.map +1 -0
- package/dist/mixpanel.d.cts +47 -0
- package/dist/mixpanel.d.ts +47 -0
- package/dist/mixpanel.js +2932 -0
- package/dist/mixpanel.js.map +1 -0
- package/dist/posthog.cjs +4115 -0
- package/dist/posthog.cjs.map +1 -0
- package/dist/posthog.d.cts +299 -0
- package/dist/posthog.d.ts +299 -0
- package/dist/posthog.js +4113 -0
- package/dist/posthog.js.map +1 -0
- package/dist/segment.cjs +6822 -0
- package/dist/segment.cjs.map +1 -0
- package/dist/segment.d.cts +49 -0
- package/dist/segment.d.ts +49 -0
- package/dist/segment.js +6794 -0
- package/dist/segment.js.map +1 -0
- package/dist/slack.cjs +368 -0
- package/dist/slack.cjs.map +1 -0
- package/dist/slack.d.cts +126 -0
- package/dist/slack.d.ts +126 -0
- package/dist/slack.js +366 -0
- package/dist/slack.js.map +1 -0
- package/dist/webhook.cjs +100 -0
- package/dist/webhook.cjs.map +1 -0
- package/dist/webhook.d.cts +53 -0
- package/dist/webhook.d.ts +53 -0
- package/dist/webhook.js +98 -0
- package/dist/webhook.js.map +1 -0
- package/examples/quickstart-custom-subscriber.ts +144 -0
- package/examples/subscriber-bigquery.ts +219 -0
- package/examples/subscriber-databricks.ts +280 -0
- package/examples/subscriber-kafka.ts +326 -0
- package/examples/subscriber-kinesis.ts +307 -0
- package/examples/subscriber-posthog.ts +421 -0
- package/examples/subscriber-pubsub.ts +336 -0
- package/examples/subscriber-snowflake.ts +232 -0
- package/package.json +141 -0
- package/src/amplitude.test.ts +231 -0
- package/src/amplitude.ts +148 -0
- package/src/event-subscriber-base.ts +325 -0
- package/src/factories.ts +197 -0
- package/src/index.ts +50 -0
- package/src/middleware.ts +489 -0
- package/src/mixpanel.test.ts +194 -0
- package/src/mixpanel.ts +134 -0
- package/src/mock-event-subscriber.ts +333 -0
- package/src/posthog.test.ts +629 -0
- package/src/posthog.ts +530 -0
- package/src/segment.test.ts +228 -0
- package/src/segment.ts +148 -0
- package/src/slack.ts +383 -0
- package/src/streaming-event-subscriber.ts +323 -0
- package/src/testing/index.ts +37 -0
- package/src/testing/mock-webhook-server.ts +242 -0
- package/src/testing/subscriber-test-harness.ts +365 -0
- package/src/webhook.test.ts +264 -0
- package/src/webhook.ts +158 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { EventAttributes } from 'autotel/event-subscriber';
|
|
2
|
+
import { E as EventSubscriber, a as EventPayload } from './event-subscriber-base-CnF3V56W.cjs';
|
|
3
|
+
import { PostHog } from 'posthog-node';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* PostHog Subscriber for autotel
|
|
7
|
+
*
|
|
8
|
+
* Send events to PostHog for product events, feature flags, and A/B testing.
|
|
9
|
+
*
|
|
10
|
+
* @example Basic usage
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { Events } from 'autotel/events';
|
|
13
|
+
* import { PostHogSubscriber } from 'autotel-subscribers/posthog';
|
|
14
|
+
*
|
|
15
|
+
* const events = new Events('checkout', {
|
|
16
|
+
* subscribers: [
|
|
17
|
+
* new PostHogSubscriber({
|
|
18
|
+
* apiKey: process.env.POSTHOG_API_KEY!,
|
|
19
|
+
* host: 'https://us.i.posthog.com' // optional, defaults to US cloud
|
|
20
|
+
* })
|
|
21
|
+
* ]
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Events go to both OpenTelemetry AND PostHog
|
|
25
|
+
* events.trackEvent('order.completed', { userId: '123', amount: 99.99 });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example Feature flags
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const subscriber = new PostHogSubscriber({ apiKey: 'phc_...' });
|
|
31
|
+
*
|
|
32
|
+
* // Check if feature is enabled
|
|
33
|
+
* const isEnabled = await subscriber.isFeatureEnabled('new-checkout', 'user-123');
|
|
34
|
+
*
|
|
35
|
+
* // Get feature flag value (string, boolean, number)
|
|
36
|
+
* const variant = await subscriber.getFeatureFlag('experiment-variant', 'user-123');
|
|
37
|
+
*
|
|
38
|
+
* // Get all flags for a user
|
|
39
|
+
* const allFlags = await subscriber.getAllFlags('user-123');
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example Person and group events
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // Identify user and set properties
|
|
45
|
+
* await subscriber.identify('user-123', {
|
|
46
|
+
* email: 'user@example.com',
|
|
47
|
+
* plan: 'premium'
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // Identify a group (e.g., organization)
|
|
51
|
+
* await subscriber.groupIdentify('company', 'acme-corp', {
|
|
52
|
+
* industry: 'saas',
|
|
53
|
+
* employees: 500
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @example Serverless configuration
|
|
58
|
+
* ```typescript
|
|
59
|
+
* // Optimized for AWS Lambda / Vercel Functions
|
|
60
|
+
* const subscriber = new PostHogSubscriber({
|
|
61
|
+
* apiKey: 'phc_...',
|
|
62
|
+
* flushAt: 1, // Send immediately (don't batch)
|
|
63
|
+
* flushInterval: 0, // Disable interval-based flushing
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @example Custom PostHog client
|
|
68
|
+
* ```typescript
|
|
69
|
+
* import { PostHog } from 'posthog-node';
|
|
70
|
+
*
|
|
71
|
+
* const customClient = new PostHog('phc_...', {
|
|
72
|
+
* host: 'https://eu.i.posthog.com',
|
|
73
|
+
* // ... other PostHog options
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* const subscriber = new PostHogSubscriber({
|
|
77
|
+
* client: customClient
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example Error handling
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const subscriber = new PostHogSubscriber({
|
|
84
|
+
* apiKey: 'phc_...',
|
|
85
|
+
* onError: (error) => {
|
|
86
|
+
* console.error('PostHog error:', error);
|
|
87
|
+
* // Send to error tracking service
|
|
88
|
+
* }
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
interface PostHogConfig {
|
|
94
|
+
/** PostHog API key (starts with phc_) - required if not providing custom client */
|
|
95
|
+
apiKey?: string;
|
|
96
|
+
/** PostHog host (defaults to US cloud) */
|
|
97
|
+
host?: string;
|
|
98
|
+
/** Enable/disable the subscriber */
|
|
99
|
+
enabled?: boolean;
|
|
100
|
+
/** Custom PostHog client instance (bypasses apiKey/host) */
|
|
101
|
+
client?: PostHog;
|
|
102
|
+
/** Flush batch when it reaches this size (default: 20, set to 1 for immediate send) */
|
|
103
|
+
flushAt?: number;
|
|
104
|
+
/** Flush interval in milliseconds (default: 10000, set to 0 to disable) */
|
|
105
|
+
flushInterval?: number;
|
|
106
|
+
/** Disable geoip lookup to reduce request size (default: false) */
|
|
107
|
+
disableGeoip?: boolean;
|
|
108
|
+
/** Request timeout in milliseconds (default: 10000) */
|
|
109
|
+
requestTimeout?: number;
|
|
110
|
+
/** Send feature flag evaluation events (default: true) */
|
|
111
|
+
sendFeatureFlags?: boolean;
|
|
112
|
+
/** Error callback for debugging and monitoring */
|
|
113
|
+
onError?: (error: Error) => void;
|
|
114
|
+
/** Enable debug logging (default: false) */
|
|
115
|
+
debug?: boolean;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* PostHog feature flag options
|
|
119
|
+
*/
|
|
120
|
+
interface FeatureFlagOptions {
|
|
121
|
+
/** Group context for group-based feature flags */
|
|
122
|
+
groups?: Record<string, string | number>;
|
|
123
|
+
/** Group properties for feature flag evaluation */
|
|
124
|
+
groupProperties?: Record<string, Record<string, any>>;
|
|
125
|
+
/** Person properties for feature flag evaluation */
|
|
126
|
+
personProperties?: Record<string, any>;
|
|
127
|
+
/** Only evaluate locally, don't send $feature_flag_called event */
|
|
128
|
+
onlyEvaluateLocally?: boolean;
|
|
129
|
+
/** Send feature flag events even if disabled globally */
|
|
130
|
+
sendFeatureFlagEvents?: boolean;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Person properties for identify calls
|
|
134
|
+
*/
|
|
135
|
+
interface PersonProperties {
|
|
136
|
+
/** Set properties (will update existing values) */
|
|
137
|
+
$set?: Record<string, any>;
|
|
138
|
+
/** Set properties only if they don't exist */
|
|
139
|
+
$set_once?: Record<string, any>;
|
|
140
|
+
/** Any custom properties */
|
|
141
|
+
[key: string]: any;
|
|
142
|
+
}
|
|
143
|
+
declare class PostHogSubscriber extends EventSubscriber {
|
|
144
|
+
readonly name = "PostHogSubscriber";
|
|
145
|
+
readonly version = "2.0.0";
|
|
146
|
+
private posthog;
|
|
147
|
+
private config;
|
|
148
|
+
private initPromise;
|
|
149
|
+
constructor(config: PostHogConfig);
|
|
150
|
+
private initialize;
|
|
151
|
+
private setupErrorHandling;
|
|
152
|
+
private ensureInitialized;
|
|
153
|
+
private extractDistinctId;
|
|
154
|
+
/**
|
|
155
|
+
* Send payload to PostHog
|
|
156
|
+
*/
|
|
157
|
+
protected sendToDestination(payload: EventPayload): Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Check if a feature flag is enabled for a user
|
|
160
|
+
*
|
|
161
|
+
* @param flagKey - Feature flag key
|
|
162
|
+
* @param distinctId - User ID or anonymous ID
|
|
163
|
+
* @param options - Feature flag evaluation options
|
|
164
|
+
* @returns true if enabled, false otherwise
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const isEnabled = await subscriber.isFeatureEnabled('new-checkout', 'user-123');
|
|
169
|
+
*
|
|
170
|
+
* // With groups
|
|
171
|
+
* const isEnabled = await subscriber.isFeatureEnabled('beta-features', 'user-123', {
|
|
172
|
+
* groups: { company: 'acme-corp' }
|
|
173
|
+
* });
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
isFeatureEnabled(flagKey: string, distinctId: string, options?: FeatureFlagOptions): Promise<boolean>;
|
|
177
|
+
/**
|
|
178
|
+
* Get feature flag value for a user
|
|
179
|
+
*
|
|
180
|
+
* @param flagKey - Feature flag key
|
|
181
|
+
* @param distinctId - User ID or anonymous ID
|
|
182
|
+
* @param options - Feature flag evaluation options
|
|
183
|
+
* @returns Flag value (string, boolean, or undefined)
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const variant = await subscriber.getFeatureFlag('experiment-variant', 'user-123');
|
|
188
|
+
* // Returns: 'control' | 'test' | 'test-2' | undefined
|
|
189
|
+
*
|
|
190
|
+
* // With person properties
|
|
191
|
+
* const variant = await subscriber.getFeatureFlag('premium-feature', 'user-123', {
|
|
192
|
+
* personProperties: { plan: 'premium' }
|
|
193
|
+
* });
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
getFeatureFlag(flagKey: string, distinctId: string, options?: FeatureFlagOptions): Promise<string | boolean | undefined>;
|
|
197
|
+
/**
|
|
198
|
+
* Get all feature flags for a user
|
|
199
|
+
*
|
|
200
|
+
* @param distinctId - User ID or anonymous ID
|
|
201
|
+
* @param options - Feature flag evaluation options
|
|
202
|
+
* @returns Object mapping flag keys to their values
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const flags = await subscriber.getAllFlags('user-123');
|
|
207
|
+
* // Returns: { 'new-checkout': true, 'experiment-variant': 'test', ... }
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
getAllFlags(distinctId: string, options?: FeatureFlagOptions): Promise<Record<string, string | number | boolean>>;
|
|
211
|
+
/**
|
|
212
|
+
* Reload feature flags from PostHog server
|
|
213
|
+
*
|
|
214
|
+
* Call this to refresh feature flag definitions without restarting.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```typescript
|
|
218
|
+
* await subscriber.reloadFeatureFlags();
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
reloadFeatureFlags(): Promise<void>;
|
|
222
|
+
/**
|
|
223
|
+
* Identify a user and set their properties
|
|
224
|
+
*
|
|
225
|
+
* @param distinctId - User ID
|
|
226
|
+
* @param properties - Person properties ($set, $set_once, or custom properties)
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* // Set properties (will update existing values)
|
|
231
|
+
* await subscriber.identify('user-123', {
|
|
232
|
+
* $set: {
|
|
233
|
+
* email: 'user@example.com',
|
|
234
|
+
* plan: 'premium'
|
|
235
|
+
* }
|
|
236
|
+
* });
|
|
237
|
+
*
|
|
238
|
+
* // Set properties only once (won't update if already exists)
|
|
239
|
+
* await subscriber.identify('user-123', {
|
|
240
|
+
* $set_once: {
|
|
241
|
+
* signup_date: '2025-01-17'
|
|
242
|
+
* }
|
|
243
|
+
* });
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
identify(distinctId: string, properties?: PersonProperties): Promise<void>;
|
|
247
|
+
/**
|
|
248
|
+
* Identify a group and set its properties
|
|
249
|
+
*
|
|
250
|
+
* Groups are useful for B2B SaaS to track organizations, teams, or accounts.
|
|
251
|
+
*
|
|
252
|
+
* @param groupType - Type of group (e.g., 'company', 'organization', 'team')
|
|
253
|
+
* @param groupKey - Unique identifier for the group
|
|
254
|
+
* @param properties - Group properties
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* await subscriber.groupIdentify('company', 'acme-corp', {
|
|
259
|
+
* $set: {
|
|
260
|
+
* name: 'Acme Corporation',
|
|
261
|
+
* industry: 'saas',
|
|
262
|
+
* employees: 500,
|
|
263
|
+
* plan: 'enterprise'
|
|
264
|
+
* }
|
|
265
|
+
* });
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
groupIdentify(groupType: string, groupKey: string | number, properties?: Record<string, any>): Promise<void>;
|
|
269
|
+
/**
|
|
270
|
+
* Track an event with group context
|
|
271
|
+
*
|
|
272
|
+
* Use this to associate events with groups (e.g., organizations).
|
|
273
|
+
*
|
|
274
|
+
* @param name - Event name
|
|
275
|
+
* @param attributes - Event attributes
|
|
276
|
+
* @param groups - Group context (e.g., { company: 'acme-corp' })
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```typescript
|
|
280
|
+
* await subscriber.trackEventWithGroups('feature.used', {
|
|
281
|
+
* userId: 'user-123',
|
|
282
|
+
* feature: 'advanced-events'
|
|
283
|
+
* }, {
|
|
284
|
+
* company: 'acme-corp'
|
|
285
|
+
* });
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
trackEventWithGroups(name: string, attributes?: EventAttributes, groups?: Record<string, string | number>): Promise<void>;
|
|
289
|
+
/**
|
|
290
|
+
* Flush pending events and clean up resources
|
|
291
|
+
*/
|
|
292
|
+
shutdown(): Promise<void>;
|
|
293
|
+
/**
|
|
294
|
+
* Handle errors with custom error handler
|
|
295
|
+
*/
|
|
296
|
+
protected handleError(error: Error, payload: EventPayload): void;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export { type FeatureFlagOptions, type PersonProperties, type PostHogConfig, PostHogSubscriber };
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { EventAttributes } from 'autotel/event-subscriber';
|
|
2
|
+
import { E as EventSubscriber, a as EventPayload } from './event-subscriber-base-CnF3V56W.js';
|
|
3
|
+
import { PostHog } from 'posthog-node';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* PostHog Subscriber for autotel
|
|
7
|
+
*
|
|
8
|
+
* Send events to PostHog for product events, feature flags, and A/B testing.
|
|
9
|
+
*
|
|
10
|
+
* @example Basic usage
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { Events } from 'autotel/events';
|
|
13
|
+
* import { PostHogSubscriber } from 'autotel-subscribers/posthog';
|
|
14
|
+
*
|
|
15
|
+
* const events = new Events('checkout', {
|
|
16
|
+
* subscribers: [
|
|
17
|
+
* new PostHogSubscriber({
|
|
18
|
+
* apiKey: process.env.POSTHOG_API_KEY!,
|
|
19
|
+
* host: 'https://us.i.posthog.com' // optional, defaults to US cloud
|
|
20
|
+
* })
|
|
21
|
+
* ]
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Events go to both OpenTelemetry AND PostHog
|
|
25
|
+
* events.trackEvent('order.completed', { userId: '123', amount: 99.99 });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example Feature flags
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const subscriber = new PostHogSubscriber({ apiKey: 'phc_...' });
|
|
31
|
+
*
|
|
32
|
+
* // Check if feature is enabled
|
|
33
|
+
* const isEnabled = await subscriber.isFeatureEnabled('new-checkout', 'user-123');
|
|
34
|
+
*
|
|
35
|
+
* // Get feature flag value (string, boolean, number)
|
|
36
|
+
* const variant = await subscriber.getFeatureFlag('experiment-variant', 'user-123');
|
|
37
|
+
*
|
|
38
|
+
* // Get all flags for a user
|
|
39
|
+
* const allFlags = await subscriber.getAllFlags('user-123');
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example Person and group events
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // Identify user and set properties
|
|
45
|
+
* await subscriber.identify('user-123', {
|
|
46
|
+
* email: 'user@example.com',
|
|
47
|
+
* plan: 'premium'
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // Identify a group (e.g., organization)
|
|
51
|
+
* await subscriber.groupIdentify('company', 'acme-corp', {
|
|
52
|
+
* industry: 'saas',
|
|
53
|
+
* employees: 500
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @example Serverless configuration
|
|
58
|
+
* ```typescript
|
|
59
|
+
* // Optimized for AWS Lambda / Vercel Functions
|
|
60
|
+
* const subscriber = new PostHogSubscriber({
|
|
61
|
+
* apiKey: 'phc_...',
|
|
62
|
+
* flushAt: 1, // Send immediately (don't batch)
|
|
63
|
+
* flushInterval: 0, // Disable interval-based flushing
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @example Custom PostHog client
|
|
68
|
+
* ```typescript
|
|
69
|
+
* import { PostHog } from 'posthog-node';
|
|
70
|
+
*
|
|
71
|
+
* const customClient = new PostHog('phc_...', {
|
|
72
|
+
* host: 'https://eu.i.posthog.com',
|
|
73
|
+
* // ... other PostHog options
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* const subscriber = new PostHogSubscriber({
|
|
77
|
+
* client: customClient
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example Error handling
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const subscriber = new PostHogSubscriber({
|
|
84
|
+
* apiKey: 'phc_...',
|
|
85
|
+
* onError: (error) => {
|
|
86
|
+
* console.error('PostHog error:', error);
|
|
87
|
+
* // Send to error tracking service
|
|
88
|
+
* }
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
interface PostHogConfig {
|
|
94
|
+
/** PostHog API key (starts with phc_) - required if not providing custom client */
|
|
95
|
+
apiKey?: string;
|
|
96
|
+
/** PostHog host (defaults to US cloud) */
|
|
97
|
+
host?: string;
|
|
98
|
+
/** Enable/disable the subscriber */
|
|
99
|
+
enabled?: boolean;
|
|
100
|
+
/** Custom PostHog client instance (bypasses apiKey/host) */
|
|
101
|
+
client?: PostHog;
|
|
102
|
+
/** Flush batch when it reaches this size (default: 20, set to 1 for immediate send) */
|
|
103
|
+
flushAt?: number;
|
|
104
|
+
/** Flush interval in milliseconds (default: 10000, set to 0 to disable) */
|
|
105
|
+
flushInterval?: number;
|
|
106
|
+
/** Disable geoip lookup to reduce request size (default: false) */
|
|
107
|
+
disableGeoip?: boolean;
|
|
108
|
+
/** Request timeout in milliseconds (default: 10000) */
|
|
109
|
+
requestTimeout?: number;
|
|
110
|
+
/** Send feature flag evaluation events (default: true) */
|
|
111
|
+
sendFeatureFlags?: boolean;
|
|
112
|
+
/** Error callback for debugging and monitoring */
|
|
113
|
+
onError?: (error: Error) => void;
|
|
114
|
+
/** Enable debug logging (default: false) */
|
|
115
|
+
debug?: boolean;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* PostHog feature flag options
|
|
119
|
+
*/
|
|
120
|
+
interface FeatureFlagOptions {
|
|
121
|
+
/** Group context for group-based feature flags */
|
|
122
|
+
groups?: Record<string, string | number>;
|
|
123
|
+
/** Group properties for feature flag evaluation */
|
|
124
|
+
groupProperties?: Record<string, Record<string, any>>;
|
|
125
|
+
/** Person properties for feature flag evaluation */
|
|
126
|
+
personProperties?: Record<string, any>;
|
|
127
|
+
/** Only evaluate locally, don't send $feature_flag_called event */
|
|
128
|
+
onlyEvaluateLocally?: boolean;
|
|
129
|
+
/** Send feature flag events even if disabled globally */
|
|
130
|
+
sendFeatureFlagEvents?: boolean;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Person properties for identify calls
|
|
134
|
+
*/
|
|
135
|
+
interface PersonProperties {
|
|
136
|
+
/** Set properties (will update existing values) */
|
|
137
|
+
$set?: Record<string, any>;
|
|
138
|
+
/** Set properties only if they don't exist */
|
|
139
|
+
$set_once?: Record<string, any>;
|
|
140
|
+
/** Any custom properties */
|
|
141
|
+
[key: string]: any;
|
|
142
|
+
}
|
|
143
|
+
declare class PostHogSubscriber extends EventSubscriber {
|
|
144
|
+
readonly name = "PostHogSubscriber";
|
|
145
|
+
readonly version = "2.0.0";
|
|
146
|
+
private posthog;
|
|
147
|
+
private config;
|
|
148
|
+
private initPromise;
|
|
149
|
+
constructor(config: PostHogConfig);
|
|
150
|
+
private initialize;
|
|
151
|
+
private setupErrorHandling;
|
|
152
|
+
private ensureInitialized;
|
|
153
|
+
private extractDistinctId;
|
|
154
|
+
/**
|
|
155
|
+
* Send payload to PostHog
|
|
156
|
+
*/
|
|
157
|
+
protected sendToDestination(payload: EventPayload): Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Check if a feature flag is enabled for a user
|
|
160
|
+
*
|
|
161
|
+
* @param flagKey - Feature flag key
|
|
162
|
+
* @param distinctId - User ID or anonymous ID
|
|
163
|
+
* @param options - Feature flag evaluation options
|
|
164
|
+
* @returns true if enabled, false otherwise
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const isEnabled = await subscriber.isFeatureEnabled('new-checkout', 'user-123');
|
|
169
|
+
*
|
|
170
|
+
* // With groups
|
|
171
|
+
* const isEnabled = await subscriber.isFeatureEnabled('beta-features', 'user-123', {
|
|
172
|
+
* groups: { company: 'acme-corp' }
|
|
173
|
+
* });
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
isFeatureEnabled(flagKey: string, distinctId: string, options?: FeatureFlagOptions): Promise<boolean>;
|
|
177
|
+
/**
|
|
178
|
+
* Get feature flag value for a user
|
|
179
|
+
*
|
|
180
|
+
* @param flagKey - Feature flag key
|
|
181
|
+
* @param distinctId - User ID or anonymous ID
|
|
182
|
+
* @param options - Feature flag evaluation options
|
|
183
|
+
* @returns Flag value (string, boolean, or undefined)
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const variant = await subscriber.getFeatureFlag('experiment-variant', 'user-123');
|
|
188
|
+
* // Returns: 'control' | 'test' | 'test-2' | undefined
|
|
189
|
+
*
|
|
190
|
+
* // With person properties
|
|
191
|
+
* const variant = await subscriber.getFeatureFlag('premium-feature', 'user-123', {
|
|
192
|
+
* personProperties: { plan: 'premium' }
|
|
193
|
+
* });
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
getFeatureFlag(flagKey: string, distinctId: string, options?: FeatureFlagOptions): Promise<string | boolean | undefined>;
|
|
197
|
+
/**
|
|
198
|
+
* Get all feature flags for a user
|
|
199
|
+
*
|
|
200
|
+
* @param distinctId - User ID or anonymous ID
|
|
201
|
+
* @param options - Feature flag evaluation options
|
|
202
|
+
* @returns Object mapping flag keys to their values
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const flags = await subscriber.getAllFlags('user-123');
|
|
207
|
+
* // Returns: { 'new-checkout': true, 'experiment-variant': 'test', ... }
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
getAllFlags(distinctId: string, options?: FeatureFlagOptions): Promise<Record<string, string | number | boolean>>;
|
|
211
|
+
/**
|
|
212
|
+
* Reload feature flags from PostHog server
|
|
213
|
+
*
|
|
214
|
+
* Call this to refresh feature flag definitions without restarting.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```typescript
|
|
218
|
+
* await subscriber.reloadFeatureFlags();
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
reloadFeatureFlags(): Promise<void>;
|
|
222
|
+
/**
|
|
223
|
+
* Identify a user and set their properties
|
|
224
|
+
*
|
|
225
|
+
* @param distinctId - User ID
|
|
226
|
+
* @param properties - Person properties ($set, $set_once, or custom properties)
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* // Set properties (will update existing values)
|
|
231
|
+
* await subscriber.identify('user-123', {
|
|
232
|
+
* $set: {
|
|
233
|
+
* email: 'user@example.com',
|
|
234
|
+
* plan: 'premium'
|
|
235
|
+
* }
|
|
236
|
+
* });
|
|
237
|
+
*
|
|
238
|
+
* // Set properties only once (won't update if already exists)
|
|
239
|
+
* await subscriber.identify('user-123', {
|
|
240
|
+
* $set_once: {
|
|
241
|
+
* signup_date: '2025-01-17'
|
|
242
|
+
* }
|
|
243
|
+
* });
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
identify(distinctId: string, properties?: PersonProperties): Promise<void>;
|
|
247
|
+
/**
|
|
248
|
+
* Identify a group and set its properties
|
|
249
|
+
*
|
|
250
|
+
* Groups are useful for B2B SaaS to track organizations, teams, or accounts.
|
|
251
|
+
*
|
|
252
|
+
* @param groupType - Type of group (e.g., 'company', 'organization', 'team')
|
|
253
|
+
* @param groupKey - Unique identifier for the group
|
|
254
|
+
* @param properties - Group properties
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* await subscriber.groupIdentify('company', 'acme-corp', {
|
|
259
|
+
* $set: {
|
|
260
|
+
* name: 'Acme Corporation',
|
|
261
|
+
* industry: 'saas',
|
|
262
|
+
* employees: 500,
|
|
263
|
+
* plan: 'enterprise'
|
|
264
|
+
* }
|
|
265
|
+
* });
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
groupIdentify(groupType: string, groupKey: string | number, properties?: Record<string, any>): Promise<void>;
|
|
269
|
+
/**
|
|
270
|
+
* Track an event with group context
|
|
271
|
+
*
|
|
272
|
+
* Use this to associate events with groups (e.g., organizations).
|
|
273
|
+
*
|
|
274
|
+
* @param name - Event name
|
|
275
|
+
* @param attributes - Event attributes
|
|
276
|
+
* @param groups - Group context (e.g., { company: 'acme-corp' })
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```typescript
|
|
280
|
+
* await subscriber.trackEventWithGroups('feature.used', {
|
|
281
|
+
* userId: 'user-123',
|
|
282
|
+
* feature: 'advanced-events'
|
|
283
|
+
* }, {
|
|
284
|
+
* company: 'acme-corp'
|
|
285
|
+
* });
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
trackEventWithGroups(name: string, attributes?: EventAttributes, groups?: Record<string, string | number>): Promise<void>;
|
|
289
|
+
/**
|
|
290
|
+
* Flush pending events and clean up resources
|
|
291
|
+
*/
|
|
292
|
+
shutdown(): Promise<void>;
|
|
293
|
+
/**
|
|
294
|
+
* Handle errors with custom error handler
|
|
295
|
+
*/
|
|
296
|
+
protected handleError(error: Error, payload: EventPayload): void;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export { type FeatureFlagOptions, type PersonProperties, type PostHogConfig, PostHogSubscriber };
|