Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(surveys): notify on API survey responses without a completion flag (#83579)

Daniel Marchuk30370b7ce933
diff --git a/frontend/src/scenes/surveys/surveyNotificationModalLogic.test.ts b/frontend/src/scenes/surveys/surveyNotificationModalLogic.test.ts
index 4801123734d0..2409c897d0c3 100644
--- a/frontend/src/scenes/surveys/surveyNotificationModalLogic.test.ts
+++ b/frontend/src/scenes/surveys/surveyNotificationModalLogic.test.ts
@@ -10,6 +10,7 @@ import {
SurveyEventName,
SurveyEventProperties,
SurveyQuestionType,
+ SurveyType,
} from '~/types'
import {
@@ -56,6 +57,7 @@ describe('surveyNotificationModalLogic', () => {
remapSurveyResponseProperties(copiedInputs, {
id: 'target-survey',
name: 'Target survey',
+ type: SurveyType.Popover,
enable_partial_responses: true,
questions: [
{ id: 'target-a', question: 'First?', type: SurveyQuestionType.Open },
@@ -223,6 +225,7 @@ describe('surveyNotificationModalLogic', () => {
remapSurveyResponseProperties(copiedInputs, {
id: 'target-survey',
name: 'Target survey',
+ type: SurveyType.Popover,
enable_partial_responses: true,
questions: [{ id: 'target-a', question: 'First?', type: SurveyQuestionType.Open }],
})
diff --git a/frontend/src/scenes/surveys/surveyNotificationModalLogic.ts b/frontend/src/scenes/surveys/surveyNotificationModalLogic.ts
index 3a37f06569c0..db545225968a 100644
--- a/frontend/src/scenes/surveys/surveyNotificationModalLogic.ts
+++ b/frontend/src/scenes/surveys/surveyNotificationModalLogic.ts
@@ -26,6 +26,7 @@ import {
buildSurveyExampleInvocationGlobals,
getSurveyNotificationFilters,
getSurveyIdBasedResponseKey,
+ surveyEmitsPartialSentEvents,
} from 'scenes/surveys/utils'
import { urls } from 'scenes/urls'
@@ -112,7 +113,7 @@ export type OpenSurveyNotificationDialogPayload = {
notification?: HogFunctionType | null
intent?: SurveyNotificationModalIntent
}
-type SurveyNotificationContext = Pick<Survey, 'id' | 'name' | 'questions' | 'enable_partial_responses'>
+type SurveyNotificationContext = Pick<Survey, 'id' | 'name' | 'questions' | 'enable_partial_responses' | 'type'>
type SurveyNotificationFormErrors = Partial<Record<Exclude<keyof SurveyNotificationForm, 'responseFilters'>, string>>
const MAX_EXAMPLE_QUESTIONS = 3
@@ -450,7 +451,7 @@ async function buildSurveyNotificationPayload({
template,
destination: form.destination,
surveyId: survey.id,
- enablePartialResponses: survey.enable_partial_responses ?? false,
+ emitsPartialSentEvents: surveyEmitsPartialSentEvents(survey),
form,
})
}
@@ -470,7 +471,7 @@ async function buildSurveyNotificationPayload({
destination: form.destination,
surveyName: survey.name,
surveyId: survey.id,
- enablePartialResponses: survey.enable_partial_responses ?? false,
+ emitsPartialSentEvents: surveyEmitsPartialSentEvents(survey),
form,
})
}
@@ -704,14 +705,14 @@ function createSurveyNotificationPayload({
destination,
surveyName,
surveyId,
- enablePartialResponses,
+ emitsPartialSentEvents,
form,
}: {
template: HogFunctionTemplateType
destination: DestinationKey
surveyName?: string | null
surveyId: string
- enablePartialResponses: boolean
+ emitsPartialSentEvents: boolean
form: SurveyNotificationForm
}): Partial<HogFunctionType> {
const destinationOption = DESTINATION_OPTIONS.find((option) => option.value === destination)
@@ -771,7 +772,7 @@ function createSurveyNotificationPayload({
inputs_schema: template.inputs_schema,
filters: getSurveyNotificationFilters(
surveyId,
- enablePartialResponses,
+ emitsPartialSentEvents,
buildResponseFilterProperties(form.responseFilters)
),
hog: template.code,
@@ -847,14 +848,14 @@ function updateSurveyNotificationPayload({
template,
destination,
surveyId,
- enablePartialResponses,
+ emitsPartialSentEvents,
form,
}: {
notification: HogFunctionType
template: HogFunctionTemplateType
destination: DestinationKey
surveyId: string
- enablePartialResponses: boolean
+ emitsPartialSentEvents: boolean
form: SurveyNotificationForm
}): Partial<HogFunctionType> {
const payload = createSurveyNotificationPayload({
@@ -862,7 +863,7 @@ function updateSurveyNotificationPayload({
destination,
surveyName: null,
surveyId,
- enablePartialResponses,
+ emitsPartialSentEvents,
form,
})
@@ -908,7 +909,7 @@ function createCopiedSurveyNotificationPayload({
destination,
surveyName: survey.name,
surveyId: survey.id,
- enablePartialResponses: survey.enable_partial_responses ?? false,
+ emitsPartialSentEvents: surveyEmitsPartialSentEvents(survey),
form,
})
@@ -931,7 +932,7 @@ function createCopiedSurveyNotificationPayload({
masking: notification.masking,
filters: getSurveyNotificationFilters(
survey.id,
- survey.enable_partial_responses ?? false,
+ surveyEmitsPartialSentEvents(survey),
buildResponseFilterProperties(form.responseFilters)
),
hog: remapSurveyResponseProperties(notification.hog, survey) ?? template.code,
diff --git a/frontend/src/scenes/surveys/utils.test.ts b/frontend/src/scenes/surveys/utils.test.ts
index 50b176b1abeb..d28ceea0ccc7 100644
--- a/frontend/src/scenes/surveys/utils.test.ts
+++ b/frontend/src/scenes/surveys/utils.test.ts
@@ -41,6 +41,7 @@ import {
sanitizeSurveyAppearance,
sanitizeSurveyDisplayConditions,
splitChoicesOnPaste,
+ surveyEmitsPartialSentEvents,
validateCSSProperty,
validateSurveyAppearance,
} from './utils'
@@ -245,6 +246,22 @@ describe('survey utils', () => {
})
})
+ // An API survey's `survey sent` events come from the integrator's own code, which has no reason
+ // to set `$survey_completed` — so requiring it left the notification silently matching nothing.
+ describe('surveyEmitsPartialSentEvents', () => {
+ it.each([
+ [SurveyType.Popover, true, true],
+ [SurveyType.Popover, false, false],
+ [SurveyType.Widget, true, true],
+ [SurveyType.API, true, false],
+ [SurveyType.API, false, false],
+ ])('%s with partial responses %s', (type, enablePartialResponses, expected) => {
+ expect(surveyEmitsPartialSentEvents({ type, enable_partial_responses: enablePartialResponses })).toBe(
+ expected
+ )
+ })
+ })
+
describe('buildSurveyExampleInvocationGlobals', () => {
it('builds a survey sent example payload with question response properties', () => {
const globals = buildSurveyExampleInvocationGlobals({
diff --git a/frontend/src/scenes/surveys/utils.ts b/frontend/src/scenes/surveys/utils.ts
index 9aca95ac876f..134d75062bdb 100644
--- a/frontend/src/scenes/surveys/utils.ts
+++ b/frontend/src/scenes/surveys/utils.ts
@@ -1241,15 +1241,29 @@ export function getSurveyDisplayConditionsSummary(survey: Survey | NewSurvey): S
}
/**
- * With partial responses off, posthog-js has no partial submission to distinguish a complete one
- * from, so it never sets `$survey_completed` and requiring `= true` matches nothing. Accept the
- * property being absent as completed too, the same way the response summary counts them
+ * True when posthog-js emits an intermediate `survey sent` event per answered question, sharing one
+ * `$survey_submission_id`, with only the last carrying `$survey_completed: true`. Requiring the
+ * property to be `true` is what keeps a notification from firing once per question, so it is only
+ * worth requiring here.
+ *
+ * An API survey has no posthog-js rendering it. The integrator sends one event per submission from
+ * their own code and marks a partial one with an explicit `$survey_completed: false`, the way
+ * posthog-js does, so absent means completed there whatever `enable_partial_responses` says.
+ */
+export function surveyEmitsPartialSentEvents(survey: Pick<Survey, 'type' | 'enable_partial_responses'>): boolean {
+ return (survey.enable_partial_responses ?? false) && survey.type !== SurveyType.API
+}
+
+/**
+ * Without intermediate partial events, posthog-js has no partial submission to distinguish a
+ * complete one from, so it never sets `$survey_completed` and requiring `= true` matches nothing.
+ * Accept the property being absent as completed too, the same way the response summary counts them
* (`enable_partial_responses` branch in `ee/surveys/summaries/headline_summary.py`). An explicit
* `false` stays excluded: a survey switched from partial to non-partial keeps its old partials.
*/
export function getSurveyNotificationFilters(
surveyId: string,
- enablePartialResponses: boolean,
+ emitsPartialSentEvents: boolean,
extraSentEventProperties: EventPropertyFilter[] = []
): CyclotronJobFiltersType {
const surveyIdProperty: EventPropertyFilter = {
@@ -1288,7 +1302,7 @@ export function getSurveyNotificationFilters(
type: 'events',
properties: sentEventProperties,
},
- ...(enablePartialResponses
+ ...(emitsPartialSentEvents
? []
: [
{