posthog-js-lite 3.4.2 → 3.5.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/lib/index.d.ts CHANGED
@@ -19,11 +19,23 @@ type PostHogCoreOptions = {
19
19
  sendFeatureFlagEvent?: boolean;
20
20
  /** Whether to load feature flags when initialized or not */
21
21
  preloadFeatureFlags?: boolean;
22
+ /**
23
+ * Whether to load remote config when initialized or not
24
+ * Experimental support
25
+ * Default: false - Remote config is loaded by default
26
+ */
27
+ disableRemoteConfig?: boolean;
28
+ /**
29
+ * Whether to load surveys when initialized or not
30
+ * Experimental support
31
+ * Default: false - Surveys are loaded by default, but requires the `PostHogSurveyProvider` to be used
32
+ */
33
+ disableSurveys?: boolean;
22
34
  /** Option to bootstrap the library with given distinctId and feature flags */
23
35
  bootstrap?: {
24
36
  distinctId?: string;
25
37
  isIdentifiedId?: boolean;
26
- featureFlags?: Record<string, boolean | string>;
38
+ featureFlags?: Record<string, FeatureFlagValue>;
27
39
  featureFlagPayloads?: Record<string, JsonType>;
28
40
  };
29
41
  /** How many times we will retry HTTP requests. Defaults to 3. */
@@ -34,6 +46,8 @@ type PostHogCoreOptions = {
34
46
  requestTimeout?: number;
35
47
  /** Timeout in milliseconds for feature flag calls. Defaults to 10 seconds for stateful clients, and 3 seconds for stateless. */
36
48
  featureFlagsRequestTimeoutMs?: number;
49
+ /** Timeout in milliseconds for remote config calls. Defaults to 3 seconds. */
50
+ remoteConfigRequestTimeoutMs?: number;
37
51
  /** For Session Analysis how long before we expire a session (defaults to 30 mins) */
38
52
  sessionExpirationTimeSeconds?: number;
39
53
  /** Whether to post events to PostHog in JSON or compressed format. Defaults to 'json' */
@@ -46,8 +60,10 @@ declare enum PostHogPersistedProperty {
46
60
  AnonymousId = "anonymous_id",
47
61
  DistinctId = "distinct_id",
48
62
  Props = "props",
63
+ FeatureFlagDetails = "feature_flag_details",
49
64
  FeatureFlags = "feature_flags",
50
65
  FeatureFlagPayloads = "feature_flag_payloads",
66
+ BootstrapFeatureFlagDetails = "bootstrap_feature_flag_details",
51
67
  BootstrapFeatureFlags = "bootstrap_feature_flags",
52
68
  BootstrapFeatureFlagPayloads = "bootstrap_feature_flag_payloads",
53
69
  OverrideFeatureFlags = "override_feature_flags",
@@ -60,7 +76,11 @@ declare enum PostHogPersistedProperty {
60
76
  InstalledAppBuild = "installed_app_build",
61
77
  InstalledAppVersion = "installed_app_version",
62
78
  SessionReplay = "session_replay",
63
- DecideEndpointWasHit = "decide_endpoint_was_hit"
79
+ DecideEndpointWasHit = "decide_endpoint_was_hit",
80
+ SurveyLastSeenDate = "survey_last_seen_date",
81
+ SurveysSeen = "surveys_seen",
82
+ Surveys = "surveys",
83
+ RemoteConfig = "remote_config"
64
84
  }
65
85
  type PostHogFetchOptions = {
66
86
  method: 'GET' | 'POST' | 'PUT' | 'PATCH';
@@ -97,31 +117,267 @@ type PostHogAutocaptureElement = {
97
117
  } & {
98
118
  [key: string]: any;
99
119
  };
100
- type PostHogDecideResponse = {
101
- config: {
102
- enable_collect_everything: boolean;
103
- };
104
- editorParams: {
105
- toolbarVersion: string;
106
- jsURL: string;
120
+ type PostHogRemoteConfig = {
121
+ sessionRecording?: boolean | {
122
+ [key: string]: JsonType;
107
123
  };
108
- isAuthenticated: true;
109
- supportedCompression: string[];
124
+ /**
125
+ * Whether surveys are enabled
126
+ */
127
+ surveys?: boolean | Survey[];
128
+ /**
129
+ * Indicates if the team has any flags enabled (if not we don't need to load them)
130
+ */
131
+ hasFeatureFlags?: boolean;
132
+ };
133
+ type FeatureFlagValue = string | boolean;
134
+ type PostHogDecideResponse = Omit<PostHogRemoteConfig, 'surveys' | 'hasFeatureFlags'> & {
110
135
  featureFlags: {
111
- [key: string]: string | boolean;
136
+ [key: string]: FeatureFlagValue;
112
137
  };
113
138
  featureFlagPayloads: {
114
139
  [key: string]: JsonType;
115
140
  };
141
+ flags: {
142
+ [key: string]: FeatureFlagDetail;
143
+ };
116
144
  errorsWhileComputingFlags: boolean;
117
- quotaLimited?: string[];
118
145
  sessionRecording?: boolean | {
119
146
  [key: string]: JsonType;
120
147
  };
148
+ quotaLimited?: string[];
149
+ requestId?: string;
150
+ };
151
+ /**
152
+ * Creates a type with all properties of T, but makes only K properties required while the rest remain optional.
153
+ *
154
+ * @template T - The base type containing all properties
155
+ * @template K - Union type of keys from T that should be required
156
+ *
157
+ * @example
158
+ * interface User {
159
+ * id: number;
160
+ * name: string;
161
+ * email?: string;
162
+ * age?: number;
163
+ * }
164
+ *
165
+ * // Makes 'id' and 'name' required, but 'email' and 'age' optional
166
+ * type RequiredUser = PartialWithRequired<User, 'id' | 'name'>;
167
+ *
168
+ * const user: RequiredUser = {
169
+ * id: 1, // Must be provided
170
+ * name: "John" // Must be provided
171
+ * // email and age are optional
172
+ * };
173
+ */
174
+ type PartialWithRequired<T, K extends keyof T> = {
175
+ [P in K]: T[P];
176
+ } & {
177
+ [P in Exclude<keyof T, K>]?: T[P];
121
178
  };
179
+ /**
180
+ * These are the fields we care about from PostHogDecideResponse for feature flags.
181
+ */
182
+ type PostHogFeatureFlagDetails = PartialWithRequired<PostHogDecideResponse, 'flags' | 'featureFlags' | 'featureFlagPayloads' | 'requestId'>;
122
183
  type JsonType = string | number | boolean | null | {
123
184
  [key: string]: JsonType;
124
- } | Array<JsonType>;
185
+ } | Array<JsonType>;
186
+ type FeatureFlagDetail = {
187
+ key: string;
188
+ enabled: boolean;
189
+ variant: string | undefined;
190
+ reason: EvaluationReason | undefined;
191
+ metadata: FeatureFlagMetadata | undefined;
192
+ };
193
+ type FeatureFlagMetadata = {
194
+ id: number | undefined;
195
+ version: number | undefined;
196
+ description: string | undefined;
197
+ payload: string | undefined;
198
+ };
199
+ type EvaluationReason = {
200
+ code: string | undefined;
201
+ condition_index: number | undefined;
202
+ description: string | undefined;
203
+ };
204
+ type SurveyAppearance = {
205
+ backgroundColor?: string;
206
+ submitButtonColor?: string;
207
+ submitButtonText?: string;
208
+ submitButtonTextColor?: string;
209
+ ratingButtonColor?: string;
210
+ ratingButtonActiveColor?: string;
211
+ autoDisappear?: boolean;
212
+ displayThankYouMessage?: boolean;
213
+ thankYouMessageHeader?: string;
214
+ thankYouMessageDescription?: string;
215
+ thankYouMessageDescriptionContentType?: SurveyQuestionDescriptionContentType;
216
+ thankYouMessageCloseButtonText?: string;
217
+ borderColor?: string;
218
+ position?: SurveyPosition;
219
+ placeholder?: string;
220
+ shuffleQuestions?: boolean;
221
+ surveyPopupDelaySeconds?: number;
222
+ widgetType?: SurveyWidgetType;
223
+ widgetSelector?: string;
224
+ widgetLabel?: string;
225
+ widgetColor?: string;
226
+ };
227
+ declare enum SurveyPosition {
228
+ Left = "left",
229
+ Right = "right",
230
+ Center = "center"
231
+ }
232
+ declare enum SurveyWidgetType {
233
+ Button = "button",
234
+ Tab = "tab",
235
+ Selector = "selector"
236
+ }
237
+ declare enum SurveyType {
238
+ Popover = "popover",
239
+ API = "api",
240
+ Widget = "widget"
241
+ }
242
+ type SurveyQuestion = BasicSurveyQuestion | LinkSurveyQuestion | RatingSurveyQuestion | MultipleSurveyQuestion;
243
+ declare enum SurveyQuestionDescriptionContentType {
244
+ Html = "html",
245
+ Text = "text"
246
+ }
247
+ type SurveyQuestionBase = {
248
+ question: string;
249
+ id?: string;
250
+ description?: string;
251
+ descriptionContentType?: SurveyQuestionDescriptionContentType;
252
+ optional?: boolean;
253
+ buttonText?: string;
254
+ originalQuestionIndex: number;
255
+ branching?: NextQuestionBranching | EndBranching | ResponseBasedBranching | SpecificQuestionBranching;
256
+ };
257
+ type BasicSurveyQuestion = SurveyQuestionBase & {
258
+ type: SurveyQuestionType.Open;
259
+ };
260
+ type LinkSurveyQuestion = SurveyQuestionBase & {
261
+ type: SurveyQuestionType.Link;
262
+ link?: string;
263
+ };
264
+ type RatingSurveyQuestion = SurveyQuestionBase & {
265
+ type: SurveyQuestionType.Rating;
266
+ display: SurveyRatingDisplay;
267
+ scale: 3 | 5 | 7 | 10;
268
+ lowerBoundLabel: string;
269
+ upperBoundLabel: string;
270
+ };
271
+ declare enum SurveyRatingDisplay {
272
+ Number = "number",
273
+ Emoji = "emoji"
274
+ }
275
+ type MultipleSurveyQuestion = SurveyQuestionBase & {
276
+ type: SurveyQuestionType.SingleChoice | SurveyQuestionType.MultipleChoice;
277
+ choices: string[];
278
+ hasOpenChoice?: boolean;
279
+ shuffleOptions?: boolean;
280
+ };
281
+ declare enum SurveyQuestionType {
282
+ Open = "open",
283
+ MultipleChoice = "multiple_choice",
284
+ SingleChoice = "single_choice",
285
+ Rating = "rating",
286
+ Link = "link"
287
+ }
288
+ declare enum SurveyQuestionBranchingType {
289
+ NextQuestion = "next_question",
290
+ End = "end",
291
+ ResponseBased = "response_based",
292
+ SpecificQuestion = "specific_question"
293
+ }
294
+ type NextQuestionBranching = {
295
+ type: SurveyQuestionBranchingType.NextQuestion;
296
+ };
297
+ type EndBranching = {
298
+ type: SurveyQuestionBranchingType.End;
299
+ };
300
+ type ResponseBasedBranching = {
301
+ type: SurveyQuestionBranchingType.ResponseBased;
302
+ responseValues: Record<string, any>;
303
+ };
304
+ type SpecificQuestionBranching = {
305
+ type: SurveyQuestionBranchingType.SpecificQuestion;
306
+ index: number;
307
+ };
308
+ type SurveyResponse = {
309
+ surveys: Survey[];
310
+ };
311
+ declare enum SurveyMatchType {
312
+ Regex = "regex",
313
+ NotRegex = "not_regex",
314
+ Exact = "exact",
315
+ IsNot = "is_not",
316
+ Icontains = "icontains",
317
+ NotIcontains = "not_icontains"
318
+ }
319
+ type Survey = {
320
+ id: string;
321
+ name: string;
322
+ description?: string;
323
+ type: SurveyType;
324
+ feature_flag_keys?: {
325
+ key: string;
326
+ value?: string;
327
+ }[];
328
+ linked_flag_key?: string;
329
+ targeting_flag_key?: string;
330
+ internal_targeting_flag_key?: string;
331
+ questions: SurveyQuestion[];
332
+ appearance?: SurveyAppearance;
333
+ conditions?: {
334
+ url?: string;
335
+ selector?: string;
336
+ seenSurveyWaitPeriodInDays?: number;
337
+ urlMatchType?: SurveyMatchType;
338
+ events?: {
339
+ repeatedActivation?: boolean;
340
+ values?: {
341
+ name: string;
342
+ }[];
343
+ };
344
+ actions?: {
345
+ values: SurveyActionType[];
346
+ };
347
+ deviceTypes?: string[];
348
+ deviceTypesMatchType?: SurveyMatchType;
349
+ };
350
+ start_date?: string;
351
+ end_date?: string;
352
+ current_iteration?: number;
353
+ current_iteration_start_date?: string;
354
+ };
355
+ type SurveyActionType = {
356
+ id: number;
357
+ name?: string;
358
+ steps?: ActionStepType[];
359
+ };
360
+ /** Sync with plugin-server/src/types.ts */
361
+ declare enum ActionStepStringMatching {
362
+ Contains = "contains",
363
+ Exact = "exact",
364
+ Regex = "regex"
365
+ }
366
+ type ActionStepType = {
367
+ event?: string;
368
+ selector?: string;
369
+ /** @deprecated Only `selector` should be used now. */
370
+ tag_name?: string;
371
+ text?: string;
372
+ /** @default StringMatching.Exact */
373
+ text_matching?: ActionStepStringMatching;
374
+ href?: string;
375
+ /** @default ActionStepStringMatching.Exact */
376
+ href_matching?: ActionStepStringMatching;
377
+ url?: string;
378
+ /** @default StringMatching.Contains */
379
+ url_matching?: ActionStepStringMatching;
380
+ };
125
381
 
126
382
  interface RetriableOptions {
127
383
  retryCount: number;
@@ -142,12 +398,15 @@ declare abstract class PostHogCoreStateless {
142
398
  readonly apiKey: string;
143
399
  readonly host: string;
144
400
  readonly flushAt: number;
401
+ readonly preloadFeatureFlags: boolean;
402
+ readonly disableSurveys: boolean;
145
403
  private maxBatchSize;
146
404
  private maxQueueSize;
147
405
  private flushInterval;
148
406
  private flushPromise;
149
407
  private requestTimeout;
150
408
  private featureFlagsRequestTimeoutMs;
409
+ private remoteConfigRequestTimeoutMs;
151
410
  private captureMode;
152
411
  private removeDebugCallback?;
153
412
  private disableGeoip;
@@ -160,6 +419,7 @@ declare abstract class PostHogCoreStateless {
160
419
  protected _retryOptions: RetriableOptions;
161
420
  protected _initPromise: Promise<void>;
162
421
  protected _isInitialized: boolean;
422
+ protected _remoteConfigResponsePromise?: Promise<PostHogRemoteConfig | undefined>;
163
423
  abstract fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
164
424
  abstract getLibraryId(): string;
165
425
  abstract getLibraryVersion(): string;
@@ -193,19 +453,36 @@ declare abstract class PostHogCoreStateless {
193
453
  *** GROUPS
194
454
  ***/
195
455
  protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): void;
456
+ protected getRemoteConfig(): Promise<PostHogRemoteConfig | undefined>;
196
457
  /***
197
458
  *** FEATURE FLAGS
198
459
  ***/
199
460
  protected getDecide(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, extraPayload?: Record<string, any>): Promise<PostHogDecideResponse | undefined>;
200
- protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<boolean | string | undefined>;
461
+ protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<{
462
+ response: FeatureFlagValue | undefined;
463
+ requestId: string | undefined;
464
+ }>;
465
+ protected getFeatureFlagDetailStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<{
466
+ response: FeatureFlagDetail | undefined;
467
+ requestId: string | undefined;
468
+ } | undefined>;
201
469
  protected getFeatureFlagPayloadStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<JsonType | undefined>;
202
- protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<PostHogDecideResponse['featureFlagPayloads'] | undefined>;
203
- protected _parsePayload(response: any): any;
204
- protected getFeatureFlagsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<PostHogDecideResponse['featureFlags'] | undefined>;
205
- protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<{
470
+ protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean, flagKeysToEvaluate?: string[]): Promise<PostHogDecideResponse['featureFlagPayloads'] | undefined>;
471
+ protected getFeatureFlagsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean, flagKeysToEvaluate?: string[]): Promise<{
472
+ flags: PostHogDecideResponse['featureFlags'] | undefined;
473
+ payloads: PostHogDecideResponse['featureFlagPayloads'] | undefined;
474
+ requestId: PostHogDecideResponse['requestId'] | undefined;
475
+ }>;
476
+ protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean, flagKeysToEvaluate?: string[]): Promise<{
206
477
  flags: PostHogDecideResponse['featureFlags'] | undefined;
207
478
  payloads: PostHogDecideResponse['featureFlagPayloads'] | undefined;
479
+ requestId: PostHogDecideResponse['requestId'] | undefined;
208
480
  }>;
481
+ protected getFeatureFlagDetailsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean, flagKeysToEvaluate?: string[]): Promise<PostHogFeatureFlagDetails | undefined>;
482
+ /***
483
+ *** SURVEYS
484
+ ***/
485
+ getSurveysStateless(): Promise<SurveyResponse['surveys']>;
209
486
  /***
210
487
  *** QUEUEING AND FLUSHING
211
488
  ***/
@@ -297,26 +574,37 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
297
574
  groupProperties(properties: {
298
575
  [type: string]: Record<string, string>;
299
576
  }): void;
577
+ private remoteConfigAsync;
300
578
  /***
301
579
  *** FEATURE FLAGS
302
580
  ***/
303
581
  private decideAsync;
582
+ private cacheSessionReplay;
583
+ private _remoteConfigAsync;
304
584
  private _decideAsync;
305
- private setKnownFeatureFlags;
306
- private setKnownFeatureFlagPayloads;
307
- getFeatureFlag(key: string): boolean | string | undefined;
585
+ private setKnownFeatureFlagDetails;
586
+ private getKnownFeatureFlagDetails;
587
+ private getKnownFeatureFlags;
588
+ private getKnownFeatureFlagPayloads;
589
+ private getBootstrappedFeatureFlagDetails;
590
+ private setBootstrappedFeatureFlagDetails;
591
+ private getBootstrappedFeatureFlags;
592
+ private getBootstrappedFeatureFlagPayloads;
593
+ getFeatureFlag(key: string): FeatureFlagValue | undefined;
308
594
  getFeatureFlagPayload(key: string): JsonType | undefined;
309
595
  getFeatureFlagPayloads(): PostHogDecideResponse['featureFlagPayloads'] | undefined;
310
596
  getFeatureFlags(): PostHogDecideResponse['featureFlags'] | undefined;
597
+ getFeatureFlagDetails(): PostHogFeatureFlagDetails | undefined;
311
598
  getFeatureFlagsAndPayloads(): {
312
599
  flags: PostHogDecideResponse['featureFlags'] | undefined;
313
600
  payloads: PostHogDecideResponse['featureFlagPayloads'] | undefined;
314
601
  };
315
602
  isFeatureEnabled(key: string): boolean | undefined;
316
603
  reloadFeatureFlags(cb?: (err?: Error, flags?: PostHogDecideResponse['featureFlags']) => void): void;
604
+ reloadRemoteConfigAsync(): Promise<PostHogRemoteConfig | undefined>;
317
605
  reloadFeatureFlagsAsync(sendAnonDistinctId?: boolean): Promise<PostHogDecideResponse['featureFlags'] | undefined>;
318
606
  onFeatureFlags(cb: (flags: PostHogDecideResponse['featureFlags']) => void): () => void;
319
- onFeatureFlag(key: string, cb: (value: string | boolean) => void): () => void;
607
+ onFeatureFlag(key: string, cb: (value: FeatureFlagValue) => void): () => void;
320
608
  overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): Promise<void>;
321
609
  /***
322
610
  *** ERROR TRACKING
@@ -343,12 +631,14 @@ type PostHogOptions = {
343
631
  autocapture?: boolean;
344
632
  persistence?: 'localStorage' | 'sessionStorage' | 'cookie' | 'memory';
345
633
  persistence_name?: string;
634
+ captureHistoryEvents?: boolean;
346
635
  } & PostHogCoreOptions;
347
636
 
348
637
  declare class PostHog extends PostHogCore {
349
638
  private _storage;
350
639
  private _storageCache;
351
640
  private _storageKey;
641
+ private _lastPathname;
352
642
  constructor(apiKey: string, options?: PostHogOptions);
353
643
  private getWindow;
354
644
  getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined;
@@ -358,6 +648,8 @@ declare class PostHog extends PostHogCore {
358
648
  getLibraryVersion(): string;
359
649
  getCustomUserAgent(): void;
360
650
  getCommonEventProperties(): any;
651
+ private setupHistoryEventTracking;
652
+ private captureNavigationEvent;
361
653
  }
362
654
 
363
655
  export { PostHog, PostHog as default };