posthog-node 2.0.0-alpha3 → 2.0.0-alpha6

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.
@@ -7,12 +7,15 @@ import {
7
7
  PostHogFetchResponse,
8
8
  PostHogPersistedProperty,
9
9
  } from '../../posthog-core/src'
10
+ import { PostHogMemoryStorage } from '../../posthog-core/src/storage-memory'
10
11
  import { EventMessageV1, GroupIdentifyMessage, IdentifyMessageV1, PostHogNodeV1 } from './types'
11
12
 
12
- export type PostHogOptions = PosthogCoreOptions
13
+ export type PostHogOptions = PosthogCoreOptions & {
14
+ persistence?: 'memory'
15
+ }
13
16
 
14
17
  class PostHog extends PostHogCore {
15
- private _memoryStorage: { [key: string]: any | undefined } = {}
18
+ private _memoryStorage = new PostHogMemoryStorage()
16
19
 
17
20
  constructor(apiKey: string, options: PostHogOptions = {}) {
18
21
  options.captureMode = options?.captureMode || 'json'
@@ -22,11 +25,11 @@ class PostHog extends PostHogCore {
22
25
  }
23
26
 
24
27
  getPersistedProperty(key: PostHogPersistedProperty): any | undefined {
25
- return this._memoryStorage[key]
28
+ return this._memoryStorage.getProperty(key)
26
29
  }
27
30
 
28
31
  setPersistedProperty(key: PostHogPersistedProperty, value: any | null): void {
29
- this._memoryStorage[key] = value !== null ? value : undefined
32
+ return this._memoryStorage.setProperty(key, value)
30
33
  }
31
34
 
32
35
  getSessionId(): string | undefined {
@@ -59,10 +62,14 @@ export class PostHogGlobal implements PostHogNodeV1 {
59
62
  }
60
63
 
61
64
  private reInit(distinctId: string): void {
62
- // Remove all state except the queue
63
- const queue = this._sharedClient.getPersistedProperty(PostHogPersistedProperty.Queue)
64
- this._sharedClient.reset()
65
- this._sharedClient.setPersistedProperty(PostHogPersistedProperty.Queue, queue)
65
+ // Certain properties we want to persist
66
+ const propertiesToKeep = [PostHogPersistedProperty.Queue, PostHogPersistedProperty.OptedOut]
67
+
68
+ for (const key in PostHogPersistedProperty) {
69
+ if (!propertiesToKeep.includes(key as any)) {
70
+ this._sharedClient.setPersistedProperty((PostHogPersistedProperty as any)[key], null)
71
+ }
72
+ }
66
73
  this._sharedClient.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId)
67
74
  }
68
75