PostHog / posthog-js-lite

Reimplementation of posthog-js to be as light and modular as possible.
https://posthog.com/docs/libraries
MIT License
70 stars 36 forks source link

Properties file storage #81

Closed davidthor closed 11 months ago

davidthor commented 1 year ago

Problem

Command line interfaces, and other processes that terminate after each call, don't persist the properties in a way that simulates the idea of a "session". We need a way for CLIs to use the same anonymous ID with each call so we can sanely correlate the ID with a distinct ID at a later time when they run a login command.

Changes

Introduces the class, PostHogFsStorage into posthog-core and updated posthog-node to use the persistence option to toggle between in-memory storage and filesystem storage of properties like the anonymous_id.

Release info Sub-libraries affected

Bump level

Libraries affected

Changelog notes

davidthor commented 1 year ago

This is great, thanks for the contribution! 🚀

Would love to see some tests, like here: https://github.com/PostHog/posthog-js-lite/blob/master/posthog-react-native/test/storage.spec.ts#L4

It doesn't look like there's currently a basis for testing storage/persistence in the node package (looks like you linked to the react-native package which this doesn't touch). Any suggestions or best practices for testing would be most appreciated.

neilkakkar commented 1 year ago

Yeah, I meant the exact same file, but for posthog-node (and FsStorage). Things should be similar enough, thanks to similar interfaces, that the file should be a good indicator of what to do for node :)

davidthor commented 1 year ago

I added a test suite for the file storage class, but I can't seem to run the full test suite locally. I get the following error on 17 of the different test files:

TypeError: Cannot assign to read only property 'performance' of object '[object global]'

      at hijackMethod (node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:946:32)
      at Object.install (node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1733:17)
      at FakeTimers.useFakeTimers (node_modules/@jest/fake-timers/build/modernFakeTimers.js:110:36)