: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
fix(funnels): correct breakdown bar widths in top-to-bottom layout (#71816)
Sam Penningtonbcd95eb35852
diff --git a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.tsx b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.tsxindex 9ee3d57596ad..d3a2a2e2af95 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.tsx@@ -11,7 +11,7 @@ import { insightLogic } from 'scenes/insights/insightLogic'import { themeLogic } from '~/layout/navigation-3000/themeLogic'import { groupsModel } from '~/models/groupsModel'-import { type ChartParams, FunnelStepReference, type FunnelStepWithConversionMetrics, StepOrderValue } from '~/types'+import { type ChartParams, type FunnelStepWithConversionMetrics, StepOrderValue } from '~/types'import { FunnelBarHorizontalTooltip } from './FunnelBarHorizontalTooltip'import {@@ -63,7 +63,6 @@ export function FunnelBarHorizontalChart({const { aggregationLabel } = useValues(groupsModel)const steps = visibleStepsWithConversionMetrics- const stepReference = funnelsFilter?.funnelStepReference || FunnelStepReference.totalconst showPersonsModal = canOpenPersonModal && showPersonsModalPropconst interactive = showPersonsModal && !inCardViewconst isUnordered = funnelsFilter?.funnelOrderType === StepOrderValue.UNORDERED@@ -72,14 +71,13 @@ export function FunnelBarHorizontalChart({const buildOptions = useMemo(() => ({- stepReference,breakdownFilter,getColor: getFunnelsColor,getLabel: (variant: FunnelStepWithConversionMetrics) =>String(variant.breakdown_value ?? variant.name ?? ''),fillerColor,}),- [stepReference, breakdownFilter, getFunnelsColor, fillerColor]+ [breakdownFilter, getFunnelsColor, fillerColor])const stepsData = useMemo(diff --git a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.test.ts b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.test.tsindex 20d7aa1d158e..4988385fbd79 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.test.ts+++ b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.test.ts@@ -1,4 +1,4 @@-import { EntityTypes, FunnelStepReference, type FunnelStepWithConversionMetrics } from '~/types'+import { EntityTypes, type FunnelStepWithConversionMetrics } from '~/types'import {buildFunnelBarHorizontalCompareData,@@ -43,7 +43,6 @@ function makeStep({}const options = {- stepReference: FunnelStepReference.total,getColor: () => '#1d4aff',getLabel: (variant: FunnelStepWithConversionMetrics) => String(variant.breakdown_value ?? variant.name),fillerColor: '#eef0f3',@@ -212,42 +211,41 @@ describe('buildFunnelBarHorizontalData', () => {})describe('reference step', () => {- it.each([- { stepReference: FunnelStepReference.total, expected: [100, 50, 20] },- { stepReference: FunnelStepReference.previous, expected: [100, 50, 20] },- ])(- 'uses precomputed fromBasisStep for non-breakdown layouts (stepReference=$stepReference)',- ({ stepReference, expected }) => {- const steps = [- makeStep({ count: 100, fromBasisStep: 1, name: 'Viewed' }),- makeStep({ count: 50, fromBasisStep: 0.5, name: 'Signed up' }),- makeStep({ count: 20, fromBasisStep: 0.2, name: 'Purchased' }),- ]- const result = buildFunnelBarHorizontalData(steps, { ...options, stepReference })- expect(dataAcross(result, 0)).toEqual(expected)- }- )-- it('honors stepReference.previous as the basis for breakdown fractions', () => {+ it('sizes breakdown stacks from the precomputed aggregate rate, not the neighboring step count', () => {+ // Optional step 2 out-counts step 1; a steps[i - 1] basis would give 260% and clamp to a full bar.const steps: FunnelStepWithConversionMetrics[] = [- makeStep({ count: 100, fromBasisStep: 1, name: 'Viewed' }),- makeStep({ count: 50, fromBasisStep: 0.5, name: 'Signed up' }),makeStep({- count: 30,- fromBasisStep: 0.3,- name: 'Purchased',+ count: 2000,+ fromBasisStep: 1,+ name: 'Viewed',nested_breakdown: [- makeStep({ count: 20, fromBasisStep: 0.4, breakdown_value: 'mobile' }),- makeStep({ count: 10, fromBasisStep: 0.2, breakdown_value: 'desktop' }),+ makeStep({ count: 1200, fromBasisStep: 1, breakdown_value: 'mobile' }),+ makeStep({ count: 800, fromBasisStep: 1, breakdown_value: 'desktop' }),+ ],+ }),+ makeStep({+ count: 50,+ fromBasisStep: 0.025,+ name: 'Connected (optional)',+ nested_breakdown: [+ makeStep({ count: 30, fromBasisStep: 0.025, breakdown_value: 'mobile' }),+ makeStep({ count: 20, fromBasisStep: 0.025, breakdown_value: 'desktop' }),+ ],+ }),+ makeStep({+ count: 130,+ fromBasisStep: 0.065,+ name: 'Pushed (optional)',+ nested_breakdown: [+ makeStep({ count: 100, fromBasisStep: 0.083, breakdown_value: 'mobile' }),+ makeStep({ count: 30, fromBasisStep: 0.0375, breakdown_value: 'desktop' }),],}),]- const result = buildFunnelBarHorizontalData(steps, {- ...options,- stepReference: FunnelStepReference.previous,- })- // Step 0 has no nested_breakdown, so the non-breakdown path is taken regardless of stepReference.- expect(dataAcross(result, 0)).toEqual([100, 50, 30])+ const result = buildFunnelBarHorizontalData(steps, options)+ expect(dataAcross(result, 0)).toEqual([60, 1.5, 5]) // mobile: count share of the aggregate rate+ expect(dataAcross(result, 1)).toEqual([40, 1, 1.5]) // desktop+ expect(result.map(stackTotal)).toEqual([100, 100, 100]) // segments + filler always close the track})})diff --git a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.ts b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.tsindex 1165723569d2..8ec57e68b27f 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.ts+++ b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.ts@@ -1,9 +1,9 @@import type { Series, TooltipContext } from '@posthog/quill-charts'-import { getReferenceStep, getStepBreakdownSeries, hasBreakdown } from 'scenes/funnels/funnelUtils'+import { getStepBreakdownSeries, hasBreakdown } from 'scenes/funnels/funnelUtils'import type { BreakdownFilter } from '~/queries/schema/schema-general'-import { FunnelStepReference, type FunnelStepWithConversionMetrics } from '~/types'+import { type FunnelStepWithConversionMetrics } from '~/types'import {buildFunnelBarHorizontalDropOff,@@ -27,7 +27,6 @@ export {} from '../shared/funnelBarHorizontalShared'interface BuildOptions {- stepReference: FunnelStepReferencebreakdownFilter?: BreakdownFilter | nullgetColor: (series: FunnelStepWithConversionMetrics) => stringgetLabel: (series: FunnelStepWithConversionMetrics) => string@@ -66,13 +65,15 @@ function buildBreakdownSegments(options: BuildOptions): Series<FunnelBarHorizontalSegmentMeta>[] {const step = steps[stepIndex]- const basisCount = getReferenceStep(steps, options.stepReference, stepIndex).count+ // Split the precomputed rate across variants by count so the stack matches the footer.+ // Deriving the basis from the neighboring step would ignore optional steps and overflow the bar.+ const fractionPerCount = step.count > 0 ? step.conversionRates.fromBasisStep / step.count : 0const breakdownCount = steps[0].nested_breakdown!.lengthconst segments: Series<FunnelBarHorizontalSegmentMeta>[] = []for (let breakdownIndex = 0; breakdownIndex < breakdownCount; breakdownIndex++) {const variant = variantAtStep(step, breakdownIndex)- const fraction = variant && basisCount > 0 ? variant.count / basisCount : 0+ const fraction = variant ? variant.count * fractionPerCount : 0// Color and label come from step 0's variant so the same breakdown reads consistently// across steps even when a later step is missing that variant.const representative = variantAtStep(steps[0], breakdownIndex)!