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 7b44996
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 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
20 changes: 17 additions & 3 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 @@ -288,7 +300,7 @@ export class PostHogSurveys {
this.getSurveys((surveys) => {
const survey = surveys.filter((x) => x.id === surveyId)[0]

this._surveyManager.canRenderSurvey(survey)
this._surveyManager?.canRenderSurvey(survey)
})
}

Expand All @@ -299,8 +311,10 @@ export class PostHogSurveys {
}
this.getSurveys((surveys) => {
const survey = surveys.filter((x) => x.id === surveyId)[0]

this._surveyManager.renderSurvey(survey, document?.querySelector(selector))
const element = document?.querySelector(selector)
if (element) {
this._surveyManager?.renderSurvey(survey, element)
}
})
}
}

0 comments on commit 7b44996

Please sign in to comment.