: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(twitter_ads): send account-local dates for stats requests (#124001)
Tom Owers61617522252e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/twitter_ads/tests/test_twitter_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/twitter_ads/tests/test_twitter_ads.pyindex 73835a88b000..3a98e4b1f010 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/twitter_ads/tests/test_twitter_ads.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/twitter_ads/tests/test_twitter_ads.py@@ -1,6 +1,6 @@import jsonfrom collections.abc import Iterable-from datetime import UTC, date, datetime, timedelta+from datetime import UTC, date, datetime, time, timedeltafrom typing import Any, castfrom urllib.parse import parse_qs, urlparsefrom zoneinfo import ZoneInfo@@ -130,21 +130,21 @@ def test_stats_limits_daily_rows_currency_and_dst(return response({"data": line_items})if "/stats/" not in path:return response({"data": {"timezone": account_timezone.key, "created_at": "2025-10-25T10:00:00Z"}})- start = datetime.fromisoformat(params["start_time"][0]).astimezone(account_timezone)- end = datetime.fromisoformat(params["end_time"][0]).astimezone(account_timezone)+ # A bare date (not a full timestamp) lets X resolve midnight-to-midnight in the account's+ # own timezone; date.fromisoformat rejects anything else, including a UTC timestamp.+ start_date = date.fromisoformat(params["start_time"][0])+ end_date = date.fromisoformat(params["end_time"][0])+ start = datetime.combine(start_date, time.min, account_timezone)+ end = datetime.combine(end_date, time.min, account_timezone)ids = params["entity_ids"][0].split(",")assert len(ids) <= 20assert end.astimezone(UTC) - start.astimezone(UTC) <= timedelta(days=7)- # X rejects a DAY window whose bounds are not midnight in the ad account's own timezone,- # which means the offset in force on each date rather than today's.- assert start.hour == end.hour == 0- assert start.minute == end.minute == 0offsets.update({start.utcoffset(), end.utcoffset()})assert params["granularity"] == ["DAY"]assert params["metric_groups"] == ["ENGAGEMENT,BILLING"]assert params["entity"] == ["CAMPAIGN" if table == "campaign_stats" else "LINE_ITEM"]- days = (end.date() - start.date()).days- calls.append((start.date(), end.date(), params["placement"][0], ids))+ days = (end_date - start_date).days+ calls.append((start_date, end_date, params["placement"][0], ids))return response({"data": [@@ -183,7 +183,7 @@ def test_stats_limits_daily_rows_currency_and_dst(assert max(row["date"] for row in rows) == date(2025, 11, 9)assert {call[2] for call in calls} == set(PLACEMENTS)# The window walked across the November fall-back, so it must have used both offsets. Without- # this the midnight assertions above would still pass on a range that never changes offset.+ # this the test would still pass on a range that never changes offset.assert offsets == {timedelta(hours=-7), timedelta(hours=-8)}assert resource.primary_keys == ["entity_id", "date", "placement"]assert resource.partition_keys == ["date"]@@ -200,6 +200,38 @@ def test_stats_limits_daily_rows_currency_and_dst(assert max(row["date"] for row in resumed) == date(2025, 11, 9)+def test_stats_requests_bare_dates_for_a_half_hour_offset_account(client: TwitterAdsClient, manager: MagicMock) -> None:+ # An account on IST (UTC+5:30) never has a UTC midnight; sending our own UTC conversion for+ # start_time/end_time would carry ":30" minutes and X rejects that with a 400.+ account_timezone = ZoneInfo("Asia/Kolkata")+ campaigns = [{"id": "campaign-00", "funding_instrument_id": "funding"}]+ requests_sent = []++ def send(request: requests.PreparedRequest, **kwargs: object) -> requests.Response:+ url = request_url(request)+ path = urlparse(url).path+ if path.endswith("/funding_instruments"):+ return response({"data": [{"id": "funding", "currency": "INR"}]})+ if path.endswith("/campaigns"):+ return response({"data": campaigns})+ if "/stats/" not in path:+ return response({"data": {"timezone": account_timezone.key, "created_at": "2025-10-25T10:00:00Z"}})+ requests_sent.append(parse_qs(urlparse(url).query))+ return response({"data": []})++ with (+ time_machine.travel("2025-11-01T20:00:00Z", tick=False),+ patch.object(client.session, "send", side_effect=send),+ ):+ resource = twitter_ads_source(client, "account", "campaign_stats", manager, date(2025, 10, 30))+ list(sync_items(resource))++ assert requests_sent+ for params in requests_sent:+ assert date.fromisoformat(params["start_time"][0])+ assert date.fromisoformat(params["end_time"][0])++def test_error_detail_rides_along_with_the_status(client: TwitterAdsClient) -> None:failure = requests.Response()failure.status_code = 400diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/twitter_ads/twitter_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/twitter_ads/twitter_ads.pyindex d032483343da..c6b9596ffb6d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/twitter_ads/twitter_ads.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/twitter_ads/twitter_ads.py@@ -219,8 +219,13 @@ class TwitterAdsClient:{"entity": STATS_TABLES[table],"entity_ids": ",".join(entity_ids[batch_start : batch_start + 20]),- "start_time": start_time.isoformat().replace("+00:00", "Z"),- "end_time": end_time.isoformat().replace("+00:00", "Z"),+ # A bare date tells X to resolve midnight-to-midnight in the account's+ # own timezone itself. An account on a half-hour-offset timezone (e.g.+ # IST) never lands on a whole UTC hour, so sending our own UTC+ # conversion here gets a 400 ("whole hours" requirement) that this+ # sidesteps entirely.+ "start_time": start.isoformat(),+ "end_time": window_end.isoformat(),"granularity": "DAY","placement": placement,"metric_groups": "ENGAGEMENT,BILLING",