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.

fix(marketing-analytics): bound the attribution window at the run time (#94142)

posthog[bot]9d080d47278d
diff --git a/products/marketing_analytics/backend/services/attribution_health.py b/products/marketing_analytics/backend/services/attribution_health.py
index 1ccf7e0acba3..5b638de31de7 100644
--- a/products/marketing_analytics/backend/services/attribution_health.py
+++ b/products/marketing_analytics/backend/services/attribution_health.py
@@ -278,8 +278,13 @@ def _fetch_utm_groups(team: Team, *, lookback_days: int) -> list[_UtmRow]:
Intentionally not restricted to `$pageview` — conversion goals are often custom
events, so attribution should reflect all UTM-tagged activity.
+
+ The window ends at the run time. Event timestamps come from the client, so a device
+ with a wrong clock can stamp an event years ahead and make `max(timestamp)` report a
+ last-seen date in the future.
"""
- since = timezone.now() - timedelta(days=lookback_days)
+ now = timezone.now()
+ since = now - timedelta(days=lookback_days)
hogql = """
SELECT
lower(trim(properties.utm_source)) AS raw_utm_source,
@@ -297,6 +302,7 @@ def _fetch_utm_groups(team: Team, *, lookback_days: int) -> list[_UtmRow]:
FROM events
WHERE
timestamp >= {since}
+ AND timestamp <= {until}
AND properties.utm_source IS NOT NULL
AND properties.utm_source != ''
GROUP BY raw_utm_source
@@ -309,6 +315,7 @@ def _fetch_utm_groups(team: Team, *, lookback_days: int) -> list[_UtmRow]:
team,
placeholders={
"since": ast.Constant(value=since),
+ "until": ast.Constant(value=now),
"limit": ast.Constant(value=HOGQL_GROUP_LIMIT),
},
)
diff --git a/products/marketing_analytics/backend/services/test_attribution_health.py b/products/marketing_analytics/backend/services/test_attribution_health.py
index e6fcb11ded52..2c22e7a2bd6c 100644
--- a/products/marketing_analytics/backend/services/test_attribution_health.py
+++ b/products/marketing_analytics/backend/services/test_attribution_health.py
@@ -1,6 +1,7 @@
from datetime import UTC, datetime, timedelta
import pytest
+import time_machine
from posthog.test.base import APIBaseTest, BaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events
from unittest.mock import AsyncMock, patch
@@ -180,6 +181,7 @@ def _pageview(
utm_source: str | None = None,
utm_medium: str | None = None,
gclid: str | None = None,
+ timestamp: datetime | None = None,
) -> None:
props: dict = {}
if utm_source is not None:
@@ -193,7 +195,7 @@ def _pageview(
event="$pageview",
team=team,
properties=props,
- timestamp=timezone.now() - timedelta(hours=1),
+ timestamp=timestamp or timezone.now() - timedelta(hours=1),
)
@@ -418,3 +420,28 @@ class TestAttributionHealthPaidSignalClickhouse(ClickhouseTestMixin, BaseTest):
assert google.events_matched_paid_last_7d == 1
assert google.events_matched_tagged_medium_last_7d == 0
+
+
+@time_machine.travel("2025-06-15", tick=False)
+class TestAttributionHealthFutureTimestampClickhouse(ClickhouseTestMixin, BaseTest):
+ CLASS_DATA_LEVEL_SETUP = False
+
+ def setUp(self) -> None:
+ super().setUp()
+ _pageview(self.team, "u1", utm_source="google")
+ _pageview(self.team, "u2", utm_source="google", timestamp=timezone.now() + timedelta(days=1800))
+ flush_persons_and_events()
+
+ def tearDown(self) -> None:
+ flush_persons_and_events()
+ super().tearDown()
+
+ @pytest.mark.asyncio
+ async def test_future_stamped_event_is_excluded(self) -> None:
+ response = await get_attribution_health(self.team, lookback_days=30)
+
+ google = next(e for e in response.integrations if e.integration_key == "google_ads")
+ assert google.events_matched_last_7d == 1
+ assert response.total_events_with_utm == 1
+ assert google.last_event_with_matching_utm_at is not None
+ assert google.last_event_with_matching_utm_at <= timezone.now()