posthog-js-lite 2.6.2 → 3.0.0-beta.2

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.
@@ -1,4 +1,4 @@
1
- import { PostHogFetchOptions, PostHogFetchResponse, PostHogAutocaptureElement, PostHogDecideResponse, PosthogCoreOptions, PostHogEventProperties, PostHogPersistedProperty, PostHogCaptureOptions, JsonType } from './types';
1
+ import { PostHogFetchOptions, PostHogFetchResponse, PostHogAutocaptureElement, PostHogDecideResponse, PostHogCoreOptions, PostHogEventProperties, PostHogPersistedProperty, PostHogCaptureOptions, JsonType } from './types';
2
2
  import { RetriableOptions } from './utils';
3
3
  export * as utils from './utils';
4
4
  import { LZString } from './lz-string';
@@ -7,46 +7,53 @@ export declare abstract class PostHogCoreStateless {
7
7
  private apiKey;
8
8
  host: string;
9
9
  private flushAt;
10
+ private maxBatchSize;
10
11
  private flushInterval;
12
+ private flushPromise;
11
13
  private requestTimeout;
14
+ private featureFlagsRequestTimeoutMs;
12
15
  private captureMode;
13
16
  private removeDebugCallback?;
14
- private debugMode;
15
17
  private disableGeoip;
16
- private _optoutOverride;
18
+ disabled: boolean;
19
+ private defaultOptIn;
17
20
  private pendingPromises;
18
21
  protected _events: SimpleEventEmitter;
19
22
  protected _flushTimer?: any;
20
23
  protected _retryOptions: RetriableOptions;
24
+ protected _initPromise: Promise<void>;
25
+ protected _isInitialized: boolean;
21
26
  abstract fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
22
27
  abstract getLibraryId(): string;
23
28
  abstract getLibraryVersion(): string;
24
29
  abstract getCustomUserAgent(): string | void;
25
30
  abstract getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined;
26
31
  abstract setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
27
- constructor(apiKey: string, options?: PosthogCoreOptions);
32
+ constructor(apiKey: string, options?: PostHogCoreOptions);
33
+ protected wrap(fn: () => void): void;
28
34
  protected getCommonEventProperties(): any;
29
35
  get optedOut(): boolean;
30
- optIn(): void;
31
- optOut(): void;
36
+ optIn(): Promise<void>;
37
+ optOut(): Promise<void>;
32
38
  on(event: string, cb: (...args: any[]) => void): () => void;
33
39
  debug(enabled?: boolean): void;
40
+ get isDebug(): boolean;
34
41
  private buildPayload;
35
- protected addPendingPromise(promise: Promise<any>): void;
42
+ protected addPendingPromise<T>(promise: Promise<T>): Promise<T>;
36
43
  /***
37
44
  *** TRACKING
38
45
  ***/
39
- protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
46
+ protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
40
47
  protected captureStateless(distinctId: string, event: string, properties?: {
41
48
  [key: string]: any;
42
- }, options?: PostHogCaptureOptions): this;
49
+ }, options?: PostHogCaptureOptions): void;
43
50
  protected aliasStateless(alias: string, distinctId: string, properties?: {
44
51
  [key: string]: any;
45
- }, options?: PostHogCaptureOptions): this;
52
+ }, options?: PostHogCaptureOptions): void;
46
53
  /***
47
54
  *** GROUPS
48
55
  ***/
49
- protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): this;
56
+ protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): void;
50
57
  /***
51
58
  *** FEATURE FLAGS
52
59
  ***/
