diff --git a/playground/nextjs/src/posthog.ts b/playground/nextjs/src/posthog.ts index 89de3feb1..22dde2fc8 100644 --- a/playground/nextjs/src/posthog.ts +++ b/playground/nextjs/src/posthog.ts @@ -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'], diff --git a/playground/segment/server.js b/playground/segment/server.js deleted file mode 100644 index 8c67259a1..000000000 --- a/playground/segment/server.js +++ /dev/null @@ -1,19 +0,0 @@ -const path = require('path') -const express = require('express') -const app = express() -const port = 3001 - -app.use('/static', express.static(__dirname + '/../../dist')) - -app.get('/segment.html', function (req, res) { - res.sendFile(__dirname + '/segment.html') -}) - -app.get('/static/recorder.js', function (req, res) { - let filePath = path.join(__dirname, '/../../node_modules/rrweb/dist/rrweb.js') - res.sendFile(filePath) -}) - -app.listen(port, () => { - console.log(`Example Segment app listening on port ${port}`) -}) diff --git a/src/extensions/surveys.tsx b/src/extensions/surveys.tsx index b1fec5638..b6de5ce52 100644 --- a/src/extensions/surveys.tsx +++ b/src/extensions/surveys.tsx @@ -457,6 +457,11 @@ export function SurveyPopup({ 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 @@ -494,6 +499,7 @@ export function SurveyPopup({ forceDisableHtml={!!forceDisableHtml} posthog={posthog} styleOverrides={style} + surveyResponseInsertID={surveyResponseInsertID} /> ) : ( = {}, 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), @@ -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) => { diff --git a/src/posthog-core.ts b/src/posthog-core.ts index a310f84e5..4727782f6 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -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, diff --git a/src/types.ts b/src/types.ts index 303b501b1..ccaa9d926 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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