Google Analytics 4 MCP server

Integrates with Google Analytics 4 to provide property management, page views, user metrics, events, traffic sources, device analytics, and custom reporting with automatic OAuth 2.0 authentication for marketing analytics and website performance monitoring.
Back to servers
Setup instructions
Provider
Gomarble AI
Release date
Jul 28, 2025
Language
Go
Stats
3 stars

The Google Analytics MCP Server provides a seamless integration between Google Analytics 4 and Claude Desktop using the Model Context Protocol (MCP). It enables you to access, analyze, and visualize your Google Analytics data directly through Claude's interface with automatic OAuth authentication and token management.

Installation

Prerequisites

Before setting up the MCP server, you'll need:

  • Python 3.10+ installed
  • A Google Cloud Platform account
  • A Google Analytics 4 property with data access

Google Cloud Platform Setup

Create Google Cloud Project

  1. Go to Google Cloud Console
  2. Create a new project:
    • Click "Select a project" → "New Project"
    • Enter project name (e.g., "Google Analytics MCP")
    • Click "Create"

Enable Google Analytics APIs

  1. In your Google Cloud Console:
    • Go to "APIs & Services" → "Library"
    • Search for "Google Analytics Data API" and enable it

Create OAuth 2.0 Credentials

  1. Go to "APIs & Services" → "Credentials"
  2. Click "+ CREATE CREDENTIALS" → "OAuth 2.0 Client ID"
  3. Configure consent screen (if first time):
    • Click "Configure Consent Screen"
    • Choose "External" (unless you have Google Workspace)
    • Fill required fields:
      • App name: "Google Analytics MCP"
      • User support email: Your email
      • Developer contact: Your email
    • Add scopes:
      • https://www.googleapis.com/auth/analytics
      • https://www.googleapis.com/auth/analytics.readonly
    • Click "Save and Continue" through all steps
  4. Create OAuth Client:
    • Application type: "Desktop application"
    • Name: "Google Analytics MCP Client"
    • Click "Create"
  5. Download credentials:
    • Click "Download JSON" button
    • Save file as client_secret_[long-string].json in your project directory

Google Analytics Access

  1. Sign in to Google Analytics
  2. Verify you have access to GA4 properties
  3. Note your property IDs (found in GA4 Admin → Property Settings)
  4. Ensure your Google account has at least Viewer access to the properties you want to query

Installation & Setup

# Clone the repository
git clone https://github.com/yourusername/google-analytics-mcp-server.git
cd google-analytics-mcp-server

# Create virtual environment (recommended)
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Environment Configuration

Create a .env file in your project directory:

# Copy the example file
cp .env.example .env

Edit .env with your credentials:

# Required: Path to OAuth credentials JSON file (downloaded from Google Cloud)
GOOGLE_ANALYTICS_OAUTH_CONFIG_PATH=/full/path/to/your/client_secret_file.json

Claude Desktop Integration

Locate Claude Configuration

Find your Claude Desktop configuration file:

macOS:

~/Library/Application Support/Claude/claude_desktop_config.json

Windows:

%APPDATA%\Claude\claude_desktop_config.json

Add MCP Server Configuration

Edit the configuration file and add your Google Analytics MCP server:

{
  "mcpServers": {
    "google-analytics": {
      "command": "/full/path/to/your/project/.venv/bin/python",
      "args": [
        "/full/path/to/your/project/server.py"
      ]
    }
  }
}

Important:

  • Use absolute paths for all file locations
  • On Windows, use forward slashes / or double backslashes \\ in paths

Restart Claude Desktop

Close and restart Claude Desktop to load the new configuration.

First-Time Authentication

Trigger OAuth Flow

  1. Open Claude Desktop
  2. Try any Google Analytics command, for example:
    "List all my Google Analytics properties"
    

Complete Authentication

  1. Browser opens automatically to Google OAuth page
  2. Sign in with your Google account (the one with Analytics access)
  3. Grant permissions by clicking "Allow"
  4. Browser shows success page
  5. Return to Claude - your command will complete automatically!

Usage Examples

Property Management

"List all my Google Analytics properties"

"Show me properties for account 123456789"

"What GA4 properties do I have access to?"

Page View Analysis

"Get page views for property 421301275 from 2025-01-01 to 2025-01-31"

"Show me top pages by page views for last month for property 421301275"

"Analyze page performance by country for property 421301275"

User Analytics

"Get active users for property 421301275 in the last 7 days"

"Show me user metrics by device category for property 421301275"

"Compare new vs returning users for last month"

Traffic Source Analysis

"Analyze traffic sources for property 421301275 from 2025-01-01 to 2025-01-31"

"Show me which channels drive the most users to my site"

"Compare organic vs paid traffic performance"

Event Tracking

"Get events data for property 421301275 in the last 30 days"

"Show me conversion events by source/medium"

"Which events are most popular on my site?"

Custom Reports

"Create a report for property 421301275 with sessions, users, and page views by country from 2025-01-01 to 2025-01-31"

"Run a custom report showing bounce rate and engagement rate by device category"

"Generate a comprehensive traffic report with sessions, conversions, and revenue by source/medium"

Advanced GA4 Examples

Sessions and Users by Country

run_report(
    property_id="421301275",
    start_date="2025-01-01",
    end_date="2025-01-31",
    metrics=["sessions", "totalUsers", "screenPageViews"],
    dimensions=["country"],
    limit=20
)

Device Performance Analysis

run_report(
    property_id="421301275",
    start_date="2025-01-01",
    end_date="2025-01-31",
    metrics=["sessions", "bounceRate", "engagementRate"],
    dimensions=["deviceCategory", "operatingSystem"],
    limit=50
)

Traffic Sources with Conversions

run_report(
    property_id="421301275",
    start_date="2025-01-01",
    end_date="2025-01-31",
    metrics=["sessions", "conversions", "totalRevenue"],
    dimensions=["source", "medium", "campaignName"],
    limit=100
)

Troubleshooting

Authentication Issues

Issue Symptoms Solution
No tokens found "Starting OAuth flow" message ✅ Normal for first-time setup - complete browser authentication
Token refresh failed "Refreshing token failed" error ✅ Delete google_analytics_token.json and re-authenticate
OAuth flow failed Browser error or no response Check credentials file path and internet connection
Permission denied "Access denied" in browser Ensure Google account has Analytics access

Configuration Issues

Issue Symptoms Solution
Environment variables missing "Environment variable not set" Check .env file and Claude config env section
File not found "FileNotFoundError" Verify absolute paths in configuration
Module import errors "ModuleNotFoundError" Run pip install -r requirements.txt
Python path issues "Command not found" Use absolute path to Python executable

API Issues

Issue Symptoms Solution
Invalid property ID "Property not found" Use numeric format: 421301275
API quota exceeded "Quota exceeded" error Wait for quota reset or request increase
Invalid date format "Invalid date" Use YYYY-MM-DD format: 2025-01-31
No data returned Empty results Check date range and property access

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "google-analytics" '{"command":"/full/path/to/your/project/.venv/bin/python","args":["/full/path/to/your/project/server.py"]}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "google-analytics": {
            "command": "/full/path/to/your/project/.venv/bin/python",
            "args": [
                "/full/path/to/your/project/server.py"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "google-analytics": {
            "command": "/full/path/to/your/project/.venv/bin/python",
            "args": [
                "/full/path/to/your/project/server.py"
            ]
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later