@@ -64,11 +71,16 @@ export declare abstract class PostHogCoreStateless {
64
71
  *** QUEUEING AND FLUSHING
65
72
  ***/
66
73
  protected enqueue(type: string, _message: any, options?: PostHogCaptureOptions): void;
67
- flushAsync(): Promise<any>;
68
- flush(callback?: (err?: any, data?: any) => void): void;
74
+ private clearFlushTimer;
75
+ /**
76
+ * Helper for flushing the queue in the background
77
+ * Avoids unnecessary promise errors
78
+ */
79
+ private flushBackground;
80
+ flush(): Promise<any[]>;
81
+ private _flush;
69
82
  private fetchWithRetry;
70
- shutdownAsync(): Promise<void>;
71
- shutdown(): void;
83
+ shutdown(shutdownTimeoutMs?: number): Promise<void>;
72
84
  }
73
85
  export declare abstract class PostHogCore extends PostHogCoreStateless {
74
86
  private sendFeatureFlagEvent;
@@ -76,8 +88,8 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
76
88
  protected _decideResponsePromise?: Promise<PostHogDecideResponse | undefined>;
77
89
  protected _sessionExpirationTimeSeconds: number;
78
90
  protected sessionProps: PostHogEventProperties;
79
- constructor(apiKey: string, options?: PosthogCoreOptions);
80
- protected setupBootstrap(options?: Partial<PosthogCoreOptions>): void;
91
+ constructor(apiKey: string, options?: PostHogCoreOptions);
92
+ protected setupBootstrap(options?: Partial<PostHogCoreOptions>): void;
81
93
  private get props();
82
94
  private set props(value);
83
95
  private clearProps;
@@ -85,15 +97,24 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
85
97
  on(event: string, cb: (...args: any[]) => void): () => void;
86
98
  reset(propertiesToKeep?: PostHogPersistedProperty[]): void;
87
99
  protected getCommonEventProperties(): any;
88
- enrichProperties(properties?: PostHogEventProperties): any;
89
- getSessionId(): string | undefined;
100
+ private enrichProperties;
101
+ /**
102
+ * * @returns {string} The stored session ID for the current session. This may be an empty string if the client is not yet fully initialized.
103
+ */
104
+ getSessionId(): string;
90
105
  resetSessionId(): void;
106
+ /**
107
+ * * @returns {string} The stored anonymous ID. This may be an empty string if the client is not yet fully initialized.
108
+ */
91
109
  getAnonymousId(): string;
110
+ /**
111
+ * * @returns {string} The stored distinct ID. This may be an empty string if the client is not yet fully initialized.
112
+ */
92
113
  getDistinctId(): string;
93
- unregister(property: string): void;
114
+ unregister(property: string): Promise<void>;
94
115
  register(properties: {
95
116
  [key: string]: any;
96
- }): void;
117
+ }): Promise<void>;
97
118
  registerForSession(properties: {
98
119
  [key: string]: any;
99
120
  }): void;
@@ -101,39 +122,39 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
101
122
  /***
102
123
  *** TRACKING
103
124
  ***/
104
- identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
125
+ identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
105
126
  capture(event: string, properties?: {
106
127
  [key: string]: any;
107
- }, options?: PostHogCaptureOptions): this;
108
- alias(alias: string): this;
109
- autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
128
+ }, options?: PostHogCaptureOptions): void;
129
+ alias(alias: string): void;
130
+ autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
110
131
  /***
111
132
  *** GROUPS
112
133
  ***/
113
134
  groups(groups: {
114
135
  [type: string]: string | number;
115
- }): this;
116
- group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
117
- groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
136
+ }): void;
137
+ group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
138
+ groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
118
139
  /***
119
140
  * PROPERTIES
120
141
  ***/
121
142
  setPersonPropertiesForFlags(properties: {
122
143
  [type: string]: string;
123
- }): this;
144
+ }): void;
124
145
  resetPersonPropertiesForFlags(): void;
125
146
  /** @deprecated - Renamed to setPersonPropertiesForFlags */
126
147
  personProperties(properties: {
127
148
  [type: string]: string;
128
- }): this;
149
+ }): void;
129
150
  setGroupPropertiesForFlags(properties: {
130
151
  [type: string]: Record<string, string>;
131
- }): this;
152
+ }): void;
132
153
  resetGroupPropertiesForFlags(): void;
