diff --git a/src/__tests__/extensions/surveys.js b/src/__tests__/extensions/surveys.js index fc80bfe5b..bba4043c0 100644 --- a/src/__tests__/extensions/surveys.js +++ b/src/__tests__/extensions/surveys.js @@ -59,6 +59,9 @@ describe('survey display logic', () => { $survey_id: 'testSurvey1', $survey_name: 'Test survey 1', sessionRecordingUrl: undefined, + $set: { + '$survey_dismiss/testSurvey1': true, + }, }) expect(localStorage.getItem(`seenSurvey_${mockSurveys[0].id}`)).toBe('true') @@ -145,12 +148,16 @@ describe('survey display logic', () => { expect(mockPostHog.capture).toBeCalledTimes(1) }) - test('when url changes, callSurveys runs again', () => { + test('callSurveys runs on interval irrespective of url change', () => { jest.useFakeTimers() jest.spyOn(global, 'setInterval') generateSurveys(mockPostHog) expect(mockPostHog.getActiveMatchingSurveys).toBeCalledTimes(1) - expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 1500) + expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 3000) + + jest.advanceTimersByTime(3000) + expect(mockPostHog.getActiveMatchingSurveys).toBeCalledTimes(2) + expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 3000) }) test('multiple choice type question elements are unique', () => { diff --git a/src/extensions/surveys.ts b/src/extensions/surveys.ts index 0cfae82e0..540f6e1d1 100644 --- a/src/extensions/surveys.ts +++ b/src/extensions/surveys.ts @@ -383,6 +383,9 @@ export const createOpenTextOrLinkPopup = ( $survey_question: survey.questions[0].question, $survey_response: surveyQuestionType === 'open' ? e.target.survey.value : 'link clicked', sessionRecordingUrl: posthog.get_session_replay_url?.(), + $set: { + [`$survey_response/${survey.id}`]: true, + }, }) if (surveyQuestionType === 'link') { window.open(question.link || undefined) @@ -457,6 +460,9 @@ export const addCancelListeners = ( $survey_name: surveyEventName, $survey_id: surveyId, sessionRecordingUrl: posthog.get_session_replay_url?.(), + $set: { + [`$survey_dismiss/${surveyId}`]: true, + }, }) }) } @@ -888,13 +894,8 @@ function nextQuestion(currentQuestionIdx: number, surveyId: string) { export function generateSurveys(posthog: PostHog) { callSurveys(posthog, true) - let currentUrl = location.href - if (location.href) { - setInterval(() => { - if (location.href !== currentUrl) { - currentUrl = location.href - callSurveys(posthog, false) - } - }, 1500) - } + // recalculate surveys every 3 seconds to check if URL or selectors have changed + setInterval(() => { + callSurveys(posthog, false) + }, 3000) }