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.

feat(insights): show conversion counts on funnel trends (#97498)

Thomas Obermüller5be9b7e08417
diff --git a/frontend/src/test/insight-testing/test-data.ts b/frontend/src/test/insight-testing/test-data.ts
index f420c01ef4ec..61aab3cbbf1d 100644
--- a/frontend/src/test/insight-testing/test-data.ts
+++ b/frontend/src/test/insight-testing/test-data.ts
@@ -287,6 +287,9 @@ export interface FunnelStepData {
breakdown_value?: string | number
// The funnels runner tags compare rows with `compare_label` (it doesn't set `compare`).
compare_label?: 'current' | 'previous'
+ // Per-period conversion counts, index-aligned with `data`. Absent where a fixture stands in for a cached result.
+ reached_from_step_count?: number[]
+ reached_to_step_count?: number[]
}
export const funnelTrendsSteps = {
@@ -296,6 +299,8 @@ export const funnelTrendsSteps = {
days,
labels,
name: '$pageview → Napped',
+ reached_from_step_count: [200, 200, 200, 200, 200],
+ reached_to_step_count: [20, 50, 80, 120, 70],
} satisfies FunnelStepData,
byBreakdown: {
hedgehog: [
diff --git a/products/product_analytics/backend/hogql_queries/funnels/funnel_trends.py b/products/product_analytics/backend/hogql_queries/funnels/funnel_trends.py
index 6d4ef9871bcc..544cbcdd2b62 100644
--- a/products/product_analytics/backend/hogql_queries/funnels/funnel_trends.py
+++ b/products/product_analytics/backend/hogql_queries/funnels/funnel_trends.py
@@ -438,13 +438,24 @@ class FunnelTrendsUDF(FunnelUDFMixin, FunnelBase):
data = []
days = []
labels = []
+ reached_from_step_count = []
+ reached_to_step_count = []
for row in summary:
timestamp: datetime = row["timestamp"]
data.append(row["conversion_rate"])
hour_min_sec = " %H:%M:%S" if interval.value == "hour" else ""
days.append(timestamp.strftime(f"%Y-%m-%d{hour_min_sec}"))
labels.append(format_label_date(timestamp, self._date_range(), self.context.team.week_start_day))
- return {"count": count, "data": data, "days": days, "labels": labels}
+ reached_from_step_count.append(row["reached_from_step_count"])
+ reached_to_step_count.append(row["reached_to_step_count"])
+ return {
+ "count": count,
+ "data": data,
+ "days": days,
+ "labels": labels,
+ "reached_from_step_count": reached_from_step_count,
+ "reached_to_step_count": reached_to_step_count,
+ }
def _date_range(self):
return QueryDateRange(
diff --git a/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_trends.py b/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_trends.py
index a86208607e33..ba553f5aeeb9 100644
--- a/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_trends.py
+++ b/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_trends.py
@@ -44,6 +44,9 @@ FORMAT_TIME = "%Y-%m-%d %H:%M:%S"
FORMAT_TIME_DAY_END = "%Y-%m-%d 23:59:59"
+_TRENDS_COUNT_KEYS = ("reached_from_step_count", "reached_to_step_count")
+
+
class TestFunnelTrendsUDF(ClickhouseTestMixin, APIBaseTest):
maxDiff = None
@@ -304,6 +307,46 @@ class TestFunnelTrendsUDF(ClickhouseTestMixin, APIBaseTest):
self.assertEqual(len(funnel_trends_persons_nonexistent_converted_results), 0)
+ def test_summarized_results_include_conversion_counts(self):
+ journeys_for(
+ {
+ "user_converted": [
+ {"event": "step one", "timestamp": datetime(2021, 6, 7, 10)},
+ {"event": "step two", "timestamp": datetime(2021, 6, 7, 11)},
+ ],
+ "user_dropped_off": [{"event": "step one", "timestamp": datetime(2021, 6, 7, 12)}],
+ },
+ self.team,
+ )
+
+ query = FunnelsQuery(
+ dateRange=DateRange(
+ date_from="2021-06-07 00:00:00",
+ date_to="2021-06-08 23:59:59",
+ ),
+ interval="day",
+ series=[
+ EventsNode(
+ event="step one",
+ ),
+ EventsNode(
+ event="step two",
+ ),
+ ],
+ funnelsFilter=FunnelsFilter(
+ funnelVizType="trends",
+ funnelWindowInterval=1,
+ funnelWindowIntervalUnit="day",
+ ),
+ )
+
+ results = FunnelsQueryRunner(query=query, team=self.team).calculate().results
+
+ self.assertEqual(1, len(results))
+ self.assertEqual([50.0, 0.0], results[0]["data"])
+ self.assertEqual([2, 0], results[0]["reached_from_step_count"])
+ self.assertEqual([1, 0], results[0]["reached_to_step_count"])
+
# minute, hour, day, week, month
def test_hour_interval(self):
query = FunnelsQuery(
@@ -3394,6 +3437,9 @@ class TestFunnelTrendsUDF(ClickhouseTestMixin, APIBaseTest):
# First Touchpoint (just "one")
results = FunnelsQueryRunner(query=funnels_query, team=self.team).calculate().results
+ # Attribution is what this test covers, and the per-period counts have their own test.
+ results = [{key: value for key, value in result.items() if key not in _TRENDS_COUNT_KEYS} for result in results]
+
self.assertEqual(
[
{
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 0841a6ed4c45..57f33c00d543 100644
--- a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineChart.test.tsx
+++ b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineChart.test.tsx
@@ -37,7 +37,7 @@ describe('FunnelLineChart', () => {
expect(getHogChart().seriesCount).toBe(1)
expect(tooltip.element.textContent).toContain(FUNNEL_CONVERSION_SERIES_LABEL)
- expect(tooltip.element.textContent).toContain('40%')
+ expect(tooltip.element.textContent).toContain('40% (80/200)')
})
it('renders a series per breakdown variant with the breakdown label on each tooltip row', async () => {
diff --git a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineChart.tsx b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineChart.tsx
index bbf5418d57f1..8a072774ac1b 100644
--- a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineChart.tsx
+++ b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/FunnelLineChart.tsx
@@ -33,6 +33,7 @@ import { InsightSeriesTooltip } from '../../shared/InsightSeriesTooltip'
import { INSIGHT_TOOLTIP_CONFIG } from '../../shared/tooltipConfig'
import { buildBaseLegendConfig } from '../../trends/shared/buildBaseLegendConfig'
import { FUNNEL_CONVERSION_SERIES_LABEL, type FunnelSeriesMeta } from '../shared/funnelSeriesMeta'
+import { formatFunnelTrendsCounts } from '../shared/funnelTrendsCounts'
import { buildFunnelLineSeries, buildFunnelLineTimeSeriesConfig, type IndexedFunnelStep } from './funnelChartTransforms'
import { type FunnelLineChartClickDeps, handleFunnelLineChartClick } from './handleFunnelLineChartClick'
@@ -223,7 +224,10 @@ export function FunnelLineChart({
dateRange={insightData?.resolved_date_range ?? undefined}
groupTypeLabel={resolvedGroupTypeLabel}
renderSeriesOverride={(datum) => datum.label ?? ''}
- renderCount={(value) => `${value}%`}
+ renderCount={(value, entry) => {
+ const counts = entry.series.meta ? formatFunnelTrendsCounts(entry.series.meta, ctx.dataIndex) : null
+ return counts ? `${value}% (${counts})` : `${value}%`
+ }}
onRowClick={
showPersonsModal
? (datum) => {
diff --git a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/funnelChartTransforms.test.ts b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/funnelChartTransforms.test.ts
index e986a634c4f6..34ed6eea1ab8 100644
--- a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/funnelChartTransforms.test.ts
+++ b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/funnelChartTransforms.test.ts
@@ -56,6 +56,19 @@ describe('funnelChartTransforms', () => {
expect(series[1].meta).toMatchObject({ breakdown_value: 'Spike', compare_label: 'previous' })
})
+ it('carries the per-period conversion counts into series meta', () => {
+ const step = makeStep({
+ reached_from_step_count: [200, 200, 200, 200, 200],
+ reached_to_step_count: [20, 40, 60, 80, 100],
+ })
+ const series = buildFunnelLineSeries([step], { getColor: () => RED })
+
+ expect(series[0].meta).toMatchObject({
+ reached_from_step_count: [200, 200, 200, 200, 200],
+ reached_to_step_count: [20, 40, 60, 80, 100],
+ })
+ })
+
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 })
diff --git a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/funnelChartTransforms.ts b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/funnelChartTransforms.ts
index 50c9d9704122..8813e9b8bfcc 100644
--- a/products/product_analytics/frontend/insights/funnels/FunnelLineChart/funnelChartTransforms.ts
+++ b/products/product_analytics/frontend/insights/funnels/FunnelLineChart/funnelChartTransforms.ts
@@ -7,20 +7,22 @@ import type { FunnelStepWithNestedBreakdown, IntervalType } from '~/types'
import { buildTrendsLineTimeSeriesConfig, buildTrendsSeries } from '../../trends/TrendsLineChart/trendsChartTransforms'
import type { FunnelSeriesMeta } from '../shared/funnelSeriesMeta'
+import type { FunnelTrendsCounts } from '../shared/funnelTrendsCounts'
// `colorIndex`, `compare`, and `compare_label` are added at runtime by `funnelDataLogic.indexedSteps`
// (the base FunnelStep types don't declare them).
-export type IndexedFunnelStep = FunnelStepWithNestedBreakdown & {
- id: number
- seriesIndex: number
- colorIndex: number
- compare?: boolean
- compare_label?: string | null
-}
+export type IndexedFunnelStep = FunnelStepWithNestedBreakdown &
+ FunnelTrendsCounts & {
+ id: number
+ seriesIndex: number
+ colorIndex: number
+ compare?: boolean
+ compare_label?: string | null
+ }
// The API populates `data`/`days`, but the base FunnelStep type marks them optional —
// normalize so the trends transform never receives undefined.
-interface NormalizedFunnelStep {
+interface NormalizedFunnelStep extends FunnelTrendsCounts {
id: string | number
label: string | null
data: number[]
@@ -41,6 +43,8 @@ function normalizeStep(step: IndexedFunnelStep): NormalizedFunnelStep {
order: step.order,
compare: step.compare,
compare_label: step.compare_label,
+ reached_from_step_count: step.reached_from_step_count,
+ reached_to_step_count: step.reached_to_step_count,
}
}
@@ -63,6 +67,8 @@ export function buildFunnelLineSeries(
compare_label: (step.compare_label ?? undefined) as SeriesDatum['compare_label'],
order: step.order,
label: step.label,
+ reached_from_step_count: step.reached_from_step_count,
+ reached_to_step_count: step.reached_to_step_count,
}),
})
}
diff --git a/products/product_analytics/frontend/insights/funnels/shared/funnelSeriesMeta.ts b/products/product_analytics/frontend/insights/funnels/shared/funnelSeriesMeta.ts
index 978e4e7cccd2..e7292e7d4ef4 100644
--- a/products/product_analytics/frontend/insights/funnels/shared/funnelSeriesMeta.ts
+++ b/products/product_analytics/frontend/insights/funnels/shared/funnelSeriesMeta.ts
@@ -1,8 +1,10 @@
import type { SeriesDatum } from 'scenes/insights/InsightTooltip/insightTooltipUtils'
+import type { FunnelTrendsCounts } from './funnelTrendsCounts'
+
export const FUNNEL_CONVERSION_SERIES_LABEL = 'Conversion'
-export type FunnelSeriesMeta = {
+export type FunnelSeriesMeta = FunnelTrendsCounts & {
days?: string[]
// Narrower than BreakdownKeyType — matches SeriesDatum so the tooltip adapter needs no cast.
breakdown_value?: SeriesDatum['breakdown_value']
diff --git a/products/product_analytics/frontend/insights/funnels/shared/funnelTrendsCounts.test.ts b/products/product_analytics/frontend/insights/funnels/shared/funnelTrendsCounts.test.ts
new file mode 100644
index 000000000000..2c2732845808
--- /dev/null
+++ b/products/product_analytics/frontend/insights/funnels/shared/funnelTrendsCounts.test.ts
@@ -0,0 +1,24 @@
+import { type FunnelTrendsCounts, formatFunnelTrendsCounts } from './funnelTrendsCounts'
+
+describe('funnelTrendsCounts', () => {
+ const series: FunnelTrendsCounts = {
+ reached_from_step_count: [200, 18, 12345],
+ reached_to_step_count: [110, 10, 6789],
+ }
+
+ it.each<[string, number, string]>([
+ ['reads the period out of both arrays, converted first', 1, '10/18'],
+ ['groups thousands so a large funnel stays readable', 2, '6,789/12,345'],
+ ])('%s', (_name, periodIndex, expected) => {
+ expect(formatFunnelTrendsCounts(series, periodIndex)).toBe(expected)
+ })
+
+ it.each<[string, FunnelTrendsCounts, number]>([
+ ['a result cached before the runner returned counts', {}, 0],
+ ['a series carrying only the entered count', { reached_from_step_count: [200] }, 0],
+ ['a series carrying only the converted count', { reached_to_step_count: [110] }, 0],
+ ['a period past the end of the counts', series, 5],
+ ])('returns null for %s', (_name, incomplete, periodIndex) => {
+ expect(formatFunnelTrendsCounts(incomplete, periodIndex)).toBeNull()
+ })
+})
diff --git a/products/product_analytics/frontend/insights/funnels/shared/funnelTrendsCounts.ts b/products/product_analytics/frontend/insights/funnels/shared/funnelTrendsCounts.ts
new file mode 100644
index 000000000000..0721119a4e6b
--- /dev/null
+++ b/products/product_analytics/frontend/insights/funnels/shared/funnelTrendsCounts.ts
@@ -0,0 +1,19 @@
+import { humanFriendlyNumber } from 'lib/utils/numbers'
+
+/** Per-period conversion counts a funnel trends series carries next to its rates, index-aligned with `data`. */
+export interface FunnelTrendsCounts {
+ /** People who entered the funnel in each period: the denominator of the period's rate. */
+ reached_from_step_count?: number[]
+ /** People who converted in each period: the numerator of the period's rate. */
+ reached_to_step_count?: number[]
+}
+
+/** Returns null when the series has no counts for the period. A cached response can lack them. */
+export function formatFunnelTrendsCounts(series: FunnelTrendsCounts, periodIndex: number): string | null {
+ const converted = series.reached_to_step_count?.[periodIndex]
+ const entered = series.reached_from_step_count?.[periodIndex]
+ if (converted == null || entered == null) {
+ return null
+ }
+ return `${humanFriendlyNumber(converted)}/${humanFriendlyNumber(entered)}`
+}
diff --git a/products/product_analytics/frontend/insights/shared/InsightSeriesTooltip.tsx b/products/product_analytics/frontend/insights/shared/InsightSeriesTooltip.tsx
index f0eb7617a7c4..730ad03c5c3b 100644
--- a/products/product_analytics/frontend/insights/shared/InsightSeriesTooltip.tsx
+++ b/products/product_analytics/frontend/insights/shared/InsightSeriesTooltip.tsx
@@ -54,8 +54,8 @@ export interface InsightSeriesTooltipProps<Meta extends InsightSeriesMetaBase> {
/** Override the auto-derived date header — stickiness passes an interval-count integer
* rather than a date, so the default calendar formatter would produce the wrong label. */
altTitle?: string | ((tooltipData: SeriesDatum[], formattedDate: string) => React.ReactNode)
- /** Override the value formatter — pie chart passes slice share alongside the raw count. */
- renderCount?: (value: number) => string
+ /** Override the value formatter: pie slice share, or the funnel counts behind a rate from the entry's meta. */
+ renderCount?: (value: number, entry: InsightSeriesTooltipEntry<Meta>) => string
/** Override the row label — lifecycle uses the status name rather than the event name. */
renderSeriesOverride?: (datum: SeriesDatum) => React.ReactNode
/** Sort rows by value descending. Pass false to preserve visual top-to-bottom order. */
@@ -340,7 +340,7 @@ export function InsightSeriesTooltip<Meta extends InsightSeriesMetaBase>({
value,
(v) =>
formatRowValue(v, {
- override: renderCount,
+ override: renderCount ? (rowValue) => renderCount(rowValue, entry) : undefined,
showPercentView,
isPercentStackView,
trendsFilter,