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): read cost dates as UTC when building precompute windows (#125347)

Xandera3750bbdc942
diff --git a/products/marketing_analytics/backend/hogql_queries/adapters/base.py b/products/marketing_analytics/backend/hogql_queries/adapters/base.py
index 94d851dcbc0f..f154a007aa97 100644
--- a/products/marketing_analytics/backend/hogql_queries/adapters/base.py
+++ b/products/marketing_analytics/backend/hogql_queries/adapters/base.py
@@ -878,7 +878,11 @@ class MarketingSourceAdapter(ABC, Generic[ConfigType]):
The window is half-open `[min, max)` to match the framework convention — using `<=` on the
upper bound double-counts boundary days that fall in two adjacent job windows."""
stats_table_name = self._level_tables().stats_table.name
- date_field = ast.Call(name="toDateTime", args=[ast.Field(chain=[stats_table_name, self._stats_date_column])])
+ date_field = ast.Call(
+ name="toDateTime",
+ # Job windows are UTC days; the team timezone would shift day D into window D-1 east of UTC.
+ args=[ast.Field(chain=[stats_table_name, self._stats_date_column]), ast.Constant(value="UTC")],
+ )
return ast.And(
exprs=[
ast.CompareOperation(
diff --git a/products/marketing_analytics/backend/hogql_queries/test_marketing_costs_precompute.py b/products/marketing_analytics/backend/hogql_queries/test_marketing_costs_precompute.py
index 27ab6d4a29c2..112742296b26 100644
--- a/products/marketing_analytics/backend/hogql_queries/test_marketing_costs_precompute.py
+++ b/products/marketing_analytics/backend/hogql_queries/test_marketing_costs_precompute.py
@@ -1,5 +1,5 @@
import uuid
-from datetime import date, datetime
+from datetime import UTC, date, datetime
from pathlib import Path
from posthog.test.base import BaseTest, ClickhouseTestMixin
@@ -152,6 +152,25 @@ class TestMarketingCostsPrecompute(ClickhouseTestMixin, BaseTest):
# cost_date is a real per-day date (not the sentinel empty), enabling daily-window caching.
assert all(r[self._col(cols, "cost_date")] is not None for r in rows)
+ @parameterized.expand([("ahead_of_utc", "Asia/Tokyo"), ("utc", "UTC"), ("behind_utc", "America/Los_Angeles")])
+ def test_daily_job_window_holds_only_its_own_cost_date(self, _name, team_timezone):
+ self.team.timezone = team_timezone
+ self.team.save()
+ mat = self._adapter().build_materialization_query("google_test")
+ assert mat is not None
+ # Run the AST directly, because a to_hogql round trip drops the window's UTC timezone.
+ mat_query = replace_placeholders(
+ mat,
+ {
+ "time_window_min": ast.Constant(value=datetime(2024, 3, 16, tzinfo=UTC)),
+ "time_window_max": ast.Constant(value=datetime(2024, 3, 17, tzinfo=UTC)),
+ },
+ )
+ result = execute_hogql_query(mat_query, self.team)
+ cols = list(result.columns or [])
+
+ assert {r[self._col(cols, "cost_date")] for r in result.results or []} == {date(2024, 3, 16)}
+
@parameterized.expand(
[
("stale_and_matured_job", "c1", "c1"),