Introduction
In the era of LLMs and AI programming assistants (such as Claude Code, Cursor, and Windsurf), the way we write code and maintain websites has undergone a paradigm shift. But did you know that AI can also act as your live digital operations assistant, directly auditing production traffic metrics?
Through the Model Context Protocol (MCP), LLMs can now be plugged directly into your Google Analytics 4 (GA4) property.
This article provides a comprehensive guide for beginner-to-intermediate users on how to install and configure the Google Analytics MCP Server from scratch. We will also explore how to use it to automate two critical production workflows:
- Automatically detecting and routing 404 broken links.
- Querying and analyzing daily traffic referral sources (Referrer).
What is Google Analytics MCP?
The Model Context Protocol (MCP) is an open-standard protocol proposed by Anthropic that standardizes the connection between LLMs and external data sources (such as databases, APIs, and file systems).
Google Analytics MCP is a concrete implementation of this protocol. It leverages the GA4 Data API to translate live site statistics—like page views, sessions, traffic sources, and error pages—into standard "tools" that the AI assistant can call at run time.
With this setup, the AI is no longer a passive chatbot; it becomes an active agent capable of invoking APIs, aggregating live metrics, and writing redirects or code fixes directly to resolve issues.
graph TD
A[User talks to LLM] --> B[AI Assistant / Claude Code / Cursor]
B -->|Calls Tool via MCP Protocol| C[Google Analytics MCP Server]
C -->|Invokes API| D[Google Analytics 4 Data API]
D -->|Returns Data| C
C -->|Processes Data| B
B -->|Generates Traffic Reports / Fixes| AInstallation & Configuration Guide
Step 1: Enable GA4 Data API & Download Credentials
For the MCP server to read GA4 telemetry, we need to create a Service Account in the Google Cloud Console:
- Visit the Google Cloud Console.
- Create or select a project (e.g.,
agent-analytics). - Search for the Google Analytics Data API in the API Library and click Enable.
- Navigate to IAM & Admin -> Service Accounts, and click Create Service Account. Fill in the name and complete the setup.
- In the Service Accounts list, click the newly created service account -> Manage Keys -> Add Key -> Create New Key, and select JSON format. Download the generated
.jsonfile and save it securely (e.g., asgoogle-credentials.json). - Crucial Step: Open the JSON key file, locate the
client_emailfield (e.g.,[email protected]), and copy it. - Go to your Google Analytics Admin Console, click Property Access Management, click the "+" icon in the top-right corner, add the service account email, and grant it Viewer permissions.
Step 2: Configure the MCP Server in Your AI Client
Let's configure it for the Claude Desktop client (which supports MCP out of the box).
- Open your Claude Desktop configuration file:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
- macOS:
- Add the following config block:
{
"mcpServers": {
"google-analytics": {
"command": "npx",
"args": [
"-y",
"@mcp-server/google-analytics"
],
"env": {
"GOOGLE_APPLICATION_CREDENTIALS": "/Users/yourname/credentials/google-credentials.json",
"GA4_PROPERTY_ID": "123456789"
}
}
}
}
[!IMPORTANT] Make sure to replace
GOOGLE_APPLICATION_CREDENTIALSwith the absolute path of your downloaded JSON credentials. ReplaceGA4_PROPERTY_IDwith your GA4 Property ID (which is a string of numbers found under Property Settings in the GA4 admin dashboard).
- Save the file and restart Claude Desktop. In the chat input area, you should see a plug icon. Hovering over it will list the
google_analytics_run_reporttool, indicating it is active.
Practical Use Case 1: Automating 404 Link Resolution
In the past, resolving 404 errors involved downloading CSV reports, identifying broken paths, and manually writing rewrite rules. With GA4 MCP, you can automate this entire cycle with a simple prompt:
User: "Check for any 404 errors on our live site in the last 24 hours. Cross-reference them with current valid paths in our codebase, and write appropriate redirects to fix them."
AI Agentic Workflow
When you submit this prompt, the AI will perform the following steps autonomously:
sequenceDiagram
participant Developer as Developer
participant AI as AI Assistant (Claude/Cursor)
participant MCP as GA4 MCP Server
participant DB as Codebase / DB
Developer->>AI: Scan and resolve live 404s
AI->>MCP: Call run_report for 404 pagePaths
MCP-->>AI: Return list of 404 paths + referrers
AI->>DB: Scan codebase for current valid slugs
DB-->>AI: Return active slugs
AI->>AI: Calculate similarity mapping (Jaccard / Levenshtein)
AI->>Developer: Present proposed redirect mapping
AI->>DB: Write rules to public/_redirects and run tests
AI->>Developer: Redirects deployed and verified successfully- Invoke Tool: The AI queries the GA4 MCP server using
google_analytics_run_report:- Dimensions:
pagePath,pageReferrer - Metrics:
screenPageViews - Filters:
pageTitlecontaining "404" or "Page Not Found".
- Dimensions:
- Analysis: The AI receives telemetry showing which paths (e.g.,
/news/google-antigravity-2-explained) are throwing 404 errors. - Self-Healing: The AI searches the local codebase, realizes the correct slug is actually
/blog/google-antigravity-2-0-explained, calculates the match, writes a redirect rule to Cloudflare's_redirectsfile, and builds the project locally to verify there are no broken links!
Practical Use Case 2: Daily Referral Analysis
Want to know who is driving traffic to your site today? Is it search engines, social channels, or referral links? Simply ask:
User: "Analyze today's traffic sources. What are the top 10 referrers and the 5 most visited pages?"
AI Execution & Reporting
Under the hood, the AI issues a report request to the GA4 MCP server with the following parameters:
- Date Range:
today - Dimensions:
pageReferrer,sessionSource,pagePath - Metrics:
activeUsers
The AI will output a clean Markdown table summarizing the metrics:
| Source / Referrer | Active Users | Page Path | Share |
|---|---|---|---|
| Direct | 120 | / (Homepage) |
45% |
| google.com | 85 | /blog/composio-ultimate-guide |
32% |
| github.com | 40 | /tutorial/openclaw-masterclass |
15% |
| bing.com | 20 | /news/apple-wwdc-2026 |
8% |
The AI will also offer observations, such as: "Github referrers are driving notable traffic to the OpenClaw tutorial today. Consider publishing a follow-up article or tweet to capitalize on this interest."
Conclusion
For beginners, the Google Analytics MCP server removes the complexity of configuring GA4 custom explorations, letting you fetch live data using plain English.
For intermediate users and developers, the server's true power lies in connecting telemetry directly to development. By bringing GA4 metrics into the terminal, you can build self-healing pipelines where the AI automatically audits errors, fixes broken paths in configuration files, builds the code locally, and pushes the resolution to production.
Set up your GA4 MCP server today and turn your AI assistant into an automated analytics operator!