« All posts

Wiring GA4 into an MCP Server: Making Analytics AI-Agent Friendly

GSC Wizard's MCP server GA4 integration reveals tricky engineering: detecting AI assistant traffic, token rotation, quota locks, and dual-client rendering.

The developer behind GSC Wizard, an SEO analytics tool, describes what happened after adding Google Analytics 4 support to its existing MCP (Model Context Protocol) server, following an earlier Search Console integration. While GA4's Data API quirks are invisible to human dashboard users, they turned into real engineering problems once an LLM became the client. The most novel of six GA4 tools detects traffic from AI assistants like ChatGPT, Perplexity, and Gemini by matching both raw domains and UTM aliases against a hand-maintained list of 18 assistants, requiring server-side regex filtering and client-side classification to stay perfectly in sync. The post also covers subtler issues, like recomputing engagement rates from raw counts after grouping (since rates can't simply be averaged), and being transparent that Google's AI Overviews traffic carries no distinct referrer and therefore can't be measured at all.

Beyond that, the piece frames an LLM as a fundamentally different API client than a human: it sends malformed dates, fires bursts of requests, and has no notion of which Google account owns which property. The fixes include tolerant date parsing that coerces garbage input to sensible defaults, token rotation across multiple accounts that only triggers on 401/403 errors, per-property serial locks with batched requests to avoid blowing shared API quotas, and logic to normalize GA4's suffix-based key-event metrics into stable field names.

Finally, the same tool output has to render differently depending on the client — rich widgets for ChatGPT via structured content, plain JSON for other MCP clients like Claude. The overall takeaway is that making live analytics data reliably usable by an AI agent is a layered engineering problem, not just an API wrapper.