: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(funnels): add funnel→hog-charts series transforms (#59558)
Sam Pennington156a8ad63a6d
diff --git a/frontend/src/scenes/funnels/viz/funnel-line-chart/funnelChartTransforms.test.ts b/frontend/src/scenes/funnels/viz/funnel-line-chart/funnelChartTransforms.test.tsnew file mode 100644index 000000000000..a26ff20a1e47--- /dev/null+++ b/frontend/src/scenes/funnels/viz/funnel-line-chart/funnelChartTransforms.test.ts@@ -0,0 +1,139 @@+import type { FunnelStepWithNestedBreakdown } from '~/types'++import { buildFunnelLineSeries, buildFunnelLineTimeSeriesConfig, type IndexedFunnelStep } from './funnelChartTransforms'++const RED = '#ff0000'++const makeStep = (overrides: Partial<IndexedFunnelStep> = {}): IndexedFunnelStep =>+ ({+ id: 0,+ seriesIndex: 0,+ action_id: 'pageview',+ average_conversion_time: null,+ median_conversion_time: null,+ count: 100,+ name: 'Step 1',+ order: 0,+ type: 'events',+ converted_people_url: '',+ dropped_people_url: null,+ data: [10, 20, 30, 40, 50],+ days: ['2024-06-10', '2024-06-11', '2024-06-12', '2024-06-13', '2024-06-14'],+ labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],+ ...overrides,+ }) as IndexedFunnelStep++describe('funnelChartTransforms', () => {+ describe('buildFunnelLineSeries', () => {+ it('builds a series per indexed step with days and breakdown_value carried in meta', () => {+ const steps: IndexedFunnelStep[] = [+ makeStep({ id: 0, order: 0, name: 'Step 1', breakdown_value: 'spike' }),+ makeStep({ id: 1, order: 1, name: 'Step 2', breakdown_value: 'spike' }),+ ]+ const series = buildFunnelLineSeries(steps, { getColor: () => RED })++ expect(series).toHaveLength(2)+ expect(series[0]).toMatchObject({+ key: '0',+ label: 'Step 1',+ data: [10, 20, 30, 40, 50],+ color: RED,+ })+ expect(series[0].meta).toEqual({+ days: ['2024-06-10', '2024-06-11', '2024-06-12', '2024-06-13', '2024-06-14'],+ breakdown_value: 'spike',+ order: 0,+ label: 'Step 1',+ })+ })++ it('normalises missing data to an empty array so the trends transform accepts it', () => {+ const step = makeStep({ data: undefined as unknown as number[] })+ const [series] = buildFunnelLineSeries([step], { getColor: () => RED })++ expect(series.data).toEqual([])+ })++ it('sets a dashed in-progress tail when incompletenessOffsetFromEnd is negative', () => {+ const step = makeStep({ data: [1, 2, 3, 4, 5, 6, 7] })+ const [series] = buildFunnelLineSeries([step], {+ getColor: () => RED,+ incompletenessOffsetFromEnd: -2,+ })++ expect(series.stroke).toEqual({ partial: { fromIndex: 5 } })+ })++ it('passes the original IndexedFunnelStep to getColor (not the normalized shape)', () => {+ const step = makeStep({ breakdown_value: 'spike' })+ const getColor = jest.fn(() => RED)+ buildFunnelLineSeries([step], { getColor })++ expect(getColor).toHaveBeenCalledWith(step, 0)+ })+ })++ describe('buildFunnelLineTimeSeriesConfig', () => {+ it('produces a percentage y-axis tick formatter', () => {+ const config = buildFunnelLineTimeSeriesConfig({+ indexedSteps: [makeStep()],+ interval: 'day',+ timezone: 'UTC',+ allDays: ['2024-06-10'],+ showTrendLines: false,+ })++ expect(config.yAxis).toMatchObject({ format: 'percentage' })+ })++ it('passes through goal lines, trend lines, and value labels', () => {+ const config = buildFunnelLineTimeSeriesConfig({+ indexedSteps: [makeStep()],+ interval: 'day',+ timezone: 'UTC',+ allDays: ['2024-06-10'],+ showTrendLines: true,+ goalLines: [{ label: 'Goal', value: 75, displayIfCrossed: true }],+ valueLabels: { formatter: (v) => `${v}%` },+ })++ expect(config.trendLines).not.toBeUndefined()+ expect(config.trendLines).not.toHaveLength(0)+ expect(config.goalLines).toHaveLength(1)+ expect(config.valueLabels).toBeTruthy()+ })++ it('omits confidence intervals and moving average (funnels do not surface either)', () => {+ const config = buildFunnelLineTimeSeriesConfig({+ indexedSteps: [makeStep()],+ interval: 'day',+ timezone: 'UTC',+ allDays: ['2024-06-10'],+ showTrendLines: false,+ })++ expect(config.confidenceIntervals).toBeUndefined()+ expect(config.movingAverage).toBeUndefined()+ })++ it('omits the trendLines field when showTrendLines is false', () => {+ const config = buildFunnelLineTimeSeriesConfig({+ indexedSteps: [makeStep()],+ interval: 'day',+ timezone: 'UTC',+ allDays: ['2024-06-10'],+ showTrendLines: false,+ })++ expect(config.trendLines).toBeUndefined()+ })+ })++ describe('type contracts', () => {+ it('IndexedFunnelStep is assignable from FunnelStepWithNestedBreakdown', () => {+ const step: FunnelStepWithNestedBreakdown = makeStep()+ const indexed: IndexedFunnelStep = { ...step, id: 0, seriesIndex: 0 }+ expect(indexed.id).toBe(0)+ })+ })+})diff --git a/frontend/src/scenes/funnels/viz/funnel-line-chart/funnelChartTransforms.ts b/frontend/src/scenes/funnels/viz/funnel-line-chart/funnelChartTransforms.tsnew file mode 100644index 000000000000..a194fbd3be3e--- /dev/null+++ b/frontend/src/scenes/funnels/viz/funnel-line-chart/funnelChartTransforms.ts@@ -0,0 +1,92 @@+import type { Series, TimeSeriesLineChartConfig, TooltipConfig } from 'lib/hog-charts'+import type { SeriesDatum } from 'scenes/insights/InsightTooltip/insightTooltipUtils'+import {+ buildTrendsLineTimeSeriesConfig,+ buildTrendsSeries,+} from 'scenes/trends/viz/trends-line-chart/trendsChartTransforms'++import type { GoalLine as SchemaGoalLine, TrendsFilter } from '~/queries/schema/schema-general'+import type { FunnelStepWithNestedBreakdown, IntervalType } from '~/types'++import type { FunnelSeriesMeta } from '../shared/funnelSeriesMeta'++export type IndexedFunnelStep = FunnelStepWithNestedBreakdown & { id: number; seriesIndex: number }++// The API populates `data`/`days`, but the base FunnelStep type marks them optional —+// normalize so the trends transform never receives undefined.+interface NormalizedFunnelStep {+ id: string | number+ label: string | null+ data: number[]+ days?: string[]+ breakdown_value?: SeriesDatum['breakdown_value']+ order: number+}++function normalizeStep(step: IndexedFunnelStep): NormalizedFunnelStep {+ return {+ id: step.id,+ label: step.name ?? null,+ data: step.data ?? [],+ days: step.days,+ breakdown_value: step.breakdown_value as SeriesDatum['breakdown_value'],+ order: step.order,+ }+}++export interface BuildFunnelLineSeriesOpts {+ incompletenessOffsetFromEnd?: number+ getColor: (step: IndexedFunnelStep, index: number) => string+}++export function buildFunnelLineSeries(+ indexedSteps: IndexedFunnelStep[],+ opts: BuildFunnelLineSeriesOpts+): Series<FunnelSeriesMeta>[] {+ const normalized = indexedSteps.map(normalizeStep)+ return buildTrendsSeries<NormalizedFunnelStep, FunnelSeriesMeta>(normalized, {+ getColor: (_, index) => opts.getColor(indexedSteps[index], index),+ incompletenessOffsetFromEnd: opts.incompletenessOffsetFromEnd,+ buildMeta: (step) => ({+ days: step.days,+ breakdown_value: step.breakdown_value,+ order: step.order,+ label: step.label,+ }),+ })+}++export interface BuildFunnelLineConfigOpts {+ indexedSteps: IndexedFunnelStep[]+ interval?: IntervalType | null+ timezone?: string+ allDays?: string[]+ goalLines?: SchemaGoalLine[] | null+ incompletenessOffsetFromEnd?: number+ showTrendLines: boolean+ valueLabels?: TimeSeriesLineChartConfig['valueLabels']+ tooltip?: TooltipConfig+ showCrosshair?: boolean+}++// Funnel trends always use a percentage y-axis; this drives the shared trends config builder.+const PERCENTAGE_TRENDS_FILTER: TrendsFilter = { aggregationAxisFormat: 'percentage' }++export function buildFunnelLineTimeSeriesConfig(opts: BuildFunnelLineConfigOpts): TimeSeriesLineChartConfig {+ const normalized = opts.indexedSteps.map(normalizeStep)+ return buildTrendsLineTimeSeriesConfig<NormalizedFunnelStep>({+ results: normalized,+ trendsFilter: PERCENTAGE_TRENDS_FILTER,+ isPercentStackView: false,+ isStickiness: false,+ interval: opts.interval,+ timezone: opts.timezone,+ allDays: opts.allDays,+ goalLines: opts.goalLines,+ incompletenessOffsetFromEnd: opts.incompletenessOffsetFromEnd,+ showTrendLines: opts.showTrendLines,+ valueLabels: opts.valueLabels,+ tooltip: opts.tooltip,+ showCrosshair: opts.showCrosshair,+ })+}diff --git a/frontend/src/scenes/funnels/viz/shared/funnelSeriesMeta.ts b/frontend/src/scenes/funnels/viz/shared/funnelSeriesMeta.tsnew file mode 100644index 000000000000..e5dd35a2ca02--- /dev/null+++ b/frontend/src/scenes/funnels/viz/shared/funnelSeriesMeta.ts@@ -0,0 +1,9 @@+import type { SeriesDatum } from 'scenes/insights/InsightTooltip/insightTooltipUtils'++export type FunnelSeriesMeta = {+ days?: string[]+ // Narrower than BreakdownKeyType — matches SeriesDatum so the tooltip adapter needs no cast.+ breakdown_value?: SeriesDatum['breakdown_value']+ order: number+ label?: string | null+}