@posthog/types 1.313.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.
Files changed (48) hide show
  1. package/LICENSE +299 -0
  2. package/README.md +103 -0
  3. package/dist/capture.d.ts +65 -0
  4. package/dist/capture.d.ts.map +1 -0
  5. package/dist/capture.js +18 -0
  6. package/dist/capture.mjs +0 -0
  7. package/dist/common.d.ts +10 -0
  8. package/dist/common.d.ts.map +1 -0
  9. package/dist/common.js +18 -0
  10. package/dist/common.mjs +0 -0
  11. package/dist/feature-flags.d.ts +44 -0
  12. package/dist/feature-flags.d.ts.map +1 -0
  13. package/dist/feature-flags.js +18 -0
  14. package/dist/feature-flags.mjs +0 -0
  15. package/dist/index.d.ts +15 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +18 -0
  18. package/dist/index.mjs +0 -0
  19. package/dist/posthog-config.d.ts +1218 -0
  20. package/dist/posthog-config.d.ts.map +1 -0
  21. package/dist/posthog-config.js +18 -0
  22. package/dist/posthog-config.mjs +0 -0
  23. package/dist/posthog.d.ts +395 -0
  24. package/dist/posthog.d.ts.map +1 -0
  25. package/dist/posthog.js +18 -0
  26. package/dist/posthog.mjs +0 -0
  27. package/dist/request.d.ts +11 -0
  28. package/dist/request.d.ts.map +1 -0
  29. package/dist/request.js +18 -0
  30. package/dist/request.mjs +0 -0
  31. package/dist/segment.d.ts +52 -0
  32. package/dist/segment.d.ts.map +1 -0
  33. package/dist/segment.js +18 -0
  34. package/dist/segment.mjs +0 -0
  35. package/dist/session-recording.d.ts +60 -0
  36. package/dist/session-recording.d.ts.map +1 -0
  37. package/dist/session-recording.js +18 -0
  38. package/dist/session-recording.mjs +0 -0
  39. package/package.json +43 -0
  40. package/src/capture.ts +101 -0
  41. package/src/common.ts +9 -0
  42. package/src/feature-flags.ts +60 -0
  43. package/src/index.ts +74 -0
  44. package/src/posthog-config.ts +1395 -0
  45. package/src/posthog.ts +490 -0
  46. package/src/request.ts +14 -0
  47. package/src/segment.ts +56 -0
  48. package/src/session-recording.ts +106 -0
