Google Analytics MCP Server

Google Analytics MCP Server

Enables LLM applications to query and analyze Google Analytics 4 data through standard MCP interfaces, supporting service account and OAuth2 authentication.

Category
访问服务器

README

Google Analytics MCP Server

A Model Context Protocol (MCP) server that provides seamless access to Google Analytics 4 data through standard MCP interfaces. This tool allows LLM applications to easily query and analyze Google Analytics data without directly dealing with the complexities of the Google Analytics Data API.

✨ Features

  • 🔐 Dual Authentication: Support both Service Account and OAuth2 User Authorization
  • Real-time Data Access: Get real-time analytics data for current active users and activity
  • Custom Reports: Create comprehensive reports with custom dimensions and metrics
  • Quick Insights: Predefined analytics insights for common use cases
  • Metadata Discovery: Get available dimensions and metrics for your Google Analytics property
  • 🔄 Auto Token Refresh: Automatically refreshes expired OAuth2 access tokens
  • Smart Error Handling: Detailed error messages with actionable solutions for permission issues
  • Standard MCP Interface: Works with any MCP-compatible client

Installation

npm install @toolsdk.ai/google-analytics-mcp

Prerequisites

  • Node.js >= 18.0.0
  • Google Analytics 4 property
  • Either:
    • Service Account credentials (default), OR
    • OAuth2 tokens from a user authorization flow

🔐 Authentication Modes

This MCP server supports two authentication modes, controlled by the GOOGLE_AUTH_MODE environment variable:

Mode Value Description
Service Account service_account (default) Use a GCP service account JSON key
OAuth2 oauth2 Use user-authorized OAuth2 tokens

Mode 1: Service Account (Default)

Use this mode for server-to-server authentication without user interaction.

Setup Steps

  1. Create a Service Account in Google Cloud Console
  2. Download the JSON key file
  3. Grant access to your GA4 property:
    • Go to Google Analytics → Admin → Property Access Management
    • Add the service account email (e.g., xxx@project.iam.gserviceaccount.com) with Viewer access

Environment Variables

# Optional: defaults to 'service_account'
GOOGLE_AUTH_MODE=service_account

# Option 1: Direct JSON string
GOOGLE_CREDENTIALS='{"type":"service_account","project_id":"...","private_key":"...","client_email":"..."}'

# Option 2: Path to JSON file
GOOGLE_CREDENTIALS_PATH=/path/to/service-account.json

Claude Desktop Configuration

{
  "mcpServers": {
    "google-analytics-mcp": {
      "command": "npx",
      "args": ["-y", "@toolsdk.ai/google-analytics-mcp"],
      "env": {
        "GOOGLE_AUTH_MODE": "service_account",
        "GOOGLE_CREDENTIALS_PATH": "/path/to/service-account.json"
      }
    }
  }
}

Mode 2: OAuth2 User Authorization

Use this mode to access GA data on behalf of a user with their own permissions.

Advantages

  • ✅ Access the user's own GA properties without service account setup
  • ✅ No need to add service accounts to GA property permissions
  • ✅ Works with the user's existing Google account permissions

⚠️ Important: This MCP server does NOT include the OAuth2 authorization flow itself. You need to implement the OAuth2 consent flow separately to obtain the tokens.

1. Implement OAuth2 Authorization Flow (Your Responsibility)

You need to implement the OAuth2 authorization flow using libraries like:

Required OAuth2 scopes:

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

Example OAuth2 flow (simplified):

import { google } from 'googleapis';

const oauth2Client = new google.auth.OAuth2(
  CLIENT_ID,
  CLIENT_SECRET,
  REDIRECT_URI
);

// Generate auth URL and redirect user
const authUrl = oauth2Client.generateAuthUrl({
  access_type: 'offline',  // Important: to get refresh_token
  scope: ['https://www.googleapis.com/auth/analytics.readonly']
});

// After user consent, exchange code for tokens
const { tokens } = await oauth2Client.getToken(code);

// Save tokens to tokens.json
fs.writeFileSync('tokens.json', JSON.stringify(tokens, null, 2));

2. tokens.json Format

After completing the OAuth2 flow, save the tokens in a tokens.json file:

{
  "access_token": "ya29.a0AWY7CknXXX...",
  "refresh_token": "1//0eXXX...",
  "scope": "https://www.googleapis.com/auth/analytics.readonly",
  "token_type": "Bearer",
  "expiry_date": 1234567890000
}
Field Description Required
access_token The OAuth2 access token ✅ Yes
refresh_token The refresh token (for auto-renewal) ⚠️ Recommended
scope Authorized scopes Optional
token_type Token type (usually "Bearer") Optional
expiry_date Token expiration timestamp (ms) ⚠️ Recommended

3. Token Loading Priority

The MCP server loads OAuth2 tokens in the following order:

  1. Direct JSON string via GOOGLE_OAUTH2_TOKENS environment variable
  2. Path specified by GOOGLE_OAUTH2_TOKEN_PATH environment variable
  3. {current working directory}/tokens.json
  4. {current working directory}/../tokens.json
  5. {src directory}/../../tokens.json

