: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
feat(cdp): add show survey template (#125445)
Daniel Marchuka33f04f4cd3b
diff --git a/nodejs/src/cdp/schema/cyclotron.ts b/nodejs/src/cdp/schema/cyclotron.tsindex 560b39f4ba6e..f70e69f8ab65 100644--- a/nodejs/src/cdp/schema/cyclotron.ts+++ b/nodejs/src/cdp/schema/cyclotron.ts@@ -35,6 +35,7 @@ export const CyclotronJobInputSchemaTypeSchema = z.object({'task_repository','task_mcp_installations','signals_scout',+ 'posthog_survey','replay_vision_scan','task_skills',]),diff --git a/nodejs/src/cdp/templates/_destinations/posthog_surveys/posthog-show-survey.template.test.ts b/nodejs/src/cdp/templates/_destinations/posthog_surveys/posthog-show-survey.template.test.tsnew file mode 100644index 000000000000..b0ea8a17f4e4--- /dev/null+++ b/nodejs/src/cdp/templates/_destinations/posthog_surveys/posthog-show-survey.template.test.ts@@ -0,0 +1,43 @@+import { DateTime } from 'luxon'++import { TemplateTester } from '../../test/test-helpers'+import { template } from './posthog-show-survey.template'++describe('posthog show survey template', () => {+ const tester = new TemplateTester(template)+ beforeEach(async () => {+ await tester.beforeEach()+ jest.spyOn(Date, 'now').mockReturnValue(DateTime.fromISO('2025-01-01T00:00:00Z').toMillis())+ })++ it('marks the person as triggered for the survey', async () => {+ const response = await tester.invoke(+ { survey_id: '0190-survey', distinct_id: '{event.distinct_id}' },+ { event: { event: 'signed_up', distinct_id: 'distinct-id', properties: {} } }+ )+ expect(response.error).toBeUndefined()+ expect(response.finished).toEqual(true)+ expect(response.capturedPostHogEvents).toEqual([+ expect.objectContaining({+ event: '$set',+ distinct_id: 'distinct-id',+ properties: expect.objectContaining({ $set: { '$survey_triggered/0190-survey': true } }),+ }),+ ])+ })++ it.each([+ ['survey', { survey_id: '', distinct_id: 'distinct-id' }, 'A survey is required'],+ [+ 'distinct ID',+ { survey_id: '0190-survey', distinct_id: '{event.properties.missing}' },+ 'Distinct ID is required',+ ],+ ])('fails without a %s', async (_, inputs, error) => {+ const response = await tester.invoke(inputs, {+ event: { event: 'signed_up', distinct_id: 'distinct-id', properties: {} },+ })+ expect(response.error).toEqual(error)+ expect(response.capturedPostHogEvents).toEqual([])+ })+})diff --git a/nodejs/src/cdp/templates/_destinations/posthog_surveys/posthog-show-survey.template.ts b/nodejs/src/cdp/templates/_destinations/posthog_surveys/posthog-show-survey.template.tsnew file mode 100644index 000000000000..cf0c95da7a93--- /dev/null+++ b/nodejs/src/cdp/templates/_destinations/posthog_surveys/posthog-show-survey.template.ts@@ -0,0 +1,55 @@+import { HogFunctionTemplate } from '~/cdp/types'++export const template: HogFunctionTemplate = {+ free: true,+ status: 'hidden',+ type: 'destination',+ id: 'template-posthog-show-survey',+ name: 'Show survey',+ description:+ 'Show an in-app survey to this person the next time they open your web or mobile app. The survey only appears to people a workflow has reached when its targeting includes the workflow condition.',+ icon_url: '/static/posthog-icon.svg',+ category: ['Custom'],+ code_language: 'hog',+ code: `+if (empty(inputs.survey_id)) {+ throw Error('A survey is required')+}++if (empty(inputs.distinct_id)) {+ throw Error('Distinct ID is required')+}++// The survey's targeting flag matches on this person property, and the SDKs pick it up on their next flag refresh.+let props := {}+props[f'$survey_triggered/{inputs.survey_id}'] := true++postHogCapture({+ 'event': '$set',+ 'distinct_id': inputs.distinct_id,+ 'properties': {+ '$set': props+ }+})+`,+ inputs_schema: [+ {+ key: 'survey_id',+ type: 'posthog_survey',+ label: 'Survey',+ secret: false,+ required: true,+ description: 'The in-app survey to show. Launch it and keep its targeting limited to workflow recipients.',+ },+ {+ type: 'string',+ key: 'distinct_id',+ label: 'Distinct ID',+ required: true,+ secret: false,+ hidden: true,+ default: '{event.distinct_id}',+ description: 'The distinct ID of the person to show the survey to.',+ },+ ],+}diff --git a/nodejs/src/cdp/templates/index.ts b/nodejs/src/cdp/templates/index.tsindex 4bc0f084aec9..584fbe272112 100644--- a/nodejs/src/cdp/templates/index.ts+++ b/nodejs/src/cdp/templates/index.ts@@ -38,6 +38,7 @@ import { template as posthogUpdateAccountRelationshipsTemplate } from './_destinimport { template as posthogUpdateAccountTemplate } from './_destinations/posthog_customer_analytics/posthog-update-account.template'import { template as posthogReplayVisionTemplate } from './_destinations/posthog_replay_vision/posthog-replay-vision.template'import { template as posthogRunScoutTemplate } from './_destinations/posthog_signals/posthog-run-scout.template'+import { template as posthogShowSurveyTemplate } from './_destinations/posthog_surveys/posthog-show-survey.template'import { template as posthogCreateTaskTemplate } from './_destinations/posthog_tasks/posthog-create-task.template'import { template as posthogAddToStaticCohortTemplate } from './_destinations/posthog_workflows/posthog-add-to-static-cohort.template'import { template as posthogClassifyTemplate } from './_destinations/posthog_workflows/posthog-classify.template'@@ -115,6 +116,7 @@ export const HOG_FUNCTION_TEMPLATES_DESTINATIONS: HogFunctionTemplate[] = [posthogUpdateTicketTemplate,posthogCreateTaskTemplate,posthogRunScoutTemplate,+ posthogShowSurveyTemplate,posthogClassifyTemplate,posthogAddToStaticCohortTemplate,posthogOptOutOfMessagesTemplate,diff --git a/nodejs/src/cdp/types.ts b/nodejs/src/cdp/types.tsindex 2bb28279f5e4..710bbb724e6b 100644--- a/nodejs/src/cdp/types.ts+++ b/nodejs/src/cdp/types.ts@@ -613,6 +613,7 @@ export type HogFunctionInputSchemaType = {| 'task_repository'| 'task_mcp_installations'| 'signals_scout'+ | 'posthog_survey'| 'replay_vision_scan'| 'task_skills'key: stringdiff --git a/nodejs/tests/fixtures/hog-bytecode/033636d3e94a50c9c8342e56c7dca3981c6f38ca7b986a95a0a9adcd998fa73e.json b/nodejs/tests/fixtures/hog-bytecode/033636d3e94a50c9c8342e56c7dca3981c6f38ca7b986a95a0a9adcd998fa73e.jsonnew file mode 100644index 000000000000..b9e929250023--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/033636d3e94a50c9c8342e56c7dca3981c6f38ca7b986a95a0a9adcd998fa73e.json@@ -0,0 +1 @@+["_H",1,32,"0190-survey",38]diff --git a/nodejs/tests/fixtures/hog-bytecode/10033203bebf0e47ce0b3cd4d7e81a9f255c81b7b8ac9efde46ad36347e9824e.json b/nodejs/tests/fixtures/hog-bytecode/10033203bebf0e47ce0b3cd4d7e81a9f255c81b7b8ac9efde46ad36347e9824e.jsonnew file mode 100644index 000000000000..3b6dae4fe696--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/10033203bebf0e47ce0b3cd4d7e81a9f255c81b7b8ac9efde46ad36347e9824e.json@@ -0,0 +1 @@+["_H",1,32,"distinct-id",38]diff --git a/nodejs/tests/fixtures/hog-bytecode/c976b7a4cd653ff0807883858541f64eade4e8a949019f53f3d3adf41a86166f.json b/nodejs/tests/fixtures/hog-bytecode/c976b7a4cd653ff0807883858541f64eade4e8a949019f53f3d3adf41a86166f.jsonnew file mode 100644index 000000000000..d29f8bfd165a--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/c976b7a4cd653ff0807883858541f64eade4e8a949019f53f3d3adf41a86166f.json@@ -0,0 +1 @@+["_H",1,32,"survey_id",32,"inputs",1,2,2,"empty",1,40,6,32,"A survey is required",2,"Error",1,49,32,"distinct_id",32,"inputs",1,2,2,"empty",1,40,6,32,"Distinct ID is required",2,"Error",1,49,42,0,36,0,32,"$survey_triggered/",32,"survey_id",32,"inputs",1,2,2,"concat",2,29,46,32,"event",32,"$set",32,"distinct_id",32,"distinct_id",32,"inputs",1,2,32,"properties",32,"$set",36,0,42,1,42,3,2,"postHogCapture",1,35,35]