package/src/posthog.ts ADDED
@@ -0,0 +1,490 @@
1
+ /**
2
+ * PostHog instance interface
3
+ *
4
+ * This interface describes the public API of the PostHog class.
5
+ * It can be used to type `window.posthog` when loading PostHog via a script tag.
6
+ */
7
+
8
+ import type { PostHogConfig } from './posthog-config'
9
+ import type { Properties, JsonType } from './common'
10
+ import type { CaptureResult, CaptureOptions } from './capture'
11
+ import type { FeatureFlagsCallback, EarlyAccessFeatureCallback, EarlyAccessFeatureStage } from './feature-flags'
12
+ import type { SessionIdChangedCallback } from './session-recording'
13
+ import type { RequestCallback } from './request'
14
+
15
+ /**
16
+ * The PostHog instance interface.
17
+ *
18
+ * This can be used to type `window.posthog` when loading PostHog via a `<script>` tag.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * import type { PostHog } from '@posthog/types'
23
+ *
24
+ * declare global {
25
+ * interface Window {
26
+ * posthog?: PostHog
27
+ * }
28
+ * }
29
+ *
30
+ * // Now you can use window.posthog with type safety
31
+ * window.posthog?.capture('my_event', { property: 'value' })
32
+ * ```
33
+ */
34
+ export interface PostHog {
35
+ /**
36
+ * The current configuration of the PostHog instance.
37
+ */
38
+ config: PostHogConfig
39
+
40
+ /**
41
+ * The library version.
42
+ */
43
+ version: string
44
+
45
+ /**
46
+ * Whether the PostHog instance has been loaded.
47
+ */
48
+ __loaded: boolean
49
+
50
+ /**
51
+ * Whether the flags endpoint has been hit.
52
+ */
53
+ flagsEndpointWasHit: boolean
54
+
55
+ // ============================================================================
56
+ // Initialization
57
+ // ============================================================================
58
+
59
+ /**
60
+ * Initializes a new instance of the PostHog capturing object.
61
+ *
62
+ * @param token - Your PostHog API token
63
+ * @param config - A dictionary of config options to override
64
+ * @param name - The name for the new posthog instance that you want created
65
+ * @returns The newly initialized PostHog instance
66
+ */
67
+ init(token: string, config?: Partial<PostHogConfig>, name?: string): PostHog
68
+
69
+ // ============================================================================
70
+ // Event Capture
71
+ // ============================================================================
72
+
73
+ /**
74
+ * Capture an event.
75
+ *
76
+ * @param event_name - The name of the event
77
+ * @param properties - A set of properties to include with the event
78
+ * @param options - Additional options for the capture
79
+ * @returns The capture result
80
+ */
81
+ capture(event_name: string, properties?: Properties | null, options?: CaptureOptions): CaptureResult | undefined
82
+
83
+ /**
84
+ * Capture an exception.
85
+ *
86
+ * @param error - The error to capture
87
+ * @param additionalProperties - Additional properties to include with the event
88
+ * @returns The capture result
89
+ */
90
+ captureException(error: unknown, additionalProperties?: Properties): CaptureResult | undefined
91
+
92
+ // ============================================================================
93
+ // User Identification
94
+ // ============================================================================
95
+
96
+ /**
97
+ * Identify a user with a distinct ID and optionally set person properties.
98
+ *
99
+ * @param new_distinct_id - The new distinct ID for the user
100
+ * @param userPropertiesToSet - Properties to set on the user (using $set)
101
+ * @param userPropertiesToSetOnce - Properties to set once on the user (using $set_once)
102
+ */
103
+ identify(new_distinct_id?: string, userPropertiesToSet?: Properties, userPropertiesToSetOnce?: Properties): void
104
+
105
+ /**
106
+ * Set properties on the current user.
107
+ *
108
+ * @param userPropertiesToSet - Properties to set on the user (using $set)
109
+ * @param userPropertiesToSetOnce - Properties to set once on the user (using $set_once)
110
+ */
111
+ setPersonProperties(userPropertiesToSet?: Properties, userPropertiesToSetOnce?: Properties): void
112
+
113
+ /**
114
+ * Create an alias for the current user.
115
+ *
116
+ * @param alias - The alias to create
117
+ * @param original - The original distinct ID (defaults to current distinct ID)
118
+ */
119
+ alias(alias: string, original?: string): CaptureResult | void | number
120
+
121
+ /**
122
+ * Get the current distinct ID.
123
+ *
124
+ * @returns The current distinct ID
125
+ */
126
+ get_distinct_id(): string
127
+
128
+ /**
129
+ * Reset the user's identity and start a new session.
130
+ *
131
+ * @param reset_device_id - Whether to reset the device ID as well
132
+ */
133
+ reset(reset_device_id?: boolean): void
134
+
135
+ /**
136
+ * Create a person profile for the current user.
137
+ */
138
+ createPersonProfile(): void
139
+
140
+ // ============================================================================
141
+ // Groups
142
+ // ============================================================================
143
+
144
+ /**
145
+ * Associate the user with a group.
146
+ *
147
+ * @param groupType - The type of group (e.g., 'company', 'project')
148
+ * @param groupKey - The unique identifier for the group
149
+ * @param groupPropertiesToSet - Properties to set on the group
150
+ */
151
+ group(groupType: string, groupKey: string, groupPropertiesToSet?: Properties): void
152
+
153
+ /**
154
+ * Get the current groups.
155
+ *
156
+ * @returns A record of group types to group keys
157
+ */
158
+ getGroups(): Record<string, any>
159
+
160
+ /**
161
+ * Reset all groups for the current user.
162
+ */
163
+ resetGroups(): void
164
+
165
+ // ============================================================================
166
+ // Feature Flags
167
+ // ============================================================================
168
+
169
+ /**
170
+ * Get the value of a feature flag.
171
+ *
172
+ * @param key - The feature flag key
173
+ * @param options - Options for the feature flag lookup
174
+ * @returns The feature flag value (boolean for simple flags, string for multivariate)
175
+ */
176
+ getFeatureFlag(key: string, options?: { send_event?: boolean }): boolean | string | undefined
177
+
178
+ /**
179
+ * Get the payload of a feature flag.
180
+ *
181
+ * @param key - The feature flag key
182
+ * @returns The feature flag payload
183
+ */
184
+ getFeatureFlagPayload(key: string): JsonType
185
+
186
+ /**
187
+ * Check if a feature flag is enabled.
188
+ *
189
+ * @param key - The feature flag key
190
+ * @param options - Options for the feature flag lookup
191
+ * @returns Whether the feature flag is enabled
192
+ */
193
+ isFeatureEnabled(key: string, options?: { send_event?: boolean }): boolean | undefined
194
+
195
+ /**
196
+ * Reload feature flags from the server.
197
+ */
198
+ reloadFeatureFlags(): void
199
+
200
+ /**
201
+ * Register a callback to be called when feature flags are loaded.
202
+ *
203
+ * @param callback - The callback to call
204
+ * @returns A function to unsubscribe
205
+ */
206
+ onFeatureFlags(callback: FeatureFlagsCallback): () => void
207
+
208
+ /**
209
+ * Set person properties to be used for feature flag evaluation.
210
+ *
211
+ * @param properties - The properties to set
212
+ * @param reloadFeatureFlags - Whether to reload feature flags after setting
213
+ */
214
+ setPersonPropertiesForFlags(properties: Properties, reloadFeatureFlags?: boolean): void
215
+
216
+ /**
217
+ * Reset person properties used for feature flag evaluation.
218
+ */
219
+ resetPersonPropertiesForFlags(): void
220
+
221
+ /**
222
+ * Set group properties to be used for feature flag evaluation.
223
+ *
224
+ * @param properties - The properties to set (keyed by group type)
225
+ * @param reloadFeatureFlags - Whether to reload feature flags after setting
226
+ */
227
+ setGroupPropertiesForFlags(properties: { [type: string]: Properties }, reloadFeatureFlags?: boolean): void
228
+
229
+ /**
230
+ * Reset group properties used for feature flag evaluation.
231
+ *
232
+ * @param group_type - Optional group type to reset (resets all if not provided)
233
+ */
234
+ resetGroupPropertiesForFlags(group_type?: string): void
235
+
236
+ // ============================================================================
237
+ // Early Access Features
238
+ // ============================================================================
239
+
240
+ /**
241
+ * Get the list of early access features.
242
+ *
243
+ * @param callback - Callback to receive the features
244
+ * @param forceReload - Whether to force a reload from the server
245
+ */
246
+ getEarlyAccessFeatures(callback: EarlyAccessFeatureCallback, forceReload?: boolean): void
247
+
248
+ /**
249
+ * Update enrollment in an early access feature.
250
+ *
251
+ * @param key - The feature key
252
+ * @param isEnrolled - Whether the user is enrolled
253
+ * @param stage - The stage of the feature
254
+ */
255
+ updateEarlyAccessFeatureEnrollment(key: string, isEnrolled: boolean, stage?: EarlyAccessFeatureStage): void
256
+
257
+ // ============================================================================
258
+ // Super Properties
259
+ // ============================================================================
260
+
261
+ /**
262
+ * Register properties to be sent with every event.
263
+ *
264
+ * @param properties - The properties to register
265
+ * @param days - Number of days to persist the properties
266
+ */
267
+ register(properties: Properties, days?: number): void
268
+
269
+ /**
270
+ * Register properties to be sent with every event, but only if they haven't been set before.
271
+ *
272
+ * @param properties - The properties to register
273
+ * @param default_value - Default value for the property
274
+ * @param days - Number of days to persist the properties
275
+ */
276
+ register_once(properties: Properties, default_value?: any, days?: number): void
277
+
278
+ /**
279
+ * Register properties for the current session only.
280
+ *
281
+ * @param properties - The properties to register
282
+ */
283
+ register_for_session(properties: Properties): void
284
+
285
+ /**
286
+ * Unregister a property so it is no longer sent with events.
287
+ *
288
+ * @param property - The property name to unregister
289
+ */
290
+ unregister(property: string): void
291
+
292
+ /**
293
+ * Unregister a session property.
294
+ *
295
+ * @param property - The property name to unregister
296
+ */
297
+ unregister_for_session(property: string): void
298
+
299
+ /**
300
+ * Get a property value from persistence.
301
+ *
302
+ * @param property_name - The property name
303
+ * @returns The property value
304
+ */
305
+ get_property(property_name: string): any | undefined
306
+
307
+ /**
308
+ * Get a session property value.
309
+ *
310
+ * @param property_name - The property name
311
+ * @returns The property value
312
+ */
313
+ getSessionProperty(property_name: string): any | undefined
314
+
315
+ // ============================================================================
316
+ // Session & Recording
317
+ // ============================================================================
318
+
319
+ /**
320
+ * Get the current session ID.
321
+ *
322
+ * @returns The current session ID
323
+ */
324
+ get_session_id(): string
325
+
326
+ /**
327
+ * Register a callback to be called when the session ID changes.
328
+ *
329
+ * @param callback - The callback to call
330
+ * @returns A function to unsubscribe
331
+ */
332
+ onSessionId(callback: SessionIdChangedCallback): () => void
333
+
334
+ /**
335
+ * Get the URL to view the current session recording.
336
+ *
337
+ * @param options - Options for the URL
338
+ * @returns The session replay URL
339
+ */
340
+ get_session_replay_url(options?: { withTimestamp?: boolean; timestampLookBack?: number }): string
341
+
342
+ /**
343
+ * Start session recording (if not already started).
344
+ *
345
+ * @param override - Options to override default behavior, or `true` to override all controls
346
+ * @param override.sampling - Override the default sampling behavior
347
+ * @param override.linked_flag - Override the default linked_flag behavior
348
+ * @param override.url_trigger - Override the default url_trigger behavior (only accepts `true`)
349
+ * @param override.event_trigger - Override the default event_trigger behavior (only accepts `true`)
350
+ */
351
+ startSessionRecording(
352
+ override?: { sampling?: boolean; linked_flag?: boolean; url_trigger?: true; event_trigger?: true } | true
353
+ ): void
354
+
355
+ /**
356
+ * Stop session recording.
357
+ */
358
+ stopSessionRecording(): void
359
+
360
+ /**
361
+ * Check if session recording has started.
362
+ *
363
+ * @returns Whether session recording has started
364
+ */
365
+ sessionRecordingStarted(): boolean
366
+
367
+ // ============================================================================
368
+ // Consent & Opt-in/out
369
+ // ============================================================================
370
+
371
+ /**
372
+ * Opt the user into capturing.
373
+ */
374
+ opt_in_capturing(): void
375
+
376
+ /**
377
+ * Opt the user out of capturing.
378
+ */
379
+ opt_out_capturing(): void
380
+
381
+ /**
382
+ * Check if the user has opted in to capturing.
383
+ *
384
+ * @returns Whether the user has opted in
385
+ */
386
+ has_opted_in_capturing(): boolean
387
+
388
+ /**
389
+ * Check if the user has opted out of capturing.
390
+ *
391
+ * @returns Whether the user has opted out
392
+ */
393
+ has_opted_out_capturing(): boolean
394
+
395
+ /**
396
+ * Get the explicit consent status.
397
+ *
398
+ * @returns The consent status
399
+ */
400
+ get_explicit_consent_status(): 'granted' | 'denied' | 'pending'
401
+
402
+ /**
403
+ * Clear the opt-in/out status.
404
+ */
405
+ clear_opt_in_out_capturing(): void
406
+
407
+ // ============================================================================
408
+ // Configuration
409
+ // ============================================================================
410
+
411
+ /**
412
+ * Update the configuration.
413
+ *
414
+ * @param config - The configuration to merge
415
+ */
416
+ set_config(config: Partial<PostHogConfig>): void
417
+
418
+ /**
419
+ * Enable or disable debug mode.
420
+ *
421
+ * @param debug - Whether to enable debug mode (defaults to true)
422
+ */
423
+ debug(debug?: boolean): void
424
+
425
+ // ============================================================================
426
+ // Surveys
427
+ // ============================================================================
428
+
429
+ /**
430
+ * Get the list of surveys.
431
+ *
432
+ * @param callback - Callback to receive the surveys
433
+ * @param forceReload - Whether to force a reload from the server
434
+ */
435
+ getSurveys(callback: (surveys: any[]) => void, forceReload?: boolean): void
436
+
437
+ /**
438
+ * Get active surveys that match the current user.
439
+ *
440
+ * @param callback - Callback to receive the surveys
441
+ * @param forceReload - Whether to force a reload from the server
442
+ */
443
+ getActiveMatchingSurveys(callback: (surveys: any[]) => void, forceReload?: boolean): void
444
+
445
+ /**
446
+ * Render a survey in a specific container.
447
+ *
448
+ * @param surveyId - The survey ID
449
+ * @param selector - CSS selector for the container
450
+ */
451
+ renderSurvey(surveyId: string, selector: string): void
452
+
453
+ /**
454
+ * Check if a survey can be rendered.
455
+ *
456
+ * @param surveyId - The survey ID
457
+ * @returns The render reason or null if can't render
458
+ */
459
+ canRenderSurvey(surveyId: string): any | null
460
+
461
+ // ============================================================================
462
+ // Events
463
+ // ============================================================================
464
+
465
+ /**
466
+ * Register an event listener.
467
+ *
468
+ * @param event - The event name (currently only 'eventCaptured' is supported)
469
+ * @param cb - The callback to call
470
+ * @returns A function to unsubscribe
471
+ */
472
+ on(event: 'eventCaptured', cb: (...args: any[]) => void): () => void
473
+
474
+ // ============================================================================
475
+ // Deprecated
476
+ // ============================================================================
477
+
478
+ /**
479
+ * @deprecated Use `setPersonProperties` instead
480
+ */
481
+ people: {
482
+ set: (prop: string | Properties, to?: string, callback?: RequestCallback) => void
483
+ set_once: (prop: string | Properties, to?: string, callback?: RequestCallback) => void
484
+ }
485
+
486
+ /**
487
+ * @deprecated Use `flagsEndpointWasHit` instead
488
+ */
489
+ decideEndpointWasHit: boolean
490
+ }
package/src/request.ts ADDED
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Request-related types
3
+ */
4
+
5
+ export type Headers = Record<string, string>
6
+
7
+ // Minimal class to allow interop between different request methods (xhr / fetch)
8
+ export interface RequestResponse {
9
+ statusCode: number
10
+ text?: string
11
+ json?: any
12
+ }
13
+
14
+ export type RequestCallback = (response: RequestResponse) => void
package/src/segment.ts ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Segment integration types
3
+ */
4
+
5
+ /**
6
+ * Segment user object
7
+ */
8
+ export type SegmentUser = {
9
+ anonymousId(): string | undefined
10
+ id(): string | undefined
11
+ }
12
+
13
+ /**
14
+ * Segment analytics object used for integration with PostHog
15
+ */
16
+ export type SegmentAnalytics = {
17
+ user: () => SegmentUser | Promise<SegmentUser>
18
+ register: (integration: SegmentPlugin) => Promise<void>
19
+ }
20
+
21
+ /**
22
+ * Segment plugin function type
23
+ */
24
+ export type SegmentFunction = (ctx: SegmentContext) => Promise<SegmentContext> | SegmentContext
25
+
26
+ /**
27
+ * Segment plugin interface
28
+ * Loosely based on https://github.com/segmentio/analytics-next/blob/master/packages/core/src/plugins/index.ts
29
+ */
30
+ export interface SegmentPlugin {
31
+ name: string
32
+ version: string
33
+ type: 'enrichment'
34
+ isLoaded: () => boolean
35
+ load: (ctx: SegmentContext, instance: any, config?: any) => Promise<unknown>
36
+ unload?: (ctx: SegmentContext, instance: any) => Promise<unknown> | unknown
37
+ ready?: () => Promise<unknown>
38
+ track?: SegmentFunction
39
+ identify?: SegmentFunction
40
+ page?: SegmentFunction
41
+ group?: SegmentFunction
42
+ alias?: SegmentFunction
43
+ screen?: SegmentFunction
44
+ }
45
+
46
+ /**
47
+ * Segment context object
48
+ */
49
+ export interface SegmentContext {
50
+ event: {
51
+ event: string
52
+ userId?: string
53
+ anonymousId?: string
54
+ properties: any
55
+ }
56
+ }
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Session recording types
3
+ */
4
+
5
+ import type { Headers } from './request'
6
+
7
+ export type SessionRecordingCanvasOptions = {
8
+ /**
9
+ * If set, records the canvas
10
+ *
11
+ * @default false
12
+ */
13
+ recordCanvas?: boolean | null
14
+
15
+ /**
16
+ * If set, records the canvas at the given FPS
17
+ * Can be set in the remote configuration
18
+ * Limited between 0 and 12
19
+ * When canvas recording is enabled, if this is not set locally, then remote config sets this as 4
20
+ *
21
+ * @default null-ish
22
+ */
23
+ canvasFps?: number | null
24
+
25
+ /**
26
+ * If set, records the canvas at the given quality
27
+ * Can be set in the remote configuration
28
+ * Must be a string that is a valid decimal between 0 and 1
29
+ * When canvas recording is enabled, if this is not set locally, then remote config sets this as "0.4"
30
+ *
31
+ * @default null-ish
32
+ */
33
+ canvasQuality?: string | null
34
+ }
35
+
36
+ /* for rrweb/network@1
37
+ ** when that is released as part of rrweb this can be removed
38
+ ** don't rely on this type, it may change without notice
39
+ */
40
+ export type InitiatorType =
41
+ | 'audio'
42
+ | 'beacon'
43
+ | 'body'
44
+ | 'css'
45
+ | 'early-hint'
46
+ | 'embed'
47
+ | 'fetch'
48
+ | 'frame'
49
+ | 'iframe'
50
+ | 'icon'
51
+ | 'image'
52
+ | 'img'
53
+ | 'input'
54
+ | 'link'
55
+ | 'navigation'
56
+ | 'object'
57
+ | 'ping'
58
+ | 'script'
59
+ | 'track'
60
+ | 'video'
61
+ | 'xmlhttprequest'
62
+
63
+ /** @deprecated - use CapturedNetworkRequest instead */
64
+ export type NetworkRequest = {
65
+ url: string
66
+ }
67
+
68
+ // we mirror PerformanceEntry since we read into this type from a PerformanceObserver,
69
+ // but we don't want to inherit its readonly-iness
70
+ type Writable<T> = { -readonly [P in keyof T]: T[P] }
71
+
72
+ // In rrweb this is called NetworkRequest, but we already exposed that as having only URL
73
+ // we also want to vary from the rrweb NetworkRequest because we want to include
74
+ // all PerformanceEntry properties too.
75
+ // that has 4 required properties
76
+ // readonly duration: DOMHighResTimeStamp;
77
+ // readonly entryType: string;
78
+ // readonly name: string;
79
+ // readonly startTime: DOMHighResTimeStamp;
80
+ // NB: properties below here are ALPHA, don't rely on them, they may change without notice
81
+ export type CapturedNetworkRequest = Writable<Omit<PerformanceEntry, 'toJSON'>> & {
82
+ // properties below here are ALPHA, don't rely on them, they may change without notice
83
+ method?: string
84
+ initiatorType?: InitiatorType
85
+ status?: number
86
+ timeOrigin?: number
87
+ timestamp?: number
88
+ startTime?: number
89
+ endTime?: number
90
+ requestHeaders?: Headers
91
+ requestBody?: string | null
92
+ responseHeaders?: Headers
93
+ responseBody?: string | null
94
+ // was this captured before fetch/xhr could have been wrapped
95
+ isInitial?: boolean
96
+ }
97
+
98
+ export type SessionIdChangedCallback = (
99
+ sessionId: string,
100
+ windowId: string | null | undefined,
101
+ changeReason?: { noSessionId: boolean; activityTimeout: boolean; sessionPastMaximumLength: boolean }
102
+ ) => void
103
+
104
+ // levels originally copied from Sentry to work with the sentry integration
105
+ // and to avoid relying on a frequently changing @sentry/types dependency
106
+ export type SeverityLevel = 'fatal' | 'error' | 'warning' | 'log' | 'info' | 'debug'