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.

feat(workflows): apply trigger link param on top of a workflow template (#127124)

Michael Reichenbach766a7693c878
diff --git a/products/workflows/frontend/Workflows/hogflows/registry/triggers/triggerTypeRegistry.ts b/products/workflows/frontend/Workflows/hogflows/registry/triggers/triggerTypeRegistry.ts
index 16f76c92b8fc..2a35e9c8179f 100644
--- a/products/workflows/frontend/Workflows/hogflows/registry/triggers/triggerTypeRegistry.ts
+++ b/products/workflows/frontend/Workflows/hogflows/registry/triggers/triggerTypeRegistry.ts
@@ -67,3 +67,11 @@ export function registerTriggerType(definition: TriggerTypeDefinition): void {
export function getRegisteredTriggerTypes(): TriggerTypeDefinition[] {
return triggerTypeDefinitions
}
+
+export function getTriggerDisplayType(config: { type: string }): string {
+ // Several tiles can share one config type (`event`, `internal-event`), so the tile is whichever
+ // one claims this config, not the type itself. Types owned by a single tile fall through to the
+ // type, which is that tile's value.
+ const match = triggerTypeDefinitions.find((t) => t.matchConfig?.(config))
+ return match ? match.value : config.type
+}
diff --git a/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx b/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx
index db9002e04bbe..d23f387170f5 100644
--- a/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx
+++ b/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx
@@ -59,6 +59,7 @@ import {
PERSON_FREQUENCY_OPTIONS,
TriggerFrequencyOption,
getRegisteredTriggerTypes,
+ getTriggerDisplayType,
} from '../registry/triggers/triggerTypeRegistry'
import { HogFlowAction } from '../types'
import { createAccountAssignmentFilterUpdate, parseAccountAssignmentFilter } from './accountAssignmentFilter'
@@ -93,14 +94,6 @@ type TriggerOptionItem = {
tag?: JSX.Element
}
-function getTriggerDisplayType(type: string, config: any): string {
- // Several tiles can share one config type (`event`, `internal-event`), so the tile is whichever
- // one claims this config, not the type itself. Types owned by a single tile fall through to the
- // type, which is that tile's value.
- const match = getRegisteredTriggerTypes().find((t) => t.matchConfig?.(config))
- return match ? match.value : type
-}
-
function TriggerTypeDropdown({
items,
selectedItem,
@@ -230,7 +223,7 @@ export function StepTriggerConfiguration({ node }: { node: Node<TriggerAction> }
const { featureFlags } = useValues(featureFlagLogic)
const type = node.data.config.type
- const displayType = getTriggerDisplayType(type, node.data.config)
+ const displayType = getTriggerDisplayType(node.data.config)
const validationResult = actionValidationErrorsById[node.id]
const allTriggerItems = useMemo(() => {
diff --git a/products/workflows/frontend/Workflows/workflowLogic.templatePrefill.test.ts b/products/workflows/frontend/Workflows/workflowLogic.templatePrefill.test.ts
new file mode 100644
index 000000000000..be9021740b80
--- /dev/null
+++ b/products/workflows/frontend/Workflows/workflowLogic.templatePrefill.test.ts
@@ -0,0 +1,100 @@
+// Registers the product trigger tiles, as StepTrigger does in the editor.
+import './hogflows/registry/triggers'
+
+import { expectLogic } from 'kea-test-utils'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import { HogFlow } from './hogflows/types'
+import { EXIT_NODE_ID, NEW_WORKFLOW, TRIGGER_NODE_ID, workflowLogic } from './workflowLogic'
+import { type WorkflowTriggerConfig } from './workflowTriggerPrefill'
+
+const template: HogFlow = {
+ ...NEW_WORKFLOW,
+ id: 'template-1',
+ name: 'Welcome email',
+ trigger_masking: { ttl: 1800, hash: '{person.id}', bytecode: ['_H', 1], threshold: null },
+ trigger: { type: 'event', filters: { events: [{ id: '$pageview', type: 'events' }] } },
+ actions: [
+ ...NEW_WORKFLOW.actions.map((action): HogFlow['actions'][number] =>
+ action.type === 'trigger'
+ ? { ...action, config: { type: 'event', filters: { events: [{ id: '$pageview', type: 'events' }] } } }
+ : action
+ ),
+ {
+ id: 'welcome_delay',
+ type: 'delay',
+ name: 'Wait before welcoming',
+ description: '',
+ created_at: 0,
+ updated_at: 0,
+ config: { delay_duration: '5m' },
+ },
+ ],
+ edges: [
+ { from: TRIGGER_NODE_ID, to: 'welcome_delay', type: 'continue' },
+ { from: 'welcome_delay', to: EXIT_NODE_ID, type: 'continue' },
+ ],
+}
+
+const linkedEventTrigger: WorkflowTriggerConfig = {
+ type: 'event',
+ filters: { events: [{ id: 'user_signed_up', type: 'events' }] },
+}
+
+const linkedInternalEventTrigger: WorkflowTriggerConfig = {
+ type: 'internal-event',
+ filters: { source: 'internal-events', events: [{ id: '$insight_alert_firing', type: 'internal_events' }] },
+}
+
+const linkedAccountPropertyTrigger: WorkflowTriggerConfig = {
+ type: 'event',
+ filters: { events: [{ id: '$account_custom_property_changed', type: 'events' }] },
+}
+
+const linkedMixedEventTrigger: WorkflowTriggerConfig = {
+ type: 'event',
+ filters: {
+ events: [
+ { id: 'user_signed_up', type: 'events' },
+ { id: '$account_custom_property_changed', type: 'events' },
+ ],
+ },
+}
+
+describe('workflowLogic template links', () => {
+ beforeEach(() => {
+ initKeaTests()
+ useMocks({
+ get: {
+ '/api/projects/:team_id/hog_flow_templates/:id/': template,
+ },
+ })
+ })
+
+ it.each([
+ ['an event trigger keeps the template frequency limit', linkedEventTrigger, template.trigger_masking],
+ ['a trigger of another type clears the template frequency limit', linkedInternalEventTrigger, null],
+ ['another event trigger tile clears the template frequency limit', linkedAccountPropertyTrigger, null],
+ ['a later event of another trigger tile clears the template frequency limit', linkedMixedEventTrigger, null],
+ ])('opens a template on the trigger passed in the link: %s', async (_, linkedTrigger, expectedMasking) => {
+ const logic = workflowLogic({
+ id: 'new',
+ templateId: template.id,
+ triggerPrefill: JSON.stringify(linkedTrigger),
+ })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadWorkflowSuccess'])
+
+ const triggerAction = logic.values.workflow.actions.find((action) => action.type === 'trigger')
+ expect(triggerAction?.config).toEqual(linkedTrigger)
+ expect(logic.values.workflow.trigger).toEqual(linkedTrigger)
+ expect(logic.values.workflow.name).toEqual('Welcome email')
+ expect(logic.values.workflow.actions.filter((action) => action.type !== 'trigger')).toEqual(
+ template.actions.filter((action) => action.type !== 'trigger')
+ )
+ expect(logic.values.workflow.edges).toEqual(template.edges)
+ expect(logic.values.workflow.trigger_masking).toEqual(expectedMasking)
+ })
+})
diff --git a/products/workflows/frontend/Workflows/workflowLogic.ts b/products/workflows/frontend/Workflows/workflowLogic.ts
index 640c21e8f80b..24274d461783 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.ts
@@ -56,7 +56,7 @@ import {
import type { AiEmailDraftStatusProps } from '../MessageAudience/AiEmailDraftStatus'
import { loadEmailBrand } from '../MessageAudience/emailBrand'
import { parseMessageDraftPrefill } from '../MessageAudience/messageDrafts'
-import { getRegisteredTriggerTypes } from './hogflows/registry/triggers/triggerTypeRegistry'
+import { getRegisteredTriggerTypes, getTriggerDisplayType } from './hogflows/registry/triggers/triggerTypeRegistry'
import {
DEFAULT_STATE,
isOneTimeSchedule,
@@ -108,6 +108,37 @@ export const EXIT_NODE_ID = 'exit_node'
export type TriggerAction = Extract<HogFlowAction, { type: 'trigger' }>
+// A tile matches on the first event only, so each event of a linked trigger gets its own tile.
+function getEventTriggerTiles(config: TriggerAction['config']): string[] {
+ if (config.type !== 'event' && config.type !== 'internal-event') {
+ return [getTriggerDisplayType(config)]
+ }
+ const { filters } = config
+ const events: unknown[] = Array.isArray(filters?.events) ? filters.events : []
+ if (events.length < 2) {
+ return [getTriggerDisplayType(config)]
+ }
+ return events.map((event) => {
+ const singleEventConfig = { ...config, filters: { ...filters, events: [event] } }
+ return getTriggerDisplayType(singleEventConfig)
+ })
+}
+
+function withTriggerConfig(workflow: HogFlow, config: TriggerAction['config']): HogFlow {
+ const templateTriggerConfig = workflow.actions.find((action) => action.type === 'trigger')?.config
+ const templateTile = templateTriggerConfig ? getTriggerDisplayType(templateTriggerConfig) : null
+ const keepsTriggerType = getEventTriggerTiles(config).every((tile) => tile === templateTile)
+ return {
+ ...workflow,
+ // A frequency hash such as {person.id} fits only some trigger types. After a type change it can resolve
+ // empty, and an empty hash skips masking, so the workflow would run without its entry limit. Compare the
+ // trigger tiles, as the editor does, because tiles such as "Account property changed" store an `event` config.
+ trigger_masking: keepsTriggerType ? workflow.trigger_masking : null,
+ trigger: config,
+ actions: workflow.actions.map((action) => (action.type === 'trigger' ? { ...action, config } : action)),
+ }
+}
+
export const NEW_WORKFLOW: HogFlow = {
id: 'new',
name: 'New workflow',
@@ -3502,7 +3533,11 @@ export const workflowLogic = kea<workflowLogicType>([
delete (newWorkflow as any).updated_at
delete (newWorkflow as any).created_by
- return (await withDefaultEmailSender(newWorkflow as HogFlow)) as typeof newWorkflow
+ const templateTriggerConfig = parseWorkflowTriggerPrefill(props.triggerPrefill)
+ const templated = templateTriggerConfig
+ ? withTriggerConfig(newWorkflow as HogFlow, templateTriggerConfig)
+ : (newWorkflow as HogFlow)
+ return (await withDefaultEmailSender(templated)) as typeof newWorkflow
}
const triggerConfig = parseWorkflowTriggerPrefill(props.triggerPrefill)
if (triggerConfig) {