Skip to content

Commit

Permalink
Reset Survey storage after posthog.reset is called
Browse files Browse the repository at this point in the history
  • Loading branch information
Phanatic committed Oct 23, 2024
1 parent 144d9f4 commit 47bd7ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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
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
14 changes: 12 additions & 2 deletions src/posthog-surveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ export class PostHogSurveys {
this.loadIfEnabled()
}

reset(): void {
localStorage.removeItem('lastSeenSurveyDate')
const surveyKeys = []
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i)
if (key?.startsWith('seenSurvey_')) {
surveyKeys.push(key)
}
}
surveyKeys.forEach((key) => localStorage.removeItem(key))
}

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

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

this._surveyManager.canRenderSurvey(survey)
})
}
Expand All @@ -299,7 +310,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

0 comments on commit 47bd7ae

Please sign in to comment.