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.

chore(insights): build funnel series as nodes in three converter callers (#104015)

Thomas Obermüller649a40fd1cc3
diff --git a/frontend/src/scenes/experiments/metricQueryUtils.test.ts b/frontend/src/scenes/experiments/metricQueryUtils.test.ts
index ab306f4193ee..0994076e4f0e 100644
--- a/frontend/src/scenes/experiments/metricQueryUtils.test.ts
+++ b/frontend/src/scenes/experiments/metricQueryUtils.test.ts
@@ -846,7 +846,7 @@ describe('Data Warehouse Support', () => {
)
})
- it('returns the correct query for funnel metrics with events and actions', () => {
+ it('returns the correct query for funnel metrics with events, actions and data warehouse steps', () => {
const metric: ExperimentMetric = {
kind: NodeKind.ExperimentMetric,
metric_type: ExperimentMetricType.FUNNEL,
@@ -855,12 +855,28 @@ describe('Data Warehouse Support', () => {
kind: NodeKind.EventsNode,
event: 'landing_page_view',
name: 'landing_page_view',
+ properties: [
+ {
+ key: 'plan',
+ value: ['paid'],
+ operator: PropertyOperator.Exact,
+ type: PropertyFilterType.Event,
+ },
+ ],
} as EventsNode,
{
kind: NodeKind.ActionsNode,
id: 123,
name: 'signup_action',
} as ActionsNode,
+ {
+ kind: NodeKind.ExperimentDataWarehouseNode,
+ table_name: 'stripe_charges',
+ name: 'Stripe charges',
+ timestamp_field: 'created_at',
+ events_join_key: 'distinct_id',
+ data_warehouse_join_key: 'customer_id',
+ } as ExperimentDataWarehouseNode,
],
}
@@ -874,12 +890,29 @@ describe('Data Warehouse Support', () => {
kind: NodeKind.EventsNode,
event: 'landing_page_view',
name: 'landing_page_view',
+ properties: [
+ {
+ key: 'plan',
+ value: ['paid'],
+ operator: PropertyOperator.Exact,
+ type: PropertyFilterType.Event,
+ },
+ ],
},
{
kind: NodeKind.ActionsNode,
id: 123,
name: 'signup_action',
},
+ {
+ kind: NodeKind.FunnelsDataWarehouseNode,
+ id: 'stripe_charges',
+ name: 'Stripe charges',
+ table_name: 'stripe_charges',
+ timestamp_field: 'created_at',
+ id_field: 'customer_id',
+ aggregation_target_field: 'distinct_id',
+ },
])
})
diff --git a/frontend/src/scenes/experiments/metricQueryUtils.ts b/frontend/src/scenes/experiments/metricQueryUtils.ts
index e1b1a2fe090e..fd619522cafe 100644
--- a/frontend/src/scenes/experiments/metricQueryUtils.ts
+++ b/frontend/src/scenes/experiments/metricQueryUtils.ts
@@ -3,9 +3,8 @@ import { match } from 'ts-pattern'
import { EXPERIMENT_DEFAULT_DURATION, FEATURE_FLAGS, FunnelLayout } from 'lib/constants'
import { dayjs } from 'lib/dayjs'
import type { FeatureFlagsSet } from 'lib/logic/featureFlagLogic'
-import { MathAvailability } from 'scenes/insights/filters/ActionFilter/ActionFilterRow/types'
+import { objectCleanWithEmpty } from 'lib/utils/objects'
-import { actionsAndEventsToSeries } from '~/queries/nodes/InsightQuery/utils/actionsAndEventsToSeries'
import type {
ActionsNode,
BreakdownFilter,
@@ -202,42 +201,51 @@ export const getQuery =
dateRange,
funnelsFilter,
interval: funnelsInterval,
- series: getFunnelSeries(funnelMetric), // Use proper conversion pipeline
+ series: getFunnelSeries(funnelMetric),
}) as FunnelsQuery
})
.otherwise(() => undefined)
}
-/**
- * converts a funnel metric to a series of events, actions, and data warehouse nodes
- * this is part of the conversion pipeline for funnel metrics.
- *
- * Funnel series are validated with:
- * Metric Series (ExperimentDataWarehouseNode) -> Filter (FunnelDatawarehouseFilter) -> Query Series (FunnelsDataWarehouseNode)
- *
- * Note: ExperimentDataWarehouseNode is converted to FunnelsDataWarehouseNode via actionsAndEventsToSeries
- */
+/** Funnel steps carry no math, so only identity, name and properties cross over. */
const getFunnelSeries = (
funnelMetric: ExperimentFunnelMetric
-): (EventsNode | ActionsNode | FunnelsDataWarehouseNode)[] => {
- const { events, actions, data_warehouse } = getFilter(funnelMetric)
-
- return actionsAndEventsToSeries(
- {
- actions,
- events,
- data_warehouse,
- } as any,
- true, // includeProperties
- MathAvailability.None, // No math for funnels
- NodeKind.FunnelsDataWarehouseNode // Convert data warehouse filters to FunnelsDataWarehouseNode
- ).filter(
- (series) =>
- series.kind === NodeKind.EventsNode ||
- series.kind === NodeKind.ActionsNode ||
- series.kind === NodeKind.FunnelsDataWarehouseNode
- ) as (EventsNode | ActionsNode | FunnelsDataWarehouseNode)[]
-}
+): (EventsNode | ActionsNode | FunnelsDataWarehouseNode)[] =>
+ funnelMetric.series.map((step) =>
+ match(step)
+ .with({ kind: NodeKind.EventsNode }, (eventStep) =>
+ objectCleanWithEmpty({
+ kind: NodeKind.EventsNode as const,
+ event: eventStep.event,
+ name: eventStep.name || eventStep.event || undefined,
+ custom_name: eventStep.custom_name,
+ properties: eventStep.properties,
+ })
+ )
+ .with({ kind: NodeKind.ActionsNode }, (actionStep) =>
+ objectCleanWithEmpty({
+ kind: NodeKind.ActionsNode as const,
+ id: actionStep.id,
+ name: actionStep.name || undefined,
+ custom_name: actionStep.custom_name,
+ properties: actionStep.properties,
+ })
+ )
+ .with({ kind: NodeKind.ExperimentDataWarehouseNode }, (dwStep) =>
+ objectCleanWithEmpty({
+ kind: NodeKind.FunnelsDataWarehouseNode as const,
+ id: dwStep.table_name,
+ name: dwStep.name || dwStep.table_name,
+ custom_name: dwStep.custom_name,
+ table_name: dwStep.table_name,
+ timestamp_field: dwStep.timestamp_field,
+ id_field: dwStep.data_warehouse_join_key,
+ aggregation_target_field: dwStep.events_join_key,
+ properties: dwStep.properties,
+ })
+ )
+ .exhaustive()
+ )
/**
* Creates an empty filter structure
diff --git a/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx b/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx
index 0194a81fa3fb..f2dc402c83a6 100644
--- a/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx
+++ b/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx
@@ -28,9 +28,10 @@ import { Link } from 'lib/lemon-ui/Link'
import { LoadingBar } from 'lib/lemon-ui/LoadingBar'
import posthog from 'lib/posthog-typed'
import { inStorybook, inStorybookTestRunner } from 'lib/utils/dom'
+import { GraphSeriesAddedSource, eventUsageLogic } from 'lib/utils/eventUsageLogic'
+import { getDefaultEventLabel, getDefaultEventName } from 'lib/utils/getAppContext'
import { humanFriendlyNumber, humanizeBytes } from 'lib/utils/numbers'
import { renderDetailWithLinks } from 'lib/utils/renderDetailWithLinks'
-import { entityFilterLogic } from 'scenes/insights/filters/ActionFilter/entityFilterLogic'
import { insightLogic, insightOverridesPresent } from 'scenes/insights/insightLogic'
import { autoRunMaxPrompt } from 'scenes/max/maxPrompt'
import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'
@@ -40,15 +41,12 @@ import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'
-import { actionsAndEventsToSeries } from '~/queries/nodes/InsightQuery/utils/actionsAndEventsToSeries'
-import { seriesToActionsAndEvents } from '~/queries/nodes/InsightQuery/utils/queryNodeToFilter'
-import { FunnelsQuery, Node, NodeKind, QueryStatus } from '~/queries/schema/schema-general'
-import { isFunnelsDataWarehouseNode } from '~/queries/utils'
+import { EventsNode, Node, NodeKind, QueryStatus } from '~/queries/schema/schema-general'
+import { isFunnelsDataWarehouseNode, setLatestVersionsOnQuery } from '~/queries/utils'
import {
AccessControlLevel,
AccessControlResourceType,
DashboardPlacement,
- FilterType,
InsightLogicProps,
SavedInsightsTabs,
SidePanelTab,
@@ -56,7 +54,6 @@ import {
import { funnelDataLogic } from 'products/product_analytics/frontend/insights/funnels/funnelDataLogic'
-import { MathAvailability } from '../filters/ActionFilter/ActionFilterRow/types'
import { insightDataLogic } from '../insightDataLogic'
import { insightVizDataLogic } from '../insightVizDataLogic'
import { SampleDataState, SampleDataVariant } from './SampleDataState'
@@ -931,21 +928,19 @@ export function FunnelSingleStepState({ actionable = true }: FunnelSingleStepSta
const { insightProps } = useValues(insightLogic)
const { series } = useValues(funnelDataLogic(insightProps))
const { updateQuerySource } = useActions(funnelDataLogic(insightProps))
+ const { reportInsightFilterAdded } = useActions(eventUsageLogic)
- const filters = series ? seriesToActionsAndEvents(series) : {}
- const setFilters = (payload: Partial<FilterType>): void => {
- updateQuerySource({
- series: actionsAndEventsToSeries(
- payload as any,
- true,
- MathAvailability.None,
- NodeKind.FunnelsDataWarehouseNode
- ),
- } as Partial<FunnelsQuery>)
+ const addFunnelStep = (): void => {
+ const defaultStep: EventsNode = setLatestVersionsOnQuery({
+ kind: NodeKind.EventsNode,
+ event: getDefaultEventName(),
+ name: getDefaultEventLabel(),
+ })
+ const nextSeries = [...(series ?? []), defaultStep]
+ updateQuerySource({ series: nextSeries })
+ reportInsightFilterAdded(nextSeries.length, GraphSeriesAddedSource.Default)
}
- const { addFilter } = useActions(entityFilterLogic({ setFilters, filters, typeKey: 'EditFunnel-action' }))
-
return (
<div
data-attr="insight-empty-state"
@@ -968,7 +963,7 @@ export function FunnelSingleStepState({ actionable = true }: FunnelSingleStepSta
<LemonButton
type="primary"
size="small"
- onClick={addFilter}
+ onClick={addFunnelStep}
data-attr="add-action-event-button-empty-state"
icon={<IconPlusSmall />}
>
diff --git a/products/product_analytics/frontend/insights/paths/pathsDataLogic.test.ts b/products/product_analytics/frontend/insights/paths/pathsDataLogic.test.ts
index 9a9fa92da0bd..381df4c3a645 100644
--- a/products/product_analytics/frontend/insights/paths/pathsDataLogic.test.ts
+++ b/products/product_analytics/frontend/insights/paths/pathsDataLogic.test.ts
@@ -67,17 +67,16 @@ describe('buildFunnelEventsFromPathNode', () => {
{
scenario: 'custom event',
node: makeNode('1_signed_up', 0),
- expected: [{ id: 'signed_up', name: 'signed_up', type: 'events', order: 0 }],
+ expected: [{ kind: NodeKind.EventsNode, event: 'signed_up', name: 'signed_up' }],
},
{
scenario: 'URL node adds $pageview with $current_url property',
node: makeNode('1_https://example.com/page', 0),
expected: [
{
- id: '$pageview',
+ kind: NodeKind.EventsNode,
+ event: '$pageview',
name: '$pageview',
- type: 'events',
- order: 0,
properties: [
{
key: '$current_url',
@@ -92,26 +91,24 @@ describe('buildFunnelEventsFromPathNode', () => {
{
scenario: 'relative path',
node: makeNode('1_/dashboard', 0),
- expected: [{ id: '/dashboard', name: '/dashboard', type: 'events', order: 0 }],
+ expected: [{ kind: NodeKind.EventsNode, event: '/dashboard', name: '/dashboard' }],
},
{
scenario: 'screen name',
node: makeNode('1_$screen', 0),
- expected: [{ id: '$screen', name: '$screen', type: 'events', order: 0 }],
+ expected: [{ kind: NodeKind.EventsNode, event: '$screen', name: '$screen' }],
},
{
- scenario: 'chain of mixed nodes walks backward via targetLinks',
+ scenario: 'chain of mixed nodes comes back in funnel step order',
node: (() => {
const start = makeNode('1_https://example.com/', 0)
return makeNode('2_signed_up', 1, start)
})(),
expected: [
- { id: 'signed_up', name: 'signed_up', type: 'events', order: 1 },
{
- id: '$pageview',
+ kind: NodeKind.EventsNode,
+ event: '$pageview',
name: '$pageview',
- type: 'events',
- order: 0,
properties: [
{
key: '$current_url',
@@ -121,6 +118,7 @@ describe('buildFunnelEventsFromPathNode', () => {
},
],
},
+ { kind: NodeKind.EventsNode, event: 'signed_up', name: 'signed_up' },
],
},
])('$scenario', ({ node, expected }) => {
diff --git a/products/product_analytics/frontend/insights/paths/pathsDataLogic.ts b/products/product_analytics/frontend/insights/paths/pathsDataLogic.ts
index c6cd525179f6..78aec890342a 100644
--- a/products/product_analytics/frontend/insights/paths/pathsDataLogic.ts
+++ b/products/product_analytics/frontend/insights/paths/pathsDataLogic.ts
@@ -5,7 +5,6 @@ import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import type { FeatureFlagsSet } from 'lib/logic/featureFlagLogic'
import { newInternalTab } from 'lib/utils/newInternalTab'
-import { MathAvailability } from 'scenes/insights/filters/ActionFilter/ActionFilterRow/types'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import type { QuerySourceUpdate } from 'scenes/insights/insightVizDataLogic'
import { keyForInsightLogicProps } from 'scenes/insights/sharedUtils'
@@ -13,11 +12,11 @@ import { pathsTitle } from 'scenes/trends/persons-modal/persons-modal-utils'
import { OpenPersonsModalProps, openPersonsModal } from 'scenes/trends/persons-modal/PersonsModal'
import { urls } from 'scenes/urls'
-import { actionsAndEventsToSeries } from '~/queries/nodes/InsightQuery/utils/actionsAndEventsToSeries'
import { InsightActorsQuery, InsightVizNode, NodeKind, PathsLink, PathsQuery } from '~/queries/schema/schema-general'
import type {
DataNode,
DateRange,
+ EventsNode,
FunnelPathsFilter,
FunnelsQuery,
InsightFilter,
@@ -31,22 +30,22 @@ import type {
} from '~/queries/schema/schema-general'
import type { PathsV2Query } from '~/queries/schema/schema-general'
import { isPathsQuery } from '~/queries/utils'
-import { ActionFilter, FunnelVizType, InsightLogicProps, PathType, PropertyFilterType, PropertyOperator } from '~/types'
+import { FunnelVizType, InsightLogicProps, PathType, PropertyFilterType, PropertyOperator } from '~/types'
import { PathNodeData } from './pathUtils'
import { Paths, PathsNode } from './types'
-export function buildFunnelEventsFromPathNode(pathItemCard: PathNodeData): ActionFilter[] {
- const events: ActionFilter[] = []
+export function buildFunnelEventsFromPathNode(pathItemCard: PathNodeData): EventsNode[] {
+ const events: EventsNode[] = []
let currentItemCard: PathNodeData | undefined = pathItemCard
while (currentItemCard) {
const rawName = currentItemCard.name.replace(/(^[0-9]+_)/, '')
const isPageview = /^https?:\/\//.test(rawName)
- events.push({
- id: isPageview ? '$pageview' : rawName,
+ // The walk runs backward from the clicked node, so each step goes in front of the last.
+ events.unshift({
+ kind: NodeKind.EventsNode,
+ event: isPageview ? '$pageview' : rawName,
name: isPageview ? '$pageview' : rawName,
- type: 'events',
- order: currentItemCard.depth,
...(isPageview && {
properties: [
{
@@ -239,8 +238,8 @@ export const pathsDataLogic = kea<pathsDataLogicType>([
openPersonsModal(modalProps)
},
viewPathToFunnel: ({ pathItemCard }) => {
- const events = buildFunnelEventsFromPathNode(pathItemCard)
- if (events.length === 0) {
+ const series = buildFunnelEventsFromPathNode(pathItemCard)
+ if (series.length === 0) {
return
}
@@ -248,12 +247,7 @@ export const pathsDataLogic = kea<pathsDataLogicType>([
kind: NodeKind.InsightVizNode,
source: {
kind: NodeKind.FunnelsQuery,
- series: actionsAndEventsToSeries(
- { events },
- true,
- MathAvailability.None,
- NodeKind.FunnelsDataWarehouseNode
- ),
+ series,
funnelsFilter: {
funnelVizType: FunnelVizType.Steps,
},