posthog-js-lite 2.3.0 → 2.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.
@@ -74,6 +74,7 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
74
74
  private flagCallReported;
75
75
  protected _decideResponsePromise?: Promise<PostHogDecideResponse | undefined>;
76
76
  protected _sessionExpirationTimeSeconds: number;
77
+ protected sessionProps: PostHogEventProperties;
77
78
  constructor(apiKey: string, options?: PosthogCoreOptions);
78
79
  protected setupBootstrap(options?: Partial<PosthogCoreOptions>): void;
79
80
  private get props();
@@ -83,15 +84,19 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
83
84
  on(event: string, cb: (...args: any[]) => void): () => void;
84
85
  reset(propertiesToKeep?: PostHogPersistedProperty[]): void;
85
86
  protected getCommonEventProperties(): any;
86
- private enrichProperties;
87
+ enrichProperties(properties?: PostHogEventProperties): any;
87
88
  getSessionId(): string | undefined;
88
89
  resetSessionId(): void;
89
90
  getAnonymousId(): string;
90
91
  getDistinctId(): string;
92
+ unregister(property: string): void;
91
93
  register(properties: {
92
94
  [key: string]: any;
93
95
  }): void;
94
- unregister(property: string): void;
96
+ registerForSession(properties: {
97
+ [key: string]: any;
98
+ }): void;
99
+ unregisterForSession(property: string): void;
95
100
  /***
96
101
  *** TRACKING
97
102
  ***/
@@ -112,9 +117,19 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
112
117
  /***
113
118
  * PROPERTIES
114
119
  ***/
120
+ setPersonPropertiesForFlags(properties: {
121
+ [type: string]: string;
122
+ }): this;
123
+ resetPersonPropertiesForFlags(): void;
124
+ /** @deprecated - Renamed to setPersonPropertiesForFlags */
115
125
  personProperties(properties: {
116
126
  [type: string]: string;
117
127
  }): this;
128
+ setGroupPropertiesForFlags(properties: {
129
+ [type: string]: Record<string, string>;
130
+ }): this;
131
+ resetGroupPropertiesForFlags(): void;
132
+ /** @deprecated - Renamed to setGroupPropertiesForFlags */
118
133
  groupProperties(properties: {
119
134
  [type: string]: Record<string, string>;
120
135
  }): this;
@@ -30,7 +30,9 @@ export declare enum PostHogPersistedProperty {
30
30
  SessionId = "session_id",
31
31
  SessionLastTimestamp = "session_timestamp",
32
32
  PersonProperties = "person_properties",
33
- GroupProperties = "group_properties"
33
+ GroupProperties = "group_properties",
34
+ InstalledAppBuild = "installed_app_build",
35
+ InstalledAppVersion = "installed_app_version"
34
36
  }
35
37
  export declare type PostHogFetchOptions = {
36
38
  method: 'GET' | 'POST' | 'PUT' | 'PATCH';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-js-lite",
3
- "version": "2.3.0",
3
+ "version": "2.5.0",
4
4
  "main": "lib/index.cjs.js",
5
5
  "module": "lib/index.esm.js",
6
6
  "types": "lib/index.d.ts",
@@ -21,6 +21,10 @@ export class PostHog extends PostHogCore {
21
21
  this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`
22
22
  this._storage = getStorage(options?.persistence || 'localStorage', window)
23
23
  this.setupBootstrap(options)
24
+
25
+ if (options?.preloadFeatureFlags !== false) {
26
+ this.reloadFeatureFlags()
27
+ }
24
28
  }
25
29
 
26
30
  getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {
@@ -6,27 +6,89 @@ import { PostHog } from '..'
6
6
 
7
7
  describe('PosthogWeb', () => {
8
8
  let fetch: jest.Mock
9
- jest.useFakeTimers()
9
+ jest.useRealTimers()
10
10
 
11
11
  beforeEach(() => {
12
- ;(global as any).window.fetch = fetch = jest.fn(() =>
13
- Promise.resolve({
12
+ ;(global as any).window.fetch = fetch = jest.fn(async (url) => {
13
+ let res: any = { status: 'ok' }
14
+ if (url.includes('decide')) {
15
+ res = {
16
+ featureFlags: {
17
+ 'feature-1': true,
18
+ 'feature-2': true,
19
+ 'json-payload': true,
20
+ 'feature-variant': 'variant',
21
+ },
22
+ // NOTE: Aren't these supposed to be strings?
23
+
24
+ featureFlagPayloads: {
25
+ 'feature-1': {
26
+ color: 'blue',
27
+ },
28
+ 'json-payload': {
29
+ a: 'payload',
30
+ },
31
+ 'feature-variant': 5,
32
+ },
33
+ }
34
+ }
35
+
36
+ return {
14
37
  status: 200,
15
- json: () => Promise.resolve({ status: 'ok' }),
16
- })
17
- ) as any
38
+ json: () => Promise.resolve(res),
39
+ }
40
+ })
18
41
  })
19
42
 
20
43
  describe('init', () => {
21
44
  it('should initialise', () => {
22
- const postHog = new PostHog('TEST_API_KEY', {
45
+ const posthog = new PostHog('TEST_API_KEY', {
46
+ flushAt: 1,
47
+ })
48
+ expect(posthog.optedOut).toEqual(false)
49
+
50
+ posthog.capture('test')
51
+
52
+ expect(fetch).toHaveBeenCalledTimes(2)
53
+ })
54
+
55
+ it('should load feature flags on init', async () => {
56
+ const posthog = new PostHog('TEST_API_KEY', {
23
57
  flushAt: 1,
24
58
  })
25
- expect(postHog.optedOut).toEqual(false)
26
59
 
27
- postHog.capture('test')
60
+ expect(fetch).toHaveBeenCalledWith('https://app.posthog.com/decide/?v=3', {
61
+ body: JSON.stringify({
62
+ token: 'TEST_API_KEY',
63
+ distinct_id: posthog.getDistinctId(),
64
+ groups: {},
65
+ person_properties: {},
66
+ group_properties: {},
67
+ $anon_distinct_id: posthog.getAnonymousId(),
68
+ }),
69
+ method: 'POST',
70
+ headers: {
71
+ 'Content-Type': 'application/json',
72
+ },
73
+ signal: expect.anything(),
74
+ })
28
75
 
29
- expect(fetch).toHaveBeenCalledTimes(1)
76
+ expect(posthog.getFeatureFlags()).toEqual({
77
+ 'feature-1': true,
78
+ 'feature-2': true,
79
+ 'json-payload': true,
80
+ 'feature-variant': 'variant',
81
+ })
82
+
83
+ expect(posthog.getFeatureFlagPayloads()).toEqual({
84
+ 'feature-1': {
85
+ color: 'blue',
86
+ },
87
+ 'json-payload': {
88
+ a: 'payload',
89
+ },
90
+ 'feature-variant': 5,
91
+ })
30
92
  })
31
93
  })
32
94
  })