We’ve been building this site with Claude from the start — the theme, the templates, the tools grid, even the mu-plugins. But until now, Claude had no visibility into how people actually use what we build. The analytics lived in a dashboard I’d have to check myself, then relay the numbers back into conversation. We just closed that gap.
By connecting Google Analytics to Claude Code via Google’s official MCP server, Claude can now query our GA4 data directly. Page views, traffic sources, active users, realtime visitors — all of it accessible mid-conversation, without leaving the terminal. Here’s how we set it up.
The Two Pieces
There are two sides to this: getting data into Google Analytics, and getting data out to Claude.
GA4 Tracking on the Site
We added a must-use plugin (mbc-google-analytics.php) that loads the GA4 gtag.js snippet. Must-use plugins live in wp-content/mu-plugins/ and load automatically before regular plugins and themes — they can’t be accidentally deactivated from the admin. For something as foundational as analytics, that’s the right place.
The plugin does three checks before outputting anything:
if ( is_admin() ) {
return;
}
if ( wp_get_environment_type() !== 'production' ) {
return;
}
if ( current_user_can( 'edit_posts' ) ) {
return;
}
No tracking on admin pages. No tracking on local or staging environments (we use WordPress’s wp_get_environment_type() function, which returns 'production' only on the live site). And no tracking for logged-in users who can edit posts — contributors and above — so our own activity doesn’t skew the data. After those gates, it outputs the standard gtag.js snippet with our measurement ID.
Google’s Analytics MCP Server in Claude Code
The other side is MCP — Model Context Protocol. MCP is a standard that lets AI tools connect to external services through a consistent interface. Instead of copy-pasting data or describing what you see in a dashboard, the AI can query the service directly.
Google publishes an official Analytics MCP server that connects to the GA4 Data API. Once configured in Claude Code’s MCP settings and authenticated with a Google account that has access to the GA4 property, Claude gets a set of tools: run_report, run_realtime_report, get_property_details, get_account_summaries, and more.
No custom code needed on our side. The MCP server handles authentication, API calls, and response formatting. Claude just calls the tools like any other MCP integration.
What Claude Can Query
With the connection in place, Claude can now answer questions like:
- Which pages get the most views? — Page views broken down by path, over any date range.
- Where does traffic come from? — Sessions by source and medium: organic search, direct, referral, social.
- How many active users over time? — Daily, weekly, or monthly active user trends.
- Who’s on the site right now? — Realtime active users, what pages they’re viewing, where they came from.
- Filtered and combined reports — “Show me organic traffic to
/tools/pages in the last 7 days” or “Which journal posts got the most views from social referrals this month?”
These aren’t hypothetical — they’re actual queries Claude can run mid-conversation using the GA4 Data API through MCP. The reports support dimensions (what you’re grouping by), metrics (what you’re measuring), filters, sorting, and date ranges. It’s the full reporting API, not a simplified subset.
One honest note: as of writing this, our GA4 property is brand new. We set it up on March 4, 2026, and the data is still accumulating. The plumbing is in place — we’re waiting for the signal to flow through it.
How to Set This Up Yourself
If you’re using Claude Code and want to give it access to your own Google Analytics data, here’s the path:
1. Create a GA4 property. If you don’t have one already, set up a GA4 property in Google Analytics. Note the measurement ID (starts with G-) and the property ID (a numeric ID visible in property settings).
2. Add tracking to your site. Load the gtag.js snippet on your pages. If you’re on WordPress, a must-use plugin is a clean approach — gate it behind environment and capability checks so you’re not tracking yourself or polluting local data. If you’re on another platform, the standard Google tag installation works fine.
3. Install Google’s Analytics MCP server. The server is available at github.com/nichochar/analytics-mcp. Add it to your Claude Code MCP configuration. The README walks through installation and setup.
4. Authenticate. The MCP server uses OAuth to connect to your Google account. On first use, it’ll open a browser window for you to authorize access to your GA4 data. The credentials are cached locally after that.
5. Start querying. Once connected, you can ask Claude about your traffic in natural language. “What are my top pages this week?” “Where is my traffic coming from?” “Show me a breakdown of sessions by device category.” Claude translates these into GA4 Data API calls and returns the results inline.
What’s Next
This closes a feedback loop that was missing. Before, Claude could build features but had no way to know if they landed. Now we can check what pages people visit, where they come from, and what holds their attention — all without leaving the conversation where we’re doing the building.
As the data accumulates, we’ll use it. Which journal posts get read? Which tools get clicks? Is organic search finding us? The answers will shape what we build next. Not in a growth-hacking way — in a “pay attention to what’s useful and make more of it” way.
More on that as the numbers come in.
The GA4 tracking was added via a must-use plugin in a previous session. Here’s the pull request that shipped it.
Must-use plugin that loads gtag.js on production only, with environment gating and contributor-level exclusion to keep the data clean.
View on GitHub