: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
feat(retention): add cohort label start index toggle (#59395)
Georgis Andonis8591c9ba319b
diff --git a/frontend/src/queries/nodes/InsightViz/InsightDisplayConfig.tsx b/frontend/src/queries/nodes/InsightViz/InsightDisplayConfig.tsxindex 63f16e76e305..1af1decee3b8 100644--- a/frontend/src/queries/nodes/InsightViz/InsightDisplayConfig.tsx+++ b/frontend/src/queries/nodes/InsightViz/InsightDisplayConfig.tsx@@ -32,6 +32,7 @@ import { ShowTrendLinesFilter } from 'scenes/insights/EditorFilters/ShowTrendLinimport { ValueOnSeriesFilter } from 'scenes/insights/EditorFilters/ValueOnSeriesFilter'import { InsightDateFilter } from 'scenes/insights/filters/InsightDateFilter'import { RetentionChartPicker } from 'scenes/insights/filters/RetentionChartPicker'+import { RetentionCohortLabelStartIndexPicker } from 'scenes/insights/filters/RetentionCohortLabelStartIndexPicker'import { RetentionDashboardDisplayPicker } from 'scenes/insights/filters/RetentionDashboardDisplayPicker'import { insightLogic } from 'scenes/insights/insightLogic'import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'@@ -322,6 +323,17 @@ export function InsightDisplayConfig(): JSX.Element {title: 'On dashboards',items: [{ label: () => <RetentionDashboardDisplayPicker /> }],},+ {+ title: (+ <h5 className="mx-2 my-1">+ Cohort labels start at{' '}+ <Tooltip title="Controls the starting index used to label cohort columns. Display only, does not affect the calculations.">+ <IconInfo className="relative top-0.5 text-lg text-secondary" />+ </Tooltip>+ </h5>+ ),+ items: [{ label: () => <RetentionCohortLabelStartIndexPicker /> }],+ },]: []),]diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.jsonindex be82fbd777f0..dd76e2ebe217 100644--- a/frontend/src/queries/schema.json+++ b/frontend/src/queries/schema.json@@ -33445,6 +33445,11 @@"enum": ["count", "sum", "avg"],"type": "string"},+ "cohortLabelStartIndex": {+ "$ref": "#/definitions/integer",+ "default": 0,+ "description": "Starting index used when labeling cohort columns (e.g. 0 for D0/D1/D2, 1 for D1/D2/D3). Display-only — does not affect retention calculations."+ },"cumulative": {"type": "boolean"},diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.tsindex 572353f04c27..2ca779565401 100644--- a/frontend/src/queries/schema/schema-general.ts+++ b/frontend/src/queries/schema/schema-general.ts@@ -1691,6 +1691,9 @@ export type RetentionFilter = {/** The selected interval to display across all cohorts (null = show all intervals for each cohort) */selectedInterval?: integer | nullgoalLines?: GoalLine[]+ /** @description Starting index used when labeling cohort columns (e.g. 0 for D0/D1/D2, 1 for D1/D2/D3). Display-only — does not affect retention calculations.+ * @default 0 */+ cohortLabelStartIndex?: integer}export interface RetentionValue {diff --git a/frontend/src/scenes/insights/filters/RetentionCohortLabelStartIndexPicker.tsx b/frontend/src/scenes/insights/filters/RetentionCohortLabelStartIndexPicker.tsxnew file mode 100644index 000000000000..2eb46cd4f981--- /dev/null+++ b/frontend/src/scenes/insights/filters/RetentionCohortLabelStartIndexPicker.tsx@@ -0,0 +1,41 @@+import { useActions, useValues } from 'kea'++import { LemonSegmentedButton } from '@posthog/lemon-ui'++import { insightLogic } from 'scenes/insights/insightLogic'+import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'++export function RetentionCohortLabelStartIndexPicker(): JSX.Element | null {+ const { insightProps, canEditInsight } = useValues(insightLogic)++ const { retentionFilter } = useValues(insightVizDataLogic(insightProps))+ const { updateInsightFilter } = useActions(insightVizDataLogic(insightProps))++ if (!canEditInsight) {+ return null+ }++ const period = retentionFilter?.period || 'Day'++ return (+ <LemonSegmentedButton+ className="pb-2 px-2"+ value={retentionFilter?.cohortLabelStartIndex ?? 0}+ onChange={(value: number) => {+ updateInsightFilter({ cohortLabelStartIndex: value })+ }}+ options={[+ {+ value: 0,+ label: `${period} 0`,+ },+ {+ value: 1,+ label: `${period} 1`,+ },+ ]}+ size="small"+ fullWidth+ />+ )+}diff --git a/frontend/src/scenes/retention/retentionGraphLogic.ts b/frontend/src/scenes/retention/retentionGraphLogic.tsindex 1b9dcf98bafd..7a5fa5e6631d 100644--- a/frontend/src/scenes/retention/retentionGraphLogic.ts+++ b/frontend/src/scenes/retention/retentionGraphLogic.ts@@ -47,14 +47,15 @@ export const retentionGraphLogic = kea<retentionGraphLogicType>([retentionFilter: RetentionFilter | null | undefined,isPropertyValueAggregation: boolean): RetentionTrendPayload[] => {- const { period } = retentionFilter || {}+ const { period, cohortLabelStartIndex } = retentionFilter || {}+ const offset = cohortLabelStartIndex ?? 0return results.map((cohortRetention: ProcessedRetentionPayload, datasetIndex) => {return {...cohortRetention,id: datasetIndex,days: cohortRetention.values.map((value) => value.cellDate.toISOString()),- labels: cohortRetention.values.map((_, index) => `${period} ${index}`),+ labels: cohortRetention.values.map((_, index) => `${period} ${index + offset}`),count: 0,label: formatRetentionCohortLabel(cohortRetention, period),data: cohortRetention.values.map((value) =>@@ -229,7 +230,8 @@ export const retentionGraphLogic = kea<retentionGraphLogicType>([return []}- const { period } = retentionFilter || {}+ const { period, cohortLabelStartIndex } = retentionFilter || {}+ const offset = cohortLabelStartIndex ?? 0const meanSeries: RetentionTrendPayload[] = []let seriesId = 0@@ -241,7 +243,7 @@ export const retentionGraphLogic = kea<retentionGraphLogicType>([}const numIntervals = meanData.meanPercentages.length- const days = Array.from({ length: numIntervals }, (_, i) => `${period} ${i}`)+ const days = Array.from({ length: numIntervals }, (_, i) => `${period} ${i + offset}`)// Use centralized breakdown display namesconst displayLabel = breakdownDisplayNames[String(meanData.label ?? '')] || meanData.label@@ -280,7 +282,8 @@ export const retentionGraphLogic = kea<retentionGraphLogicType>([if (!retentionFilter || !results) {return []}- const { period, retentionCustomBrackets, selectedInterval } = retentionFilter+ const { period, retentionCustomBrackets, selectedInterval, cohortLabelStartIndex } = retentionFilter+ const offset = cohortLabelStartIndex ?? 0// When an interval is selected, show cohort dates on x-axisif (selectedInterval !== null && selectedInterval !== undefined) {@@ -296,11 +299,11 @@ export const retentionGraphLogic = kea<retentionGraphLogicType>([const unit = dateOptionPlurals[period || 'Day']if (retentionCustomBrackets) {- const labels = [`${period || 'Day'} 0`]+ const labels = [`${period || 'Day'} ${offset}`]let cumulativeTotal = 1for (const bracketSize of retentionCustomBrackets) {- const start = cumulativeTotal- const end = cumulativeTotal + bracketSize - 1+ const start = cumulativeTotal + offset+ const end = cumulativeTotal + bracketSize - 1 + offsetif (start === end) {labels.push(`${unit} ${start}`)} else {@@ -310,7 +313,7 @@ export const retentionGraphLogic = kea<retentionGraphLogicType>([}return labels}- return results?.[0]?.values.map((_, i) => `${period} ${i}`)+ return results?.[0]?.values.map((_, i) => `${period} ${i + offset}`)},],}),diff --git a/frontend/src/scenes/retention/retentionTableLogic.ts b/frontend/src/scenes/retention/retentionTableLogic.tsindex b35cd90f1ba9..a5b3f35c4387 100644--- a/frontend/src/scenes/retention/retentionTableLogic.ts+++ b/frontend/src/scenes/retention/retentionTableLogic.ts@@ -102,13 +102,14 @@ export const retentionTableLogic = kea<retentionTableLogicType>([(results: ProcessedRetentionPayload[], insightQuery: InsightQueryNode | null): string[] => {if (results.length > 0 && results[0].values.length > 0) {if (isRetentionQuery(insightQuery) && insightQuery.retentionFilter?.retentionCustomBrackets) {- const { period, retentionCustomBrackets } = insightQuery.retentionFilter+ const { period, retentionCustomBrackets, cohortLabelStartIndex } = insightQuery.retentionFilter+ const offset = cohortLabelStartIndex ?? 0const unit = capitalizeFirstLetter(dateOptionPlurals[period || 'Day'])- const labels = [`${period || 'Day'} 0`]+ const labels = [`${period || 'Day'} ${offset}`]let cumulativeTotal = 1for (const bracketSize of retentionCustomBrackets) {- const start = cumulativeTotal- const end = cumulativeTotal + bracketSize - 1+ const start = cumulativeTotal + offset+ const end = cumulativeTotal + bracketSize - 1 + offsetif (start === end) {labels.push(`${unit} ${start}`)} else {@@ -119,7 +120,10 @@ export const retentionTableLogic = kea<retentionTableLogicType>([return labels}if (isRetentionQuery(insightQuery)) {- return results[0].values.map((_, i) => `${insightQuery.retentionFilter?.period || 'Day'} ${i}`)+ const offset = insightQuery.retentionFilter?.cohortLabelStartIndex ?? 0+ return results[0].values.map(+ (_, i) => `${insightQuery.retentionFilter?.period || 'Day'} ${i + offset}`+ )}}return []diff --git a/posthog/schema.py b/posthog/schema.pyindex 95183a3445c2..7858f3b7e6f6 100644--- a/posthog/schema.py+++ b/posthog/schema.py@@ -17979,6 +17979,13 @@ class RetentionFilter(BaseModel):default=AggregationType.COUNT,description="The aggregation type to use for retention",)+ cohortLabelStartIndex: int | None = Field(+ default=0,+ description=(+ "Starting index used when labeling cohort columns (e.g. 0 for D0/D1/D2, 1"+ " for D1/D2/D3). Display-only — does not affect retention calculations."+ ),+ )cumulative: bool | None = NonecustomAggregationTarget: bool | None = Field(default=None,diff --git a/products/product_analytics/frontend/generated/api.schemas.ts b/products/product_analytics/frontend/generated/api.schemas.tsindex 70a85baf7138..2493a2ab14ec 100644--- a/products/product_analytics/frontend/generated/api.schemas.ts+++ b/products/product_analytics/frontend/generated/api.schemas.ts@@ -2158,6 +2158,8 @@ export interface RetentionFilterApi {aggregationPropertyType?: AggregationPropertyType1Api | null/** The aggregation type to use for retention */aggregationType?: AggregationTypeApi | null+ /** Starting index used when labeling cohort columns (e.g. 0 for D0/D1/D2, 1 for D1/D2/D3). Display-only — does not affect retention calculations. */+ cohortLabelStartIndex?: number | nullcumulative?: boolean | null/** For data warehouse based retention insights when the aggregation target can't be mapped to persons or groups. */customAggregationTarget?: boolean | nulldiff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 7f915ec87687..8a3c73439924 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -2457,6 +2457,8 @@ export namespace Schemas {aggregationPropertyType?: AggregationPropertyType1 | null;/** The aggregation type to use for retention */aggregationType?: AggregationType | null;+ /** Starting index used when labeling cohort columns (e.g. 0 for D0/D1/D2, 1 for D1/D2/D3). Display-only — does not affect retention calculations. */+ cohortLabelStartIndex?: number | null;cumulative?: boolean | null;/** For data warehouse based retention insights when the aggregation target can't be mapped to persons or groups. */customAggregationTarget?: boolean | null;