posthog-node 5.73.1 → 6.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.
Files changed (156) hide show
  1. package/MIGRATION.md +556 -0
  2. package/README.md +3 -1
  3. package/dist/ai-capture/routing.d.ts +0 -3
  4. package/dist/ai-capture/routing.d.ts.map +1 -1
  5. package/dist/ai-capture/routing.js +2 -14
  6. package/dist/ai-capture/routing.mjs +1 -4
  7. package/dist/capture-lane/lane.d.ts +109 -0
  8. package/dist/capture-lane/lane.d.ts.map +1 -0
  9. package/dist/capture-lane/lane.js +308 -0
  10. package/dist/capture-lane/lane.mjs +270 -0
  11. package/dist/capture-lane/limits.d.ts +7 -0
  12. package/dist/capture-lane/limits.d.ts.map +1 -0
  13. package/dist/{capture-v1/config.js → capture-lane/limits.js} +13 -11
  14. package/dist/capture-lane/limits.mjs +4 -0
  15. package/dist/capture-lane/settings.d.ts +30 -0
  16. package/dist/capture-lane/settings.d.ts.map +1 -0
  17. package/dist/capture-lane/settings.js +100 -0
  18. package/dist/capture-lane/settings.mjs +47 -0
  19. package/dist/capture-v1/compression.d.ts +18 -0
  20. package/dist/capture-v1/compression.d.ts.map +1 -0
  21. package/dist/capture-v1/compression.js +70 -0
  22. package/dist/capture-v1/compression.mjs +26 -0
  23. package/dist/capture-v1/errors.d.ts +23 -7
  24. package/dist/capture-v1/errors.d.ts.map +1 -1
  25. package/dist/capture-v1/errors.js +37 -8
  26. package/dist/capture-v1/errors.mjs +29 -6
  27. package/dist/capture-v1/sender.d.ts +63 -23
  28. package/dist/capture-v1/sender.d.ts.map +1 -1
  29. package/dist/capture-v1/sender.js +281 -74
  30. package/dist/capture-v1/sender.mjs +275 -77
  31. package/dist/capture-v1/serialize.d.ts +28 -0
  32. package/dist/capture-v1/serialize.d.ts.map +1 -0
  33. package/dist/capture-v1/serialize.js +82 -0
  34. package/dist/capture-v1/serialize.mjs +38 -0
  35. package/dist/capture-v1/transform.d.ts +10 -25
  36. package/dist/capture-v1/transform.d.ts.map +1 -1
  37. package/dist/capture-v1/transform.js +33 -64
  38. package/dist/capture-v1/transform.mjs +28 -53
  39. package/dist/capture-v1/types.d.ts +16 -14
  40. package/dist/capture-v1/types.d.ts.map +1 -1
  41. package/dist/capture-v1/types.js +27 -1
  42. package/dist/capture-v1/types.mjs +3 -1
  43. package/dist/capture-v1/uuid.d.ts +9 -0
  44. package/dist/capture-v1/uuid.d.ts.map +1 -0
  45. package/dist/capture-v1/uuid.js +75 -0
  46. package/dist/capture-v1/uuid.mjs +34 -0
  47. package/dist/client.d.ts +201 -94
  48. package/dist/client.d.ts.map +1 -1
  49. package/dist/client.js +485 -246
  50. package/dist/client.mjs +485 -246
  51. package/dist/compression.node.d.ts +13 -0
  52. package/dist/compression.node.d.ts.map +1 -0
  53. package/dist/{capture-v1/routing.js → compression.node.js} +23 -16
  54. package/dist/compression.node.mjs +18 -0
  55. package/dist/entrypoints/index.node.d.ts +2 -0
  56. package/dist/entrypoints/index.node.d.ts.map +1 -1
  57. package/dist/entrypoints/index.node.js +11 -4
  58. package/dist/entrypoints/index.node.mjs +4 -0
  59. package/dist/event-defaults.d.ts +46 -0
  60. package/dist/event-defaults.d.ts.map +1 -0
  61. package/dist/event-defaults.js +141 -0
  62. package/dist/event-defaults.mjs +79 -0
  63. package/dist/exports.d.ts +3 -0
  64. package/dist/exports.d.ts.map +1 -1
  65. package/dist/exports.js +22 -11
  66. package/dist/exports.mjs +1 -0
  67. package/dist/extensions/context/context.d.ts +0 -1
  68. package/dist/extensions/context/context.d.ts.map +1 -1
  69. package/dist/extensions/context/context.js +4 -3
  70. package/dist/extensions/context/context.mjs +4 -3
  71. package/dist/extensions/context/event-context.d.ts.map +1 -1
  72. package/dist/extensions/context/event-context.js +3 -0
  73. package/dist/extensions/context/event-context.mjs +3 -0
  74. package/dist/extensions/context/types.d.ts +9 -1
  75. package/dist/extensions/context/types.d.ts.map +1 -1
  76. package/dist/extensions/error-tracking/index.d.ts +1 -1
  77. package/dist/extensions/error-tracking/index.d.ts.map +1 -1
  78. package/dist/extensions/error-tracking/index.js +71 -7
  79. package/dist/extensions/error-tracking/index.mjs +75 -11
  80. package/dist/extensions/express.d.ts +8 -0
  81. package/dist/extensions/express.d.ts.map +1 -1
  82. package/dist/extensions/express.js +3 -3
  83. package/dist/extensions/express.mjs +4 -4
  84. package/dist/extensions/fetch-request.d.ts.map +1 -1
  85. package/dist/extensions/fetch-request.js +2 -2
  86. package/dist/extensions/fetch-request.mjs +3 -3
  87. package/dist/extensions/nestjs.d.ts.map +1 -1
  88. package/dist/extensions/nestjs.js +7 -4
  89. package/dist/extensions/nestjs.mjs +8 -5
  90. package/dist/extensions/tracing-headers.d.ts +7 -0
  91. package/dist/extensions/tracing-headers.d.ts.map +1 -1
  92. package/dist/extensions/tracing-headers.js +11 -1
  93. package/dist/extensions/tracing-headers.mjs +9 -2
  94. package/dist/feature-flag-evaluations.d.ts +5 -5
  95. package/dist/feature-flag-evaluations.d.ts.map +1 -1
  96. package/dist/feature-flag-evaluations.js +16 -9
  97. package/dist/feature-flag-evaluations.mjs +16 -9
  98. package/dist/flag-events.d.ts +1 -1
  99. package/dist/flag-events.d.ts.map +1 -1
  100. package/dist/flag-events.js +2 -2
  101. package/dist/flag-events.mjs +2 -2
  102. package/dist/flags-response.d.ts +2 -0
  103. package/dist/flags-response.d.ts.map +1 -1
  104. package/dist/flags-response.js +6 -0
  105. package/dist/flags-response.mjs +4 -1
  106. package/dist/types.d.ts +192 -27
  107. package/dist/types.d.ts.map +1 -1
  108. package/dist/types.js +1 -0
  109. package/dist/types.mjs +1 -0
  110. package/dist/version.d.ts +1 -1
  111. package/dist/version.d.ts.map +1 -1
  112. package/dist/version.js +1 -1
  113. package/dist/version.mjs +1 -1
  114. package/package.json +5 -4
  115. package/src/ai-capture/routing.ts +0 -6
  116. package/src/capture-lane/lane.ts +414 -0
  117. package/src/capture-lane/limits.ts +8 -0
  118. package/src/capture-lane/settings.ts +119 -0
  119. package/src/capture-v1/compression.ts +50 -0
  120. package/src/capture-v1/errors.ts +50 -11
  121. package/src/capture-v1/sender.ts +405 -111
  122. package/src/capture-v1/serialize.ts +77 -0
  123. package/src/capture-v1/transform.ts +59 -92
  124. package/src/capture-v1/types.ts +19 -15
  125. package/src/capture-v1/uuid.ts +53 -0
  126. package/src/client.ts +885 -446
  127. package/src/compression.node.ts +30 -0
  128. package/src/entrypoints/index.node.ts +6 -0
  129. package/src/event-defaults.ts +177 -0
  130. package/src/exports.ts +3 -0
  131. package/src/extensions/context/context.ts +4 -4
  132. package/src/extensions/context/event-context.ts +3 -1
  133. package/src/extensions/context/types.ts +10 -1
  134. package/src/extensions/error-tracking/index.ts +51 -7
  135. package/src/extensions/express.ts +12 -3
  136. package/src/extensions/fetch-request.ts +7 -3
  137. package/src/extensions/nestjs.ts +9 -5
  138. package/src/extensions/tracing-headers.ts +26 -7
  139. package/src/feature-flag-evaluations.ts +31 -23
  140. package/src/flag-events.ts +5 -2
  141. package/src/flags-response.ts +6 -1
  142. package/src/types.ts +212 -44
  143. package/src/version.ts +1 -1
  144. package/dist/ai-capture/batching.d.ts +0 -11
  145. package/dist/ai-capture/batching.d.ts.map +0 -1
  146. package/dist/ai-capture/batching.js +0 -77
  147. package/dist/ai-capture/batching.mjs +0 -36
  148. package/dist/capture-v1/config.d.ts +0 -11
  149. package/dist/capture-v1/config.d.ts.map +0 -1
  150. package/dist/capture-v1/config.mjs +0 -8
  151. package/dist/capture-v1/routing.d.ts +0 -21
  152. package/dist/capture-v1/routing.d.ts.map +0 -1
  153. package/dist/capture-v1/routing.mjs +0 -7
  154. package/src/ai-capture/batching.ts +0 -47
  155. package/src/capture-v1/config.ts +0 -18
  156. package/src/capture-v1/routing.ts +0 -28