133
154
  /** @deprecated - Renamed to setGroupPropertiesForFlags */
134
155
  groupProperties(properties: {
135
156
  [type: string]: Record<string, string>;
136
- }): this;
157
+ }): void;
137
158
  /***
138
159
  *** FEATURE FLAGS
139
160
  ***/
@@ -154,7 +175,7 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
154
175
  reloadFeatureFlagsAsync(sendAnonDistinctId?: boolean): Promise<PostHogDecideResponse['featureFlags'] | undefined>;
155
176
  onFeatureFlags(cb: (flags: PostHogDecideResponse['featureFlags']) => void): () => void;
156
177
  onFeatureFlag(key: string, cb: (value: string | boolean) => void): () => void;
157
- overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): void;
178
+ overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): Promise<void>;
158
179
  }
159
180
  export * from './types';
160
181
  export { LZString };
@@ -1,20 +1,38 @@
1
- export declare type PosthogCoreOptions = {
1
+ export declare type PostHogCoreOptions = {
2
+ /** PostHog API host, usually 'https://app.posthog.com' or 'https://eu.posthog.com' */
2
3
  host?: string;
4
+ /** The number of events to queue before sending to PostHog (flushing) */
3
5
  flushAt?: number;
6
+ /** The interval in milliseconds between periodic flushes */
4
7
  flushInterval?: number;
5
- enable?: boolean;
8
+ /** The maximum number of queued messages to be flushed as part of a single batch (must be higher than `flushAt`) */
9
+ maxBatchSize?: number;
10
+ /** If set to true the SDK is essentially disabled (useful for local environments where you don't want to track anything) */
11
+ disabled?: boolean;
12
+ /** If set to false the SDK will not track until the `optIn` function is called. */
13
+ defaultOptIn?: boolean;
14
+ /** Whether to track that `getFeatureFlag` was called (used by Experiments) */
6
15
  sendFeatureFlagEvent?: boolean;
16
+ /** Whether to load feature flags when initialized or not */
7
17
  preloadFeatureFlags?: boolean;
18
+ /** Option to bootstrap the library with given distinctId and feature flags */
8
19
  bootstrap?: {
9
20
  distinctId?: string;
10
21
  isIdentifiedId?: boolean;
11
22
  featureFlags?: Record<string, boolean | string>;
12
23
  featureFlagPayloads?: Record<string, JsonType>;
13
24
  };
25
+ /** How many times we will retry HTTP requests. Defaults to 3. */
14
26
  fetchRetryCount?: number;
27
+ /** The delay between HTTP request retries, Defaults to 3 seconds. */
15
28
  fetchRetryDelay?: number;
29
+ /** Timeout in milliseconds for any calls. Defaults to 10 seconds. */
16
30
  requestTimeout?: number;
31
+ /** Timeout in milliseconds for feature flag calls. Defaults to 10 seconds for stateful clients, and 3 seconds for stateless. */
32
+ featureFlagsRequestTimeoutMs?: number;
33
+ /** For Session Analysis how long before we expire a session (defaults to 30 mins) */
17
34
  sessionExpirationTimeSeconds?: number;
35
+ /** Whether to post events to PostHog in JSON or compressed format */
18
36
  captureMode?: 'json' | 'form';
19
37
  disableGeoip?: boolean;
20
38
  };
@@ -92,7 +110,7 @@ export declare type PostHogDecideResponse = {
92
110
  errorsWhileComputingFlags: boolean;
93
111
  sessionRecording: boolean;
94
112
  };
95
- export declare type PosthogFlagsAndPayloadsResponse = {
113
+ export declare type PostHogFlagsAndPayloadsResponse = {
96
114
  featureFlags: PostHogDecideResponse['featureFlags'];
97
115
  featureFlagPayloads: PostHogDecideResponse['featureFlagPayloads'];
98
116
  };
@@ -1,11 +1,12 @@
1
1
  export declare function assert(truthyValue: any, message: string): void;
2
2
  export declare function removeTrailingSlash(url: string): string;
3
3
  export interface RetriableOptions {
4
- retryCount?: number;
5
- retryDelay?: number;
6
- retryCheck?: (err: any) => boolean;
4
+ retryCount: number;
5
+ retryDelay: number;
6
+ retryCheck: (err: any) => boolean;
7
7
  }
8
- export declare function retriable<T>(fn: () => Promise<T>, props?: RetriableOptions): Promise<T>;
8
+ export declare function retriable<T>(fn: () => Promise<T>, props: RetriableOptions): Promise<T>;
9
9
  export declare function currentTimestamp(): number;
10
10
  export declare function currentISOTime(): string;
11
11
  export declare function safeSetTimeout(fn: () => void, timeout: number): any;
12
+ export declare const isPromise: (obj: any) => obj is Promise<any>;
@@ -1,6 +1,6 @@
1
- import { PosthogCoreOptions } from '../../posthog-core/src';
1
+ import { PostHogCoreOptions } from '../../posthog-core/src';
2
2
  export declare type PostHogOptions = {
3
3
  autocapture?: boolean;
4
4
  persistence?: 'localStorage' | 'sessionStorage' | 'cookie' | 'memory';
5
5
  persistence_name?: string;
6
- } & PosthogCoreOptions;
6
+ } & PostHogCoreOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-js-lite",
3
- "version": "2.6.2",
3
+ "version": "3.0.0-beta.2",
4
4
  "main": "lib/index.cjs.js",
5
5
  "module": "lib/index.esm.js",
6
6
  "types": "lib/index.d.ts",
package/src/types.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { PosthogCoreOptions } from '../../posthog-core/src'
1
+ import { PostHogCoreOptions } from '../../posthog-core/src'
2
2
 
3
3
  export type PostHogOptions = {
4
4
  autocapture?: boolean
5
5
  persistence?: 'localStorage' | 'sessionStorage' | 'cookie' | 'memory'
6
6
  persistence_name?: string
7
- } & PosthogCoreOptions
7
+ } & PostHogCoreOptions
@@ -2,9 +2,10 @@
2
2
  * @jest-environment jsdom
