posthog-js-lite 2.0.0-alpha5 → 2.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.
- package/README.md +68 -0
- package/lib/index.cjs.js +211 -81
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +37 -11
- package/lib/index.esm.js +211 -81
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/index.d.ts +24 -8
- package/lib/posthog-core/src/types.d.ts +13 -3
- package/lib/posthog-core/src/utils.d.ts +0 -1
- package/lib/posthog-web/src/storage.d.ts +1 -5
- package/package.json +2 -2
- package/src/posthog-web.ts +5 -1
- package/src/storage.ts +18 -9
|
@@ -5,9 +5,10 @@ import { LZString } from './lz-string';
|
|
|
5
5
|
import { SimpleEventEmitter } from './eventemitter';
|
|
6
6
|
export declare abstract class PostHogCore {
|
|
7
7
|
private apiKey;
|
|
8
|
-
|
|
8
|
+
host: string;
|
|
9
9
|
private flushAt;
|
|
10
10
|
private flushInterval;
|
|
11
|
+
private requestTimeout;
|
|
11
12
|
private captureMode;
|
|
12
13
|
private sendFeatureFlagEvent;
|
|
13
14
|
private flagCallReported;
|
|
@@ -15,8 +16,6 @@ export declare abstract class PostHogCore {
|
|
|
15
16
|
protected _events: SimpleEventEmitter;
|
|
16
17
|
protected _flushTimer?: any;
|
|
17
18
|
protected _decideResponsePromise?: Promise<PostHogDecideResponse>;
|
|
18
|
-
protected _decideTimer?: any;
|
|
19
|
-
protected _decidePollInterval: number;
|
|
20
19
|
protected _retryOptions: RetriableOptions;
|
|
21
20
|
protected _sessionExpirationTimeSeconds: number;
|
|
22
21
|
abstract fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
|
|
@@ -28,18 +27,21 @@ export declare abstract class PostHogCore {
|
|
|
28
27
|
private _optoutOverride;
|
|
29
28
|
constructor(apiKey: string, options?: PosthogCoreOptions);
|
|
30
29
|
protected getCommonEventProperties(): any;
|
|
30
|
+
protected setupBootstrap(options?: Partial<PosthogCoreOptions>): void;
|
|
31
31
|
private get props();
|
|
32
32
|
private set props(value);
|
|
33
|
+
private clearProps;
|
|
33
34
|
private _props;
|
|
34
35
|
get optedOut(): boolean;
|
|
35
36
|
optIn(): void;
|
|
36
37
|
optOut(): void;
|
|
37
38
|
on(event: string, cb: (...args: any[]) => void): () => void;
|
|
38
|
-
reset(): void;
|
|
39
|
+
reset(propertiesToKeep?: PostHogPersistedProperty[]): void;
|
|
39
40
|
debug(enabled?: boolean): void;
|
|
40
41
|
private buildPayload;
|
|
41
42
|
getSessionId(): string | undefined;
|
|
42
43
|
resetSessionId(): void;
|
|
44
|
+
getAnonymousId(): string;
|
|
43
45
|
getDistinctId(): string;
|
|
44
46
|
register(properties: {
|
|
45
47
|
[key: string]: any;
|
|
@@ -51,7 +53,7 @@ export declare abstract class PostHogCore {
|
|
|
51
53
|
identify(distinctId?: string, properties?: PostHogEventProperties): this;
|
|
52
54
|
capture(event: string, properties?: {
|
|
53
55
|
[key: string]: any;
|
|
54
|
-
}): this;
|
|
56
|
+
}, forceSendFeatureFlags?: boolean): this;
|
|
55
57
|
alias(alias: string): this;
|
|
56
58
|
autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties): this;
|
|
57
59
|
/***
|
|
@@ -62,17 +64,31 @@ export declare abstract class PostHogCore {
|
|
|
62
64
|
}): this;
|
|
63
65
|
group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties): this;
|
|
64
66
|
groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties): this;
|
|
67
|
+
/***
|
|
68
|
+
* PROPERTIES
|
|
69
|
+
***/
|
|
70
|
+
personProperties(properties: {
|
|
71
|
+
[type: string]: string;
|
|
72
|
+
}): this;
|
|
73
|
+
groupProperties(properties: {
|
|
74
|
+
[type: string]: Record<string, string>;
|
|
75
|
+
}): this;
|
|
65
76
|
/***
|
|
66
77
|
*** FEATURE FLAGS
|
|
67
78
|
***/
|
|
68
79
|
private decideAsync;
|
|
69
80
|
private _decideAsync;
|
|
70
|
-
|
|
81
|
+
private setKnownFeatureFlags;
|
|
82
|
+
getFeatureFlag(key: string): boolean | string | undefined;
|
|
71
83
|
getFeatureFlags(): PostHogDecideResponse['featureFlags'] | undefined;
|
|
72
|
-
isFeatureEnabled(key: string
|
|
73
|
-
reloadFeatureFlagsAsync(): Promise<PostHogDecideResponse['featureFlags']>;
|
|
84
|
+
isFeatureEnabled(key: string): boolean | undefined;
|
|
85
|
+
reloadFeatureFlagsAsync(sendAnonDistinctId?: boolean): Promise<PostHogDecideResponse['featureFlags']>;
|
|
74
86
|
onFeatureFlags(cb: (flags: PostHogDecideResponse['featureFlags']) => void): () => void;
|
|
87
|
+
onFeatureFlag(key: string, cb: (value: string | boolean) => void): () => void;
|
|
75
88
|
overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): void;
|
|
89
|
+
_sendFeatureFlags(event: string, properties?: {
|
|
90
|
+
[key: string]: any;
|
|
91
|
+
}): void;
|
|
76
92
|
/***
|
|
77
93
|
*** QUEUEING AND FLUSHING
|
|
78
94
|
***/
|
|
@@ -5,13 +5,19 @@ export declare type PosthogCoreOptions = {
|
|
|
5
5
|
enable?: boolean;
|
|
6
6
|
sendFeatureFlagEvent?: boolean;
|
|
7
7
|
preloadFeatureFlags?: boolean;
|
|
8
|
-
|
|
8
|
+
bootstrap?: {
|
|
9
|
+
distinctId?: string;
|
|
10
|
+
isIdentifiedId?: boolean;
|
|
11
|
+
featureFlags?: Record<string, boolean | string>;
|
|
12
|
+
};
|
|
9
13
|
fetchRetryCount?: number;
|
|
10
14
|
fetchRetryDelay?: number;
|
|
15
|
+
requestTimeout?: number;
|
|
11
16
|
sessionExpirationTimeSeconds?: number;
|
|
12
17
|
captureMode?: 'json' | 'form';
|
|
13
18
|
};
|
|
14
19
|
export declare enum PostHogPersistedProperty {
|
|
20
|
+
AnonymousId = "anonymous_id",
|
|
15
21
|
DistinctId = "distinct_id",
|
|
16
22
|
Props = "props",
|
|
17
23
|
FeatureFlags = "feature_flags",
|
|
@@ -19,7 +25,9 @@ export declare enum PostHogPersistedProperty {
|
|
|
19
25
|
Queue = "queue",
|
|
20
26
|
OptedOut = "opted_out",
|
|
21
27
|
SessionId = "session_id",
|
|
22
|
-
SessionLastTimestamp = "session_timestamp"
|
|
28
|
+
SessionLastTimestamp = "session_timestamp",
|
|
29
|
+
PersonProperties = "person_properties",
|
|
30
|
+
GroupProperties = "group_properties"
|
|
23
31
|
}
|
|
24
32
|
export declare type PostHogFetchOptions = {
|
|
25
33
|
method: 'GET' | 'POST' | 'PUT' | 'PATCH';
|
|
@@ -28,11 +36,13 @@ export declare type PostHogFetchOptions = {
|
|
|
28
36
|
headers: {
|
|
29
37
|
[key: string]: string;
|
|
30
38
|
};
|
|
31
|
-
body
|
|
39
|
+
body?: string;
|
|
40
|
+
signal?: AbortSignal;
|
|
32
41
|
};
|
|
33
42
|
export declare type PostHogFetchResponse = {
|
|
34
43
|
status: number;
|
|
35
44
|
text: () => Promise<string>;
|
|
45
|
+
json: () => Promise<any>;
|
|
36
46
|
};
|
|
37
47
|
export declare type PostHogQueueItem = {
|
|
38
48
|
message: any;
|
|
@@ -9,5 +9,4 @@ export declare function retriable<T>(fn: () => Promise<T>, props?: RetriableOpti
|
|
|
9
9
|
export declare function generateUUID(globalThis?: any): string;
|
|
10
10
|
export declare function currentTimestamp(): number;
|
|
11
11
|
export declare function currentISOTime(): string;
|
|
12
|
-
export declare function isUndefined(obj: any): boolean;
|
|
13
12
|
export declare function safeSetTimeout(fn: () => void, timeout: number): any;
|
|
@@ -7,8 +7,4 @@ export declare type PostHogStorage = {
|
|
|
7
7
|
getAllKeys: () => readonly string[];
|
|
8
8
|
};
|
|
9
9
|
export declare const cookieStore: PostHogStorage;
|
|
10
|
-
export declare const
|
|
11
|
-
export declare const _sessionStore: PostHogStorage;
|
|
12
|
-
export declare const localStore: PostHogStorage | undefined;
|
|
13
|
-
export declare const sessionStorage: PostHogStorage | undefined;
|
|
14
|
-
export declare const getStorage: (type: PostHogOptions['persistence']) => PostHogStorage;
|
|
10
|
+
export declare const getStorage: (type: PostHogOptions['persistence'], window: Window | undefined) => PostHogStorage;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "posthog-js-lite",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"main": "lib/index.cjs.js",
|
|
5
5
|
"module": "lib/index.esm.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "jest -c jest.config.js",
|
|
9
|
-
"
|
|
9
|
+
"prepublishOnly": "cd .. && yarn build"
|
|
10
10
|
}
|
|
11
11
|
}
|
package/src/posthog-web.ts
CHANGED
|
@@ -19,7 +19,8 @@ export class PostHog extends PostHogCore {
|
|
|
19
19
|
|
|
20
20
|
// posthog-js stores options in one object on
|
|
21
21
|
this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`
|
|
22
|
-
this._storage = getStorage(options?.persistence || 'localStorage')
|
|
22
|
+
this._storage = getStorage(options?.persistence || 'localStorage', window)
|
|
23
|
+
this.setupBootstrap(options)
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {
|
|
@@ -47,12 +48,15 @@ export class PostHog extends PostHogCore {
|
|
|
47
48
|
fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {
|
|
48
49
|
return window.fetch(url, options)
|
|
49
50
|
}
|
|
51
|
+
|
|
50
52
|
getLibraryId(): string {
|
|
51
53
|
return 'posthog-js-lite'
|
|
52
54
|
}
|
|
55
|
+
|
|
53
56
|
getLibraryVersion(): string {
|
|
54
57
|
return version
|
|
55
58
|
}
|
|
59
|
+
|
|
56
60
|
getCustomUserAgent(): void {
|
|
57
61
|
return
|
|
58
62
|
}
|
package/src/storage.ts
CHANGED
|
@@ -92,9 +92,6 @@ const createStorageLike = (store: any): PostHogStorage => {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
export const _localStore = createStorageLike(window.localStorage)
|
|
96
|
-
export const _sessionStore = createStorageLike(window.sessionStorage)
|
|
97
|
-
|
|
98
95
|
const checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__'): boolean => {
|
|
99
96
|
if (!window) {
|
|
100
97
|
return false
|
|
@@ -112,8 +109,8 @@ const checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__')
|
|
|
112
109
|
}
|
|
113
110
|
}
|
|
114
111
|
|
|
115
|
-
|
|
116
|
-
|
|
112
|
+
let localStore: PostHogStorage | undefined = undefined
|
|
113
|
+
let sessionStore: PostHogStorage | undefined = undefined
|
|
117
114
|
|
|
118
115
|
const createMemoryStorage = (): PostHogStorage => {
|
|
119
116
|
const _cache: { [key: string]: any | undefined } = {}
|
|
@@ -146,14 +143,26 @@ const createMemoryStorage = (): PostHogStorage => {
|
|
|
146
143
|
return store
|
|
147
144
|
}
|
|
148
145
|
|
|
149
|
-
export const getStorage = (type: PostHogOptions['persistence']): PostHogStorage => {
|
|
146
|
+
export const getStorage = (type: PostHogOptions['persistence'], window: Window | undefined): PostHogStorage => {
|
|
147
|
+
if (window) {
|
|
148
|
+
if (!localStorage) {
|
|
149
|
+
const _localStore = createStorageLike(window.localStorage)
|
|
150
|
+
localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (!sessionStore) {
|
|
154
|
+
const _sessionStore = createStorageLike(window.sessionStorage)
|
|
155
|
+
sessionStore = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
150
159
|
switch (type) {
|
|
151
160
|
case 'cookie':
|
|
152
|
-
return cookieStore || localStore ||
|
|
161
|
+
return cookieStore || localStore || sessionStore || createMemoryStorage()
|
|
153
162
|
case 'localStorage':
|
|
154
|
-
return localStore ||
|
|
163
|
+
return localStore || sessionStore || createMemoryStorage()
|
|
155
164
|
case 'sessionStorage':
|
|
156
|
-
return
|
|
165
|
+
return sessionStore || createMemoryStorage()
|
|
157
166
|
case 'memory':
|
|
158
167
|
return createMemoryStorage()
|
|
159
168
|
default:
|