: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(insights): keep funnel series colors stable (#99097)
Sam Pennington347fcf8fa5d0
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex a24e300731a7..882865470042 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -3065,9 +3065,9 @@ snapshots:insights-funnelbarhorizontalchart--breakdown-narrow--light:hash: v1.k794b7964.2b9f03c8989cdaf7ae1f3e3d02bd1fa6f397fb5c661606dbd1c37ac08295fe54.or8-CpruZkNNbylxWbqBJCkfV7CJqORRm_y2BaE6q_cinsights-funnelbarhorizontalchart--default--dark:- hash: v1.k794b7964.f011a655d8dc1eafb11a9830e8783c08c3a14c4c8ca3ccf08a36f686edaea71c.tdW3aO9QQYirn_75pNWDlKSmyvRoyOkaVQya7VRejUA+ hash: v1.k794b7964.f9ac7dc1e214e006c2e5c775fc8a619d7f45fd1e64d764bf9ab3b3b9d6f5bd9b.wnbeBsYPxVtxszJzztoAlI8urWXEdxZJq-NWqoyggAEinsights-funnelbarhorizontalchart--default--light:- hash: v1.k794b7964.12179cfcb1aa09aca736c81fea823577963aac1c12c3a2abe01c6cfcf31cac2b.SikkivwGywIQHXjEWBc69l5SMigCMtYx-DmMWMJ0b9g+ hash: v1.k794b7964.036e89548439e5bd022bbd160db92c810fe4bbe2db1e308b4399c8383e425a07.DeoUX5Vn17eGjpdk0fWUlkqCX16pFYJbjeVAAwnEeXAinsights-funnelbarhorizontalchart--default-narrow--dark:hash: v1.k794b7964.00ffd091da542465f6b15fb03ad6bc2aa3118855c0f29612b9ebe9f93ee2d428.p0EYCXIBkMJ0Ed8aFkDkd-IRVCoJKzUPMPkrMNTAHJoinsights-funnelbarhorizontalchart--default-narrow--light:@@ -3077,9 +3077,9 @@ snapshots:insights-funnelbarhorizontalchart--funnel-top-to-bottom-breakdown-compare--light:hash: v1.k794b7964.69d24116a127fce32e031acc3053b696adf6434551435ab9580406d3fa6d5cd5.xBMAqnyCfkb6pT0YZp1xG0h-mx1Dk593HGmCzh8Nu_winsights-funnelbarhorizontalchart--funnel-top-to-bottom-compare--dark:- hash: v1.k794b7964.c0cb3c277a6081f617f8ea8669b80691aae8284a398eeaf75be2f855537f6a7c.YWqcUF2RMT4ILrETfQAfyuyrLbF7EACFRBAi4HTeAZQ+ hash: v1.k794b7964.f94c302de8dfbed206926739a37006b96720c7a9ad6686efc86611d609285fcd.GgyaREXehsa_-BnURcyo8yFDKDMPBjyDechGJuOD2FUinsights-funnelbarhorizontalchart--funnel-top-to-bottom-compare--light:- hash: v1.k794b7964.42740d8b45dfc8f7cb167e71d930c1cc42b10c44ba97402a6b160f29698c6792.7x2CWt4-sb-tW06e0FLN_r97SDmvhyyHoOxMpCuElg8+ hash: v1.k794b7964.81a3bd20e5e694a30e8137edf405d2ddd54c5848e6074e16ab7a3145de78d4ec.hixqKFoxTq6GIJ6BG8s4mH2y5snHdwonZZhuXEdmpG4insights-funnelcorrelationtable--default--dark:hash: v1.k794b7964.0af5256b33397ecdb46b1ac61212b729dfed0d19852b4ed18e0eda9a0b0050ed.OQxSp3ebKc2qKtkfvM3ISvz8-U4EJiCVB1XoN1qCAK8insights-funnelcorrelationtable--default--light:diff --git a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.test.ts b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.test.tsindex 4988385fbd79..18936f5d294a 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.test.ts+++ b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.test.ts@@ -102,13 +102,17 @@ describe('buildFunnelBarHorizontalData', () => {expect(first.series[1].visibility?.tooltip).toBe(false)})- it('colors the segment from the step and the filler from options.fillerColor', () => {- const getColor = jest.fn(() => '#abcabc')- const [first] = buildFunnelBarHorizontalData(noBreakdownSteps, { ...options, getColor })-- expect(first.series[0].color).toBe('#abcabc')- expect(first.series[1].color).toBe(options.fillerColor)- expect(getColor).toHaveBeenCalledWith(noBreakdownSteps[0])+ it('keeps the segment color from the first step and the filler color across steps', () => {+ const getColor = (series: FunnelStepWithConversionMetrics): string =>+ series.count === noBreakdownSteps[0].count ? '#abcabc' : '#defdef'+ const result = buildFunnelBarHorizontalData(noBreakdownSteps, { ...options, getColor })++ expect(result.map((step) => step.series[0].color)).toEqual(['#abcabc', '#abcabc', '#abcabc'])+ expect(result.map((step) => step.series[1].color)).toEqual([+ options.fillerColor,+ options.fillerColor,+ options.fillerColor,+ ])})})@@ -193,11 +197,14 @@ describe('buildFunnelBarHorizontalData', () => {const breakdownFilter = { breakdown: '$browser' }it('collapses to one segment + filler, sourced from the single visible variant', () => {- const result = buildFunnelBarHorizontalData(collapsedSteps, { ...options, breakdownFilter })+ const getColor = (series: FunnelStepWithConversionMetrics): string =>+ series.breakdown_value === 'mobile' && series.count === 100 ? '#abcabc' : '#defdef'+ const result = buildFunnelBarHorizontalData(collapsedSteps, { ...options, breakdownFilter, getColor })expect(result.every((s) => s.series.length === 2)).toBe(true)expect(result[0].series[0].label).toBe('mobile')expect(dataAcross(result, 0)).toEqual([100, 50])+ expect(result.map((step) => step.series[0].color)).toEqual(['#abcabc', '#abcabc'])expect(result[0].series[0].meta?.breakdownIndex).toBe(0)expect(dataAcross(result, 1)).toEqual([0, 50])})@@ -329,6 +336,18 @@ describe('buildFunnelBarHorizontalData', () => {// chart treats the blank gap above it as inert (no hover, tooltip, pointer cursor, or click).expect(result.map((s) => s.bars[0].series[1].trackData)).toEqual([[100], [100]])expect(result.map((s) => s.bars[1].series[1].trackData)).toEqual([[80], [80]])++ // A step that lost the current period shifts previous into slot 0, so the entry level has to+ // come from the matching period rather than the slot.+ const sparseSteps = [+ compareSteps[0],+ { ...compareSteps[1], nested_breakdown: [compareSteps[1].nested_breakdown![1]] },+ ]+ const sparseResult = buildFunnelBarHorizontalCompareData(sparseSteps, options)++ expect(sparseResult[1].bars[0].series[0].data[0]).toBe(40)+ expect(sparseResult[1].bars[0].series[1].data[0]).toBe(40)+ expect(sparseResult[1].bars[0].series[1].trackData).toEqual([80])})// At the first step every bar sits exactly at its own entry level, so drop-off is always 0 —@@ -376,17 +395,22 @@ describe('buildFunnelBarHorizontalData', () => {})})- it('colors each bar from the current step’s own variant, dimming the previous period', () => {- const getColor = jest.fn((v: FunnelStepWithConversionMetrics) =>- v.compare_label === 'previous' ? '#dimmed' : '#solid'- )+ it('keeps each period color from its first-step variant', () => {+ const getColor = (series: FunnelStepWithConversionMetrics): string =>+ `${series.compare_label}-${series.count}`const result = buildFunnelBarHorizontalCompareData(compareSteps, { ...options, getColor })- expect(result[1].bars[0].series[0].color).toBe('#solid')- expect(result[1].bars[1].series[0].color).toBe('#dimmed')- // representative is step 1's own variant (per-step color), not step 0's- expect(getColor).toHaveBeenCalledWith(compareSteps[1].nested_breakdown![0])- expect(getColor).toHaveBeenCalledWith(compareSteps[1].nested_breakdown![1])+ expect(result.map((step) => step.bars.map((bar) => bar.series[0].color))).toEqual([+ ['current-100', 'previous-80'],+ ['current-100', 'previous-80'],+ ])++ const sparseSteps = [+ compareSteps[0],+ { ...compareSteps[1], nested_breakdown: [compareSteps[1].nested_breakdown![1]] },+ ]+ const sparseResult = buildFunnelBarHorizontalCompareData(sparseSteps, { ...options, getColor })+ expect(sparseResult[1].bars[0].series[0].color).toBe('previous-80')})it('tags both the segment and filler of each bar with its period breakdownIndex', () => {@@ -502,20 +526,38 @@ describe('buildFunnelBarHorizontalData', () => {expect(currentDropOff?.visibility?.tooltip).toBe(false)})- it('dims each breakdown value’s previous-period segment', () => {- const getColor = jest.fn((v: FunnelStepWithConversionMetrics) =>- v.compare_label === 'previous' ? '#dimmed' : '#solid'- )- const [step0] = buildFunnelBarHorizontalCompareData(breakdownCompareSteps, { ...options, getColor })+ it('keeps each breakdown-period color from its first-step variant', () => {+ const getColor = (series: FunnelStepWithConversionMetrics): string =>+ `${series.breakdown_value}-${series.compare_label}-${series.count}`+ const result = buildFunnelBarHorizontalCompareData(breakdownCompareSteps, { ...options, getColor })- expect(step0.bars[0].series.filter((s) => !s.meta?.isDropOff).map((s) => s.color)).toEqual([- '#solid',- '#solid',- ])- expect(step0.bars[1].series.filter((s) => !s.meta?.isDropOff).map((s) => s.color)).toEqual([- '#dimmed',- '#dimmed',+ expect(+ result.map((step) =>+ step.bars.map((bar) =>+ bar.series.filter((series) => !series.meta?.isDropOff).map((series) => series.color)+ )+ )+ ).toEqual([+ [+ ['mobile-current-60', 'desktop-current-40'],+ ['mobile-previous-45', 'desktop-previous-30'],+ ],+ [+ ['mobile-current-60', 'desktop-current-40'],+ ['mobile-previous-45', 'desktop-previous-30'],+ ],])++ const sparseSteps = [+ breakdownCompareSteps[0],+ {+ ...breakdownCompareSteps[1],+ nested_breakdown: breakdownCompareSteps[1].nested_breakdown!.slice(2),+ },+ ]+ const sparseResult = buildFunnelBarHorizontalCompareData(sparseSteps, { ...options, getColor })+ expect(sparseResult[1].bars[0].series[0].color).toBe('desktop-current-40')+ expect(sparseResult[1].bars[1].series[0].color).toBe('desktop-previous-30')})})})diff --git a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.ts b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.tsindex a8c5ec847e4d..27793d1fabb5 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.ts+++ b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/funnelBarHorizontalTransforms.ts@@ -3,7 +3,7 @@ import type { Series, TooltipContext } from '@posthog/quill-charts'import type { BreakdownFilter } from '~/queries/schema/schema-general'import { type FunnelStepWithConversionMetrics } from '~/types'-import { getStepBreakdownSeries, hasBreakdown } from '../funnelUtils'+import { getStepBreakdownSeries, getVisibilityKey, hasBreakdown } from '../funnelUtils'import {buildFunnelBarHorizontalDropOff,buildFunnelBarHorizontalFiller,@@ -54,7 +54,9 @@ export function buildFunnelBarHorizontalData(const breakdown = isBreakdownLayout(steps)return steps.map((step, stepIndex) => ({label: String(stepIndex),- series: breakdown ? buildBreakdownSegments(steps, stepIndex, options) : buildSingleSegment(step, options),+ series: breakdown+ ? buildBreakdownSegments(steps, stepIndex, options)+ : buildSingleSegment(step, steps[0], options),}))}@@ -98,8 +100,8 @@ export interface FunnelBarHorizontalCompareStep {/** Builds the top-to-bottom compare layout: one bar per period, per step. Each bar is scaled to the* shared baseline already baked into `conversionRates.fromBasisStep` (so the larger period's first- * step fills the track and the other is proportional), and takes its color from the *current step's*- * variant — both periods share step i's color, with `getColor` dimming the `previous` series. Drop-off+ * step fills the track and the other is proportional), and takes its color from the matching first-step+ * variant so a series keeps its color across steps. `getColor` dims the `previous` series. Drop-off* stops at each period's entry level (its first-step share of the shared baseline); the space above is* the blank volume gap, left as whitespace rather than rendered as drop-off. */export function buildFunnelBarHorizontalCompareData(@@ -114,15 +116,15 @@ export function buildFunnelBarHorizontalCompareData(return buildBreakdownCompareStacks(steps, options)}- // Each period's entry level on the shared axis, indexed to match nested_breakdown order.- const entryLevels = firstNested.map((variant) => variant.conversionRates.fromBasisStep * RATE_TO_PERCENT)return steps.map((step, stepIndex) => {const bars = (step.nested_breakdown ?? []).map((variant, breakdownIndex) => {+ const representative =+ firstNested.find((candidate) => candidate.compare_label === variant.compare_label) ?? variantconst segment: Series<FunnelBarHorizontalSegmentMeta> = {key: `${FUNNEL_BAR_HORIZONTAL_SEGMENT_KEY_PREFIX}${breakdownIndex}`,label: options.getLabel(variant),data: [variant.conversionRates.fromBasisStep * RATE_TO_PERCENT],- color: options.getColor(variant),+ color: options.getColor(representative),meta: { isDropOff: false, breakdownIndex },}return {@@ -131,7 +133,7 @@ export function buildFunnelBarHorizontalCompareData(segment,buildFunnelBarHorizontalDropOff([segment],- entryLevels[breakdownIndex] ?? 0,+ representative.conversionRates.fromBasisStep * RATE_TO_PERCENT,options.fillerColor,breakdownIndex),@@ -267,6 +269,7 @@ function buildBreakdownCompareStacks(steps: FunnelStepWithConversionMetrics[],options: BuildOptions): FunnelBarHorizontalCompareStep[] {+ const firstNested = steps[0]?.nested_breakdown ?? []const firstStep = periodTotals(steps[0])// Both stacks share the larger period's first-step total, so the smaller stack sits proportionally short.const basis = Math.max(firstStep.current, firstStep.previous, 0)@@ -278,11 +281,17 @@ function buildBreakdownCompareStacks(const current: Series<FunnelBarHorizontalSegmentMeta>[] = []const previous: Series<FunnelBarHorizontalSegmentMeta>[] = [];(step.nested_breakdown ?? []).forEach((variant, breakdownIndex) => {+ const representative =+ firstNested.find(+ (candidate) =>+ candidate.compare_label === variant.compare_label &&+ getVisibilityKey(candidate.breakdown_value) === getVisibilityKey(variant.breakdown_value)+ ) ?? variantconst segment: Series<FunnelBarHorizontalSegmentMeta> = {key: `${FUNNEL_BAR_HORIZONTAL_SEGMENT_KEY_PREFIX}${breakdownIndex}`,label: options.getLabel(variant),data: [toPercent(variant.count)],- color: options.getColor(variant),+ color: options.getColor(representative),meta: { isDropOff: false, breakdownIndex },};(variant.compare_label === 'previous' ? previous : current).push(segment)@@ -324,16 +333,18 @@ function periodTotals(step: FunnelStepWithConversionMetrics | undefined): { currfunction buildSingleSegment(step: FunnelStepWithConversionMetrics,+ firstStep: FunnelStepWithConversionMetrics,options: BuildOptions): Series<FunnelBarHorizontalSegmentMeta>[] {const displayStep = getStepBreakdownSeries(step, options.breakdownFilter) ?? step+ const colorRepresentative = getStepBreakdownSeries(firstStep, options.breakdownFilter) ?? firstStepconst isSingleBreakdownCollapse = displayStep !== stepconst segment: Series<FunnelBarHorizontalSegmentMeta> = {key: `${FUNNEL_BAR_HORIZONTAL_SEGMENT_KEY_PREFIX}0`,label: options.getLabel(displayStep),data: [displayStep.conversionRates.fromBasisStep * RATE_TO_PERCENT],- color: options.getColor(displayStep),+ color: options.getColor(colorRepresentative),meta: { isDropOff: false, breakdownIndex: isSingleBreakdownCollapse ? 0 : null },}