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.

fix(funnels): order trends tooltip rows by descending conversion value (#64243)

Xander3af141bb43fe
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 2f26b1c22ac5..a7c244ec0b78 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -2260,6 +2260,10 @@ snapshots:
hash: v1.k794b7964.91d6de8992e7b49f3dbe2989778ae54a5e9478a6136e1f5b34eb1eab4356711f.lWuIgyArPItzhZJDTIwVB-xZL8uCnt67DTANC5OKtJY
insights-funnellinechart--with-legend--light:
hash: v1.k794b7964.6b4d0969a75cf3df9e5c989ce373d153c77d1eedaedb72105ee78145fd9414e3.ve7yvcN4Bx2DPN9sNmeG1y-KKrJXxjH8IJdRtMg2nks
+ insights-funnellinechart-tooltip--breakdown-ordered-by-conversion--dark:
+ hash: v1.k794b7964.28fcb86c4f61884b1c2d0d6956482149d06c7a49a8425aa040b4ae36e2ac6c50.i5loD1ggBoEypNfx7cLJUjqPTUWcUXid0YcZUP0igpM
+ insights-funnellinechart-tooltip--breakdown-ordered-by-conversion--light:
+ hash: v1.k794b7964.c49fe1a8841b7a749961b5211db4b7bc4cbd830ad12e7aad45c5ebcbcbcc4183.RGiYqt2JTZvwcW0YsGiabk4O3F5Kb_mBIrdxp4w8saA
insights-funnelpropertycorrelationtable--default--dark:
hash: v1.k794b7964.c35a775de5f646fde698940d41eef4f82a28c73a3343a2cd5ae88bb65d5c7dda.zrfCnSRUrx6zbKiA_UyVw7m7hKgwbLbkgjnuwMUxk00
insights-funnelpropertycorrelationtable--default--light:
diff --git a/frontend/src/test/insight-testing/test-data.ts b/frontend/src/test/insight-testing/test-data.ts
index c57ead1df0a2..a30fb39f8afd 100644
--- a/frontend/src/test/insight-testing/test-data.ts
+++ b/frontend/src/test/insight-testing/test-data.ts
@@ -294,6 +294,35 @@ export const funnelTrendsSteps = {
breakdown_value: 'Bramble',
},
] satisfies FunnelStepData[],
+ // Query order (Safari, Chrome, Firefox), alphabetical order, and value-at-index-2 order
+ // (Firefox 60, Safari 40, Chrome 20) all differ, so a tooltip sorted by descending value
+ // can't accidentally match the input order.
+ browser: [
+ {
+ count: 40,
+ data: [30, 35, 40, 45, 38],
+ days,
+ labels,
+ name: '$pageview → Napped',
+ breakdown_value: 'Safari',
+ },
+ {
+ count: 20,
+ data: [10, 15, 20, 25, 18],
+ days,
+ labels,
+ name: '$pageview → Napped',
+ breakdown_value: 'Chrome',
+ },
+ {
+ count: 60,
+ data: [50, 55, 60, 65, 58],
+ days,
+ labels,
+ name: '$pageview → Napped',
+ breakdown_value: 'Firefox',
+ },
+ ] satisfies FunnelStepData[],
} as Record<string, FunnelStepData[]>,
// Compare-to-previous + breakdown: current and previous rows per breakdown value, each
// tagged with `compare_label` so the tooltip can split them into their own rows.
diff --git a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineChart.test.tsx b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineChart.test.tsx
index 8babdcd7664c..9a572dcd8e88 100644
--- a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineChart.test.tsx
+++ b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineChart.test.tsx
@@ -80,6 +80,35 @@ describe('FunnelLineChart', () => {
expect(tooltip.row('Bramble')).toContain('30%')
})
+ describe('orders breakdown rows by descending conversion value', () => {
+ // Captured once from a single render; the assertions only read these strings, so the
+ // per-test cleanup() between it.each cases doesn't matter.
+ let orderedRows: { label: string; value: string | undefined }[]
+
+ beforeAll(async () => {
+ renderInsight({
+ query: buildFunnelsQuery({
+ breakdownFilter: { breakdown: 'browser', breakdown_type: 'event' },
+ }),
+ featureFlags: HOG_CHARTS_FUNNEL_FLAG,
+ })
+
+ const tooltip = await chart.hoverTooltip(2)
+ orderedRows = tooltip.rows().map((label) => ({ label, value: tooltip.row(label) }))
+ })
+
+ // The query returns Safari, Chrome, Firefox, but at index 2 the conversion values are
+ // Firefox=60%, Safari=40%, Chrome=20%, so rows must be re-ordered by descending value.
+ it.each([
+ { position: 0, browser: 'Firefox', pct: '60%' },
+ { position: 1, browser: 'Safari', pct: '40%' },
+ { position: 2, browser: 'Chrome', pct: '20%' },
+ ])('row $position is $browser ($pct)', ({ position, browser, pct }) => {
+ expect(orderedRows[position].label).toContain(browser)
+ expect(orderedRows[position].value).toContain(pct)
+ })
+ })
+
it('shows a distinct conversion value for each breakdown × compare series', async () => {
renderInsight({
query: buildFunnelsQuery({
diff --git a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineTooltip.stories.tsx b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineTooltip.stories.tsx
new file mode 100644
index 000000000000..e187d8ffc678
--- /dev/null
+++ b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineTooltip.stories.tsx
@@ -0,0 +1,80 @@
+import { Meta, StoryObj } from '@storybook/react'
+import { useMountedLogic } from 'kea'
+
+import type { Series, TooltipContext } from '@posthog/quill-charts'
+
+import { cohortsModel } from '~/models/cohortsModel'
+import type { BreakdownFilter } from '~/queries/schema/schema-general'
+
+import type { FunnelSeriesMeta } from '../shared/funnelSeriesMeta'
+import { FunnelLineTooltip } from './FunnelLineTooltip'
+
+const days = ['2024-06-10', '2024-06-11', '2024-06-12', '2024-06-13', '2024-06-14']
+const DATA_INDEX = 2
+
+const breakdownFilter: BreakdownFilter = { breakdown: '$browser', breakdown_type: 'event' }
+
+const COLORS = ['#1d4aff', '#621da6', '#42827e', '#f4a261', '#e76f51']
+
+// Breakdown series are supplied in a deliberately scrambled order (40, 20, 55, 60, 10).
+// The tooltip must reorder rows by conversion value descending, so the screenshot reads
+// Firefox 60% → Edge 55% → Safari 40% → Chrome 20% → Opera 10% top to bottom.
+const breakdownRows: { label: string; value: number }[] = [
+ { label: 'Safari', value: 40 },
+ { label: 'Chrome', value: 20 },
+ { label: 'Edge', value: 55 },
+ { label: 'Firefox', value: 60 },
+ { label: 'Opera', value: 10 },
+]
+
+function buildContext(rows: { label: string; value: number }[]): TooltipContext<FunnelSeriesMeta> {
+ const seriesData = rows.map(({ label, value }, idx) => {
+ const series: Series<FunnelSeriesMeta> = {
+ key: label,
+ label,
+ data: days.map(() => value),
+ meta: { days, breakdown_value: label, order: 0, label },
+ }
+ return { series, value, color: COLORS[idx % COLORS.length] }
+ })
+ return {
+ dataIndex: DATA_INDEX,
+ label: days[DATA_INDEX],
+ seriesData,
+ position: { x: 0, y: 0 },
+ hoverPosition: null,
+ canvasBounds: { x: 0, y: 0, width: 0, height: 0, top: 0, right: 0, bottom: 0, left: 0 } as DOMRect,
+ isPinned: true,
+ onUnpin: () => {},
+ }
+}
+
+type Story = StoryObj<{}>
+
+const meta: Meta = {
+ title: 'Insights/FunnelLineChart/Tooltip',
+ component: FunnelLineTooltip,
+ parameters: {
+ layout: 'centered',
+ mockDate: '2024-06-12',
+ testOptions: { snapshotBrowsers: ['chromium'] },
+ },
+}
+export default meta
+
+export const BreakdownOrderedByConversion: Story = {
+ render: () => {
+ useMountedLogic(cohortsModel)
+
+ return (
+ <FunnelLineTooltip
+ context={buildContext(breakdownRows)}
+ timezone="UTC"
+ interval="day"
+ breakdownFilter={breakdownFilter}
+ groupTypeLabel="persons"
+ onRowClick={() => {}}
+ />
+ )
+ },
+}
diff --git a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineTooltip.tsx b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineTooltip.tsx
index 3ee1d6656c91..6e73a04a8872 100644
--- a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineTooltip.tsx
+++ b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineTooltip.tsx
@@ -32,29 +32,37 @@ export function FunnelLineTooltip({
groupTypeLabel,
onRowClick,
}: FunnelLineTooltipProps): React.ReactElement {
- const seriesData = useMemo<SeriesDatum[]>(
- () =>
- context.seriesData.map((entry, idx) => {
- const meta = entry.series.meta ?? ({} as FunnelSeriesMeta)
- return {
- id: idx,
- dataIndex: context.dataIndex,
- datasetIndex: idx,
- // Each conversion-rate line is a single value, so every series shares one tooltip
- // value column. The inverted layout instead separates them into rows keyed by
- // breakdown_value/compare_label. Distinct orders would split them across columns
- // the layout never renders, leaving every series but the first showing "–".
- order: 0,
- label: entry.series.label,
- color: entry.color,
- count: entry.value,
- breakdown_value: meta.breakdown_value,
- compare_label: meta.compare_label,
- date_label: meta.days?.[context.dataIndex],
- }
- }),
- [context.seriesData, context.dataIndex]
- )
+ const seriesData = useMemo<SeriesDatum[]>(() => {
+ const data = context.seriesData.map((entry, idx) => {
+ const meta = entry.series.meta ?? ({} as FunnelSeriesMeta)
+ return {
+ id: idx,
+ dataIndex: context.dataIndex,
+ datasetIndex: idx,
+ // Each conversion-rate line is a single value, so every series shares one tooltip
+ // value column. The inverted layout instead separates them into rows keyed by
+ // breakdown_value/compare_label. Distinct orders would split them across columns
+ // the layout never renders, leaving every series but the first showing "–".
+ order: 0,
+ label: entry.series.label,
+ color: entry.color,
+ count: entry.value,
+ breakdown_value: meta.breakdown_value,
+ compare_label: meta.compare_label,
+ date_label: meta.days?.[context.dataIndex],
+ }
+ })
+ // Order rows by conversion value descending (matching the trends tooltip) so the
+ // highest-converting series sits at the top, ties broken alphabetically. datasetIndex
+ // keeps the original series index for persons-modal scoping; only the display id is
+ // re-derived from the sorted order.
+ data.sort(
+ (a, b) =>
+ b.count - a.count ||
+ (a.label === undefined || b.label === undefined ? 0 : a.label.localeCompare(b.label))
+ )
+ return data.map((s, id) => ({ ...s, id }))
+ }, [context.seriesData, context.dataIndex])
const date = context.seriesData[0]?.series.meta?.days?.[context.dataIndex]