GO

Google Analytics MCP Server for LLMs 200+ Metrics

Access Google Analytics through an MCP server to aggregate 200+ dimensions and metrics for LLM analysis and actionable insights.

Quick Install
npx -y @surendranb/google-analytics-mcp

Overview

This MCP (Model Context Protocol) server exposes Google Analytics (GA4) reporting as a tool that LLMs can call programmatically. It translates LLM tool calls into Google Analytics Data API requests and returns structured, model-friendly JSON containing up to 200+ dimensions and metrics. The server is intended to bridge LLMs and analytics systems so models can generate data-informed insights, summaries, or next-step suggestions without embedding credentials or ad-hoc scraping logic.

Using an MCP-compatible HTTP endpoint keeps the LLM integration simple: the model requests a named tool with a JSON payload describing the report (metrics, dimensions, date ranges, and filters). The server handles authentication, batching, aggregation, and pagination against the GA Data API and normalizes results for use in prompts, chain-of-thought, or downstream analysis.

Features

  • Exposes Google Analytics (GA4) metrics and dimensions through an MCP-compatible HTTP API
  • Supports 200+ GA4 dimensions and metrics via the Google Analytics Data API
  • Authentication via Google service account (Application Default Credentials)
  • Date range, filters, segments, and dimension combinations supported
  • Batch requests and automatic pagination handling for large reports
  • Results normalized into compact JSON suitable for LLM consumption
  • Docker and local development workflows
  • Simple request schema that maps cleanly to LLM tool-calling patterns

Installation / Configuration

Prerequisites:

  • Python 3.9+
  • Google Cloud project with a service account and GA4 property
  • Google Analytics Data API enabled (for GA4)
  1. Clone the repo and install dependencies:
git clone https://github.com/surendranb/google-analytics-mcp.git
cd google-analytics-mcp
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
  1. Create and configure a Google service account and download the JSON key. Enable the Analytics Data API for your project and grant the service account access to your GA4 property.

  2. Set environment variables (example .env):

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
export GA4_PROPERTY_ID="properties/123456789"
export MCP_PORT=8080
# Optionally:
export LOG_LEVEL=info
  1. Run locally (example using uvicorn):
uvicorn app.main:app --host 0.0.0.0 --port ${MCP_PORT:-8080} --reload
  1. Or run with Docker:
docker build -t ga-mcp .
docker run -e GOOGLE_APPLICATION_CREDENTIALS="/creds/service-account.json" \
  -e GA4_PROPERTY_ID="properties/123456789" \
  -v /local/path/service-account.json:/creds/service-account.json:ro \
  -p 8080:8080 ga-mcp

Environment variables summary:

VariablePurpose
GOOGLE_APPLICATION_CREDENTIALSPath to Google service account JSON
GA4_PROPERTY_IDGA4 property identifier (e.g., properties/123456789)
MCP_PORTServer port (default 8080)
LOG_LEVELOptional logging level (debug/info/error)

Available Resources

  • Repository: https://github.com/surendranb/google-analytics-mcp
  • Google Analytics Data API (GA4): https://developers.google.com/analytics/devguides/reporting/data/v1
  • MCP (Model Context Protocol) concept: use the server as an LLM-invokable tool that returns structured JSON

The server implements endpoints that map directly to report requests. Typical endpoints:

  • POST /mcp/query — submit a metrics/dimensions request (MCP tool call)
  • GET /mcp/manifest — describe available metrics/dimensions and sample payloads

Example Request / Response

Example request payload (LLM tool call format):

{
  "tool": "google_analytics",
  "params": {
    "metrics": ["activeUsers", "totalRevenue"],
    "dimensions": ["country", "deviceCategory"],
    "startDate": "2024-03-01",
    "endDate": "2024-03-31",
    "filters": "country==United States",
    "limit": 1000
  }
}

Example trimmed response:

{
  "status": "ok",
  "rows": [
    {"country":"United States","deviceCategory":"mobile","activeUsers":12345,"totalRevenue":9876.50},
    {"country":"United States","deviceCategory":"desktop","activeUsers":6789,"totalRevenue":4321.10}
  ],
  "meta": {"rowCount":2,"queriedMetrics":["activeUsers","totalRevenue"]}
}

Use Cases

  • Funnel analysis for prompt engineering: An LLM can request stepwise conversion rates (sessions → add-to-cart → purchase) by asking the MCP server for relevant event counts and time windows, then generate suggestions to improve conversion.
  • Anomaly detection and alerts: Periodically query critical metrics (e.g., active users, error events) with the MCP API and let the model produce human-readable explanations and recommended remediation steps when a drop/spike is detected.
  • Revenue attribution summaries: Aggregate revenue and purchase metrics by medium, campaign, or country, so an LLM can synthesize attribution narratives (top-performing campaigns, ROI highlights).
  • Cohort comparison and retention: Retrieve cohort size and retention metrics for different acquisition channels, enabling the model to summarize which cohorts retain best and suggest focused growth experiments.
  • Enrich conversational analytics: When integrated into a chat or assistant, the model can answer questions like “How did mobile revenue trend last month vs. the previous month?” by calling the MCP server, receiving structured data, and returning a concise explanation.

Notes and Best Practices

  • This server uses the GA4 Data API — ensure you use GA4 (not Universal Analytics).
  • Limit data exposure in prompts: return compact, relevant rows/aggregates rather than entire raw tables.
  • Implement rate limiting or caching when calling the GA Data API from automated LLM workflows to avoid quota hits.
  • Inspect the /mcp/manifest endpoint to learn exact metric/dimension identifiers supported by the server.

For full details, examples, and advanced configuration, see the GitHub repository: https://github.com/surendranb/google-analytics-mcp.