: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
chore(surveys): share typed survey creation reporting (#107840)
Paul D'Ambra7a168249aa8c
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 46e60fe15531..4b24d131aa78 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -13421,7 +13421,7 @@ snapshots:scenes-app-oauth-authorize--default-scopes--dark:hash: v1.k794b7964.21a1602ada47b6704da18634c4c1386ba17b890500a833c52bd029cc98f69114.HOL3_R7J0JLMO_znhYmoxsZLaj0iqkemCyEIsK-HkA4scenes-app-oauth-authorize--default-scopes--light:- hash: v1.k794b7964.2c212df3cd88973bea95657e48b357c70a20772ecbe8ef37f57031a78186d32d.QOqtPFDfX_91zkoeXO487oseTb6yQpwPehH8tO4Dg78+ hash: v1.k794b7964.46a019ffd7b34638c5e9c2eee36427046bed7a23ad699255503d463a62998fc0.xcN6YfaXqD_mFjIa-71GtZzRmvZAe6uE-GJqKjPISVoscenes-app-oauth-authorize--every-scope-requested--dark:hash: v1.k794b7964.5b544e241fa57ad7971a6872fce18c2d3e9dbbcfd5c6b4e2a861cc60683a7eda.SymJ-itmx0Mon5EALqTxh99r_VbtywIv8gFXju-9wRsscenes-app-oauth-authorize--every-scope-requested--light:diff --git a/frontend/src/lib/utils/eventUsageLogic.ts b/frontend/src/lib/utils/eventUsageLogic.tsindex 4b99d649e4c3..867e63704033 100644--- a/frontend/src/lib/utils/eventUsageLogic.ts+++ b/frontend/src/lib/utils/eventUsageLogic.ts@@ -73,7 +73,6 @@ import {import { captureBIWorksheetAction } from 'products/business_intelligence/frontend/biEditorAnalytics'import { getExperimentStatus } from 'products/experiments/frontend/experimentStatus'-import { getSurveyConfigurationProperties } from 'products/surveys/frontend/surveyUsage'import type { ExperimentMetricUnion } from '../../queries/schema/schema-general'import type { FunnelCorrelationResultsType, Realm, UserType } from '../../types'@@ -1559,24 +1558,6 @@ export interface eventUsageLogicActions {survey: SurveytotalDurationMs: number}- reportSurveyCreated: (- survey: Survey,- isDuplicate?: boolean,- creationSource?: 'form_builder' | 'full_editor' | 'llm_analytics' | 'quick_create' | 'template' | 'wizard',- createdFrom?: SURVEY_CREATED_SOURCE- ) => {- createdFrom: SURVEY_CREATED_SOURCE | undefined- creationSource:- | 'form_builder'- | 'full_editor'- | 'llm_analytics'- | 'quick_create'- | 'template'- | 'wizard'- | undefined- isDuplicate: boolean | undefined- survey: Survey- }reportSurveyCycleDetected: (survey: NewSurvey | Survey) => {survey: NewSurvey | Survey}@@ -2042,12 +2023,6 @@ export const eventUsageLogic = kea<eventUsageLogicType>([reportFlagsCodeExampleLanguage: (language: string) => ({language,}),- reportSurveyCreated: (- survey: Survey,- isDuplicate?: boolean,- creationSource?: 'wizard' | 'full_editor' | 'quick_create' | 'template' | 'llm_analytics' | 'form_builder',- createdFrom?: SURVEY_CREATED_SOURCE- ) => ({ survey, isDuplicate, creationSource, createdFrom }),reportUserFeedbackButtonClicked: (source: SURVEY_CREATED_SOURCE, meta: Record<string, any>) => ({source,meta,@@ -2789,24 +2764,6 @@ export const eventUsageLogic = kea<eventUsageLogicType>([language,})},- reportSurveyCreated: ({ survey, isDuplicate, creationSource, createdFrom }) => {- posthog.capture('survey created', {- // The web app is the only place this event is emitted from — there is no backend- // equivalent — so stamping the surface here is what puts surveys in a `source`- // breakdown at all, rather than showing up as unattributed.- source: 'web',- name: survey.name,- id: survey.id,- survey_type: survey.type,- questions_length: survey.questions.length,- question_types: survey.questions.map((question) => question.type),- is_duplicate: isDuplicate ?? false,- creation_source: creationSource ?? 'full_editor',- created_from: createdFrom,- linked_insight_id: survey.linked_insight_id,- ...getSurveyConfigurationProperties(survey),- })- },reportSurveyTemplateClicked: ({ template, source }) => {posthog.capture('survey template clicked', {template,diff --git a/frontend/src/scenes/surveys/quick-create/quickSurveyFormLogic.ts b/frontend/src/scenes/surveys/quick-create/quickSurveyFormLogic.tsindex b842246a3e6a..835205c7c4e4 100644--- a/frontend/src/scenes/surveys/quick-create/quickSurveyFormLogic.ts+++ b/frontend/src/scenes/surveys/quick-create/quickSurveyFormLogic.ts@@ -8,7 +8,6 @@ import { lemonToast } from '@posthog/lemon-ui'import api from 'lib/api'import { SetupTaskId, globalSetupLogic } from 'lib/components/ProductSetup'import { dayjs } from 'lib/dayjs'-import { eventUsageLogic } from 'lib/utils/eventUsageLogic'import { addProductIntent } from 'lib/utils/product-intents'import { urls } from 'scenes/urls'@@ -23,6 +22,8 @@ import {SurveyType,} from '~/types'+import { reportSurveyCreated } from 'products/surveys/frontend/surveyUsage'+import type { SurveyAppearance, SurveyDisplayConditions } from '../../../types'import { NewSurvey, SURVEY_CREATED_SOURCE, SURVEY_RATING_SCALE, defaultSurveyAppearance } from '../constants'import { surveysLogic } from '../surveysLogic'@@ -262,7 +263,7 @@ export const quickSurveyFormLogic = kea<quickSurveyFormLogicType>([const response = await api.surveys.create(surveyData)- eventUsageLogic.actions.reportSurveyCreated(response, false, 'quick_create', props.source)+ reportSurveyCreated(response, false, 'quick_create', props.source)addProductIntent({product_type: ProductKey.SURVEYS,intent_context: ProductIntentContext.SURVEY_CREATED,diff --git a/frontend/src/scenes/surveys/surveyLogic.tsx b/frontend/src/scenes/surveys/surveyLogic.tsxindex cb49f88bebb1..d1be97f6a773 100644--- a/frontend/src/scenes/surveys/surveyLogic.tsx+++ b/frontend/src/scenes/surveys/surveyLogic.tsx@@ -98,7 +98,7 @@ import {} from '~/types'import { surveysGenerateTranslationsCreate } from 'products/surveys/frontend/generated/api'-import { reportSurveyEdited } from 'products/surveys/frontend/surveyUsage'+import { reportSurveyCreated, reportSurveyEdited } from 'products/surveys/frontend/surveyUsage'import type { ProductIntentProperties } from '../../lib/utils/product-intents'import type {@@ -816,31 +816,6 @@ export interface surveyLogicActions {survey: SurveytotalDurationMs: number} // eventUsageLogic- reportSurveyCreated: (- survey: Survey,- isDuplicate?: boolean | undefined,- creationSource?:- | 'form_builder'- | 'full_editor'- | 'llm_analytics'- | 'quick_create'- | 'template'- | 'wizard'- | undefined,- createdFrom?: SURVEY_CREATED_SOURCE | undefined- ) => {- createdFrom: SURVEY_CREATED_SOURCE | undefined- creationSource:- | 'form_builder'- | 'full_editor'- | 'llm_analytics'- | 'quick_create'- | 'template'- | 'wizard'- | undefined- isDuplicate: boolean | undefined- survey: Survey- } // eventUsageLogicreportSurveyCycleDetected: (survey: NewSurvey | Survey) => {survey: NewSurvey | Survey} // eventUsageLogic@@ -1540,7 +1515,7 @@ export const surveyLogic = kea<surveyLogicType>([surveysLogic,['loadSurveys'],eventUsageLogic,- ['reportSurveyCreated', 'reportSurveyCycleDetected', 'reportSurveyConsolidatedResultsQuery'],+ ['reportSurveyCycleDetected', 'reportSurveyConsolidatedResultsQuery'],teamLogic,['addProductIntent'],],@@ -2055,7 +2030,7 @@ export const surveyLogic = kea<surveyLogicType>([lemonToast.success(<>Survey {survey.name} created</>)actions.loadSurveys()router.actions.replace(urls.survey(survey.id))- actions.reportSurveyCreated(survey)+ reportSurveyCreated(survey)globalSetupLogic.findMounted()?.actions.markTaskAsCompleted(SetupTaskId.CreateSurvey)const surveyType = survey.type ? `${survey.type} ` : ''tryShowMCPHint('surveys.create', {diff --git a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.tsindex 2a2b0de1e89c..a52b0b360d7d 100644--- a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts+++ b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts@@ -28,7 +28,7 @@ import { urls } from 'scenes/urls'import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'import { Breadcrumb, LinkSurveyQuestion, Survey, SurveyQuestionType, SurveySchedule, SurveyType } from '~/types'-import { reportSurveyEdited } from 'products/surveys/frontend/surveyUsage'+import { reportSurveyCreated, reportSurveyEdited } from 'products/surveys/frontend/surveyUsage'import type { ProductIntentProperties } from '../../../lib/utils/product-intents'import type { TeamPublicType, TeamType } from '../../../types'@@ -103,31 +103,6 @@ export interface surveyWizardLogicValues {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface surveyWizardLogicActions {- reportSurveyCreated: (- survey: Survey,- isDuplicate?: boolean | undefined,- creationSource?:- | 'form_builder'- | 'full_editor'- | 'llm_analytics'- | 'quick_create'- | 'template'- | 'wizard'- | undefined,- createdFrom?: SURVEY_CREATED_SOURCE | undefined- ) => {- createdFrom: SURVEY_CREATED_SOURCE | undefined- creationSource:- | 'form_builder'- | 'full_editor'- | 'llm_analytics'- | 'quick_create'- | 'template'- | 'wizard'- | undefined- isDuplicate: boolean | undefined- survey: Survey- } // eventUsageLogicreportSurveyTemplateClicked: (template: SurveyTemplateType,source?: string | undefined@@ -237,7 +212,7 @@ export const surveyWizardLogic = kea<surveyWizardLogicType>([surveysLogic,['loadSurveys'],eventUsageLogic,- ['reportSurveyCreated', 'reportSurveyTemplateClicked'],+ ['reportSurveyTemplateClicked'],teamLogic,['addProductIntent'],],@@ -520,7 +495,7 @@ export const surveyWizardLogic = kea<surveyWizardLogicType>([launchSurveySuccess: ({ survey }) => {lemonToast.success(`Survey ${survey.name} created`)actions.loadSurveys()- actions.reportSurveyCreated(survey, false, 'wizard')+ reportSurveyCreated(survey, false, 'wizard')actions.addProductIntent({product_type: ProductKey.SURVEYS,intent_context: ProductIntentContext.SURVEY_CREATED,@@ -551,7 +526,7 @@ export const surveyWizardLogic = kea<surveyWizardLogicType>([saveDraftSuccess: ({ survey }) => {lemonToast.success(`Survey "${survey.name}" saved as draft`)actions.loadSurveys()- actions.reportSurveyCreated(survey, false, 'wizard')+ reportSurveyCreated(survey, false, 'wizard')actions.addProductIntent({product_type: ProductKey.SURVEYS,intent_context: ProductIntentContext.SURVEY_CREATED,diff --git a/products/ai_observability/frontend/feedback-view/wizard/feedbackSurveyWizardLogic.ts b/products/ai_observability/frontend/feedback-view/wizard/feedbackSurveyWizardLogic.tsindex fddf93d7fd92..434d7ca79cfd 100644--- a/products/ai_observability/frontend/feedback-view/wizard/feedbackSurveyWizardLogic.ts+++ b/products/ai_observability/frontend/feedback-view/wizard/feedbackSurveyWizardLogic.ts@@ -6,7 +6,6 @@ import { lemonToast } from '@posthog/lemon-ui'import api from 'lib/api'import { dayjs } from 'lib/dayjs'-import { eventUsageLogic } from 'lib/utils/eventUsageLogic'import { addProductIntent } from 'lib/utils/product-intents'import { SURVEY_CREATED_SOURCE, defaultSurveyAppearance } from 'scenes/surveys/constants'import { isThumbQuestion } from 'scenes/surveys/utils'@@ -16,6 +15,8 @@ import { urls } from 'scenes/urls'import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'import { Survey, SurveyAppearance, SurveyQuestionBranchingType, SurveyQuestionType, SurveyType } from '~/types'+import { reportSurveyCreated } from 'products/surveys/frontend/surveyUsage'+import type { TeamPublicType, TeamType } from '../../../../../frontend/src/types'export type WizardStep = 'intro' | 'configure' | 'implement'@@ -264,7 +265,7 @@ export const feedbackSurveyWizardLogic = kea<feedbackSurveyWizardLogicType>([enable_partial_responses: true,})- eventUsageLogic.actions.reportSurveyCreated(survey, false, 'llm_analytics')+ reportSurveyCreated(survey, false, 'llm_analytics')addProductIntent({product_type: ProductKey.SURVEYS,intent_context: ProductIntentContext.SURVEY_CREATED,diff --git a/products/surveys/frontend/surveyUsage.test.ts b/products/surveys/frontend/surveyUsage.test.tsindex e4e2857e31ed..acdea8283688 100644--- a/products/surveys/frontend/surveyUsage.test.ts+++ b/products/surveys/frontend/surveyUsage.test.ts@@ -1,10 +1,10 @@import posthog from 'posthog-js'-import { Survey, SurveyQuestionType } from '~/types'+import { Survey, SurveyQuestionType, SurveyType } from '~/types'-import { reportSurveyEdited } from './surveyUsage'+import { reportSurveyCreated, reportSurveyEdited } from './surveyUsage'-describe('reportSurveyEdited', () => {+describe('surveyUsage', () => {it('reports survey configuration without question text or responses', () => {const capture = jest.spyOn(posthog, 'capture').mockImplementation()const survey = {@@ -38,4 +38,49 @@ describe('reportSurveyEdited', () => {})capture.mockRestore()})+ it.each([+ { isDuplicate: undefined, creationSource: undefined, expectedDuplicate: false, expectedSource: 'full_editor' },+ { isDuplicate: true, creationSource: 'wizard' as const, expectedDuplicate: true, expectedSource: 'wizard' },+ ])(+ 'reports survey creation with source $expectedSource',+ ({ isDuplicate, creationSource, expectedDuplicate, expectedSource }) => {+ const capture = jest.spyOn(posthog, 'capture').mockImplementation()+ const survey = {+ id: 'survey-example',+ name: 'Example survey',+ type: SurveyType.Popover,+ linked_insight_id: null,+ questions: [{ type: SurveyQuestionType.Open, question: 'Private question' }],+ conditions: null,+ iteration_count: null,+ iteration_frequency_days: null,+ appearance: null,+ enable_partial_responses: false,+ } as Survey++ reportSurveyCreated(survey, isDuplicate, creationSource)++ expect(capture).toHaveBeenCalledWith('survey created', {+ source: 'web',+ name: 'Example survey',+ id: 'survey-example',+ survey_type: SurveyType.Popover,+ questions_length: 1,+ question_types: [SurveyQuestionType.Open],+ is_duplicate: expectedDuplicate,+ creation_source: expectedSource,+ created_from: undefined,+ linked_insight_id: null,+ events_count: undefined,+ recurring_survey_iteration_count: 0,+ recurring_survey_iteration_interval: 0,+ shuffle_questions_enabled: false,+ shuffle_question_options_enabled_count: 0,+ has_branching_logic: false,+ has_partial_responses: false,+ skipping_submit_button: false,+ })+ capture.mockRestore()+ }+ )})diff --git a/products/surveys/frontend/surveyUsage.ts b/products/surveys/frontend/surveyUsage.tsindex 30dc6ff1b842..12ffc0022900 100644--- a/products/surveys/frontend/surveyUsage.ts+++ b/products/surveys/frontend/surveyUsage.ts@@ -1,5 +1,7 @@import posthog from 'posthog-js'+import { SURVEY_CREATED_SOURCE } from 'scenes/surveys/constants'+import { MultipleSurveyQuestion, Survey, SurveyQuestionType } from '~/types'type SurveyConfigurationProperties = {@@ -13,7 +15,22 @@ type SurveyConfigurationProperties = {skipping_submit_button: boolean}-export function getSurveyConfigurationProperties(survey: Survey): SurveyConfigurationProperties {+type SurveyCreationSource = 'form_builder' | 'full_editor' | 'llm_analytics' | 'quick_create' | 'template' | 'wizard'++type SurveyCreatedProperties = SurveyConfigurationProperties & {+ source: 'web'+ name: Survey['name']+ id: Survey['id']+ survey_type: Survey['type']+ questions_length: number+ question_types: SurveyQuestionType[]+ is_duplicate: boolean+ creation_source: SurveyCreationSource+ created_from: SURVEY_CREATED_SOURCE | undefined+ linked_insight_id: Survey['linked_insight_id']+}++function getSurveyConfigurationProperties(survey: Survey): SurveyConfigurationProperties {const questionsWithShuffledOptions = survey.questions.filter((question) => {return question.hasOwnProperty('shuffleOptions') && (question as MultipleSurveyQuestion).shuffleOptions})@@ -41,6 +58,30 @@ export function getSurveyConfigurationProperties(survey: Survey): SurveyConfigur}}+function getSurveyCreatedProperties(+ survey: Survey,+ isDuplicate?: boolean,+ creationSource?: SurveyCreationSource,+ createdFrom?: SURVEY_CREATED_SOURCE+): SurveyCreatedProperties {+ return {+ // The web app is the only place this event is emitted from — there is no backend+ // equivalent — so stamping the surface here is what puts surveys in a `source`+ // breakdown at all, rather than showing up as unattributed.+ source: 'web',+ name: survey.name,+ id: survey.id,+ survey_type: survey.type,+ questions_length: survey.questions.length,+ question_types: survey.questions.map((question) => question.type),+ is_duplicate: isDuplicate ?? false,+ creation_source: creationSource ?? 'full_editor',+ created_from: createdFrom,+ linked_insight_id: survey.linked_insight_id,+ ...getSurveyConfigurationProperties(survey),+ }+}+export function reportSurveyEdited(survey: Survey): void {posthog.capture('survey edited', {name: survey.name,@@ -50,3 +91,12 @@ export function reportSurveyEdited(survey: Survey): void {...getSurveyConfigurationProperties(survey),})}++export function reportSurveyCreated(+ survey: Survey,+ isDuplicate?: boolean,+ creationSource?: SurveyCreationSource,+ createdFrom?: SURVEY_CREATED_SOURCE+): void {+ posthog.capture('survey created', getSurveyCreatedProperties(survey, isDuplicate, creationSource, createdFrom))+}