: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(surveys): dedupe open-choice responses in survey results (#64832)
Ben Lea92c79609e404
diff --git a/frontend/src/scenes/surveys/surveyLogic.test.ts b/frontend/src/scenes/surveys/surveyLogic.test.tsindex 38e28f8abc37..c3e9b9913e0f 100644--- a/frontend/src/scenes/surveys/surveyLogic.test.ts+++ b/frontend/src/scenes/surveys/surveyLogic.test.ts@@ -2411,11 +2411,14 @@ describe('mergeResponsesByQuestion', () => {expect(result.totalResponses).toBe(100)})- it('merges choice question with open choice: appends "Other" data to aggregate data', () => {+ it('merges choice question with open choice without duplicating open-choice answers', () => {const aggregate: ResponsesByQuestion = {'choice-q': {type: SurveyQuestionType.SingleChoice,- data: [{ label: 'Yes', value: 5, isPredefined: true }],+ data: [+ { label: 'Yes', value: 5, isPredefined: true },+ { label: 'Custom answer', value: 1, isPredefined: false },+ ],totalResponses: 6,noResponseCount: 0,},@@ -2434,6 +2437,60 @@ describe('mergeResponsesByQuestion', () => {expect(result.data).toHaveLength(2)expect(result.totalResponses).toBe(6)++ const customAnswers = result.data.filter((d) => d.label === 'Custom answer')+ expect(customAnswers).toHaveLength(1)+ expect(customAnswers[0]).toEqual({+ label: 'Custom answer',+ value: 1,+ isPredefined: false,+ distinctId: 'u1',+ timestamp: 'ts',+ })+ expect(result.data.find((d) => d.label === 'Yes')).toEqual({ label: 'Yes', value: 5, isPredefined: true })+ })++ it('keeps each open-choice answer exactly once while preserving predefined choices', () => {+ const aggregate: ResponsesByQuestion = {+ 'choice-q': {+ type: SurveyQuestionType.SingleChoice,+ data: [+ { label: 'Yes', value: 3, isPredefined: true },+ { label: 'No', value: 0, isPredefined: true },+ { label: 'Airtable', value: 1, isPredefined: false },+ { label: 'folk', value: 1, isPredefined: false },+ { label: 'Freshsales', value: 1, isPredefined: false },+ ],+ totalResponses: 6,+ noResponseCount: 0,+ },+ }+ const openEnded: ResponsesByQuestion = {+ 'choice-q': {+ type: SurveyQuestionType.SingleChoice,+ data: [+ { label: 'Airtable', value: 1, isPredefined: false, distinctId: 'u1', timestamp: 't1' },+ { label: 'folk', value: 1, isPredefined: false, distinctId: 'u2', timestamp: 't2' },+ { label: 'Freshsales', value: 1, isPredefined: false, distinctId: 'u3', timestamp: 't3' },+ ],+ totalResponses: 0,+ noResponseCount: 0,+ },+ }++ const merged = mergeResponsesByQuestion(aggregate, openEnded)+ const result = merged['choice-q'] as ChoiceQuestionProcessedResponses++ const nonPredefined = result.data.filter((d) => !d.isPredefined)+ expect(nonPredefined).toHaveLength(3)+ expect(nonPredefined.every((d) => d.distinctId && d.timestamp)).toBe(true)+ expect(nonPredefined.map((d) => d.label).sort()).toEqual(['Airtable', 'Freshsales', 'folk'])++ const predefined = result.data.filter((d) => d.isPredefined)+ expect(predefined).toEqual([+ { label: 'Yes', value: 3, isPredefined: true },+ { label: 'No', value: 0, isPredefined: true },+ ])})it('passes through aggregate-only questions unchanged', () => {diff --git a/frontend/src/scenes/surveys/surveyLogic.tsx b/frontend/src/scenes/surveys/surveyLogic.tsxindex b2c711a6c6f1..12fca03e8661 100644--- a/frontend/src/scenes/surveys/surveyLogic.tsx+++ b/frontend/src/scenes/surveys/surveyLogic.tsx@@ -599,7 +599,8 @@ export function mergeResponsesByQuestion(merged[qid] = { ...openData, totalResponses: agg.totalResponses }} else {const aggChoice = agg as ChoiceQuestionProcessedResponses- merged[qid] = { ...aggChoice, data: [...aggChoice.data, ...openData.data] }+ const predefinedFromAggregate = aggChoice.data.filter((d) => d.isPredefined)+ merged[qid] = { ...aggChoice, data: [...predefinedFromAggregate, ...openData.data] }}}return merged