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): surface top-pages query errors

xunguangzlj-cloud8e67c978b56e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 648be42..9f4818a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+
+- GA4 `top-pages` reports now surface failed landing-page queries instead of
+ returning an empty successful report when the daily query succeeded.
+
## [2.5.0] - 2026-10-22
Security and accuracy fixes from a full audit, and Claude Desktop installs again.
diff --git a/scripts/ga4_report.py b/scripts/ga4_report.py
index 0c36887..33065c4 100644
--- a/scripts/ga4_report.py
+++ b/scripts/ga4_report.py
@@ -264,7 +264,7 @@ def top_pages_report(
"pages": report.get("top_pages", []),
"total_organic_sessions": report.get("totals", {}).get("sessions", 0),
"quota_tokens_used": report.get("quota_tokens_used"),
- "error": report.get("error"),
+ "error": report.get("error") or report.get("pages_error"),
}
diff --git a/tests/test_ga4_top_pages_errors.py b/tests/test_ga4_top_pages_errors.py
new file mode 100644
index 0000000..4111e2a
--- /dev/null
+++ b/tests/test_ga4_top_pages_errors.py
@@ -0,0 +1,99 @@
+"""GA4 热门落地页查询失败时必须保留错误。"""
+
+from __future__ import annotations
+
+import json
+import sys
+from pathlib import Path
+from unittest.mock import Mock
+
+import pytest
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
+pytest.importorskip("google.analytics.data_v1beta")
+
+import ga4_report # noqa: E402
+from google.analytics.data_v1beta.types import RunReportResponse # noqa: E402
+
+
+def client(monkeypatch):
+ client = Mock()
+ monkeypatch.setattr(ga4_report, "_build_ga4_client", lambda: client)
+ return client
+
+
+def daily_response():
+ return RunReportResponse(rows=[{
+ "dimension_values": [{"value": "20261101"}],
+ "metric_values": [{"value": value} for value in ("10", "8", "12", "0.2", "15", "0.8")],
+ }])
+
+
+def test_top_pages_surfaces_failed_landing_page_query(client):
+ client.run_report.side_effect = [daily_response(), RuntimeError("landing pages unavailable")]
+
+ result = ga4_report.top_pages_report("123456789")
+
+ assert result["error"] == "Error fetching top pages: landing pages unavailable"
+ assert result["pages"] == []
+ assert result["total_organic_sessions"] == 10
+ assert client.run_report.call_count == 2
+ assert client.run_report.call_args.args[0].dimensions[0].name == "landingPage"
+
+
[email protected]("has_pages", [False, True])
+def test_successful_top_pages_keeps_its_response(client, has_pages):
+ rows = [{
+ "dimension_values": [{"value": "/guide"}],
+ "metric_values": [{"value": value} for value in ("10", "8", "12", "0.2", "0.8")],
+ }] if has_pages else []
+ client.run_report.side_effect = [daily_response(), RunReportResponse(rows=rows)]
+
+ result = ga4_report.top_pages_report("123456789")
+
+ assert result["error"] is None
+ assert result["total_organic_sessions"] == 10
+ assert len(result["pages"]) == int(has_pages)
+ if has_pages:
+ assert result["pages"][0]["landing_page"] == "/guide"
+
+
+def test_top_pages_retains_primary_daily_query_error(client):
+ client.run_report.side_effect = RuntimeError("daily report unavailable")
+
+ result = ga4_report.top_pages_report("123456789")
+
+ assert result["error"] == "GA4 API error: daily report unavailable"
+ assert client.run_report.call_count == 1
+
+
+def test_organic_report_preserves_partial_daily_data(client):
+ client.run_report.side_effect = [daily_response(), RuntimeError("landing pages unavailable")]
+
+ result = ga4_report.organic_traffic_report("123456789")
+
+ assert result["error"] is None
+ assert result["pages_error"] == "Error fetching top pages: landing pages unavailable"
+ assert result["daily_data"][0]["sessions"] == 10
+
+
[email protected]("json_output", [False, True])
+def test_cli_exposes_landing_page_query_failure(client, monkeypatch, capsys, json_output):
+ client.run_report.side_effect = [daily_response(), RuntimeError("landing pages unavailable")]
+ argv = ["ga4_report.py", "--property", "123456789", "--report", "top-pages"]
+ monkeypatch.setattr(sys, "argv", argv + (["--json"] if json_output else []))
+
+ if json_output:
+ ga4_report.main()
+ else:
+ with pytest.raises(SystemExit) as exc:
+ ga4_report.main()
+ assert exc.value.code == 1
+
+ output = capsys.readouterr()
+ assert "landing pages unavailable" in output.err
+ if json_output:
+ assert json.loads(output.out)["error"] == "Error fetching top pages: landing pages unavailable"
+ else:
+ assert "Top Organic Landing Pages" not in output.out