: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(meta-ads): stop zeroed insights rows from overwriting stored stats (#121405)
posthog[bot]91f551506db8
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.pyindex 1682caba384b..1a2c16aed44d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py@@ -516,6 +516,32 @@ SHRINK_EXHAUSTED_ERROR_MESSAGE = "Meta could not return this data even at the smENTITY_PAGE_REFUSED_ERROR_MESSAGE = "Meta could not return this page even at the smallest page size (retryable)"SMALLEST_PAGE_LIMIT_MAX_RETRIES = 3+INCONSISTENT_INSIGHTS_ERROR_MESSAGE = (+ "Meta returned 0 impressions together with a nonzero CPM or CTR, which can't be correct. "+ "The sync stopped so it doesn't overwrite your stored metrics. Run the sync again later"+)+++def _float_metric(row: dict, field: str) -> float | None:+ try:+ return float(row[field])+ except (KeyError, TypeError, ValueError):+ return None+++def _is_inconsistent_insights_row(row: dict) -> bool:+ """Return True for an Insights row whose base metrics contradict its own ratios.++ Meta derives ``cpm`` from spend and impressions, and ``ctr`` from clicks and impressions, so+ zero impressions with a positive ``cpm`` or ``ctr`` is impossible. Meta has been observed to+ return such rows for multi-day ``time_range`` requests: ``spend``, ``impressions`` and+ ``clicks`` come back as ``"0"`` and the reach fields are missing, while the ratios keep their+ real values. A row with real zero delivery has no ratios, so it does not match.+ """+ if _float_metric(row, "impressions") != 0:+ return False+ return any((_float_metric(row, field) or 0) > 0 for field in ("cpm", "ctr"))+def _parse_json_leniently(response: Response) -> dict | None:"""Parse a Meta API response body as JSON, tolerating trailing garbage after it.@@ -1011,6 +1037,9 @@ def _iter_time_range_pagination(those pages alone, so we exhaust this ladder before restarting the chunkat a smaller size and re-yielding them.+ A page with rows that contradict their own ratios (see ``_is_inconsistent_insights_row``)+ also drops the chunk size to one day, and raises if a single day still returns one.+Resume state captures both levels: ``chunk_since`` + ``chunk_size_days``for the outer loop, ``chunk_next_url`` when the crash happened mid-chunk,and ``chunk_limit`` so a reduced limit persists across resumes.@@ -1143,7 +1172,26 @@ def _iter_time_range_pagination(continuemalformed_json_attempts = 0- yield response_payload.get("data", [])+ rows = response_payload.get("data", [])+ inconsistent_row = next((row for row in rows if _is_inconsistent_insights_row(row)), None)+ if inconsistent_row is not None:+ logger.warning(+ "meta_ads_inconsistent_insights_rows",+ chunk_since=current_start.strftime("%Y-%m-%d"),+ chunk_until=current_end.strftime("%Y-%m-%d"),+ date_start=inconsistent_row.get("date_start"),+ )+ # Single-day requests have returned correct values for the same days, so re-read+ # the window one day at a time. The page is not yielded: its rows would overwrite+ # correct stored metrics with zeros.+ if current_end > current_start:+ chunk_size_days = 1+ _save(current_start, chunk_size_days, None)+ restart_chunk = True+ break+ raise Exception(f"{INCONSISTENT_INSIGHTS_ERROR_MESSAGE} (date: {inconsistent_row.get('date_start')})")++ yield rowsnext_url = response_payload.get("paging", {}).get("next")if not next_url:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.pyindex 9c35f2d76b81..80bb31066561 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py@@ -36,6 +36,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.meta_ads ifrom products.warehouse_sources.backend.temporal.data_imports.sources.meta_ads.meta_ads import (AD_ACCOUNT_LISTING_TIMEOUT_SECONDS,ENTITY_PAGE_REFUSED_ERROR_MESSAGE,+ INCONSISTENT_INSIGHTS_ERROR_MESSAGE,MALFORMED_JSON_MAX_ATTEMPTS,MAX_AD_ACCOUNT_PAGES,META_AD_ACCOUNT_LISTING_UNAVAILABLE_MESSAGE,@@ -1082,6 +1083,44 @@ class TestTimeRangePagination:tr = json.loads(mock_get.return_value.get.call_args_list[1].kwargs["params"]["time_range"])assert tr == {"since": "2026-03-01", "until": "2026-03-07"}+ def test_inconsistent_multi_day_chunk_is_reread_one_day_at_a_time(self) -> None:+ manager = _build_manager()+ zeroed = {"campaign_id": "c", "date_start": "2026-03-01", "impressions": "0", "spend": "0", "cpm": "3.1"}+ day_one = {"campaign_id": "c", "date_start": "2026-03-01", "impressions": "3000", "spend": "9.3", "cpm": "3.1"}+ no_delivery = {"campaign_id": "c", "date_start": "2026-03-02", "impressions": "0", "spend": "0"}++ with scripted_network(+ [+ ScriptedResponse(json={"data": [zeroed, no_delivery], "paging": {}}),+ ScriptedResponse(json={"data": [day_one], "paging": {}}),+ ScriptedResponse(json={"data": [no_delivery], "paging": {}}),+ ]+ ) as network:+ batches = list(+ _iter_time_range_pagination(+ self.URL, self.PARAMS, {"since": "2026-03-01", "until": "2026-03-02"}, None, manager+ )+ )++ assert batches == [[day_one], [no_delivery]]+ assert [json.loads(request.param("time_range") or "") for request in network.requests_log] == [+ {"since": "2026-03-01", "until": "2026-03-02"},+ {"since": "2026-03-01", "until": "2026-03-01"},+ {"since": "2026-03-02", "until": "2026-03-02"},+ ]+ restart_save: MetaAdsResumeConfig = manager.save_state.call_args_list[0].args[0]+ assert (restart_save.chunk_since, restart_save.chunk_size_days) == ("2026-03-01", 1)++ def test_inconsistent_single_day_chunk_raises_instead_of_yielding(self) -> None:+ zeroed = {"campaign_id": "c", "date_start": "2026-03-01", "impressions": "0", "spend": "0", "ctr": "0.09"}++ with scripted_network([ScriptedResponse(json={"data": [zeroed], "paging": {}})]):+ gen = _iter_time_range_pagination(+ self.URL, self.PARAMS, {"since": "2026-03-01", "until": "2026-03-01"}, None, _build_manager()+ )+ with pytest.raises(Exception, match=INCONSISTENT_INSIGHTS_ERROR_MESSAGE):+ next(gen)+def test_heavy_query_subcode_retries_unchanged_then_shrinks_chunk(self, monkeypatch) -> None:monkeypatch.setattr(meta_ads_module, "_backoff_sleep", lambda attempt: None)manager = _build_manager()