posthog-js-lite 2.0.0-alpha4 → 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 +257 -78
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +42 -14
- package/lib/index.esm.js +257 -78
- 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/posthog-web.d.ts +2 -5
- package/lib/posthog-web/src/storage.d.ts +2 -4
- package/lib/posthog-web/src/types.d.ts +6 -0
- package/package.json +2 -2
- package/src/posthog-web.ts +8 -8
- package/src/storage.ts +62 -5
- package/src/types.ts +7 -0
|
@@ -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;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import { PostHogCore,
|
|
2
|
-
|
|
3
|
-
autocapture?: boolean;
|
|
4
|
-
persistence_name?: string;
|
|
5
|
-
}
|
|
1
|
+
import { PostHogCore, PostHogFetchOptions, PostHogFetchResponse, PostHogPersistedProperty } from '../../posthog-core/src';
|
|
2
|
+
import { PostHogOptions } from './types';
|
|
6
3
|
export declare class PostHog extends PostHogCore {
|
|
7
4
|
private _storage;
|
|
8
5
|
private _storageCache;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PostHogOptions } from './types';
|
|
1
2
|
export declare type PostHogStorage = {
|
|
2
3
|
getItem: (key: string) => string | null | undefined;
|
|
3
4
|
setItem: (key: string, value: string) => void;
|
|
@@ -6,7 +7,4 @@ export declare type PostHogStorage = {
|
|
|
6
7
|
getAllKeys: () => readonly string[];
|
|
7
8
|
};
|
|
8
9
|
export declare const cookieStore: PostHogStorage;
|
|
9
|
-
export declare const
|
|
10
|
-
export declare const _sessionStore: PostHogStorage;
|
|
11
|
-
export declare const localStore: PostHogStorage | undefined;
|
|
12
|
-
export declare const sessionStorage: PostHogStorage | undefined;
|
|
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
|
@@ -1,21 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
2
|
PostHogCore,
|
|
3
|
-
PosthogCoreOptions,
|
|
4
3
|
PostHogFetchOptions,
|
|
5
4
|
PostHogFetchResponse,
|
|
6
5
|
PostHogPersistedProperty,
|
|
7
6
|
} from '../../posthog-core/src'
|
|
8
7
|
import { getContext } from './context'
|
|
9
|
-
import {
|
|
8
|
+
import { PostHogStorage, getStorage } from './storage'
|
|
10
9
|
import { version } from '../package.json'
|
|
11
|
-
|
|
12
|
-
export interface PostHogOptions extends PosthogCoreOptions {
|
|
13
|
-
autocapture?: boolean
|
|
14
|
-
persistence_name?: string
|
|
15
|
-
}
|
|
10
|
+
import { PostHogOptions } from './types'
|
|
16
11
|
|
|
17
12
|
export class PostHog extends PostHogCore {
|
|
18
|
-
private _storage
|
|
13
|
+
private _storage: PostHogStorage
|
|
19
14
|
private _storageCache: any
|
|
20
15
|
private _storageKey: string
|
|
21
16
|
|
|
@@ -24,6 +19,8 @@ export class PostHog extends PostHogCore {
|
|
|
24
19
|
|
|
25
20
|
// posthog-js stores options in one object on
|
|
26
21
|
this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`
|
|
22
|
+
this._storage = getStorage(options?.persistence || 'localStorage', window)
|
|
23
|
+
this.setupBootstrap(options)
|
|
27
24
|
}
|
|
28
25
|
|
|
29
26
|
getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {
|
|
@@ -51,12 +48,15 @@ export class PostHog extends PostHogCore {
|
|
|
51
48
|
fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {
|
|
52
49
|
return window.fetch(url, options)
|
|
53
50
|
}
|
|
51
|
+
|
|
54
52
|
getLibraryId(): string {
|
|
55
53
|
return 'posthog-js-lite'
|
|
56
54
|
}
|
|
55
|
+
|
|
57
56
|
getLibraryVersion(): string {
|
|
58
57
|
return version
|
|
59
58
|
}
|
|
59
|
+
|
|
60
60
|
getCustomUserAgent(): void {
|
|
61
61
|
return
|
|
62
62
|
}
|
package/src/storage.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { PostHogOptions } from './types'
|
|
2
|
+
|
|
1
3
|
export type PostHogStorage = {
|
|
2
4
|
getItem: (key: string) => string | null | undefined
|
|
3
5
|
setItem: (key: string, value: string) => void
|
|
@@ -90,9 +92,6 @@ const createStorageLike = (store: any): PostHogStorage => {
|
|
|
90
92
|
}
|
|
91
93
|
}
|
|
92
94
|
|
|
93
|
-
export const _localStore = createStorageLike(window.localStorage)
|
|
94
|
-
export const _sessionStore = createStorageLike(window.sessionStorage)
|
|
95
|
-
|
|
96
95
|
const checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__'): boolean => {
|
|
97
96
|
if (!window) {
|
|
98
97
|
return false
|
|
@@ -110,5 +109,63 @@ const checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__')
|
|
|
110
109
|
}
|
|
111
110
|
}
|
|
112
111
|
|
|
113
|
-
|
|
114
|
-
|
|
112
|
+
let localStore: PostHogStorage | undefined = undefined
|
|
113
|
+
let sessionStore: PostHogStorage | undefined = undefined
|
|
114
|
+
|
|
115
|
+
const createMemoryStorage = (): PostHogStorage => {
|
|
116
|
+
const _cache: { [key: string]: any | undefined } = {}
|
|
117
|
+
|
|
118
|
+
const store: PostHogStorage = {
|
|
119
|
+
getItem(key) {
|
|
120
|
+
return _cache[key]
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
setItem(key, value) {
|
|
124
|
+
_cache[key] = value !== null ? value : undefined
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
removeItem(key) {
|
|
128
|
+
delete _cache[key]
|
|
129
|
+
},
|
|
130
|
+
clear() {
|
|
131
|
+
for (const key in _cache) {
|
|
132
|
+
delete _cache[key]
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
getAllKeys() {
|
|
136
|
+
const keys = []
|
|
137
|
+
for (const key in _cache) {
|
|
138
|
+
keys.push(key)
|
|
139
|
+
}
|
|
140
|
+
return keys
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
return store
|
|
144
|
+
}
|
|
145
|
+
|
|
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
|
+
|
|
159
|
+
switch (type) {
|
|
160
|
+
case 'cookie':
|
|
161
|
+
return cookieStore || localStore || sessionStore || createMemoryStorage()
|
|
162
|
+
case 'localStorage':
|
|
163
|
+
return localStore || sessionStore || createMemoryStorage()
|
|
164
|
+
case 'sessionStorage':
|
|
165
|
+
return sessionStore || createMemoryStorage()
|
|
166
|
+
case 'memory':
|
|
167
|
+
return createMemoryStorage()
|
|
168
|
+
default:
|
|
169
|
+
return createMemoryStorage()
|
|
170
|
+
}
|
|
171
|
+
}
|
package/src/types.ts
ADDED