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(surveys): Reset Survey storage after posthog.reset is called #1494

Merged
merged 2 commits into from
Oct 24, 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
10 changes: 10 additions & 0 deletions src/__tests__/surveys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ describe('surveys', () => {
expect(instance._send_request).toHaveBeenCalledTimes(1)
})

it('posthog.reset() removes surveys tracking properties from storage', () => {
localStorage.setItem('seenSurvey_XYZ', '1')
localStorage.setItem('seenSurvey_ABC', '1')
localStorage.setItem('lastSeenSurveyDate', 'some date here')
surveys.reset()
expect(localStorage.getItem('lastSeenSurveyDate')).toBeNull()
expect(localStorage.getItem('seenSurvey_XYZ')).toBeNull()
expect(localStorage.getItem('seenSurvey_ABC')).toBeNull()
})

it('getSurveys registers the survey event receiver if a survey has events', () => {
surveysResponse = { surveys: surveysWithEvents }
surveys.getSurveys((data) => {
Expand Down
18 changes: 15 additions & 3 deletions src/extensions/surveys/surveys-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { VNode, cloneElement, createContext } from 'preact'
// We cast the types here which is dangerous but protected by the top level generateSurveys call
const window = _window as Window & typeof globalThis
const document = _document as Document

const SurveySeenPrefix = 'seenSurvey_'
export const style = (appearance: SurveyAppearance | null) => {
const positions = {
left: 'left: 30px;',
Expand Down Expand Up @@ -648,14 +648,26 @@ export const getSurveySeen = (survey: Survey): boolean => {
}

export const getSurveySeenKey = (survey: Survey): string => {
let surveySeenKey = `seenSurvey_${survey.id}`
let surveySeenKey = `${SurveySeenPrefix}${survey.id}`
if (survey.current_iteration && survey.current_iteration > 0) {
surveySeenKey = `seenSurvey_${survey.id}_${survey.current_iteration}`
surveySeenKey = `${SurveySeenPrefix}${survey.id}_${survey.current_iteration}`
}

return surveySeenKey
}

export const getSurveySeenStorageKeys = (): string[] => {
const surveyKeys = []
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i)
if (key?.startsWith(SurveySeenPrefix)) {
surveyKeys.push(key)
}
}

return surveyKeys
}

const getSurveyInteractionProperty = (survey: Survey, action: string): string => {
let surveyProperty = `$survey_${action}/${survey.id}`
if (survey.current_iteration && survey.current_iteration > 0) {
Expand Down
1 change: 1 addition & 0 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,7 @@ export class PostHog {
this.consent.reset()
this.persistence?.clear()
this.sessionPersistence?.clear()
this.surveys?.reset()
this.persistence?.set_property(USER_STATE, 'anonymous')
this.sessionManager?.resetSessionId()
const uuid = this.config.get_device_id(uuidv7())
Expand Down
9 changes: 7 additions & 2 deletions src/posthog-surveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { assignableWindow, document, window } from './utils/globals'
import { DecideResponse } from './types'
import { logger } from './utils/logger'
import { isNullish } from './utils/type-utils'
import { getSurveySeenStorageKeys } from './extensions/surveys/surveys-utils'

const LOGGER_PREFIX = '[Surveys]'

Expand Down Expand Up @@ -73,6 +74,12 @@ export class PostHogSurveys {
this.loadIfEnabled()
}

reset(): void {
localStorage.removeItem('lastSeenSurveyDate')
const surveyKeys = getSurveySeenStorageKeys()
surveyKeys.forEach((key) => localStorage.removeItem(key))
}

loadIfEnabled() {
const surveysGenerator = assignableWindow?.__PosthogExtensions__?.generateSurveys

Expand Down Expand Up @@ -287,7 +294,6 @@ export class PostHogSurveys {
}
this.getSurveys((surveys) => {
const survey = surveys.filter((x) => x.id === surveyId)[0]

this._surveyManager.canRenderSurvey(survey)
})
}
Expand All @@ -299,7 +305,6 @@ export class PostHogSurveys {
}
this.getSurveys((surveys) => {
const survey = surveys.filter((x) => x.id === surveyId)[0]

this._surveyManager.renderSurvey(survey, document?.querySelector(selector))
})
}
Expand Down
Loading