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

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';
@@ -9,44 +9,49 @@ export declare abstract class PostHogCoreStateless {
9
9
  private flushAt;
10
10
  private flushInterval;
11
11
  private requestTimeout;
12
+ private featureFlagsRequestTimeoutMs;
12
13
  private captureMode;
13
14
  private removeDebugCallback?;
14
- private debugMode;
15
15
  private disableGeoip;
16
- private _optoutOverride;
16
+ disabled: boolean;
17
+ private defaultOptIn;
17
18
  private pendingPromises;
18
19
  protected _events: SimpleEventEmitter;
19
20
  protected _flushTimer?: any;
20
21
  protected _retryOptions: RetriableOptions;
22
+ protected _initPromise: Promise<void>;
23
+ protected _isInitialized: boolean;
21
24
  abstract fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
22
25
  abstract getLibraryId(): string;
23
26
  abstract getLibraryVersion(): string;
24
27
  abstract getCustomUserAgent(): string | void;
25
28
  abstract getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined;
26
29
  abstract setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
27
- constructor(apiKey: string, options?: PosthogCoreOptions);
30
+ constructor(apiKey: string, options?: PostHogCoreOptions);
31
+ protected wrap(fn: () => void): void;
28
32
  protected getCommonEventProperties(): any;
29
33
  get optedOut(): boolean;
30
- optIn(): void;
31
- optOut(): void;
34
+ optIn(): Promise<void>;
35
+ optOut(): Promise<void>;
32
36
  on(event: string, cb: (...args: any[]) => void): () => void;
33
37
  debug(enabled?: boolean): void;
38
+ get isDebug(): boolean;
34
39
  private buildPayload;
35
40
  protected addPendingPromise(promise: Promise<any>): void;
36
41
  /***
37
42
  *** TRACKING
38
43
  ***/
39
- protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
44
+ protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
40
45
  protected captureStateless(distinctId: string, event: string, properties?: {
41
46
  [key: string]: any;
42
- }, options?: PostHogCaptureOptions): this;
47
+ }, options?: PostHogCaptureOptions): void;
43
48
  protected aliasStateless(alias: string, distinctId: string, properties?: {
44
49
  [key: string]: any;
45
- }, options?: PostHogCaptureOptions): this;
50
+ }, options?: PostHogCaptureOptions): void;
46
51
  /***
47
52
  *** GROUPS
48
53
  ***/
49
- protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): this;
54
+ protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): void;
50
55
  /***
51
56
  *** FEATURE FLAGS
52
57
  ***/
@@ -67,8 +72,8 @@ export declare abstract class PostHogCoreStateless {
67
72
  flushAsync(): Promise<any>;
68
73
  flush(callback?: (err?: any, data?: any) => void): void;
69
74
  private fetchWithRetry;
70
- shutdownAsync(): Promise<void>;
71
- shutdown(): void;
75
+ shutdownAsync(shutdownTimeoutMs?: number): Promise<void>;
76
+ shutdown(shutdownTimeoutMs?: number): void;
72
77
  }
73
78
  export declare abstract class PostHogCore extends PostHogCoreStateless {
74
79
  private sendFeatureFlagEvent;
@@ -76,8 +81,8 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
76
81
  protected _decideResponsePromise?: Promise<PostHogDecideResponse | undefined>;
77
82
  protected _sessionExpirationTimeSeconds: number;
78
83
  protected sessionProps: PostHogEventProperties;
79
- constructor(apiKey: string, options?: PosthogCoreOptions);
80
- protected setupBootstrap(options?: Partial<PosthogCoreOptions>): void;
84
+ constructor(apiKey: string, options?: PostHogCoreOptions);
85
+ protected setupBootstrap(options?: Partial<PostHogCoreOptions>): void;
81
86
  private get props();
82
87
  private set props(value);
83
88
  private clearProps;
@@ -85,15 +90,24 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
85
90
  on(event: string, cb: (...args: any[]) => void): () => void;
86
91
  reset(propertiesToKeep?: PostHogPersistedProperty[]): void;
87
92
  protected getCommonEventProperties(): any;
88
- enrichProperties(properties?: PostHogEventProperties): any;
89
- getSessionId(): string | undefined;
93
+ private enrichProperties;
94
+ /**
95
+ * * @returns {string} The stored session ID for the current session. This may be an empty string if the client is not yet fully initialized.
96
+ */
97
+ getSessionId(): string;
90
98
  resetSessionId(): void;
99
+ /**
100
+ * * @returns {string} The stored anonymous ID. This may be an empty string if the client is not yet fully initialized.
101
+ */
91
102
  getAnonymousId(): string;
103
+ /**
104
+ * * @returns {string} The stored distinct ID. This may be an empty string if the client is not yet fully initialized.
105
+ */
92
106
  getDistinctId(): string;
93
- unregister(property: string): void;
107
+ unregister(property: string): Promise<void>;
94
108
  register(properties: {
95
109
  [key: string]: any;
96
- }): void;
110
+ }): Promise<void>;
97
111
  registerForSession(properties: {
98
112
  [key: string]: any;
99
113
  }): void;
@@ -101,39 +115,39 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
101
115
  /***
102
116
  *** TRACKING
103
117
  ***/
104
- identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
118
+ identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
105
119
  capture(event: string, properties?: {
106
120
  [key: string]: any;
107
- }, options?: PostHogCaptureOptions): this;
108
- alias(alias: string): this;
109
- autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
121
+ }, options?: PostHogCaptureOptions): void;
122
+ alias(alias: string): void;
123
+ autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
110
124
  /***
111
125
  *** GROUPS
112
126
  ***/
113
127
  groups(groups: {
114
128
  [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;
129
+ }): void;
130
+ group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
131
+ groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
118
132
  /***
119
133
  * PROPERTIES
120
134
  ***/
121
135
  setPersonPropertiesForFlags(properties: {
122
136
  [type: string]: string;
123
- }): this;
137
+ }): void;
124
138
  resetPersonPropertiesForFlags(): void;
125
139
  /** @deprecated - Renamed to setPersonPropertiesForFlags */
126
140
  personProperties(properties: {
127
141
  [type: string]: string;
128
- }): this;
142
+ }): void;
129
143
  setGroupPropertiesForFlags(properties: {
130
144
  [type: string]: Record<string, string>;
131
- }): this;
145
+ }): void;
132
146
  resetGroupPropertiesForFlags(): void;
133
147
  /** @deprecated - Renamed to setGroupPropertiesForFlags */
134
148
  groupProperties(properties: {
135
149
  [type: string]: Record<string, string>;
136
- }): this;
150
+ }): void;
137
151
  /***
138
152
  *** FEATURE FLAGS
139
153
  ***/
@@ -154,7 +168,7 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
154
168
  reloadFeatureFlagsAsync(sendAnonDistinctId?: boolean): Promise<PostHogDecideResponse['featureFlags'] | undefined>;
155
169
  onFeatureFlags(cb: (flags: PostHogDecideResponse['featureFlags']) => void): () => void;
156
170
  onFeatureFlag(key: string, cb: (value: string | boolean) => void): () => void;
157
- overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): void;
171
+ overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): Promise<void>;
158
172
  }
159
173
  export * from './types';
160
174
  export { LZString };
@@ -1,20 +1,36 @@
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
+ /** If set to true the SDK is essentially disabled (useful for local environments where you don't want to track anything) */
9
+ disabled?: boolean;
10
+ /** If set to false the SDK will not track until the `optIn` function is called. */
11
+ defaultOptIn?: boolean;
12
+ /** Whether to track that `getFeatureFlag` was called (used by Experiments) */
6
13
  sendFeatureFlagEvent?: boolean;
14
+ /** Whether to load feature flags when initialized or not */
7
15
  preloadFeatureFlags?: boolean;
16
+ /** Option to bootstrap the library with given distinctId and feature flags */
8
17
  bootstrap?: {
9
18
  distinctId?: string;
10
19
  isIdentifiedId?: boolean;
11
20
  featureFlags?: Record<string, boolean | string>;
12
21
  featureFlagPayloads?: Record<string, JsonType>;
13
22
  };
23
+ /** How many times we will retry HTTP requests. Defaults to 3. */
14
24
  fetchRetryCount?: number;
25
+ /** The delay between HTTP request retries, Defaults to 3 seconds. */
15
26
  fetchRetryDelay?: number;
27
+ /** Timeout in milliseconds for any calls. Defaults to 10 seconds. */
16
28
  requestTimeout?: number;
29
+ /** Timeout in milliseconds for feature flag calls. Defaults to 10 seconds for stateful clients, and 3 seconds for stateless. */
30
+ featureFlagsRequestTimeoutMs?: number;
31
+ /** For Session Analysis how long before we expire a session (defaults to 30 mins) */
17
32
  sessionExpirationTimeSeconds?: number;
33
+ /** Whether to post events to PostHog in JSON or compressed format */
18
34
  captureMode?: 'json' | 'form';
19
35
  disableGeoip?: boolean;
20
36
  };
@@ -92,7 +108,7 @@ export declare type PostHogDecideResponse = {
92
108
  errorsWhileComputingFlags: boolean;
93
109
  sessionRecording: boolean;
94
110
  };
95
- export declare type PosthogFlagsAndPayloadsResponse = {
111
+ export declare type PostHogFlagsAndPayloadsResponse = {
96
112
  featureFlags: PostHogDecideResponse['featureFlags'];
97
113
  featureFlagPayloads: PostHogDecideResponse['featureFlagPayloads'];
98
114
  };
@@ -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.1",
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
  }