Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't use session storage in memory mode #1521

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading