: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(data-warehouse): accept any Google Ads customer ID format (#62421)
Tom Owers16f31965dd31
diff --git a/posthog/temporal/data_imports/sources/google_ads/configs.py b/posthog/temporal/data_imports/sources/google_ads/configs.pyindex 471c378448a7..0555520e32d0 100644--- a/posthog/temporal/data_imports/sources/google_ads/configs.py+++ b/posthog/temporal/data_imports/sources/google_ads/configs.py@@ -8,6 +8,7 @@ protobuf descriptors. Nothing here imports the SDK — that import is deferred trun path in ``source.py``. See ``google_ads.py`` for the SDK-backed functions."""+import reimport dataclassesfrom posthog.temporal.data_imports.sources.common import config@@ -26,14 +27,17 @@ class GoogleAdsResumeConfig:def clean_customer_id(s: str | None) -> str | None:- """Clean customer IDs from Google Ads.+ """Normalize a Google Ads customer ID to its bare digits.- Customer IDs can contain dashes, but we need the ID without them.+ The Google Ads UI shows customer IDs as ``123-456-7890`` but the API wants the+ bare ``1234567890``. Users paste them with dashes, spaces, or surrounding+ whitespace, so strip everything that isn't a digit — any of those forms then+ works rather than getting rejected at setup time."""if not s:return s- return s.strip().replace("-", "")+ return re.sub(r"\D", "", s)@config.configdiff --git a/posthog/temporal/data_imports/sources/google_ads/source.py b/posthog/temporal/data_imports/sources/google_ads/source.pyindex 75a4321d72c5..590ae2937613 100644--- a/posthog/temporal/data_imports/sources/google_ads/source.py+++ b/posthog/temporal/data_imports/sources/google_ads/source.py@@ -189,19 +189,26 @@ class GoogleAdsSource(def validate_config(self, job_inputs: dict) -> tuple[bool, list[str]]:is_valid, errors = super().validate_config(job_inputs)- customer_id = job_inputs.get("customer_id", "")- if customer_id and not re.match(r"^\d{3}-\d{3}-\d{4}$", customer_id):+ # Normalize before validating: `clean_customer_id` strips dashes, spaces and+ # whitespace, so `123-456-7890`, `1234567890`, or a copy-pasted value all pass.+ # The same normalization is applied wherever the id is sent to the API. We guard+ # on the raw value so a non-numeric entry (which normalizes to empty) is still+ # rejected rather than silently slipping through.+ raw_customer_id = job_inputs.get("customer_id", "")+ if raw_customer_id and not re.fullmatch(r"\d{10}", clean_customer_id(raw_customer_id) or ""):errors.append(- "Please enter a valid Google Ads customer ID. This should be 10-digits and in XXX-XXX-XXXX format."+ "Please enter a valid Google Ads customer ID — the 10-digit number from your "+ "Google Ads account (dashes optional).")is_valid = Falseis_mcc_account = job_inputs.get("is_mcc_account", {})if is_mcc_account.get("enabled"):- mcc_client_id = is_mcc_account.get("mcc_client_id", "")- if mcc_client_id and not re.match(r"^\d{3}-\d{3}-\d{4}$", mcc_client_id):+ raw_mcc_client_id = is_mcc_account.get("mcc_client_id", "")+ if raw_mcc_client_id and not re.fullmatch(r"\d{10}", clean_customer_id(raw_mcc_client_id) or ""):errors.append(- "Please enter a valid Google Ads manager customer ID. This should be 10-digits and in XXX-XXX-XXXX format."+ "Please enter a valid Google Ads manager customer ID — the 10-digit number from "+ "your manager account (dashes optional).")is_valid = Falsediff --git a/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py b/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.pyindex 7b6247802bd0..d2a2e80d14d8 100644--- a/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py+++ b/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py@@ -1,7 +1,72 @@import pytest+from posthog.temporal.data_imports.sources.google_ads.configs import clean_customer_idfrom posthog.temporal.data_imports.sources.google_ads.source import GoogleAdsSource+_CUSTOMER_ID_ERROR = "valid Google Ads customer ID"+_MANAGER_ID_ERROR = "valid Google Ads manager customer ID"+++class TestCleanCustomerId:+ @pytest.mark.parametrize(+ "raw, expected",+ [+ ("123-456-7890", "1234567890"),+ ("1234567890", "1234567890"),+ (" 123-456-7890 ", "1234567890"),+ ("123 456 7890", "1234567890"),+ ("", ""),+ (None, None),+ ],+ )+ def test_strips_to_bare_digits(self, raw, expected):+ assert clean_customer_id(raw) == expected+++class TestGoogleAdsValidateConfig:+ def setup_method(self):+ self.source = GoogleAdsSource()++ def _customer_id_errors(self, job_inputs: dict) -> list[str]:+ _, errors = self.source.validate_config(job_inputs)+ return [e for e in errors if _CUSTOMER_ID_ERROR in e]++ def _manager_id_errors(self, job_inputs: dict) -> list[str]:+ _, errors = self.source.validate_config(job_inputs)+ return [e for e in errors if _MANAGER_ID_ERROR in e]++ @pytest.mark.parametrize(+ "customer_id",+ ["123-456-7890", "1234567890", "123 456 7890", " 123-456-7890 "],+ )+ def test_accepts_any_common_customer_id_format(self, customer_id):+ assert self._customer_id_errors({"customer_id": customer_id}) == []++ @pytest.mark.parametrize(+ "customer_id",+ ["12345", "123-456-789", "abcd", "123-456-78901"],+ )+ def test_rejects_invalid_customer_id(self, customer_id):+ assert len(self._customer_id_errors({"customer_id": customer_id})) == 1++ @pytest.mark.parametrize(+ "mcc_client_id",+ ["123-456-7890", "1234567890", "123 456 7890", " 123-456-7890 "],+ )+ def test_accepts_any_common_manager_customer_id_format(self, mcc_client_id):+ job_inputs = {+ "customer_id": "1234567890",+ "is_mcc_account": {"enabled": True, "mcc_client_id": mcc_client_id},+ }+ assert self._manager_id_errors(job_inputs) == []++ def test_rejects_invalid_manager_customer_id(self):+ job_inputs = {+ "customer_id": "1234567890",+ "is_mcc_account": {"enabled": True, "mcc_client_id": "123"},+ }+ assert len(self._manager_id_errors(job_inputs)) == 1+class TestGoogleAdsNonRetryableErrors:def setup_method(self):diff --git a/posthog/temporal/tests/data_imports/test_google_ads_source.py b/posthog/temporal/tests/data_imports/test_google_ads_source.pyindex 01ab074c001f..41ef0cf8731b 100644--- a/posthog/temporal/tests/data_imports/test_google_ads_source.py+++ b/posthog/temporal/tests/data_imports/test_google_ads_source.py@@ -394,13 +394,16 @@ class TestGoogleAdsSourceValidation:@pytest.mark.parametrize("customer_id,expected_valid",[+ # Any value that normalizes to exactly 10 digits is accepted, regardless of dashes/spaces.("123-456-7890", True),("000-000-0000", True),("999-999-9999", True),- ("1234567890", False),+ ("1234567890", True),+ ("123-4567-890", True),+ ("12-3456-7890", True),+ ("123 456 7890", True),+ # Fewer or more than 10 digits, or non-numeric input, is rejected.("123-456-789", False),- ("123-4567-890", False),- ("12-3456-7890", False),("abc-def-ghij", False),("", True), # Empty is valid at config level, caught by required field validation],