: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): give Google Analytics setup failures an actionable message (#103411)
Tom Owers8b1da83759cf
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 0d45482379b4..30bf46f3ae14 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@@ -3,6 +3,7 @@ from typing import Optional, castimport requestsfrom google.auth.exceptions import RefreshError+from posthog.exceptions_capture import capture_exceptionfrom posthog.models.integration import Integrationfrom products.warehouse_sources.backend.facade.source_config import (@@ -39,6 +40,16 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.google_ana)from products.warehouse_sources.backend.types import ExternalDataSourceType+# Fallback messages for unexpected failures during credential validation. The raw exception can+# embed OAuth tokens, ids, or an HTML error body, so we capture it for debugging and show generic+# guidance instead of surfacing `str(e)` to the user.+_LOAD_CONNECTION_ERROR = (+ "PostHog couldn't load your Google Analytics connection. Reconnect your Google account, then try again."+)+_PROPERTY_METADATA_ERROR = (+ "PostHog couldn't reach Google Analytics to read your property. Wait a few minutes, then try again."+)+@SourceRegistry.registerclass GoogleAnalyticsSource(ResumableSource[GoogleAnalyticsSourceConfig, GoogleAnalyticsResumeConfig], OAuthMixin):@@ -196,7 +207,8 @@ class GoogleAnalyticsSource(ResumableSource[GoogleAnalyticsSourceConfig, GoogleA"The Google Analytics connection for this source no longer exists. Please reconnect your Google account.",)except Exception as e:- return False, f"Could not load Google Analytics credentials: {e}"+ capture_exception(e)+ return False, _LOAD_CONNECTION_ERRORtry:get_property_metadata(session, property_id)@@ -214,7 +226,8 @@ class GoogleAnalyticsSource(ResumableSource[GoogleAnalyticsSourceConfig, GoogleAf"GA4 property '{property_id}' was not found. Verify the numeric property ID in ""Google Analytics admin settings.",)- return False, f"Failed to read Google Analytics property metadata: {e}"+ capture_exception(e)+ return False, _PROPERTY_METADATA_ERRORexcept RefreshError:# Raised while AuthorizedSession refreshes the OAuth access token (e.g. invalid_scope or# invalid_grant): the stored token is missing the required permissions, or has expired or@@ -227,7 +240,8 @@ class GoogleAnalyticsSource(ResumableSource[GoogleAnalyticsSourceConfig, GoogleA"account and grant access to Google Analytics.",)except Exception as e:- return False, f"Failed to read Google Analytics property metadata: {e}"+ capture_exception(e)+ return False, _PROPERTY_METADATA_ERRORreturn True, Nonediff --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 1bf5853dbcb4..4a396c9dfbc9 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@@ -194,7 +194,7 @@ def _http_error(status_code: int) -> requests.HTTPError:(401, "rejected the credentials"),(403, "rejected the credentials"),(404, "was not found"),- (500, "Failed to read Google Analytics property metadata"),+ (500, "couldn't reach Google Analytics"),],)def test_validate_credentials_maps_http_errors(status_code, expected_substring):@@ -234,14 +234,36 @@ def test_validate_credentials_maps_token_refresh_error():def test_validate_credentials_handles_session_failure():+ # The credential-load exception can carry an OAuth token or an HTML error body, so the+ # setup form gets a reconnect prompt and none of the raw text.with mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.google_analytics.source.google_analytics_session",- side_effect=Exception("no integration"),+ side_effect=Exception("token ya29.SECRET rejected"),):ok, message = GoogleAnalyticsSource().validate_credentials(_config(), team_id=1)assert ok is False- assert "Could not load Google Analytics credentials" in (message or "")+ assert "Reconnect your Google account" in (message or "")+ assert "ya29.SECRET" not in (message or "")+++def test_validate_credentials_hides_unexpected_metadata_failure_detail():+ # An unexpected metadata failure used to reach the setup form as `str(e)`, which for a+ # requests error is the full URL and response body.+ 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=Exception("https://analyticsdata.googleapis.com/v1beta/properties/1?key=SECRET"),+ ),+ ):+ ok, message = GoogleAnalyticsSource().validate_credentials(_config(), team_id=1)++ assert ok is False+ assert "couldn't reach Google Analytics" in (message or "")+ assert "googleapis.com" not in (message or "")def test_validate_credentials_handles_missing_integration():