4. Environment Variables (OAuth2 Mode)

GOOGLE_AUTH_MODE=oauth2

# Option 1: Direct JSON string
GOOGLE_OAUTH2_TOKENS='{"access_token":"ya29...","refresh_token":"1//0e...","expiry_date":1234567890000}'

# Option 2: Path to tokens.json
GOOGLE_OAUTH2_TOKEN_PATH=/path/to/your/tokens.json

# Optional: For automatic token refresh
GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=your-client-secret

Claude Desktop Configuration (OAuth2)

{
  "mcpServers": {
    "google-analytics-mcp": {
      "command": "npx",
      "args": ["-y", "@toolsdk.ai/google-analytics-mcp"],
      "env": {
        "GOOGLE_AUTH_MODE": "oauth2",
        "GOOGLE_OAUTH2_TOKEN_PATH": "/path/to/your/tokens.json",
        "GOOGLE_CLIENT_ID": "your-client-id (optional)",
        "GOOGLE_CLIENT_SECRET": "your-client-secret (optional)"
      }
    }
  }
}

⚠️ Token Lifecycle Management (OAuth2 Mode)

Access Token Expiration

  • Google OAuth2 access tokens typically expire after 1 hour
  • The MCP server will automatically attempt to refresh tokens if refresh_token is provided

Automatic Token Refresh

When configured with GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET, the server will:

  1. Detect when the access token is expired
  2. Use the refresh_token to obtain a new access_token
  3. Automatically save the new tokens to tokens.json

Refresh Token Considerations

⚠️ Important Notes about Refresh Tokens:

  1. Refresh tokens can expire or be revoked:

    • If the user revokes access in their Google Account settings
    • If the refresh token is unused for 6 months (for non-verified apps)
    • If you've exceeded the limit of 100 refresh tokens per user per client
  2. Getting a refresh token:

    • You only get a refresh_token on the first authorization
    • Use access_type: 'offline' in your auth URL
    • Use prompt: 'consent' to force re-consent and get a new refresh token
  3. Recommended practices:

    • Always request access_type: 'offline' during OAuth2 flow
    • Store and protect the refresh_token securely
    • Implement re-authorization flow for when refresh tokens become invalid
    • Monitor for invalid_grant errors which indicate the refresh token is no longer valid

Available Tools

analytics_report

Get comprehensive Google Analytics data with custom dimensions and metrics. Can create any type of report.

Parameters:

  • propertyId (string, required): Google Analytics property ID
  • startDate (string, required): Start date (YYYY-MM-DD)
  • endDate (string, required): End date (YYYY-MM-DD)
  • dimensions (array, optional): Dimensions to query
  • metrics (array, required): Metrics to query
  • dimensionFilter (object, optional): Filter by dimension values
  • metricFilter (object, optional): Filter by metric values
  • orderBy (object, optional): Sort results by dimension or metric
  • limit (number, optional): Limit number of results (default: 100)

realtime_data

Get real-time analytics data for current active users and activity.

Parameters:

  • propertyId (string, required): Google Analytics property ID
  • dimensions (array, optional): Dimensions for real-time data
  • metrics (array, optional): Real-time metrics (default: ['activeUsers'])
  • limit (number, optional): Limit number of results (default: 50)

quick_insights

Get predefined analytics insights for common use cases.

Parameters:

  • propertyId (string, required): Google Analytics property ID
  • startDate (string, required): Start date (YYYY-MM-DD)
  • endDate (string, required): End date (YYYY-MM-DD)
  • reportType (string, required): Type of quick insight report (overview, top_pages, traffic_sources, etc.)
  • limit (number, optional): Limit number of results (default: 20)

get_metadata

Get available dimensions and metrics for Google Analytics property.

Parameters:

  • propertyId (string, required): Google Analytics property ID
  • type (string, optional): Type of metadata to retrieve (dimensions, metrics, both)

search_metadata

Search for specific dimensions or metrics by name or category.

Parameters:

  • propertyId (string, required): Google Analytics property ID
  • query (string, required): Search term to find dimensions/metrics
  • type (string, optional): Type of metadata to search (dimensions, metrics, both)
  • category (string, optional): Filter by category

Error Handling

The server provides detailed error messages with actionable solutions based on the authentication mode:

Service Account Mode:

  • Permission errors will prompt you to add the service account email to GA property access

OAuth2 Mode:

  • Permission errors will suggest re-authorization
  • Token errors will indicate refresh or re-authorization needs

Environment Variables Summary

Variable Mode Description
GOOGLE_AUTH_MODE Both service_account (default) or oauth2
GOOGLE_CREDENTIALS Service Account JSON string of service account key
GOOGLE_CREDENTIALS_PATH Service Account Path to service account JSON file
GOOGLE_OAUTH2_TOKENS OAuth2 JSON string of OAuth2 tokens
GOOGLE_OAUTH2_TOKEN_PATH OAuth2 Path to tokens.json
GOOGLE_CLIENT_ID OAuth2 Client ID for token refresh
GOOGLE_CLIENT_SECRET OAuth2 Client secret for token refresh

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选