: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): make funnel totals follow the selected breakdown values (#119356)
Sam Pennington5fe8ea4e2946
diff --git a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.tsx b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.tsxindex 000f5e178725..dfe1c9c52d17 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.tsx@@ -56,6 +56,7 @@ export function FunnelBarHorizontalChart({querySource,isComparedFunnel,insightData,+ hasPartialBreakdownSelection,} = useValues(funnelDataLogic(insightProps))const { canOpenPersonModal } = useValues(funnelPersonsModalLogic(insightProps))const { openPersonsModalForStep, openPersonsModalForSeries } = useActions(funnelPersonsModalLogic(insightProps))@@ -158,6 +159,7 @@ export function FunnelBarHorizontalChart({breakdownFilter={breakdownFilter}groupTypeLabel={groupTypeLabel}showPersonsModal={showPersonsModal}+ isPartialBreakdownSelection={hasPartialBreakdownSelection}resolvedDateRange={insightData?.resolved_date_range}compareTo={querySource?.compareFilter?.compare_to}/>diff --git a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalTooltip.tsx b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalTooltip.tsxindex 18f8a362a5cf..70c6bc7c538f 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalTooltip.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalTooltip.tsx@@ -16,6 +16,7 @@ interface FunnelBarHorizontalTooltipProps {breakdownFilter: BreakdownFilter | null | undefinedgroupTypeLabel: stringshowPersonsModal: boolean+ isPartialBreakdownSelection: booleanresolvedDateRange?: Parameters<typeof funnelComparePeriodDateRange>[1]compareTo?: Parameters<typeof funnelComparePeriodDateRange>[2]}@@ -28,6 +29,7 @@ export function FunnelBarHorizontalTooltip({breakdownFilter,groupTypeLabel,showPersonsModal,+ isPartialBreakdownSelection,resolvedDateRange,compareTo,}: FunnelBarHorizontalTooltipProps): JSX.Element | null {@@ -49,6 +51,7 @@ export function FunnelBarHorizontalTooltip({groupTypeLabel,breakdownFilter,aggregateConversionRate,+ isPartialBreakdownSelection,comparePeriodDateRange,}diff --git a/products/product_analytics/frontend/insights/funnels/FunnelCanvasLabel.tsx b/products/product_analytics/frontend/insights/funnels/FunnelCanvasLabel.tsxindex 042c5422c29e..c2c4a13f3a14 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelCanvasLabel.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelCanvasLabel.tsx@@ -20,7 +20,9 @@ import { funnelDataLogic } from './funnelDataLogic'export function FunnelCanvasLabel(): JSX.Element | null {const { insightProps, insight, supportsCreatingExperiment, derivedName } = useValues(insightLogic)- const { conversionMetrics, aggregationTargetLabel, funnelVizType } = useValues(funnelDataLogic(insightProps))+ const { conversionMetrics, aggregationTargetLabel, funnelVizType, hasPartialBreakdownSelection } = useValues(+ funnelDataLogic(insightProps)+ )const { updateInsightFilter } = useActions(funnelDataLogic(insightProps))const { addProductIntentForCrossSell } = useActions(teamLogic)@@ -30,7 +32,11 @@ export function FunnelCanvasLabel(): JSX.Element | null {<><span className="flex items-center text-secondary mr-1"><Tooltip- title={`Overall conversion rate for all ${aggregationTargetLabel.plural} on the entire funnel.`}+ title={+ hasPartialBreakdownSelection+ ? `Overall conversion rate for ${aggregationTargetLabel.plural} in the selected breakdown values on the entire funnel.`+ : `Overall conversion rate for all ${aggregationTargetLabel.plural} on the entire funnel.`+ }><IconInfo className="mr-1 text-xl shrink-0" /></Tooltip>@@ -45,11 +51,15 @@ export function FunnelCanvasLabel(): JSX.Element | null {<><span className="flex items-center text-secondary"><Tooltip- title={`Median (50th percentile) of the total time each ${aggregationTargetLabel.singular} spent in the entire funnel.`}+ title={`Median (50th percentile) of the total time each ${aggregationTargetLabel.singular} spent in the entire funnel.${hasPartialBreakdownSelection ? ' This median includes all breakdown values.' : ''}`}><IconInfo className="mr-1 text-xl shrink-0" /></Tooltip>- <span>Median time to convert</span>+ <span>+ {hasPartialBreakdownSelection+ ? 'Median time to convert (all breakdowns)'+ : 'Median time to convert'}+ </span></span>{funnelVizType === FunnelVizType.TimeToConvert && <FunnelStepsPicker />}<span className="text-secondary mr-1">:</span>diff --git a/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.tsx b/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.tsxindex 4775c2440786..03dac1b108af 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.tsx@@ -68,6 +68,7 @@ export function FunnelStepsBarChart({insightData,funnelsFilter,isStepOptional,+ hasPartialBreakdownSelection,} = useValues(funnelDataLogic(insightProps))const { canOpenPersonModal } = useValues(funnelPersonsModalLogic(insightProps))const { openPersonsModalForSeries } = useActions(funnelPersonsModalLogic(insightProps))@@ -129,11 +130,20 @@ export function FunnelStepsBarChart({breakdownFilter={breakdownFilter}groupTypeLabel={groupTypeLabel}showPersonsModal={showPersonsModal}+ isPartialBreakdownSelection={hasPartialBreakdownSelection}resolvedDateRange={insightData?.resolved_date_range}compareTo={querySource?.compareFilter?.compare_to}/>),- [steps, breakdownFilter, groupTypeLabel, showPersonsModal, insightData?.resolved_date_range, querySource]+ [+ steps,+ breakdownFilter,+ groupTypeLabel,+ showPersonsModal,+ hasPartialBreakdownSelection,+ insightData?.resolved_date_range,+ querySource,+ ])const renderStepFooter = useCallback(diff --git a/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarTooltip.tsx b/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarTooltip.tsxindex bb2871916e76..7ca7fb822341 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarTooltip.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarTooltip.tsx@@ -13,6 +13,7 @@ interface FunnelStepsBarTooltipProps {breakdownFilter: BreakdownFilter | null | undefinedgroupTypeLabel: stringshowPersonsModal: boolean+ isPartialBreakdownSelection: booleanresolvedDateRange?: Parameters<typeof funnelComparePeriodDateRange>[1]compareTo?: Parameters<typeof funnelComparePeriodDateRange>[2]}@@ -23,6 +24,7 @@ export function FunnelStepsBarTooltip({breakdownFilter,groupTypeLabel,showPersonsModal,+ isPartialBreakdownSelection,resolvedDateRange,compareTo,}: FunnelStepsBarTooltipProps): JSX.Element | null {@@ -50,6 +52,7 @@ export function FunnelStepsBarTooltip({groupTypeLabel,breakdownFilter,aggregateConversionRate,+ isPartialBreakdownSelection,comparePeriodDateRange,}diff --git a/products/product_analytics/frontend/insights/funnels/funnelDataLogic.test.ts b/products/product_analytics/frontend/insights/funnels/funnelDataLogic.test.tsindex 55bc0a9868cc..b66fb3f852e2 100644--- a/products/product_analytics/frontend/insights/funnels/funnelDataLogic.test.ts+++ b/products/product_analytics/frontend/insights/funnels/funnelDataLogic.test.ts@@ -1117,6 +1117,39 @@ describe('funnelDataLogic', () => {})})+ it('sums step totals over the selected breakdown values when the baseline is hidden', async () => {+ const insight: Partial<InsightModel> = {+ result: funnelResultWithBreakdown.result,+ }+ const query: FunnelsQuery = {+ kind: NodeKind.FunnelsQuery,+ series: [],+ funnelsFilter: {+ hiddenLegendBreakdowns: ['Baseline', 'Firefox'],+ },+ }++ await expectLogic(logic, () => {+ logic.actions.updateQuerySource(query)+ builtDataNodeLogic.actions.loadDataSuccess(insight)+ }).toMatchValues({+ hasPartialBreakdownSelection: true,+ visibleStepsWithConversionMetrics: [+ expect.objectContaining({ count: 148 }),+ expect.objectContaining({+ count: 72,+ droppedOffFromPrevious: 76,+ conversionRates: expect.objectContaining({ fromPrevious: 72 / 148, total: 72 / 148 }),+ nested_breakdown: [+ expect.objectContaining({ breakdown_value: ['Chrome'], count: 66 }),+ expect.objectContaining({ breakdown_value: ['Safari'], count: 6 }),+ ],+ }),+ ],+ conversionMetrics: expect.objectContaining({ totalRate: 72 / 148 }),+ })+ })+it('with multi breakdown', async () => {const insight: Partial<InsightModel> = {result: funnelResultWithMultiBreakdown.result,diff --git a/products/product_analytics/frontend/insights/funnels/funnelDataLogic.ts b/products/product_analytics/frontend/insights/funnels/funnelDataLogic.tsindex f719b8d12ad7..7e0c5bbf0318 100644--- a/products/product_analytics/frontend/insights/funnels/funnelDataLogic.ts+++ b/products/product_analytics/frontend/insights/funnels/funnelDataLogic.ts@@ -310,6 +310,7 @@ export interface funnelDataLogicValues {dataset: FlattenedFunnelStepByBreakdown | FunnelStepWithConversionMetrics) => [DataColorTheme | null, DataColorToken | null]hasFunnelResults: boolean+ hasPartialBreakdownSelection: booleanhiddenLegendBreakdowns: string[] | undefinedhistogramGraphData: HistogramGraphDatum[] | nullhistogramGraphDataPrevious: HistogramGraphDatum[] | null@@ -496,12 +497,20 @@ export interface funnelDataLogicMeta {resultCustomizations: (funnelsFilter: FunnelsFilter | null | undefined) => Record<string, ResultCustomizationByValue> | undefined+ hasPartialBreakdownSelection: (+ flattenedBreakdowns: FlattenedFunnelStepByBreakdown[],+ hiddenLegendBreakdowns: string[] | undefined,+ isComparedFunnel: boolean+ ) => booleanvisibleStepsWithConversionMetrics: (stepsWithConversionMetrics: FunnelStepWithConversionMetrics[],flattenedBreakdowns: FlattenedFunnelStepByBreakdown[],hiddenLegendBreakdowns: string[] | undefined,isComparedFunnel: boolean,- isBreakdownCompareFunnel: boolean+ isBreakdownCompareFunnel: boolean,+ hasPartialBreakdownSelection: boolean,+ funnelsFilter: FunnelsFilter | null | undefined,+ isStepOptional: (step: number) => boolean) => FunnelStepWithConversionMetrics[]timeConversionResults: (results: FunnelResultType,@@ -528,6 +537,7 @@ export interface funnelDataLogicMeta {) => numberconversionMetrics: (steps: FunnelStepWithNestedBreakdown[],+ visibleStepsWithConversionMetrics: FunnelStepWithConversionMetrics[],funnelsFilter: FunnelsFilter | null | undefined,timeConversionResults: FunnelsTimeConversionBins | null,insightData: Record<string, any>@@ -1012,6 +1022,30 @@ export const funnelDataLogic = kea<funnelDataLogicType>([(s) => [s.funnelsFilter],(funnelsFilter: FunnelsFilter | null | undefined) => funnelsFilter?.resultCustomizations,],+ // True when the table hides some breakdown values and also hides the baseline (the total).+ // Step totals then follow the selected values instead of the whole funnel.+ hasPartialBreakdownSelection: [+ (s) => [s.flattenedBreakdowns, s.hiddenLegendBreakdowns, s.isComparedFunnel],+ (+ flattenedBreakdowns: FlattenedFunnelStepByBreakdown[],+ hiddenLegendBreakdowns: string[] | undefined,+ isComparedFunnel: boolean+ ): boolean => {+ if (isComparedFunnel || !hiddenLegendBreakdowns?.length) {+ return false+ }+ const isVisible = (b: FlattenedFunnelStepByBreakdown): boolean =>+ !hiddenLegendBreakdowns.includes(getVisibilityKey(b.breakdown_value))+ const baseline = flattenedBreakdowns.find((b) => b.isBaseline)+ const values = flattenedBreakdowns.filter((b) => !b.isBaseline)+ return (+ values.length > 1 &&+ !(baseline && isVisible(baseline)) &&+ values.some(isVisible) &&+ !values.every(isVisible)+ )+ },+ ],visibleStepsWithConversionMetrics: [(s) => [s.stepsWithConversionMetrics,@@ -1019,13 +1053,19 @@ export const funnelDataLogic = kea<funnelDataLogicType>([s.hiddenLegendBreakdowns,s.isComparedFunnel,s.isBreakdownCompareFunnel,+ s.hasPartialBreakdownSelection,+ s.funnelsFilter,+ s.isStepOptional,],(steps: FunnelStepWithConversionMetrics[],flattenedBreakdowns: FlattenedFunnelStepByBreakdown[],hiddenLegendBreakdowns: string[] | undefined,isComparedFunnel: boolean,- isBreakdownCompareFunnel: boolean+ isBreakdownCompareFunnel: boolean,+ hasPartialBreakdownSelection: boolean,+ funnelsFilter: FunnelsFilter | null | undefined,+ isStepOptional: (step: number) => boolean): FunnelStepWithConversionMetrics[] => {// Pure compare already shaped nested_breakdown into [current, previous]; skip the// breakdown baseline-prepend / hidden-legend reordering, which assumes real breakdowns.@@ -1086,7 +1126,7 @@ export const funnelDataLogic = kea<funnelDataLogicType>([const breakdownOrder = new Map<string, number>()flattenedBreakdowns.forEach((b, i) => breakdownOrder.set(getVisibilityKey(b.breakdown_value), i))- return steps.map((step, stepIndex) => {+ const visibleSteps = steps.map((step, stepIndex) => {const nested = (baseLineSteps?.steps? [baseLineSteps.steps[stepIndex], ...(step?.nested_breakdown ?? [])]@@ -1110,6 +1150,22 @@ export const funnelDataLogic = kea<funnelDataLogicType>([nested_breakdown: nested,}})+ if (!hasPartialBreakdownSelection) {+ return visibleSteps+ }+ const selectedTotals = stepsWithConversionMetrics(+ visibleSteps.map((step) => ({+ ...step,+ count: sum((step.nested_breakdown ?? []).map((b) => b.count)),+ nested_breakdown: undefined,+ })),+ funnelsFilter?.funnelStepReference || FunnelStepReference.total,+ steps.map((_, i) => i + 1).filter(isStepOptional)+ )+ return selectedTotals.map((step, i) => ({+ ...step,+ nested_breakdown: visibleSteps[i].nested_breakdown,+ }))},],@@ -1202,9 +1258,16 @@ export const funnelDataLogic = kea<funnelDataLogicType>([],conversionMetrics: [- (s) => [s.steps, s.funnelsFilter, s.timeConversionResults, s.insightData],+ (s) => [+ s.steps,+ s.visibleStepsWithConversionMetrics,+ s.funnelsFilter,+ s.timeConversionResults,+ s.insightData,+ ],(steps: FunnelStepWithNestedBreakdown[],+ visibleSteps: FunnelStepWithConversionMetrics[],funnelsFilter: FunnelsFilter | null | undefined,timeConversionResults: FunnelsTimeConversionBins | null,insightData: Record<string, any>@@ -1237,15 +1300,18 @@ export const funnelDataLogic = kea<funnelDataLogicType>([}}- const toStep = getLastFilledStep(steps)- const fromStep = getReferenceStep(steps, FunnelStepReference.total)+ const toStep = getLastFilledStep(visibleSteps)+ const fromStep = getReferenceStep(visibleSteps, FunnelStepReference.total)return {// The median of the total funnel time isn't the sum of per-step medians, so it's// computed breakdown-agnostically on the backend and carried as a top-level field.medianTime: (insightData as Partial<FunnelsQueryResponse>).total_median_conversion_time ?? null,stepRate: fromStep.count === 0 ? 0 : toStep.count / fromStep.count,- totalRate: steps[0].count === 0 ? 0 : steps[steps.length - 1].count / steps[0].count,+ totalRate:+ visibleSteps[0].count === 0+ ? 0+ : visibleSteps[visibleSteps.length - 1].count / visibleSteps[0].count,}},],diff --git a/products/product_analytics/frontend/insights/funnels/shared/FunnelStepTooltip.tsx b/products/product_analytics/frontend/insights/funnels/shared/FunnelStepTooltip.tsxindex 99a0fa72efe7..845bc3acfc47 100644--- a/products/product_analytics/frontend/insights/funnels/shared/FunnelStepTooltip.tsx+++ b/products/product_analytics/frontend/insights/funnels/shared/FunnelStepTooltip.tsx@@ -22,6 +22,8 @@ export interface FunnelStepTooltipProps {breakdownFilter: BreakdownFilter | null | undefinedcomparePeriodDateRange?: string | nullaggregateConversionRate?: number | null+ /** True when the aggregate rate covers only the breakdown values selected in the table, not the baseline. */+ isPartialBreakdownSelection?: boolean/** True when the cursor is over the unfilled track area (drop-off region) rather than the bar. */isDropOffHover?: boolean/** Series color — renders a swatch dot next to the step header. */@@ -36,6 +38,7 @@ export function FunnelStepTooltip({breakdownFilter,comparePeriodDateRange,aggregateConversionRate,+ isPartialBreakdownSelection = false,isDropOffHover = false,color,}: FunnelStepTooltipProps): JSX.Element {@@ -79,7 +82,10 @@ export function FunnelStepTooltip({}rows.push({ label: 'Conversion so far', value: percentage(series.conversionRates.total, 2, true) })if (stepIndex > 0 && aggregateConversionRate != null) {- rows.push({ label: 'Baseline conversion rate', value: percentage(aggregateConversionRate, 2, true) })+ rows.push({+ label: isPartialBreakdownSelection ? 'Conversion rate of selected values' : 'Baseline conversion rate',+ value: percentage(aggregateConversionRate, 2, true),+ })}if (stepIndex > 0 && series.median_conversion_time != null) {rows.push({