Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

fix(ga4): classify API errors by status, not by digits in the message

Agrici Daniel1867b4ac62df
diff --git a/scripts/ga4_report.py b/scripts/ga4_report.py
index 811ff0b..6d2c834 100644
--- a/scripts/ga4_report.py
+++ b/scripts/ga4_report.py
@@ -47,6 +47,20 @@ except ImportError:
GA4_SCOPES = ["https://www.googleapis.com/auth/analytics.readonly"]
+def _api_status(error: Exception) -> int | None:
+ """HTTP status carried by a Google API exception, or None.
+
+ The status comes from the exception itself, never from its text: with the
+ REST transport the message includes the request URL, so a property ID
+ such as 314040392 would read as a 403 or a 404 to a substring test.
+ """
+ code = getattr(error, "code", None)
+ try:
+ return int(code)
+ except (TypeError, ValueError):
+ return None
+
+
def _build_ga4_client():
"""Build the GA4 BetaAnalyticsDataClient."""
credentials = get_oauth_credentials(GA4_SCOPES)
@@ -166,13 +180,14 @@ def organic_traffic_report(
except Exception as e:
error_str = str(e)
- if "403" in error_str or "PERMISSION_DENIED" in error_str:
+ status = _api_status(e)
+ if status == 403 or "PERMISSION_DENIED" in error_str:
result["error"] = (
f"Permission denied for property '{property_id}'. "
"Add the service account email as Viewer in "
"GA4 Admin > Property Access Management."
)
- elif "404" in error_str or "NOT_FOUND" in error_str:
+ elif status == 404 or "NOT_FOUND" in error_str:
result["error"] = (
f"Property '{property_id}' not found. "
"Verify the numeric property ID in GA4 Admin > Property Details."
diff --git a/tests/test_ga4_top_pages_errors.py b/tests/test_ga4_top_pages_errors.py
index 23fa726..e9745f3 100644
--- a/tests/test_ga4_top_pages_errors.py
+++ b/tests/test_ga4_top_pages_errors.py
@@ -97,3 +97,30 @@ def test_cli_exposes_landing_page_query_failure(client, monkeypatch, capsys, jso
assert json.loads(output.out)["error"] == "Error fetching top pages: landing pages unavailable"
else:
assert "Top Organic Landing Pages" not in output.out
+
+
+def test_error_kind_comes_from_the_exception_not_from_digits_in_its_text(client):
+ """A quota error on a property whose ID contains 403 is not a permission error."""
+ from google.api_core import exceptions
+
+ quota = exceptions.TooManyRequests(
+ "429 POST https://analyticsdata.googleapis.com/v1beta/properties/314040392:runReport: quota"
+ )
+ client.run_report.side_effect = quota
+ result = ga4_report.organic_traffic_report("314040392")
+ assert result["error"].startswith("GA4 API error:")
+ assert "Permission denied" not in result["error"]
+ assert "not found" not in result["error"]
+
+
[email protected]("exc_name,expected", [
+ ("PermissionDenied", "Permission denied"),
+ ("Forbidden", "Permission denied"),
+ ("NotFound", "not found"),
+])
+def test_permission_and_not_found_are_still_recognised(client, exc_name, expected):
+ from google.api_core import exceptions
+
+ client.run_report.side_effect = getattr(exceptions, exc_name)("request failed")
+ result = ga4_report.organic_traffic_report("123456789")
+ assert expected in result["error"]