@@ -1,6 +1,6 @@
1
1
  import { FeatureFlagValue, JsonType, UnresolvedFlagReason } from '@posthog/core'
2
2
 
3
- import type { ParsedFlagRecord } from './flags-response'
3
+ import { isServerFailedFlagRecord, type ParsedFlagRecord } from './flags-response'
4
4
  import { FeatureFlagError } from './types'
5
5
 
6
6
  /**
@@ -41,10 +41,9 @@ export type FlagCalledEventParams = {
41
41
  disableGeoip: boolean | undefined
42
42
  properties: Record<string, any>
43
43
  /**
44
- * The parsed `/flags` record that supplied the read's value. Absent when the value came from
45
- * local evaluation or an override, or the flag is missing. A config version 2 record adds its
46
- * attribution to `$feature_flag_called`, and a complete experiment split also sends
47
- * `$experiment_exposure`.
44
+ * The parsed `/flags` record supplying the read's value or failure diagnostics. Absent for
45
+ * local evaluation, overrides and missing flags. A failed record keeps config/reason
46
+ * attribution, but only a successful experiment split sends `$experiment_exposure`.
48
47
  */
49
48
  record?: ParsedFlagRecord
50
49
  }
@@ -92,6 +91,7 @@ export class FeatureFlagEvaluations {
92
91
  private readonly _groups: Record<string, string | number> | undefined
93
92
  private readonly _disableGeoip: boolean | undefined
94
93
  private readonly _flags: Record<string, EvaluatedFlagRecord>
94
+ private readonly _failedFlags: Record<string, EvaluatedFlagRecord>
95
95
  private readonly _unresolvedFlags: Readonly<Record<string, UnresolvedFlagReason>>
96
96
  private readonly _requestId: string | undefined
97
97
  private readonly _evaluatedAt: number | undefined
@@ -125,7 +125,15 @@ export class FeatureFlagEvaluations {
125
125
  this._distinctId = init.distinctId
126
126
  this._groups = init.groups
127
127
  this._disableGeoip = init.disableGeoip
128
- this._flags = init.flags
128
+ this._flags = Object.create(null)
129
+ this._failedFlags = Object.create(null)
130
+ for (const [key, flag] of Object.entries(init.flags)) {
131
+ if (flag.parsed && isServerFailedFlagRecord(flag.parsed)) {
132
+ this._failedFlags[key] = flag
133
+ } else {
134
+ this._flags[key] = flag
135
+ }
136
+ }
129
137
  this._unresolvedFlags = Object.freeze({ ...init.unresolvedFlags })
130
138
  this._requestId = init.requestId
131
139
  this._evaluatedAt = init.evaluatedAt
@@ -212,9 +220,9 @@ export class FeatureFlagEvaluations {
212
220
  * excluded from the slice.
213
221
  */
214
222
  onlyAccessed(): FeatureFlagEvaluations {
215
- const filtered: Record<string, EvaluatedFlagRecord> = {}
223
+ const filtered: Record<string, EvaluatedFlagRecord> = Object.create(null)
216
224
  for (const key of this._accessed) {
217
- const flag = this._flags[key]
225
+ const flag = this._flags[key] ?? this._failedFlags[key]
218
226
  if (flag) {
219
227
  filtered[key] = flag
220
228
  }
@@ -230,10 +238,10 @@ export class FeatureFlagEvaluations {
230
238
  * Branching on a filtered key that was excluded from the slice is a no-op.
231
239
  */
232
240
  only(keys: string[]): FeatureFlagEvaluations {
233
- const filtered: Record<string, EvaluatedFlagRecord> = {}
241
+ const filtered: Record<string, EvaluatedFlagRecord> = Object.create(null)
234
242
  const missing: string[] = []
235
243
  for (const key of keys) {
236
- const flag = this._flags[key]
244
+ const flag = this._flags[key] ?? this._failedFlags[key]
237
245
  if (flag) {
238
246
  filtered[key] = flag
239
247
  } else {
@@ -332,13 +340,14 @@ export class FeatureFlagEvaluations {
332
340
  // the slice doesn't mean the flag is missing from PostHog — it was filtered out.
333
341
  // Don't fire a misleading `flag_missing` event; slices are intended for `capture()`,
334
342
  // not for further branching.
335
- if (this._isSlice && !(key in this._flags)) {
343
+ if (this._isSlice && !(key in this._flags) && !(key in this._failedFlags)) {
336
344
  return
337
345
  }
338
346
 
339
- const flag = this._flags[key]
347
+ const failed = key in this._failedFlags
348
+ const flag = this._flags[key] ?? this._failedFlags[key]
340
349
  const response: FeatureFlagValue | undefined =
341
- flag === undefined ? undefined : flag.enabled === false ? false : (flag.variant ?? true)
350
+ flag === undefined || failed ? undefined : flag.enabled === false ? false : (flag.variant ?? true)
342
351
 
343
352
  const properties: Record<string, any> = {
344
353
  $feature_flag: key,
@@ -362,8 +371,7 @@ export class FeatureFlagEvaluations {
362
371
 
363
372
  // Build the comma-joined `$feature_flag_error` matching the single-flag path's
364
373
  // granularity: response-level errors (errors-while-computing, quota-limited) are
365
- // combined with per-flag errors (flag-missing or local-evaluation-inconclusive) so consumers
366
- // can filter by type.
374
+ // combined with per-flag errors. A failed fallback does not resolve a local inconclusive result.
367
375
  const errors: string[] = []
368
376
  if (this._errorsWhileComputing) {
369
377
  errors.push(FeatureFlagError.ERRORS_WHILE_COMPUTING)
@@ -371,12 +379,13 @@ export class FeatureFlagEvaluations {
371
379
  if (this._quotaLimited) {
372
380
  errors.push(FeatureFlagError.QUOTA_LIMITED)
373
381
  }
374
- if (flag === undefined) {
375
- errors.push(
376
- Object.prototype.hasOwnProperty.call(this._unresolvedFlags, key)
377
- ? FeatureFlagError.LOCAL_EVALUATION_INCONCLUSIVE
378
- : FeatureFlagError.FLAG_MISSING
379
- )
382
+ if (failed) {
383
+ errors.push(FeatureFlagError.EVALUATION_FAILED)
384
+ }
385
+ if (Object.prototype.hasOwnProperty.call(this._unresolvedFlags, key)) {
386
+ errors.push(FeatureFlagError.LOCAL_EVALUATION_INCONCLUSIVE)
387
+ } else if (flag === undefined) {
388
+ errors.push(FeatureFlagError.FLAG_MISSING)
380
389
  }
381
390
  if (errors.length > 0) {
382
391
  properties.$feature_flag_error = errors.join(',')
@@ -389,8 +398,7 @@ export class FeatureFlagEvaluations {
389
398
  groups: this._groups,
390
399
  disableGeoip: this._disableGeoip,
391
400
  properties,
392
- // The record that supplied the value. A remotely evaluated experiment split also sends
393
- // $experiment_exposure.
401
+ // Failed records retain config/reason diagnostics, but never send an experiment exposure.
394
402
  record: flag?.parsed,
395
403
  })
396
404
  }
@@ -154,8 +154,11 @@ export function applyFlagEventCapture(properties: Record<string, any>, capture:
154
154
  *
155
155
  * @internal
156
156
  */
157
- export function minimizeConfigVersion2FlagCalledProperties(properties: Record<string, any>): Record<string, any> {
158
- const minimal = minimizeFlagCalledEventProperties(properties)
157
+ export function minimizeConfigVersion2FlagCalledProperties(
158
+ properties: Record<string, any>,
159
+ transportKeys: readonly string[] = []
160
+ ): Record<string, any> {
161
+ const minimal = minimizeFlagCalledEventProperties(properties, transportKeys)
159
162
  for (const name of CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES) {
160
163
  if (properties[name] !== undefined) {
161
164
  minimal[name] = properties[name]
@@ -152,6 +152,11 @@ export function parsedFlagRecordOf(detail: FeatureFlagDetail): ParsedFlagRecord
152
152
  }
153
153
  }
154
154
 
155
+ /** Whether a readable record reports a server evaluation failure, rather than a parser failure. */
156
+ export function isServerFailedFlagRecord(record: ParsedFlagRecord): boolean {
157
+ return record.failed && record.unreadableField === undefined
158
+ }
159
+
155
160
  /** Whether a flag's record had a known field with the wrong JSON type, so it could not be read. */
156
161
  export function isUnreadableFlagRecord(detail: FeatureFlagDetail): boolean {
157
162
  return parsedRecords.get(detail)?.unreadableField !== undefined
@@ -221,7 +226,7 @@ function readTypedRecord(key: string, raw: unknown): ReadRecord {
221
226
  description: metadata.description,
222
227
  // A number or object value is its own payload; a payload the record also carries
223
228
  // is ignored. Other payloads are kept as sent, like a v2 record's. A failed record
224
- // reads as disabled, so its payload is null, as a failed v2 record's is.
229
+ // supplies no value or payload to getters.
225
230
  payload: (failed ? null : (payload ?? rawMetadata?.payload)) as string | undefined,
226
231
  has_experiment: metadata.hasExperiment,
227
232
  }
package/src/types.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type {
2
+ ErrorTracking,
2
3
  PostHogCoreOptions,
3
4
  ExceptionRateLimiterConfig,
4
5
  FeatureFlagValue,
@@ -8,6 +9,7 @@ import type {
8
9
  PostHogLogsConfig,
9
10
  PostHogFetchOptions,
10
11
  PostHogFetchResponse,
12
+ PostHogEventProperties,
11
13
  PostHogFlagsAndPayloadsResponse,
12
14
  Properties,
13
15
  Span,
@@ -15,6 +17,10 @@ import type {
15
17
  TracesConfig,
16
18
  } from '@posthog/core'
17
19
  import { ContextData, ContextOptions } from './extensions/context/types'
20
+ import type { V1EventOptions } from './capture-v1/types'
21
+
22
+ /** Processing options sent with an event to capture v1. */
23
+ export type EventOptions = V1EventOptions
18
24
 
19
25
  import type { Emails } from './emails/types'
20
26
  import type { FeatureFlagEvaluations } from './feature-flag-evaluations'
@@ -24,17 +30,29 @@ export type IdentifyMessage = {
24
30
  distinctId: string
25
31
  properties?: Record<string | number, any>
26
32
  disableGeoip?: boolean
33
+ /**
34
+ * Processing options sent with this event to capture v1.
35
+ */
36
+ options?: EventOptions
27
37
  }
28
38
 
29
39
  export type SetPersonPropertiesMessage = {
30
40
  distinctId: string
31
41
  properties?: Record<string | number, any>
32
42
  propertiesOnce?: Record<string | number, any>
43
+ /**
44
+ * Processing options sent with this event to capture v1.
45
+ */
46
+ options?: EventOptions
33
47
  }
34
48
 
35
49
  export type UnsetPersonPropertiesMessage = {
36
50
  distinctId: string
37
51
  properties: string | string[]
52
+ /**
53
+ * Processing options sent with this event to capture v1.
54
+ */
55
+ options?: EventOptions
38
56
  }
39
57
 
40
58
  export type SendFeatureFlagsOptions = {
@@ -45,7 +63,11 @@ export type SendFeatureFlagsOptions = {
45
63
  }
46
64
 
47
65
  export type EventMessage = Omit<IdentifyMessage, 'distinctId'> & {
48
- distinctId?: string // Optional - can be provided via context
66
+ /**
67
+ * The event's distinct id. When it is missing, empty or only whitespace, the context distinct id
68
+ * is used; without one, the event gets a generated id and `options.process_person_profile: false`.
69
+ */
70
+ distinctId?: string
49
71
  event: string
50
72
  groups?: Record<string, string | number> // Mapping of group type to group id
51
73
  /**
@@ -72,12 +94,58 @@ export type EventMessage = Omit<IdentifyMessage, 'distinctId'> & {
72
94
  _originatedFromCaptureException?: boolean
73
95
  }
74
96
 
97
+ /** Options for `captureException()` and `captureExceptionImmediate()`. */
98
+ export type CaptureExceptionOptions = {
99
+ /** The event's distinct id. Without one, the context distinct id is used, else a personless id. */
100
+ distinctId?: string
101
+ /**
102
+ * Properties to add to the event. Exception properties the SDK or PostHog owns, such as
103
+ * `$exception_list` and `$exception_level`, are ignored: use `level` to set the level.
104
+ */
105
+ properties?: Record<string | number, any>
106
+ /** Per-event options, as in `capture()`. */
107
+ options?: EventOptions
108
+ /** Overrides the generated event UUID. Must be a valid UUID. */
109
+ uuid?: string
110
+ /** A `FeatureFlagEvaluations` snapshot to attach the same flag context as your other events. */
111
+ flags?: FeatureFlagEvaluations
112
+ /**
113
+ * The exception's severity, sent as `$exception_level`. An unknown value is ignored.
114
+ *
115
+ * @default 'error'
116
+ */
117
+ level?: ErrorTracking.SeverityLevel
118
+ }
119
+
120
+ /**
121
+ * An event as `prepareEventMessage()` builds it: the shape of `EventMessage`, with the distinct id
122
+ * resolved and the properties and options filled from every layer, after `beforeSend`.
123
+ */
124
+ export type PreparedEventMessage = {
125
+ distinctId: string
126
+ event: string
127
+ properties: PostHogEventProperties
128
+ /** The event's options, sent to capture unchanged. */
129
+ options?: EventOptions
130
+ uuid?: string
131
+ timestamp?: Date
132
+ disableGeoip?: boolean
133
+ }
134
+
75
135
  export type GroupIdentifyMessage = {
76
136
  groupType: string
77
137
  groupKey: string // Unique identifier for the group
78
138
  properties?: Record<string | number, any>
79
- distinctId?: string // optional distinctId to associate message with a person
139
+ /**
140
+ * The event's distinct id. When it is missing, empty or only whitespace, the context distinct id
141
+ * is used, then `$<groupType>_<groupKey>`.
142
+ */
143
+ distinctId?: string
80
144
  disableGeoip?: boolean
145
+ /**
146
+ * Processing options sent with this event to capture v1.
147
+ */
148
+ options?: EventOptions
81
149
  }
82
150
 
83
151
  export type PropertyGroup = {
@@ -168,8 +236,34 @@ export type FeatureFlagEvaluationRuntime = 'all' | 'client' | 'server'
168
236
 
169
237
  export type BeforeSendFn = (event: EventMessage) => EventMessage | null
170
238
 
171
- export type PostHogOptions = Omit<PostHogCoreOptions, 'before_send' | 'flushInterval' | 'maxQueueSize'> & {
239
+ export type PostHogOptions = Omit<
240
+ PostHogCoreOptions,
241
+ | 'before_send'
242
+ | 'disable_capture_url_hashes'
243
+ | 'disableGeoip'
244
+ | 'flushInterval'
245
+ | 'maxQueueSize'
246
+ | 'fetchRetryCount'
247
+ | 'fetchRetryDelay'
248
+ | 'disableCompression'
249
+ | 'requestTimeout'
250
+ > & {
172
251
  persistence?: 'memory'
252
+ /**
253
+ * Whether to strip URL fragments (`#...`) from automatically captured request URLs.
254
+ *
255
+ * @default false
256
+ */
257
+ disableCaptureUrlHashes?: boolean
258
+ /**
259
+ * Whether to disable GeoIP lookups for events and feature flag requests. When on, every event
260
+ * gets `$geoip_disable: true` unless the event, its context or `register()` sets
261
+ * `$geoip_disable`. A per-call `disableGeoip` argument sets it for that event, under the event's
262
+ * own `$geoip_disable` property.
263
+ *
264
+ * @default true
265
+ */
266
+ disableGeoip?: boolean
173
267
  /**
174
268
  * The interval in milliseconds between periodic flushes
175
269
  *
@@ -177,13 +271,88 @@ export type PostHogOptions = Omit<PostHogCoreOptions, 'before_send' | 'flushInte
177
271
  */
178
272
  flushInterval?: number
179
273
  /**
180
- * The maximum number of cached messages either in memory or on the local storage (must be higher than `flushAt`)
274
+ * The maximum number of analytics events held in memory waiting to be sent (must be higher than `flushAt`).
275
+ * When the queue is full, the oldest event is dropped and reported through the `'error'` event.
276
+ * `captureAi` events have their own queue, sized by `captureAiMaxQueueSize`, and do not count toward this limit.
181
277
  *
182
278
  * @default 10000
183
279
  */
184
280
  maxQueueSize?: number
185
281
  /**
186
- * Lets the Express and NestJS integrations read the session ID, and the distinct ID of an identified user, from the
282
+ * Whether the Express, NestJS, Hono, and `instrumentRequest` integrations read the `X-POSTHOG-DISTINCT-ID` and
283
+ * `X-POSTHOG-SESSION-ID` request headers into the request context. A distinct id or session id
284
+ * you pass explicitly always wins over these headers, and they are never used for authentication.
285
+ * Turning it off does not turn off `readPostHogCookie`. To stop all request linking, set
286
+ * `captureTracingHeaders: false` and leave `readPostHogCookie` off (its default).
287
+ *
288
+ * @default true
289
+ */
290
+ captureTracingHeaders?: boolean
291
+ /**
292
+ * How many times a capture request is retried after its first attempt, on both capture lanes.
293
+ * Waits start at 100 ms and double each retry, with jitter, up to 30 seconds; a `Retry-After`
294
+ * header can raise a wait. Only HTTP 404, 408, 429, 500, 502, 503 and 504 and network errors are retried.
295
+ * When not set, `fetchRetryCount` is used.
296
+ *
297
+ * @default 3
298
+ */
299
+ captureMaxRetries?: number
300
+ /**
301
+ * How many times HTTP requests are retried. Capture requests use `captureMaxRetries` when it is set.
302
+ *
303
+ * @default 3
304
+ */
305
+ fetchRetryCount?: number
306
+ /**
307
+ * The delay between HTTP request retries in milliseconds. Capture requests do not use it:
308
+ * see `captureMaxRetries` for their retry waits.
309
+ *
310
+ * @default 3000
311
+ */
312
+ fetchRetryDelay?: number
313
+ /**
314
+ * How analytics capture requests compress their body. `'deflate'` is zlib-wrapped. `'zstd'` needs
315
+ * Node 22.15 or later and falls back to `'gzip'` elsewhere, including the edge build. When not set,
316
+ * `disableCompression: true` means `'none'`.
317
+ *
318
+ * @default 'gzip'
319
+ */
320
+ captureCompression?: 'none' | 'gzip' | 'deflate' | 'zstd'
321
+ /**
322
+ * How `captureAi` requests compress their body, with the same values as `captureCompression`.
323
+ * It does not read `captureCompression` or `disableCompression`.
324
+ *
325
+ * @default 'gzip'
326
+ */
327
+ captureAiCompression?: 'none' | 'gzip' | 'deflate' | 'zstd'
328
+ /**
329
+ * Timeout in milliseconds for any calls. `captureAi` requests use `captureAiRequestTimeoutMs`.
330
+ *
331
+ * @default 10000
332
+ */
333
+ requestTimeout?: number
334
+ /**
335
+ * The maximum number of `captureAi` events held in memory waiting to be sent. When the queue is
336
+ * full, the oldest AI event is dropped and reported through the `'error'` event.
337
+ *
338
+ * @default 1000
339
+ */
340
+ captureAiMaxQueueSize?: number
341
+ /**
342
+ * Timeout in milliseconds for each `captureAi` request. Analytics capture requests use `requestTimeout`.
343
+ *
344
+ * @default 30000
345
+ */
346
+ captureAiRequestTimeoutMs?: number
347
+ /**
348
+ * Whether to disable GZIP compression. Analytics capture requests use `captureCompression` when it
349
+ * is set, and `captureAi` requests use `captureAiCompression`.
350
+ *
351
+ * @default false
352
+ */
353
+ disableCompression?: boolean
354
+ /**
355
+ * Lets the Express, NestJS, Hono, and `instrumentRequest` integrations read the session ID, and the distinct ID of an identified user, from the
187
356
  * posthog-js cookie when a request has neither tracing header.
188
357
  *
189
358
  * Turn it on only when posthog-js stores opt-out consent in a cookie, or when you do not use opt-out: the server
@@ -195,7 +364,8 @@ export type PostHogOptions = Omit<PostHogCoreOptions, 'before_send' | 'flushInte
195
364
  * `optOutCapturingByDefault` when posthog-js uses `opt_out_capturing_by_default`, so a visitor with no consent cookie
196
365
  * counts as opted out. A request sent right
197
366
  * after `identify()` or `reset()` can still carry the previous identity, because posthog-js saves the cookie with a
198
- * short delay.
367
+ * short delay. When `captureTracingHeaders` is `false`, the tracing headers are ignored and every request falls
368
+ * back to the cookie. To stop all request linking, also leave this option off.
199
369
  *
200
370
  * @default false
201
371
  */
@@ -301,12 +471,15 @@ export type PostHogOptions = Omit<PostHogCoreOptions, 'before_send' | 'flushInte
301
471
  enableExceptionAutocapture?: boolean
302
472
  /**
303
473
  * The interval in milliseconds between polls for refreshing feature flag definitions. Defaults to 30 seconds.
474
+ * The minimum is 100 ms, and a lower value is raised to it.
304
475
  * Set to null to disable automatic polling. Definitions are still loaded on initialization;
305
476
  * call reloadFeatureFlags() to refresh them manually. Until refreshed, local evaluation uses the last loaded definitions.
306
477
  */
307
478
  featureFlagsPollingInterval?: number | null
308
- // Maximum number of distinct ids held in the cache that deduplicates $feature_flag_called calls
309
- // per user. When the cache is full the least recently used distinct ids are evicted.
479
+ /**
480
+ * Maximum number of distinct ids held in the cache that deduplicates $feature_flag_called calls
481
+ * per user. When the cache is full the least recently used distinct ids are evicted. Defaults to 50000.
482
+ */
310
483
  maxCacheSize?: number
311
484
  fetch?: (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>
312
485
  // Whether to enable local evaluation and definition polling. Defaults to true when a secretKey, personalApiKey, or flagDefinitionCacheProvider is provided.
@@ -344,8 +517,21 @@ export type PostHogOptions = Omit<PostHogCoreOptions, 'before_send' | 'flushInte
344
517
  * Allows modification or dropping of events before they're sent to PostHog.
345
518
  * If an array is provided, the functions are run in order.
346
519
  * If a function returns null, the event will be dropped.
520
+ *
521
+ * @remarks
522
+ * The hook gets a copy of the finished event: the event's own values, then the values filled
523
+ * from context, `register()` and `registerOptions()`, then the values the SDK adds (such as
524
+ * `$is_server`, `$geoip_disable`, feature flag properties and `options.process_person_profile`).
525
+ * It has the final say: it can change or remove any of them, and nothing is added after it.
526
+ * Set `$geoip_disable` in `properties` to change GeoIP; the `disableGeoip` and `groups` fields
527
+ * are already applied to the properties it sees.
528
+ *
529
+ * On `$identify`, `$create_alias`, `$merge_dangerously`, `$groupidentify`, `$set` and `$set_once`
530
+ * events it sees `options.process_person_profile` and no `$process_person_profile` property.
531
+ * Those events get their default from the event name the call used, so a hook that renames an
532
+ * event to one of them must set `options.process_person_profile` itself.
347
533
  */
348
- before_send?: BeforeSendFn | BeforeSendFn[]
534
+ beforeSend?: BeforeSendFn | BeforeSendFn[]
349
535
  /**
350
536
  * Evaluation contexts for feature flags.
351
537
  * When set, only feature flags that have at least one matching evaluation tag
@@ -368,7 +554,7 @@ export type PostHogOptions = Omit<PostHogCoreOptions, 'before_send' | 'flushInte
368
554
  *
369
555
  * @default []
370
556
  */
371
- custom_blocked_useragents?: string[]
557
+ customBlockedUserAgents?: string[]
372
558
  /**
373
559
  * PREVIEW - MAY CHANGE WITHOUT WARNING - DO NOT USE IN PRODUCTION
374
560
  * Enables collection of bot traffic as $bot_pageview events instead of dropping them.
@@ -386,7 +572,7 @@ export type PostHogOptions = Omit<PostHogCoreOptions, 'before_send' | 'flushInte
386
572
  * })
387
573
  * ```
388
574
  */
389
- __preview_capture_bot_pageviews?: boolean
575
+ __previewCaptureBotPageviews?: boolean
390
576
  /**
391
577
  * When enabled, all feature flag evaluations will use local evaluation only,
392
578
  * never falling back to server-side evaluation. This prevents unexpected server
@@ -444,7 +630,8 @@ export type PostHogOptions = Omit<PostHogCoreOptions, 'before_send' | 'flushInte
444
630
  * react-native events. Set this to `false` when running the SDK as a
445
631
  * client/CLI so the event is attributed to the device OS normally instead of
446
632
  * being marked as a server event. When `false`, the `$is_server` property is
447
- * omitted entirely.
633
+ * omitted entirely. A `$is_server` value set on the event, in context or with
634
+ * `register()` wins over this setting.
448
635
  *
449
636
  * @default true
450
637
  * @example
@@ -530,6 +717,7 @@ export type PostHogFeatureFlag = {
530
717
  export const FeatureFlagError = {
531
718
  ERRORS_WHILE_COMPUTING: 'errors_while_computing_flags',
532
719
  FLAG_MISSING: 'flag_missing',
720
+ EVALUATION_FAILED: 'evaluation_failed',
533
721
  LOCAL_EVALUATION_INCONCLUSIVE: 'local_evaluation_inconclusive',
534
722
  QUOTA_LIMITED: 'quota_limited',
535
723
  UNKNOWN_ERROR: 'unknown_error',
@@ -581,32 +769,16 @@ export interface IPostHog {
581
769
  /**
582
770
  * @description Capture an exception as a $exception event.
583
771
  * @param error The error to capture.
584
- * @param distinctId Optional user distinct ID.
585
- * @param additionalProperties Optional additional properties to include.
586
- * @param uuid Optional event UUID.
587
- * @param flags Optional `FeatureFlagEvaluations` snapshot to attach to the event.
772
+ * @param options Optional distinct id, properties, options, uuid, flags snapshot and level.
588
773
  */
589
- captureException(
590
- error: unknown,
591
- distinctId?: string,
592
- additionalProperties?: Record<string | number, any>,
593
- uuid?: EventMessage['uuid'],
594
- flags?: FeatureFlagEvaluations
595
- ): void
774
+ captureException(error: unknown, options?: CaptureExceptionOptions): void
596
775
 
597
776
  /**
598
777
  * @description Capture an exception as a $exception event immediately.
599
778
  * @param error The error to capture.
600
- * @param distinctId Optional user distinct ID.
601
- * @param additionalProperties Optional additional properties to include.
602
- * @param flags Optional `FeatureFlagEvaluations` snapshot to attach to the event.
779
+ * @param options Optional distinct id, properties, options, uuid, flags snapshot and level.
603
780
  */
604
- captureExceptionImmediate(
605
- error: unknown,
606
- distinctId?: string,
607
- additionalProperties?: Record<string | number, any>,
608
- flags?: FeatureFlagEvaluations
609
- ): Promise<void>
781
+ captureExceptionImmediate(error: unknown, options?: CaptureExceptionOptions): Promise<void>
610
782
 
611
783
  /**
612
784
  * @description Capture an AI event on the dedicated AI capture endpoint.
@@ -683,7 +855,7 @@ export interface IPostHog {
683
855
  * @param distinctId the current unique id
684
856
  * @param alias the unique ID of the user before
685
857
  */
686
- alias(data: { distinctId: string; alias: string }): void
858
+ alias(data: { distinctId: string; alias: string; disableGeoip?: boolean; options?: EventOptions }): void
687
859
 
688
860
  /**
689
861
  * @description To marry up whatever a user does before they sign up or log in with what they do after you need to make an alias call.
@@ -691,7 +863,12 @@ export interface IPostHog {
691
863
  * @param distinctId the current unique id
692
864
  * @param alias the unique ID of the user before
693
865
  */
694
- aliasImmediate(data: { distinctId: string; alias: string }): Promise<void>
866
+ aliasImmediate(data: {
867
+ distinctId: string
868
+ alias: string
869
+ disableGeoip?: boolean
870
+ options?: EventOptions
871
+ }): Promise<void>
695
872
 
696
873
  /**
697
874
  * @description PostHog feature flags (https://posthog.com/docs/features/feature-flags)
@@ -938,22 +1115,13 @@ export interface IPostHog {
938
1115
 
939
1116
  /**
940
1117
  * @description Run a function with specific context that will be applied to all events captured within that context.
941
- * @param data Context data to apply (sessionId, distinctId, properties, enableExceptionAutocapture)
1118
+ * @param data Context data to apply (sessionId, distinctId, properties, options)
942
1119
  * @param fn Function to run with the context
943
1120
  * @param options Context options (fresh)
944
1121
  * @returns The return value of the function
945
1122
  */
946
1123
  withContext<T>(data: Partial<ContextData>, fn: () => T, options?: ContextOptions): T
947
1124
 
948
- /**
949
- * @description Set context without a callback wrapper. Must be called in the same
950
- * async scope that makes PostHog calls. Prefer `withContext()` when you can wrap
951
- * code in a callback.
952
- * @param data Context data to apply (distinctId, sessionId, properties)
953
- * @param options Context options (fresh)
954
- */
955
- enterContext(data: Partial<ContextData>, options?: ContextOptions): void
956
-
957
1125
  /**
958
1126
  * @description Get the current context data.
959
1127
  * @returns The current context data, or undefined if no context is set
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = "5.73.1"
1
+ export const version = "6.0.0"
@@ -1,11 +0,0 @@
1
- import { PostHogEventProperties } from '@posthog/core';
2
- export type AiBatchPartition = {
3
- batches: PostHogEventProperties[][];
4
- dropped: {
5
- event: string;
6
- bytes: number;
7
- }[];
8
- };
9
- export declare function eventByteSize(message: PostHogEventProperties): number;
10
- export declare function partitionAiBatch(messages: (PostHogEventProperties | undefined)[], maxEventBytes?: number, targetBatchBytes?: number): AiBatchPartition;
11
- //# sourceMappingURL=batching.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"batching.d.ts","sourceRoot":"","sources":["../../src/ai-capture/batching.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAqB,MAAM,eAAe,CAAA;AAMzE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,sBAAsB,EAAE,EAAE,CAAA;IACnC,OAAO,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CAC5C,CAAA;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,sBAAsB,GAAG,MAAM,CAErE;AAED,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,CAAC,sBAAsB,GAAG,SAAS,CAAC,EAAE,EAChD,aAAa,GAAE,MAA2B,EAC1C,gBAAgB,GAAE,MAA8B,GAC/C,gBAAgB,CA2BlB"}