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(data-imports): pin google search console metric types to prevent int64/double drift (#74620)

Tom Owersd1818aadbbeb
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py
index 07ac0418baf5..7e12c4d2a76e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py
@@ -417,10 +417,15 @@ def _row_to_dict(row: dict[str, Any], dimensions: list[str], iter_date: dt.date
# partition in the warehouse. The iterator already calls one day at a time, so
# any row returned belongs to that day — inject it explicitly here.
out["date"] = iter_date
- out["clicks"] = row.get("clicks", 0)
- out["impressions"] = row.get("impressions", 0)
- out["ctr"] = row.get("ctr", 0.0)
- out["position"] = row.get("position", 0.0)
+ # Pin each metric to its numeric type. Google serializes an exact-zero `ctr`/`position` as a
+ # JSON integer (`0`, not `0.0`), so a day where every row has zero clicks yields an all-int
+ # column that the pipeline stores as int64. A later day's fractional rate then arrives as
+ # double and can't cast into that int64 column, failing the sync until a full reset. Coercing
+ # the rates to float (and the counts to int) keeps the stored Delta type stable across days.
+ out["clicks"] = int(row.get("clicks", 0))
+ out["impressions"] = int(row.get("impressions", 0))
+ out["ctr"] = float(row.get("ctr", 0.0))
+ out["position"] = float(row.get("position", 0.0))
return out
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console.py
index 68deb396138e..6dbc74379516 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console.py
@@ -103,6 +103,19 @@ def test_row_to_dict_handles_missing_metrics():
assert out["position"] == 0.0
+def test_row_to_dict_coerces_integer_serialized_rates_to_float():
+ # Google serializes an exact-zero rate as JSON `0`, which json.loads decodes to a Python int.
+ # Left as-is, an all-zero-clicks day would store `ctr`/`position` as int64 and reject a later
+ # day's fractional rate. The rates must always be floats; the counts always ints.
+ row = {"keys": ["2026-04-15"], "clicks": 3, "impressions": 20, "ctr": 0, "position": 0}
+ out = _row_to_dict(row, ["date"])
+
+ assert isinstance(out["ctr"], float)
+ assert isinstance(out["position"], float)
+ assert isinstance(out["clicks"], int)
+ assert isinstance(out["impressions"], int)
+
+
def test_row_to_dict_injects_iter_date_when_date_not_in_dimensions():
# `searchAppearance` schema can't include date in its API request, so the iterator
# supplies the date externally to keep the per-day partition.