: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(marketing): follow the dashboard date range in retention (#101936)
Javier Bahamondescbfa8489781c
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionTab.tsx b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionTab.tsxindex ba10ccc2066c..01c3dc3ef785 100644--- a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionTab.tsx+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/RetentionTab/RetentionTab.tsx@@ -1,7 +1,7 @@import { BindLogic, useActions, useValues } from 'kea'import { IconGear } from '@posthog/icons'-import { LemonButton, LemonDivider, LemonSelect, LemonSwitch, Popover } from '@posthog/lemon-ui'+import { LemonBanner, LemonButton, LemonDivider, LemonSelect, LemonSwitch, Popover } from '@posthog/lemon-ui'import { CompareFilter } from 'lib/components/CompareFilter/CompareFilter'import { DateFilter } from 'lib/components/DateFilter/DateFilter'@@ -18,6 +18,7 @@ import { marketingAnalyticsLogic } from '../../logic/marketingAnalyticsLogic'import { BREAKDOWN_LABELS } from '../../logic/marketingBreakdown'import {MARKETING_ANALYTICS_RETENTION_COLLECTION_ID,+ MAX_ACQUISITION_PERIOD_DAYS,marketingRetentionLogic,} from '../../logic/marketingRetentionLogic'@@ -27,7 +28,7 @@ const RETENTION_DATE_OPTIONS = dateMapping.filter(({ values }) =>export function RetentionTab(): JSX.Element {const {- dateFilter,+ acquisitionPeriodTooLong,breakdownBy,excludeDirectTraffic,excludeUnattributed,@@ -37,7 +38,6 @@ export function RetentionTab(): JSX.Element {query,} = useValues(marketingRetentionLogic)const {- setDates,setBreakdownBy,setExcludeDirectTraffic,setExcludeUnattributed,@@ -45,6 +45,8 @@ export function RetentionTab(): JSX.Element {setOptionsOpen,setComparePreviousPeriod,} = useActions(marketingRetentionLogic)+ const { dateFilter } = useValues(marketingAnalyticsLogic)+ const { setDates } = useActions(marketingAnalyticsLogic)const optionsContent = (<div className="flex w-80 max-w-[90vw] flex-col gap-4 p-3"><div>@@ -141,7 +143,20 @@ export function RetentionTab(): JSX.Element {}/><div className="mt-4 flex flex-col gap-4 pb-8">- <RetentionResults query={query} attachTo={marketingAnalyticsLogic} />+ {acquisitionPeriodTooLong ? (+ <LemonBanner+ type="warning"+ action={{+ children: `Use the last ${MAX_ACQUISITION_PERIOD_DAYS} days`,+ onClick: () => setDates(`-${MAX_ACQUISITION_PERIOD_DAYS}d`, null),+ }}+ >+ Retention follows people acquired in a period of up to {MAX_ACQUISITION_PERIOD_DAYS} days.+ Pick a shorter date range to see the table.+ </LemonBanner>+ ) : (+ <RetentionResults query={query} attachTo={marketingAnalyticsLogic} />+ )}</div></div></BindLogic>diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingRetentionLogic.test.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingRetentionLogic.test.tsnew file mode 100644index 000000000000..4e6fe9df1847--- /dev/null+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingRetentionLogic.test.ts@@ -0,0 +1,33 @@+import { expectLogic } from 'kea-test-utils'++import { initKeaTests } from '~/test/init'++import { marketingAnalyticsLogic } from './marketingAnalyticsLogic'+import { marketingRetentionLogic } from './marketingRetentionLogic'++it('follows the dashboard date range', async () => {+ initKeaTests()+ const logic = marketingRetentionLogic()+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()+ marketingAnalyticsLogic.actions.setDates('-14d', null)+ expect(logic.values.query.dateRange).toEqual({ date_from: '-14d', date_to: null })+ marketingAnalyticsLogic.actions.setDates('2026-08-01', '2026-08-31')+ expect(logic.values.query.dateRange).toEqual({ date_from: '2026-08-01', date_to: '2026-08-31' })+ logic.unmount()+})++it.each([+ ['-90d', null, false],+ ['2026-06-01', '2026-08-30', false],+ ['-180d', null, true],+ ['all', null, true],+ ['2026-01-01', '2026-06-30', true],+])('flags an acquisition period the backend rejects (%s to %s)', (dateFrom, dateTo, tooLong) => {+ initKeaTests()+ const logic = marketingRetentionLogic()+ logic.mount()+ marketingAnalyticsLogic.actions.setDates(dateFrom, dateTo)+ expect(logic.values.acquisitionPeriodTooLong).toBe(tooLong)+ logic.unmount()+})diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingRetentionLogic.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingRetentionLogic.tsindex 4fa8f346a214..c1eb985d6d38 100644--- a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingRetentionLogic.ts+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/logic/marketingRetentionLogic.ts@@ -1,6 +1,7 @@-import { MakeLogicType, actions, kea, path, reducers, selectors } from 'kea'+import { MakeLogicType, actions, connect, kea, path, reducers, selectors } from 'kea'-import { getDefaultInterval } from 'lib/utils/dateFilters'+import { dayjs } from 'lib/dayjs'+import { dateStringToDayJs } from 'lib/utils/dateFilters'import {MarketingAnalyticsAttributionBreakdown,@@ -9,9 +10,14 @@ import {} from '~/queries/schema/schema-general'import { IntervalType } from '~/types'+import { marketingAnalyticsLogic } from './marketingAnalyticsLogic'+/** Keeps the tab's refresh button off the dashboard tiles, which sit in the scene-level collection. */export const MARKETING_ANALYTICS_RETENTION_COLLECTION_ID = 'marketing-analytics-retention'+/** Mirrors MAX_SUMMARY_ACQUISITION_DAYS in the query runner, which rejects longer periods. */+export const MAX_ACQUISITION_PERIOD_DAYS: number = 90+interface DateFilter {dateFrom: string | nulldateTo: string | null@@ -20,9 +26,14 @@ interface DateFilter {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface marketingRetentionLogicValues {+ dateFilter: {+ dateFrom: string | null+ dateTo: string | null+ interval: IntervalType+ } // marketingAnalyticsLogic+ acquisitionPeriodTooLong: booleanbreakdownBy: MarketingAnalyticsAttributionBreakdowncomparePreviousPeriod: boolean- dateFilter: DateFilterexcludeDirectTraffic: booleanexcludeUnattributed: booleanonlyNewUsers: boolean@@ -38,13 +49,6 @@ export interface marketingRetentionLogicActions {setComparePreviousPeriod: (comparePreviousPeriod: boolean) => {comparePreviousPeriod: boolean}- setDates: (- dateFrom: string | null,- dateTo: string | null- ) => {- dateFrom: string | null- dateTo: string | null- }setExcludeDirectTraffic: (excludeDirectTraffic: boolean) => {excludeDirectTraffic: boolean}@@ -62,12 +66,21 @@ export interface marketingRetentionLogicActions {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface marketingRetentionLogicMeta {__keaTypeGenInternalSelectorTypes: {+ acquisitionPeriodTooLong: (dateFilter: {+ dateFrom: string | null+ dateTo: string | null+ interval: IntervalType+ }) => booleanquery: (breakdownBy: MarketingAnalyticsAttributionBreakdown,excludeDirectTraffic: boolean,excludeUnattributed: boolean,onlyNewUsers: boolean,- dateFilter: DateFilter,+ dateFilter: {+ dateFrom: string | null+ dateTo: string | null+ interval: IntervalType+ },comparePreviousPeriod: boolean) => MarketingAnalyticsRetentionQuery}@@ -82,8 +95,10 @@ export type marketingRetentionLogicType = MakeLogicType<export const marketingRetentionLogic = kea<marketingRetentionLogicType>([path(['scenes', 'webAnalytics', 'marketingRetentionLogic']),+ connect(() => ({+ values: [marketingAnalyticsLogic, ['dateFilter']],+ })),actions({- setDates: (dateFrom: string | null, dateTo: string | null) => ({ dateFrom, dateTo }),setComparePreviousPeriod: (comparePreviousPeriod: boolean) => ({ comparePreviousPeriod }),setBreakdownBy: (breakdownBy: MarketingAnalyticsAttributionBreakdown) => ({ breakdownBy }),setExcludeDirectTraffic: (excludeDirectTraffic: boolean) => ({ excludeDirectTraffic }),@@ -92,16 +107,6 @@ export const marketingRetentionLogic = kea<marketingRetentionLogicType>([setOptionsOpen: (optionsOpen: boolean) => ({ optionsOpen }),}),reducers({- dateFilter: [- { dateFrom: '-30d', dateTo: null, interval: getDefaultInterval('-30d', null) } as DateFilter,- {- setDates: (_, { dateFrom, dateTo }) => ({- dateFrom,- dateTo,- interval: getDefaultInterval(dateFrom, dateTo),- }),- },- ],comparePreviousPeriod: [true,{ setComparePreviousPeriod: (_, { comparePreviousPeriod }) => comparePreviousPeriod },@@ -120,6 +125,17 @@ export const marketingRetentionLogic = kea<marketingRetentionLogicType>([optionsOpen: [false, { setOptionsOpen: (_, { optionsOpen }) => optionsOpen }],}),selectors({+ acquisitionPeriodTooLong: [+ (s) => [s.dateFilter],+ (dateFilter: DateFilter): boolean => {+ const from = dateStringToDayJs(dateFilter.dateFrom)+ if (!from) {+ return true+ }+ const to = dateStringToDayJs(dateFilter.dateTo) ?? dayjs()+ return to.diff(from, 'day') > MAX_ACQUISITION_PERIOD_DAYS+ },+ ],query: [(s) => [s.breakdownBy,