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(warehouse_sources): floor Apple Search Ads report lookback within Apple's API limit (#86111)

Tom Owers76d41b16cf9d
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/apple_search_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/apple_search_ads.py
index 97dd0a5fcb22..00f8e911be88 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/apple_search_ads.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/apple_search_ads.py
@@ -308,8 +308,8 @@ def _report_start_date(
configured = _to_date(start_date) if start_date else None
if configured is not None:
- # Floor the configured date so an implausibly old start can't fan the report out over
- # thousands of empty windows and exhaust the import worker.
+ # Apple rejects a DAILY-granularity report whose startTime is more than 24 months in
+ # the past with a 400, so a configured date older than that is floored here.
return max(configured, today - timedelta(days=MAX_INITIAL_LOOKBACK_DAYS))
return today - timedelta(days=DEFAULT_INITIAL_LOOKBACK_DAYS)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/settings.py
index 246551ea458e..04ad5cb71994 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/settings.py
@@ -29,10 +29,11 @@ REPORT_LOOKBACK_SECONDS = REPORT_WINDOW_DAYS * 24 * 60 * 60
# How far back the first sync of a report table reaches when the user gives no start date.
DEFAULT_INITIAL_LOOKBACK_DAYS = 365
-# The earliest a configured start date may reach. Apple Search Ads has held no reporting data
-# from before it launched, so a start date older than this is a typo — clamp it rather than fan
-# a report out over thousands of empty windows (one report request per window per campaign).
-MAX_INITIAL_LOOKBACK_DAYS = 11 * 365
+# The earliest a configured start date may reach. Apple's Reporting API rejects a DAILY-
+# granularity report (the only granularity this source requests) whose startTime is more than
+# 24 months in the past with a 400 — treating a month as 30 days keeps this clamp safely inside
+# that limit despite calendar-month variation.
+MAX_INITIAL_LOOKBACK_DAYS = 24 * 30
@dataclass(frozen=True)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py
index 3a75228d5d21..e9ad21bfd1ec 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py
@@ -406,6 +406,11 @@ class TestReportWindows:
else:
assert resolved == (expected or today - timedelta(days=DEFAULT_INITIAL_LOOKBACK_DAYS))
+ def test_max_initial_lookback_stays_within_apples_daily_report_limit(self) -> None:
+ # Apple's Reporting API rejects a DAILY-granularity report whose startTime is more than
+ # 24 months in the past with a 400 — this guards against re-widening the floor past it.
+ assert MAX_INITIAL_LOOKBACK_DAYS <= 24 * 30
+
class TestFlattenReportRows:
def test_granularity_buckets_become_one_row_per_day(self) -> None: