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(marketing-analytics): compare channels side by side in retention (#88029)

Javier Bahamondes6dad227af37e
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionCohortTable.tsx b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionCohortTable.tsx
index af6f7a4fb294..1b8a560d820c 100644
--- a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionCohortTable.tsx
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionCohortTable.tsx
@@ -22,6 +22,7 @@ import {
MARKETING_ANALYTICS_RETENTION_COLLECTION_ID,
marketingRetentionLogic,
} from '../../logic/marketingRetentionLogic'
+import { RetentionSummaryTable } from './RetentionSummaryTable'
const EXPANDED_BY_DEFAULT = 3
@@ -161,6 +162,7 @@ export function RetentionCohortTable({
return (
<div className="flex flex-col gap-4">
{caveats.length > 0 && <div className="text-secondary text-xs">{caveats.join(' ')}</div>}
+ <RetentionSummaryTable rows={rows} labels={labels} dimensionLabel={dimensionLabel} />
<div>
<div className="text-muted mb-1 text-xs font-semibold uppercase">
All {dimensionLabel.toLowerCase()}s
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionSummaryTable.tsx b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionSummaryTable.tsx
new file mode 100644
index 000000000000..fefe4f0d919e
--- /dev/null
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionSummaryTable.tsx
@@ -0,0 +1,90 @@
+import { LemonTable, LemonTableColumn, Tooltip } from '@posthog/lemon-ui'
+
+import { gradateColor } from 'lib/utils/colors'
+import { humanFriendlyNumber, percentage } from 'lib/utils/numbers'
+
+import { MarketingAnalyticsRetentionRow } from '~/queries/schema/schema-general'
+
+import { displayBreakdownValue } from '../../logic/marketingBreakdown'
+import { SUMMARY_PERIODS, SummaryRow, summarizeByBreakdown } from './summarizeByBreakdown'
+
+/** Same values as the per-cohort tables, so a rate reads as the same shade in both. */
+const CELL_COLOR = '#1d4aff'
+const CELL_COLOR_FLOOR = 0.1
+const CELL_TEXT_LIGHT_THRESHOLD = 0.4
+
+export function RetentionSummaryTable({
+ rows,
+ labels,
+ dimensionLabel,
+}: {
+ rows: MarketingAnalyticsRetentionRow[]
+ labels: string[]
+ dimensionLabel: string
+}): JSX.Element {
+ const summary = summarizeByBreakdown(rows)
+
+ const columns: LemonTableColumn<SummaryRow, any>[] = [
+ {
+ title: dimensionLabel,
+ dataIndex: 'breakdownValue',
+ render: (_, row) => (
+ <span className="font-semibold">{displayBreakdownValue(row.breakdownValue, dimensionLabel)}</span>
+ ),
+ },
+ {
+ title: 'Acquired',
+ dataIndex: 'acquired',
+ align: 'right',
+ sorter: (a, b) => a.acquired - b.acquired,
+ render: (_, row) => humanFriendlyNumber(row.acquired),
+ },
+ ...labels.slice(0, SUMMARY_PERIODS).map((label, period) => ({
+ title: label,
+ key: label,
+ align: 'center' as const,
+ // -1 puts a period no cohort has reached below every real rate, rather than level with 0%.
+ defaultSortOrder: -1 as const,
+ sorter: (a: SummaryRow, b: SummaryRow) => (a.cells[period].rate ?? -1) - (b.cells[period].rate ?? -1),
+ render: (_: any, row: SummaryRow) => {
+ const cell = row.cells[period]
+ if (cell.rate === null) {
+ return (
+ <Tooltip title="No cohort has finished this period yet.">
+ <span className="text-muted">–</span>
+ </Tooltip>
+ )
+ }
+ return (
+ <Tooltip
+ title={`${humanFriendlyNumber(cell.returned)} of ${humanFriendlyNumber(cell.eligible)}, across ${cell.cohorts} ${cell.cohorts === 1 ? 'cohort' : 'cohorts'}`}
+ >
+ <div
+ className="rounded px-2 py-1"
+ // eslint-disable-next-line react/forbid-dom-props
+ style={{
+ backgroundColor: gradateColor(CELL_COLOR, cell.rate, CELL_COLOR_FLOOR),
+ color: cell.rate > CELL_TEXT_LIGHT_THRESHOLD ? '#fff' : 'var(--text-3000)',
+ }}
+ >
+ {percentage(cell.rate, 1)}
+ </div>
+ </Tooltip>
+ )
+ },
+ })),
+ ]
+
+ return (
+ <div>
+ <div className="text-muted mb-1 text-xs font-semibold uppercase">
+ Compare {dimensionLabel.toLowerCase()}s
+ </div>
+ <LemonTable columns={columns} dataSource={summary} size="small" firstColumnSticky />
+ <div className="text-secondary mt-1 text-xs">
+ Each column only counts cohorts that finished that period, so later columns rest on fewer people.
+ Compare within a column, not across.
+ </div>
+ </div>
+ )
+}
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/summarizeByBreakdown.test.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/summarizeByBreakdown.test.ts
new file mode 100644
index 000000000000..a6f92d9543af
--- /dev/null
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/summarizeByBreakdown.test.ts
@@ -0,0 +1,81 @@
+import { BREAKDOWN_OTHER_STRING_LABEL } from 'scenes/insights/utils'
+
+import { MarketingAnalyticsRetentionRow } from '~/queries/schema/schema-general'
+
+import { SUMMARY_PERIODS, summarizeByBreakdown } from './summarizeByBreakdown'
+
+const row = (
+ breakdownValue: string,
+ cohortIndex: number,
+ cohortSize: number,
+ /** One entry per period. `null` is a period the cohort has not finished living through. */
+ counts: (number | null)[]
+): MarketingAnalyticsRetentionRow => ({
+ breakdownValue,
+ cohortDate: '2026-08-02T00:00:00Z',
+ cohortIndex,
+ cohortSize,
+ values: Array.from({ length: SUMMARY_PERIODS }, (_, period) => {
+ const count = counts[period] ?? null
+ return {
+ count: count ?? 0,
+ rate: count === null ? null : count / cohortSize,
+ complete: count !== null,
+ }
+ }),
+})
+
+describe('summarizeByBreakdown', () => {
+ it('weights each period by cohort size rather than averaging the cohorts', () => {
+ // A 100-person cohort retaining 50% and a 900-person cohort retaining 10% is 14%, not the 30%
+ // an unweighted average of the two cohort rates would report.
+ const [summary] = summarizeByBreakdown([row('google', 0, 100, [100, 50]), row('google', 1, 900, [900, 90])])
+
+ expect(summary.cells[1].returned).toBe(140)
+ expect(summary.cells[1].eligible).toBe(1000)
+ expect(summary.cells[1].rate).toBeCloseTo(0.14)
+ })
+
+ it('leaves cohorts that have not finished a period out of that period entirely', () => {
+ // A zero from the unfinished cohort would deflate the column instead of being absent from it.
+ const [summary] = summarizeByBreakdown([row('google', 0, 100, [100, 40]), row('google', 1, 100, [100, null])])
+
+ expect(summary.cells[0].cohorts).toBe(2)
+ expect(summary.cells[1].cohorts).toBe(1)
+ expect(summary.cells[1].eligible).toBe(100)
+ expect(summary.cells[1].rate).toBeCloseTo(0.4)
+ })
+
+ it('reports no rate for a period no cohort has reached', () => {
+ const [summary] = summarizeByBreakdown([row('google', 0, 100, [100, null])])
+
+ expect(summary.cells[2].rate).toBeNull()
+ expect(summary.cells[2].cohorts).toBe(0)
+ })
+
+ it('ranks by how many people the value brought in', () => {
+ const summary = summarizeByBreakdown([row('small', 0, 10, [10]), row('big', 0, 500, [500])])
+
+ expect(summary.map((s) => s.breakdownValue)).toEqual(['big', 'small'])
+ expect(summary[0].acquired).toBe(500)
+ })
+
+ it('leaves the folded "Other" row out of the ranking', () => {
+ // Its 5000 would outrank every real channel, and it is a sum of the tail, not a channel.
+ const summary = summarizeByBreakdown([
+ row(BREAKDOWN_OTHER_STRING_LABEL, 0, 5000, [5000, 4000]),
+ row('google', 0, 100, [100, 50]),
+ ])
+
+ expect(summary.map((s) => s.breakdownValue)).toEqual(['google'])
+ })
+
+ it('does not mutate the rows the per-cohort tables also render', () => {
+ const source = [row('google', 0, 100, [100, 40])]
+
+ summarizeByBreakdown(source)
+
+ expect(source[0].cohortSize).toBe(100)
+ expect(source[0].values[1].count).toBe(40)
+ })
+})
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/summarizeByBreakdown.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/summarizeByBreakdown.ts
new file mode 100644
index 000000000000..0ecfea302fb5
--- /dev/null
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/summarizeByBreakdown.ts
@@ -0,0 +1,70 @@
+import { MarketingAnalyticsRetentionRow } from '~/queries/schema/schema-general'
+
+import { isFoldedBreakdownValue } from '../../logic/marketingBreakdown'
+
+export const SUMMARY_PERIODS = 5
+
+export interface SummaryCell {
+ returned: number
+ /** Only cohorts that fully lived through this period, so an unfinished one can't deflate it. */
+ eligible: number
+ rate: number | null
+ cohorts: number
+}
+
+export interface SummaryRow {
+ breakdownValue: string
+ acquired: number
+ /** Always SUMMARY_PERIODS long. */
+ cells: SummaryCell[]
+}
+
+/**
+ * Rates are weighted by cohort size, so a week that acquired 50 people cannot swing a column as hard
+ * as one that acquired 5,000. The folded "Other" row is left out: it is a sum of the long tail, so
+ * ranking it against real channels floats a row nobody can act on above them.
+ */
+export function summarizeByBreakdown(rows: MarketingAnalyticsRetentionRow[]): SummaryRow[] {
+ const byValue = new Map<string, SummaryRow>()
+
+ for (const row of rows) {
+ if (isFoldedBreakdownValue(row.breakdownValue)) {
+ continue
+ }
+ let summary = byValue.get(row.breakdownValue)
+ if (!summary) {
+ summary = {
+ breakdownValue: row.breakdownValue,
+ acquired: 0,
+ cells: Array.from({ length: SUMMARY_PERIODS }, () => ({
+ returned: 0,
+ eligible: 0,
+ rate: null,
+ cohorts: 0,
+ })),
+ }
+ byValue.set(row.breakdownValue, summary)
+ }
+
+ summary.acquired += row.cohortSize
+ row.values.slice(0, SUMMARY_PERIODS).forEach((cell, period) => {
+ if (!cell.complete) {
+ return
+ }
+ const into = summary.cells[period]
+ into.returned += cell.count
+ into.eligible += row.cohortSize
+ into.cohorts += 1
+ })
+ }
+
+ return [...byValue.values()]
+ .map((summary) => ({
+ ...summary,
+ cells: summary.cells.map((cell) => ({
+ ...cell,
+ rate: cell.eligible ? cell.returned / cell.eligible : null,
+ })),
+ }))
+ .sort((a, b) => b.acquired - a.acquired)
+}