: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(experiments): compute retention metrics in the daily timeseries run (#107604)
Juraj Majerik59c161c23988
diff --git a/posthog/temporal/experiments/README.md b/posthog/temporal/experiments/README.mdindex fbe5b2c803a9..e16c53ceae8c 100644--- a/posthog/temporal/experiments/README.md+++ b/posthog/temporal/experiments/README.md@@ -42,7 +42,7 @@ The experiment page reads results through `GET /metrics_recalculation/latest`, w- A metric qualifies when its newest completed row under the config fingerprint has a `query_to` between the workflow start and now. Yesterday's row for a metric that failed today does not qualify, and neither does a future-dated day-end row from the backfill workflow.- The activity creates a completed recalculation with trigger `timeseries_sync` and copies each qualifying row under the recalc fingerprint at one shared `query_to`, one second past the newest point. Copies at a point's own `query_to` would share the `(experiment, metric_uuid, query_to)` key with the timeseries row and rewrite its fingerprint.-- A supported metric without a qualifying point is counted in `total_metrics` but gets no copy, so the frontend sees the gap and heals it with a real run. Metric types the daily run cannot compute (`DAILY_TIMESERIES_METRIC_TYPES` in `metric_resolution.py`, which excludes retention) are left out of `total_metrics` and `metric_uuids` entirely.+- A supported metric without a qualifying point is counted in `total_metrics` but gets no copy, so the frontend sees the gap and heals it with a real run. Every buildable metric type (`DAILY_TIMESERIES_METRIC_TYPES` in `metric_resolution.py`) is computed daily; legacy metrics without a `metric_type` are left out of `total_metrics` and `metric_uuids` entirely.- The row belongs to the experiment's daily run, not to one workflow. The inline and saved metric workflows both run the activity for the same experiment in the same hour, each with its own run start. The first pass creates the row; a later pass finds the day's `timeseries_sync` row and adds the copies it still lacks, so the row covers every metric the daily run computed whichever workflow finishes last.- When no sync row exists for the day, the activity skips if any other recalculation, finished or executing, already has a `query_to` at or past the oldest qualifying point.diff --git a/products/experiments/backend/temporal/metric_resolution.py b/products/experiments/backend/temporal/metric_resolution.pyindex 8a00af9c3b93..ce914b43cd89 100644--- a/products/experiments/backend/temporal/metric_resolution.py+++ b/products/experiments/backend/temporal/metric_resolution.py@@ -26,9 +26,10 @@ METRIC_BUILDERS: dict[str, type[ExperimentMetric]] = {"retention": ExperimentRetentionMetric,}-# The daily timeseries activities build only these. The sync that turns their points into a recalculation reads-# the same set, so a metric the daily run never computes is not counted as a gap in the row it creates.-DAILY_TIMESERIES_METRIC_TYPES: frozenset[str] = frozenset({"mean", "funnel", "ratio"})+# The daily timeseries activities compute every buildable type. A type the recalculation computes but the+# daily run skips publishes nothing, so the results panel recomputes on first open every morning for the+# experiments that use it; a deliberate future exclusion must subtract from METRIC_BUILDERS explicitly.+DAILY_TIMESERIES_METRIC_TYPES: frozenset[str] = frozenset(METRIC_BUILDERS)def is_daily_timeseries_metric(metric: dict[str, Any] | None) -> bool:diff --git a/products/experiments/backend/test/test_timeseries_sync.py b/products/experiments/backend/test/test_timeseries_sync.pyindex 3e85faa71698..b9b2e5a0732d 100644--- a/products/experiments/backend/test/test_timeseries_sync.py+++ b/products/experiments/backend/test/test_timeseries_sync.py@@ -173,17 +173,20 @@ class TestSyncTimeseriesRecalculation(BaseTest):assert recalc.metric_uuids == ["sm1"]assert [r["metric_uuid"] for r in get_run_results(recalc)] == ["sm1"]- def test_leaves_metric_types_the_daily_run_cannot_compute_out_of_the_row(self):+ def test_publishes_retention_metrics_like_any_other_type(self):+ """Retention was once outside the daily pipeline, so its absence from the published run made the+ page recompute every metric on first open each morning. It now counts and publishes."""exp = self._experiment("sync-retention", [_mean_metric("m1"), _retention_metric("r1")])self._timeseries_point(exp, "m1", IN_RUN, {"m": 1})+ self._timeseries_point(exp, "r1", LATER_IN_RUN, {"r": 1})recalculation_id = self._sync(exp)assert recalculation_id is not Nonerecalc = ExperimentMetricsRecalculation.objects.get(id=recalculation_id)- assert recalc.total_metrics == 1- assert recalc.metric_uuids == ["m1"]- assert {r["metric_uuid"] for r in get_run_results(recalc)} == {"m1"}+ assert recalc.total_metrics == 2+ assert recalc.metric_uuids == ["m1", "r1"]+ assert {r["metric_uuid"] for r in get_run_results(recalc)} == {"m1", "r1"}def test_counts_a_supported_metric_without_a_point_as_a_gap(self):exp = self._experiment("sync-gap", [_mean_metric("m1"), _mean_metric("m2")])