Skip to content

Commit

Permalink
feat(surveys): Make selector targeting work, add user props (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar authored Oct 26, 2023
1 parent d6b2c90 commit 10138ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/__tests__/extensions/surveys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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', () => {
Expand Down
19 changes: 10 additions & 9 deletions src/extensions/surveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -457,6 +460,9 @@ export const addCancelListeners = (
$survey_name: surveyEventName,
$survey_id: surveyId,
sessionRecordingUrl: posthog.get_session_replay_url?.(),
$set: {
[`$survey_dismiss/${surveyId}`]: true,
},
})
})
}
Expand Down Expand Up @@ -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)
}

0 comments on commit 10138ef

Please sign in to comment.