3
3
  */
4
4
 
5
+ import { waitForPromises } from 'posthog-core/test/test-utils/test-utils'
5
6
  import { PostHog } from '..'
6
7
 
7
- describe('PosthogWeb', () => {
8
+ describe('PostHogWeb', () => {
8
9
  let fetch: jest.Mock
9
10
  jest.useRealTimers()
10
11
 
@@ -41,13 +42,14 @@ describe('PosthogWeb', () => {
41
42
  })
42
43
 
43
44
  describe('init', () => {
44
- it('should initialise', () => {
45
+ it('should initialise', async () => {
45
46
  const posthog = new PostHog('TEST_API_KEY', {
46
47
  flushAt: 1,
47
48
  })
48
49
  expect(posthog.optedOut).toEqual(false)
49
50
 
50
51
  posthog.capture('test')
52
+ await waitForPromises()
51
53
 
52
54
  expect(fetch).toHaveBeenCalledTimes(2)
53
55
  })
@@ -57,6 +59,8 @@ describe('PosthogWeb', () => {
57
59
  flushAt: 1,
58
60
  })
59
61
 
62
+ await waitForPromises()
63
+
60
64
  expect(fetch).toHaveBeenCalledWith('https://app.posthog.com/decide/?v=3', {
61
65
  body: JSON.stringify({
62
66
  token: 'TEST_API_KEY',
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "extends": "../tsconfig.json",
3
3
  "compilerOptions": {
4
- "lib": ["DOM", "ES2015", "ES2022.Error"]
4
+ "lib": ["DOM", "ES2020", "ES2022.Error"]
5
5
  }
6
6
  }