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

feat(Surveys): Enable partial survey responses #1597

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions playground/nextjs/src/posthog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ if (typeof window !== 'undefined') {
session_recording: {
recordCrossOriginIframes: true,
},
disable_survey_partial_response: false,
debug: true,
disable_web_experiments: false,
scroll_root_selector: ['#scroll_element', 'html'],
Expand Down
19 changes: 0 additions & 19 deletions playground/segment/server.js

This file was deleted.

36 changes: 28 additions & 8 deletions src/extensions/surveys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@
removeSurveyFromFocus: (id: string) => void
isPopup?: boolean
}) {
const surveyResponseInsertID = useMemo(
() => survey.id + Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),
[survey.id]
)

const isPreviewMode = Number.isInteger(previewPageIndex)
// NB: The client-side code passes the millisecondDelay in seconds, but setTimeout expects milliseconds, so we multiply by 1000
const surveyPopupDelayMilliseconds = survey.appearance?.surveyPopupDelaySeconds
Expand Down Expand Up @@ -494,6 +499,7 @@
forceDisableHtml={!!forceDisableHtml}
posthog={posthog}
styleOverrides={style}
surveyResponseInsertID={surveyResponseInsertID}
/>
) : (
<ConfirmationMessage
Expand Down Expand Up @@ -521,6 +527,7 @@
survey: Survey
forceDisableHtml: boolean
posthog?: PostHog
surveyResponseInsertID?: string
styleOverrides?: React.CSSProperties
}) {
const textColor = getContrastingTextColor(
Expand Down Expand Up @@ -557,18 +564,31 @@
// Old SDK, no branching
if (!posthog.getNextSurveyStep) {
const isLastDisplayedQuestion = displayQuestionIndex === survey.questions.length - 1
if (isLastDisplayedQuestion) {
sendSurveyEvent({ ...questionsResponses, [responseKey]: res }, survey, posthog)
} else {
setCurrentQuestionIndex(displayQuestionIndex + 1)
}
logger.info(
`POSTHOG SURVEYS: Sending survey response with id : ${surveyResponseInsertID} `,

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, 4.7.2)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Unit tests

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, 5.5.4)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, latest)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Safari

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Firefox

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Build and check ES5/ES6 support

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test with React

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Chrome

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on IE11

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Lint

Cannot find name 'surveyResponseInsertID'.

Check failure on line 568 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Cypress

Cannot find name 'surveyResponseInsertID'.
questionsResponses
)
sendSurveyEvent(
{ ...questionsResponses, [responseKey]: res },
survey,
posthog,
isLastDisplayedQuestion,
surveyResponseInsertID

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, 4.7.2)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Unit tests

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, 5.5.4)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, latest)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Safari

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Firefox

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Build and check ES5/ES6 support

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test with React

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Chrome

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on IE11

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Lint

Cannot find name 'surveyResponseInsertID'.

Check failure on line 576 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Cypress

Cannot find name 'surveyResponseInsertID'.
)
setCurrentQuestionIndex(displayQuestionIndex + 1)
return
}

const nextStep = posthog.getNextSurveyStep(survey, displayQuestionIndex, res)
if (nextStep === SurveyQuestionBranchingType.End) {
sendSurveyEvent({ ...questionsResponses, [responseKey]: res }, survey, posthog)
} else {
logger.info(`POSTHOG SURVEYS: Sending survey response with id : ${surveyResponseInsertID}`, questionsResponses)

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, 4.7.2)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Unit tests

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, 5.5.4)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, latest)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Safari

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Firefox

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Build and check ES5/ES6 support

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test with React

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Chrome

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on IE11

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Lint

Cannot find name 'surveyResponseInsertID'.

Check failure on line 583 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Cypress

Cannot find name 'surveyResponseInsertID'.
sendSurveyEvent(
{ ...questionsResponses, [responseKey]: res },
survey,
posthog,
nextStep === SurveyQuestionBranchingType.End,
surveyResponseInsertID

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, 4.7.2)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Unit tests

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, 5.5.4)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Check minimum TS version (18, latest)

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Safari

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Firefox

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Build and check ES5/ES6 support

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test with React

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on Chrome

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Test on IE11

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Lint

Cannot find name 'surveyResponseInsertID'.

Check failure on line 589 in src/extensions/surveys.tsx

View workflow job for this annotation

GitHub Actions / Cypress

Cannot find name 'surveyResponseInsertID'.
)
if (nextStep !== SurveyQuestionBranchingType.End) {
setCurrentQuestionIndex(nextStep)
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/extensions/surveys/surveys-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -532,14 +532,24 @@ export const createShadow = (styleSheet: string, surveyId: string, element?: Ele
export const sendSurveyEvent = (
responses: Record<string, string | number | string[] | null> = {},
survey: Survey,
posthog?: PostHog
posthog?: PostHog,
surveyCompleted?: boolean,
surveyResponseInsertID?: string
) => {
if (!posthog) return

if (!surveyCompleted && posthog.config.disable_survey_partial_response) {
// do not send partial responses
return
}

localStorage.setItem(getSurveySeenKey(survey), 'true')

posthog.capture('survey sent', {
$survey_name: survey.name,
$survey_completed: surveyCompleted || false,
$survey_id: survey.id,
$survey_response_id: surveyResponseInsertID,
$survey_iteration: survey.current_iteration,
$survey_iteration_start_date: survey.current_iteration_start_date,
$survey_questions: survey.questions.map((question) => question.question),
Expand All @@ -549,7 +559,10 @@ export const sendSurveyEvent = (
[getSurveyInteractionProperty(survey, 'responded')]: true,
},
})
window.dispatchEvent(new Event('PHSurveySent'))

if (surveyCompleted) {
window.dispatchEvent(new Event('PHSurveySent'))
}
}

export const dismissedSurveyEvent = (survey: Survey, posthog?: PostHog, readOnly?: boolean) => {
Expand Down
1 change: 1 addition & 0 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const defaultConfig = (): PostHogConfig => ({
disable_persistence: false,
disable_web_experiments: true, // disabled in beta.
disable_surveys: false,
disable_survey_partial_response: true,
enable_recording_console_log: undefined, // When undefined, it falls back to the server-side setting
secure_cookie: window?.location?.protocol === 'https:',
ip: true,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export interface PostHogConfig {
/** @deprecated - use `disable_persistence` instead */
disable_cookie?: boolean
disable_surveys: boolean
disable_survey_partial_response: boolean
disable_web_experiments: boolean
/** If set, posthog-js will never load external scripts such as those needed for Session Replay or Surveys. */
disable_external_dependency_loading?: boolean
Expand Down
Loading