: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(google-analytics): tell property access failures apart from bad credentials (#110589)
Tom Owers309519f12459
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/source.pyindex cbcc67c873cb..e4ad6760c613 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/source.py@@ -50,6 +50,21 @@ _PROPERTY_METADATA_ERROR = ("PostHog couldn't reach Google Analytics to read your property. Wait a few minutes, then try again.")+# Google answers the metadata probe with 403 both when the account can't read the property (or the+# property doesn't exist) and when the user unticked the Analytics scope on the consent screen. Only+# a 401 means the token itself is bad, so reconnecting is the fix for the scope and 401 cases only.+_CREDENTIALS_REJECTED_ERROR = (+ "Google rejected the credentials for this connection. Reconnect your Google account, then try again."+)+_PROPERTY_ACCESS_ERROR = (+ "Your connected Google account can't read this Google Analytics property. Check the property ID, "+ "or reconnect with an account that has access to it."+)+_MISSING_SCOPE_ERROR = (+ "Your Google connection doesn't include Google Analytics access. Reconnect your Google account "+ "and allow Google Analytics access when Google asks."+)+@SourceRegistry.registerclass GoogleAnalyticsSource(ResumableSource[GoogleAnalyticsSourceConfig, GoogleAnalyticsResumeConfig], OAuthMixin):@@ -224,12 +239,12 @@ class GoogleAnalyticsSource(ResumableSource[GoogleAnalyticsSourceConfig, GoogleAget_property_metadata(session, property_id)except requests.HTTPError as e:status = e.response.status_code if e.response is not None else None- if status in (401, 403):- return (- False,- f"Google Analytics rejected the credentials for property '{property_id}'. Please reconnect "- "your account and ensure it has read access to the property.",- )+ if status == 401:+ return False, _CREDENTIALS_REJECTED_ERROR+ if status == 403:+ if "ACCESS_TOKEN_SCOPE_INSUFFICIENT" in e.response.text:+ return False, _MISSING_SCOPE_ERROR+ return False, _PROPERTY_ACCESS_ERRORif status == 404:return (False,diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.pyindex 317674a5181d..28e003eb049c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py@@ -182,29 +182,35 @@ def test_validate_credentials_names_common_wrong_ids(wrong_id, expected_substrinassert expected_substring in (message or "")-def _http_error(status_code: int) -> requests.HTTPError:+def _http_error(status_code: int, body: str = "") -> requests.HTTPError:response = mock.MagicMock()response.status_code = status_code+ response.text = bodyreturn requests.HTTPError(response=response)@pytest.mark.parametrize(- "status_code,expected_substring",+ "status_code,body,expected_substring",[- (401, "rejected the credentials"),- (403, "rejected the credentials"),- (404, "was not found"),- (500, "couldn't reach Google Analytics"),+ (401, "", "rejected the credentials"),+ (403, '{"error": {"status": "PERMISSION_DENIED"}}', "can't read this Google Analytics property"),+ (+ 403,+ '{"error": {"details": [{"reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT"}]}}',+ "allow Google Analytics access",+ ),+ (404, "", "was not found"),+ (500, "", "couldn't reach Google Analytics"),],)-def test_validate_credentials_maps_http_errors(status_code, expected_substring):+def test_validate_credentials_maps_http_errors(status_code, body, expected_substring):with (mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.google_analytics.source.google_analytics_session"),mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.google_analytics.source.get_property_metadata",- side_effect=_http_error(status_code),+ side_effect=_http_error(status_code, body),),):ok, message = GoogleAnalyticsSource().validate_credentials(_config(), team_id=1)