GA4 MCP Server

GA4 MCP Server

Enables interaction with Google Analytics 4 to manage accounts, run standard or realtime reports, and validate event parameters. It supports dual authentication methods and integrates with GTM for end-to-end data collection orchestration.

Category
访问服务器

README

GA4 MCP Server

A Model Context Protocol (MCP) server for Google Analytics 4 (GA4) that provides tools for querying GA4 data through the Admin API and Data API.

Features

  • Dual Authentication: Supports both OAuth Playground tokens and Service Account credentials
  • 8 GA4 Tools:
    • Account and property management (Admin API)
    • Report execution and realtime data (Data API)
    • GTM → GA4 parameter validation (cross-platform orchestration)
  • Compatible with Claude Desktop and other MCP-enabled clients
  • GTM Integration: Works with GTM MCP Server for end-to-end event validation

Installation

npm install
npm run build

Quick Start (OAuth Setup)

1. Create OAuth Credentials

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the Google Analytics Data API and Google Analytics Admin API
  4. Go to CredentialsCreate CredentialsOAuth client ID
  5. Select Desktop app as the application type
  6. Download or copy the Client ID and Client Secret

2. Run Setup Script

# Set your OAuth credentials
export GA4_CLIENT_ID="your-client-id.apps.googleusercontent.com"
export GA4_CLIENT_SECRET="your-client-secret"

# Run setup to authenticate with Google
npm run setup

This will:

  • Open a browser for Google login
  • Request read-only access to Google Analytics
  • Save tokens to ~/.ga4-mcp/tokens.json

3. Use the Server

Once authenticated, you can use the MCP server with Claude Desktop or other clients.

Authentication

The server supports two authentication methods, checked in this order:

1. OAuth Playground Tokens (Recommended for personal use)

Set these environment variables:

  • GA4_ACCESS_TOKEN - OAuth access token
  • GA4_REFRESH_TOKEN - OAuth refresh token
  • GA4_CLIENT_ID - OAuth client ID
  • GA4_CLIENT_SECRET - OAuth client secret

Or create ~/.ga4-mcp/tokens.json:

{
  "access_token": "your-access-token",
  "refresh_token": "your-refresh-token",
  "client_id": "your-client-id",
  "client_secret": "your-client-secret"
}

2. Service Account (Recommended for production)

Options (in priority order):

  1. Set GA4_SERVICE_ACCOUNT_JSON environment variable with JSON string
  2. Set GOOGLE_APPLICATION_CREDENTIALS to the JSON file path
  3. Place JSON file at ~/.ga4-mcp/credentials.json
  4. Place JSON file in ./Credential/ folder

Usage

Claude Desktop Configuration

Add to your Claude Desktop configuration (~/.config/claude-desktop/config.json):

{
  "mcpServers": {
    "ga4": {
      "command": "node",
      "args": ["/path/to/ga4-mcp-server/dist/index.js"],
      "env": {
        "GA4_ACCESS_TOKEN": "your-access-token",
        "GA4_REFRESH_TOKEN": "your-refresh-token",
        "GA4_CLIENT_ID": "your-client-id",
        "GA4_CLIENT_SECRET": "your-client-secret"
      }
    }
  }
}

Direct Execution

# With OAuth environment variables
GA4_ACCESS_TOKEN="..." GA4_REFRESH_TOKEN="..." GA4_CLIENT_ID="..." GA4_CLIENT_SECRET="..." node dist/index.js

# With Service Account
GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json" node dist/index.js

Available Tools

Admin API Tools

Tool Description
ga4_account_summaries List all GA4 accounts and properties the user has access to
ga4_property_details Get detailed information about a specific property
ga4_google_ads_links List Google Ads accounts linked to a property
ga4_property_annotations List annotations for a property (limited support)

Data API Tools

Tool Description
ga4_run_report Run a standard GA4 report with dimensions, metrics, and date ranges
ga4_run_realtime_report Run a realtime report for the last 30 minutes
ga4_custom_dimensions_metrics Get custom dimensions and metrics defined for a property

GTM Validation Tool

Tool Description
ga4_validate_gtm_params Validate GTM event parameters against GA4 custom dimensions and data collection

Example Usage

Get Account Summaries

{
  "tool": "ga4_account_summaries"
}

Run a Report

{
  "tool": "ga4_run_report",
  "arguments": {
    "propertyId": "123456789",
    "dateRanges": [
      {"startDate": "30daysAgo", "endDate": "yesterday"}
    ],
    "dimensions": ["country", "deviceCategory"],
    "metrics": ["activeUsers", "sessions"]
  }
}

Run Realtime Report

{
  "tool": "ga4_run_realtime_report",
  "arguments": {
    "propertyId": "123456789",
    "dimensions": ["country"],
    "metrics": ["activeUsers"]
  }
}

Validate GTM Parameters

{
  "tool": "ga4_validate_gtm_params",
  "arguments": {
    "propertyId": "123456789",
    "gtmEvents": [
      {
        "eventName": "purchase",
        "parameters": ["transaction_id", "value", "currency"]
      }
    ],
    "startDate": "7daysAgo",
    "endDate": "yesterday"
  }
}

Or with GTM Export JSON:

{
  "tool": "ga4_validate_gtm_params",
  "arguments": {
    "propertyId": "123456789",
    "gtmExportJson": { "...GTM container export data..." }
  }
}

GTM Integration (Claude Agent Orchestration)

This server works with the GTM MCP Server to provide end-to-end validation of GTM event parameters in GA4.

Architecture

┌─────────────────────────────────────────────────────────────────────┐
│                        Claude Agent                                 │
│                    (Orchestrator Role)                              │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│  ① GTM MCP Server                  ② GA4 MCP Server                │
│  (gtmAgent/mcp-server)             (ga4-mcp-server)                 │
│                                                                     │
│  gtm_export_full()                 ga4_validate_gtm_params()        │
│  gtm_tag().list()                  ga4_custom_dimensions_metrics()  │
│       │                            ga4_run_report()                 │
│       │                                   ▲                         │
│       └───────────────────────────────────┘                         │
│                Agent relays data                                    │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

Workflow

  1. Extract from GTM: gtm_export_full() → Get GA4 Event tags and parameters
  2. Validate in GA4: ga4_validate_gtm_params() → Check registration and collection
  3. Get recommendations: Parameters not registered or not collecting data

Claude Desktop Configuration (Both Servers)

{
  "mcpServers": {
    "gtm": {
      "command": "node",
      "args": ["/path/to/gtmAgent/mcp-server/dist/index.js"],
      "env": {}
    },
    "ga4": {
      "command": "node",
      "args": ["/path/to/ga4-mcp-server/dist/index.js"],
      "env": {
        "GA4_ACCESS_TOKEN": "your-access-token",
        "GA4_REFRESH_TOKEN": "your-refresh-token",
        "GA4_CLIENT_ID": "your-client-id",
        "GA4_CLIENT_SECRET": "your-client-secret"
      }
    }
  }
}

API Call Efficiency

Scenario Traditional Optimized Reduction
5 events × 21 params 105 calls 22 calls 79%
14 events × 32 params 448 calls 33 calls 93%

The validation tool queries metadata once, then makes one API call per unique parameter instead of per event-parameter combination.

API Documentation References

Required Google API Scopes

The server uses the following scope:

  • https://www.googleapis.com/auth/analytics.readonly

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选