: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 Appcues destination (#65501)
Harley Alexandera4180a1d5fa3
diff --git a/nodejs/src/cdp/templates/_destinations/appcues/appcues.template.test.ts b/nodejs/src/cdp/templates/_destinations/appcues/appcues.template.test.tsnew file mode 100644index 000000000000..7a60174d2c11--- /dev/null+++ b/nodejs/src/cdp/templates/_destinations/appcues/appcues.template.test.ts@@ -0,0 +1,286 @@+import { parseJSON } from '~/utils/json-parse'++import { TemplateTester } from '../../test/test-helpers'+import { template } from './appcues.template'++describe('appcues template', () => {+ const tester = new TemplateTester(template)++ beforeEach(async () => {+ await tester.beforeEach()+ })++ const baseInputs = {+ accountId: '12345',+ apiKey: 'test_api_key',+ apiSecret: 'test_api_secret',+ region: 'US',+ userId: 'user-123',+ include_all_properties: false,+ }++ describe('track calls', () => {+ it('sends a track event to the events endpoint', async () => {+ const response = await tester.invokeMapping(+ 'Track Calls',+ baseInputs,+ {+ event: {+ event: 'Product Viewed',+ distinct_id: 'user-123',+ timestamp: '2024-01-01T00:00:00Z',+ properties: { product_id: 'widget-123' },+ },+ },+ {+ eventName: 'Product Viewed',+ attributes: { product_id: 'widget-123' },+ }+ )++ expect(response.error).toBeUndefined()+ expect(response.finished).toBe(false)+ const queueParams = response.invocation.queueParameters as any+ expect(queueParams.url).toBe('https://api.appcues.com/v2/accounts/12345/users/user-123/events')+ expect(queueParams.method).toBe('POST')++ const decoded = Buffer.from(queueParams.headers.Authorization.replace('Basic ', ''), 'base64').toString()+ expect(decoded).toBe('test_api_key:test_api_secret')++ const body = parseJSON(queueParams.body)+ expect(body.name).toBe('Product Viewed')+ expect(body.timestamp).toBe('2024-01-01T00:00:00Z')+ expect(body.attributes).toEqual({ product_id: 'widget-123' })+ })++ it('includes group_id when provided', async () => {+ const response = await tester.invokeMapping(+ 'Track Calls',+ baseInputs,+ {+ event: {+ event: 'Plan Upgraded',+ distinct_id: 'user-123',+ timestamp: '2024-01-01T00:00:00Z',+ properties: {},+ },+ },+ {+ eventName: 'Plan Upgraded',+ groupId: 'company-99',+ attributes: {},+ }+ )++ const body = parseJSON((response.invocation.queueParameters as any).body)+ expect(body.group_id).toBe('company-99')+ })++ it('uses the EU endpoint when region is EU', async () => {+ const response = await tester.invokeMapping(+ 'Track Calls',+ { ...baseInputs, region: 'EU' },+ {+ event: {+ event: 'Product Viewed',+ distinct_id: 'user-123',+ timestamp: '2024-01-01T00:00:00Z',+ properties: {},+ },+ },+ {+ eventName: 'Product Viewed',+ attributes: {},+ }+ )++ expect((response.invocation.queueParameters as any).url).toBe(+ 'https://api.eu.appcues.com/v2/accounts/12345/users/user-123/events'+ )+ })+ })++ describe('identify calls', () => {+ it('sends a profile update to the profile endpoint', async () => {+ const response = await tester.invokeMapping(+ 'Identify Calls',+ baseInputs,+ {+ event: {+ event: '$identify',+ distinct_id: 'user-123',+ timestamp: '2024-01-01T00:00:00Z',+ properties: {},+ },+ person: {+ properties: { email: '[email protected]', first_name: 'Jane' },+ },+ },+ {+ profileProperties: { email: '[email protected]', first_name: 'Jane' },+ }+ )++ expect(response.error).toBeUndefined()+ expect(response.finished).toBe(false)+ const queueParams = response.invocation.queueParameters as any+ expect(queueParams.url).toBe('https://api.appcues.com/v2/accounts/12345/users/user-123/profile')+ expect(queueParams.method).toBe('PATCH')++ const body = parseJSON(queueParams.body)+ expect(body).toEqual({ email: '[email protected]', first_name: 'Jane' })+ })++ it('skips when there are no profile properties to send', async () => {+ const response = await tester.invokeMapping(+ 'Identify Calls',+ baseInputs,+ {+ event: {+ event: '$identify',+ distinct_id: 'user-123',+ timestamp: '2024-01-01T00:00:00Z',+ properties: {},+ },+ person: { properties: {} },+ },+ {+ profileProperties: {},+ }+ )++ expect(response.error).toBeUndefined()+ expect(response.finished).toBe(true)+ expect(response.invocation.queueParameters).toBeUndefined()+ })+ })++ describe('user id handling', () => {+ it('skips when user ID is empty', async () => {+ const response = await tester.invokeMapping(+ 'Track Calls',+ { ...baseInputs, userId: '' },+ {+ event: {+ event: 'Product Viewed',+ distinct_id: 'user-123',+ timestamp: '2024-01-01T00:00:00Z',+ properties: {},+ },+ },+ {+ eventName: 'Product Viewed',+ attributes: {},+ }+ )++ expect(response.error).toBeUndefined()+ expect(response.finished).toBe(true)+ expect(response.invocation.queueParameters).toBeUndefined()+ })+ })++ describe('include all properties', () => {+ it('merges non-$ event properties into track attributes', async () => {+ const response = await tester.invokeMapping(+ 'Track Calls',+ { ...baseInputs, include_all_properties: true },+ {+ event: {+ event: 'Product Viewed',+ distinct_id: 'user-123',+ timestamp: '2024-01-01T00:00:00Z',+ properties: { product_id: 'widget-123', $browser: 'Chrome' },+ },+ },+ {+ eventName: 'Product Viewed',+ attributes: { source: 'web' },+ }+ )++ const body = parseJSON((response.invocation.queueParameters as any).body)+ expect(body.attributes).toEqual({ product_id: 'widget-123', source: 'web' })+ expect(body.attributes.$browser).toBeUndefined()+ })++ it('merges non-$ person properties into the identify profile', async () => {+ const response = await tester.invokeMapping(+ 'Identify Calls',+ { ...baseInputs, include_all_properties: true },+ {+ event: {+ event: '$identify',+ distinct_id: 'user-123',+ timestamp: '2024-01-01T00:00:00Z',+ properties: {},+ },+ person: {+ properties: { email: '[email protected]', company: 'Acme', $initial_referrer: 'google' },+ },+ },+ {+ profileProperties: { plan: 'enterprise' },+ }+ )++ const body = parseJSON((response.invocation.queueParameters as any).body)+ expect(body).toEqual({ email: '[email protected]', company: 'Acme', plan: 'enterprise' })+ expect(body.$initial_referrer).toBeUndefined()+ })+ })++ describe('error handling', () => {+ const cases = [+ {+ mapping: 'Track Calls',+ event: { event: 'Product Viewed', properties: {} },+ mappingInputs: { eventName: 'Product Viewed', attributes: {} },+ },+ {+ mapping: 'Identify Calls',+ event: { event: '$identify', properties: {} },+ mappingInputs: { profileProperties: { email: '[email protected]' } },+ },+ ]++ it.each(cases)('throws on API failure for $mapping', async ({ mapping, event, mappingInputs }) => {+ let response = await tester.invokeMapping(+ mapping,+ baseInputs,+ {+ event: { ...event, distinct_id: 'user-123', timestamp: '2024-01-01T00:00:00Z' },+ person: { properties: { email: '[email protected]' } },+ },+ mappingInputs+ )++ response = await tester.invokeFetchResponse(response.invocation, {+ status: 401,+ body: { error: 'Unauthorized' },+ })++ expect(response.error).toMatch(/Error from Appcues API \(status 401\)/)+ })++ it.each(cases)('finishes on a successful response for $mapping', async ({ mapping, event, mappingInputs }) => {+ let response = await tester.invokeMapping(+ mapping,+ baseInputs,+ {+ event: { ...event, distinct_id: 'user-123', timestamp: '2024-01-01T00:00:00Z' },+ person: { properties: { email: '[email protected]' } },+ },+ mappingInputs+ )++ response = await tester.invokeFetchResponse(response.invocation, {+ status: 200,+ body: { status: 200, title: 'OK' },+ })++ expect(response.error).toBeUndefined()+ expect(response.finished).toBe(true)+ })+ })+})diff --git a/nodejs/src/cdp/templates/_destinations/appcues/appcues.template.ts b/nodejs/src/cdp/templates/_destinations/appcues/appcues.template.tsnew file mode 100644index 000000000000..b589b5a68c49--- /dev/null+++ b/nodejs/src/cdp/templates/_destinations/appcues/appcues.template.ts@@ -0,0 +1,248 @@+import { HogFunctionTemplate } from '~/cdp/types'++export const template: HogFunctionTemplate = {+ status: 'beta',+ free: true,+ type: 'destination',+ id: 'template-appcues',+ name: 'Appcues',+ description:+ 'Forward PostHog events and person properties to Appcues to power onboarding flows, in-app messages, and targeting.',+ icon_url: '/static/services/appcues.png',+ category: ['User Engagement Platforms'],+ code_language: 'hog',+ code: `+let userId := inputs.userId+if (empty(userId)) {+ print('No user ID set. Skipping as a user ID is required.')+ return+}++let baseUrl := 'https://api.appcues.com'+if (inputs.region == 'EU') {+ baseUrl := 'https://api.eu.appcues.com'+}++let credentials := base64Encode(f'{inputs.apiKey}:{inputs.apiSecret}')+let headers := {+ 'Content-Type': 'application/json',+ 'Authorization': f'Basic {credentials}'+}++if (event.event in ('$identify', '$set')) {+ // Identify -> update the Appcues user profile+ let profile := {}+ if (inputs.include_all_properties) {+ for (let key, value in person.properties) {+ if (not key like '$%') {+ profile[key] := value+ }+ }+ }+ for (let key, value in inputs.profileProperties) {+ if (not empty(value)) {+ profile[key] := value+ }+ }++ if (empty(profile)) {+ print('No profile properties to send. Skipping.')+ return+ }++ let res := fetch(f'{baseUrl}/v2/accounts/{inputs.accountId}/users/{userId}/profile', {+ 'method': 'PATCH',+ 'headers': headers,+ 'body': profile+ })++ if (res.status >= 400) {+ throw Error(f'Error from Appcues API (status {res.status}): {res.body}')+ }+} else {+ // Track -> record a user event+ let body := {+ 'name': inputs.eventName,+ 'timestamp': event.timestamp+ }++ if (not empty(inputs.groupId)) {+ body['group_id'] := inputs.groupId+ }++ let attributes := {}+ if (inputs.include_all_properties) {+ for (let key, value in event.properties) {+ if (not key like '$%') {+ attributes[key] := value+ }+ }+ }+ for (let key, value in inputs.attributes) {+ if (not empty(value)) {+ attributes[key] := value+ }+ }+ if (not empty(attributes)) {+ body['attributes'] := attributes+ }++ let res := fetch(f'{baseUrl}/v2/accounts/{inputs.accountId}/users/{userId}/events', {+ 'method': 'POST',+ 'headers': headers,+ 'body': body+ })++ if (res.status >= 400) {+ throw Error(f'Error from Appcues API (status {res.status}): {res.body}')+ }+}+`,+ inputs_schema: [+ {+ key: 'accountId',+ type: 'string',+ label: 'Account ID',+ description: 'Your numeric Appcues account ID, found on your Appcues account settings page.',+ secret: false,+ required: true,+ },+ {+ key: 'apiKey',+ type: 'string',+ label: 'API Key',+ description: 'Your Appcues API key. Create one under settings in Appcues Studio (studio.appcues.com).',+ secret: true,+ required: true,+ },+ {+ key: 'apiSecret',+ type: 'string',+ label: 'API Secret',+ description: 'The API secret paired with your API key from Appcues Studio.',+ secret: true,+ required: true,+ },+ {+ key: 'region',+ type: 'choice',+ label: 'Data region',+ description: 'The Appcues data region for your account.',+ default: 'US',+ choices: [+ { label: 'US', value: 'US' },+ { label: 'EU', value: 'EU' },+ ],+ secret: false,+ required: true,+ },+ {+ key: 'userId',+ type: 'string',+ label: 'User ID',+ description: 'The identifier for the user in Appcues. Defaults to the PostHog distinct ID.',+ default: '{event.distinct_id}',+ secret: false,+ required: true,+ },+ {+ key: 'include_all_properties',+ type: 'boolean',+ label: 'Include all properties',+ description:+ 'If set, all person properties are sent as profile attributes on identify, and all event properties are sent as attributes on track. Internal properties prefixed with $ are excluded. May cause timeouts for persons with many properties.',+ default: false,+ secret: false,+ required: true,+ },+ ],+ filters: {+ events: [],+ actions: [],+ filter_test_accounts: true,+ },+ mapping_templates: [+ {+ name: 'Track Calls',+ include_by_default: true,+ filters: {+ events: [+ {+ id: null,+ name: 'All events',+ type: 'events',+ properties: [+ {+ key: "event not in ('$identify', '$set', '$groupidentify')",+ type: 'hogql',+ },+ ],+ },+ ],+ },+ inputs_schema: [+ {+ key: 'eventName',+ type: 'string',+ label: 'Event name',+ description: 'The name of the event to send to Appcues.',+ default: '{event.event}',+ secret: false,+ required: true,+ },+ {+ key: 'groupId',+ type: 'string',+ label: 'Group ID',+ description: 'Optional group/account identifier to associate the event with.',+ default: '',+ secret: false,+ required: false,+ },+ {+ key: 'attributes',+ type: 'dictionary',+ label: 'Event attributes',+ description: 'Map of attribute names to values sent with the event.',+ default: {},+ secret: false,+ required: false,+ },+ ],+ },+ {+ name: 'Identify Calls',+ include_by_default: true,+ filters: {+ events: [+ {+ id: null,+ name: 'All events',+ type: 'events',+ properties: [+ {+ key: "event in ('$identify', '$set')",+ type: 'hogql',+ },+ ],+ },+ ],+ },+ inputs_schema: [+ {+ key: 'profileProperties',+ type: 'dictionary',+ label: 'Profile properties',+ description: 'Map of Appcues profile attribute names to values. Sent on identify events.',+ default: {+ email: '{person.properties.email}',+ first_name: '{person.properties.first_name}',+ last_name: '{person.properties.last_name}',+ },+ secret: false,+ required: false,+ },+ ],+ },+ ],+}diff --git a/nodejs/src/cdp/templates/_destinations/coming-soon/coming-soon-destinations.template.ts b/nodejs/src/cdp/templates/_destinations/coming-soon/coming-soon-destinations.template.tsindex 8165390b98c9..abfd33709a9c 100644--- a/nodejs/src/cdp/templates/_destinations/coming-soon/coming-soon-destinations.template.ts+++ b/nodejs/src/cdp/templates/_destinations/coming-soon/coming-soon-destinations.template.ts@@ -1036,12 +1036,6 @@ const destinationDefinitions: DestinationConfig[] = [},// User Engagement Platforms- {- name: 'Appcues',- id: 'coming-soon-appcues',- icon_url: '/static/coming-soon-destinations/Appcues.svg',- category: ['User Engagement Platforms'],- },{name: 'Leanplum',id: 'coming-soon-leanplum',diff --git a/nodejs/src/cdp/templates/index.ts b/nodejs/src/cdp/templates/index.tsindex 70a27beda4bb..1bd3ddb4265c 100644--- a/nodejs/src/cdp/templates/index.ts+++ b/nodejs/src/cdp/templates/index.ts@@ -2,6 +2,7 @@ import { DESTINATION_PLUGINS, TRANSFORMATION_PLUGINS } from '../legacy-plugins'import { SEGMENT_DESTINATIONS } from '../segment/segment-templates'import { HogFunctionTemplate, NativeTemplate } from '../types'import { template as accoilTemplate } from './_destinations/accoil/accoil.template'+import { template as appcuesTemplate } from './_destinations/appcues/appcues.template'import { template as clickupTemplate } from './_destinations/clickup/clickup.template'import { allComingSoonTemplates } from './_destinations/coming-soon/coming-soon-destinations.template'import { template as emailTemplate } from './_destinations/email/email.template'@@ -78,6 +79,7 @@ export const HOG_FUNCTION_TEMPLATES_DESTINATIONS: HogFunctionTemplate[] = [posthogUpdateAccountTemplate,hubspotCompanyTemplate,accoilTemplate,+ appcuesTemplate,klimeTemplate,unifyTemplate,]