Skip to content

Commit

Permalink
Don't use session storage in memory mode
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Nov 12, 2024
1 parent ec5e822 commit ec6bc53
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
44 changes: 44 additions & 0 deletions src/__tests__/posthog-persistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { PostHogPersistence } from '../posthog-persistence'
import { SESSION_ID, USER_STATE } from '../constants'
import { PostHogConfig } from '../types'
import Mock = jest.Mock
import { PostHog } from '../posthog-core'
import { window } from '../utils/globals'
import { uuidv7 } from '../uuidv7'

let referrer = '' // No referrer by default
Object.defineProperty(document, 'referrer', { get: () => referrer })
Expand Down Expand Up @@ -203,4 +206,45 @@ describe('persistence', () => {
expect(lib.properties()).toEqual(expectedProps())
})
})

describe('posthog', () => {
it('should not store anything in localstorage, or cookies when in sessionStorage mode', () => {
const token = uuidv7()
const persistenceKey = `ph_${token}_posthog`
const posthog = new PostHog().init(token, {
persistence: 'sessionStorage',
})
posthog.register({ distinct_id: 'test', test_prop: 'test_val' })
posthog.capture('test_event')
expect(window.localStorage.getItem(persistenceKey)).toEqual(null)
expect(document.cookie).toEqual('')
expect(window.sessionStorage.getItem(persistenceKey)).toBeTruthy()
})

it('should not store anything in localstorage, sessionstorage, or cookies when in memory mode', () => {
const token = uuidv7()
const persistenceKey = `ph_${token}_posthog`
const posthog = new PostHog().init(token, {
persistence: 'memory',
})
posthog.register({ distinct_id: 'test', test_prop: 'test_val' })
posthog.capture('test_event')
expect(window.localStorage.getItem(persistenceKey)).toEqual(null)
expect(window.sessionStorage.getItem(persistenceKey)).toEqual(null)
expect(document.cookie).toEqual('')
})

it('should not store anything in cookies when in localstorage mode', () => {
const token = uuidv7()
const persistenceKey = `ph_${token}_posthog`
const posthog = new PostHog().init(token, {
persistence: 'localStorage',
})
posthog.register({ distinct_id: 'test', test_prop: 'test_val' })
posthog.capture('test_event')
expect(window.localStorage.getItem(persistenceKey)).toBeTruthy()
expect(window.sessionStorage.getItem(persistenceKey)).toBeTruthy()
expect(document.cookie).toEqual('')
})
})
})
4 changes: 2 additions & 2 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export class PostHog {

this.persistence = new PostHogPersistence(this.config)
this.sessionPersistence =
this.config.persistence === 'sessionStorage'
this.config.persistence === 'sessionStorage' || this.config.persistence === 'memory'
? this.persistence
: new PostHogPersistence({ ...this.config, persistence: 'sessionStorage' })

Expand Down Expand Up @@ -1786,7 +1786,7 @@ export class PostHog {

this.persistence?.update_config(this.config, oldConfig)
this.sessionPersistence =
this.config.persistence === 'sessionStorage'
this.config.persistence === 'sessionStorage' || this.config.persistence === 'memory'
? this.persistence
: new PostHogPersistence({ ...this.config, persistence: 'sessionStorage' })

Expand Down

0 comments on commit ec6bc53

Please sign in to comment.