Skip to main content
Glama
Google Analytics MCP Server

Google Analytics MCP Server

Google Analytics MCP

用于 Google Analytics 4 的本地 MCP 服务器。将 GA4 Data API 封装在专注于 SEO 和付费 报告的工具中。

工具

工具

用途

ga4_organic_search_performance

SEO:按着陆页、来源、国家或设备划分的自然搜索会话/用户/互动/转化

ga4_paid_search_performance

付费:按广告系列、着陆页、关键词或来源划分的付费搜索会话/转化/成本/CPC

ga4_all_paid_performance

付费:所有付费渠道(搜索 + 社交 + 视频 + 展示)的成本和 ROAS

ga4_google_ads_performance

付费:Google Ads 广告系列 × 广告组 × 关键词(需要 GA4 ↔ Google Ads 关联)

ga4_landing_page_performance

按会话/互动/转化排序的顶级着陆页(可选渠道筛选)

ga4_channel_performance

默认渠道组汇总

ga4_campaign_performance

广告系列 × 来源 × 媒介细分

ga4_conversions_by_source

按来源/媒介/渠道划分的转化和关键事件

ga4_realtime_active_users

过去 30 分钟内的活跃用户

ga4_run_report

灵活的逃生舱 — 任意维度 + 指标

设置

git clone https://github.com/ZLeventer/google-analytics-mcp-server.git
cd google-analytics-mcp-server
npm install
npm run build

凭据

将 .env.example 复制为 .env 并填写:

  • GA4_PROPERTY_ID — 来自 GA4 管理 → 媒体资源设置的数字媒体资源 ID(例如 123456789)。不是衡量 ID (G-XXXXXXX)。

  • GOOGLE_APPLICATION_CREDENTIALS — 服务账号 JSON 密钥文件的绝对路径。

创建服务账号

  1. 前往 console.cloud.google.com → 选择/创建一个项目

  2. API 和服务 → 库 → 启用 Google Analytics Data API

  3. IAM 和管理 → 服务账号 → 创建服务账号(命名为 ga4-mcp 或类似名称)

  4. 在创建的账号上 → 密钥 → 添加密钥 → 创建新密钥 → JSON → 下载

  5. 将 JSON 保存为该目录下的 service-account.json(已包含在 .gitignore 中)

  6. 在 GA4 → 管理 → 媒体资源访问权限管理 中,点击 + → 粘贴服务账号电子邮件 (ga4-mcp@project-id.iam.gserviceaccount.com) → 分配 查看者 角色

Google SDK 会自动读取 GOOGLE_APPLICATION_CREDENTIALS — 无需更改代码。

安装到 Claude Code

claude mcp add google-analytics \
  --env-file "/absolute/path/to/google-analytics-mcp-server/.env" \
  -- node "/absolute/path/to/google-analytics-mcp-server/dist/index.js"

或者手动添加到 ~/.claude.json:

{
  "mcpServers": {
    "google-analytics": {
      "command": "node",
      "args": ["/absolute/path/to/google-analytics-mcp-server/dist/index.js"],
      "env": {
        "GA4_PROPERTY_ID": "123456789",
        "GOOGLE_APPLICATION_CREDENTIALS": "/absolute/path/to/google-analytics-mcp-server/service-account.json"
      }
    }
  }
}

安装后重启 Claude Code。

日期格式

所有日期参数均接受 GA4 格式:

  • YYYY-MM-DD(例如 2026-04-01)

  • NdaysAgo(例如 28daysAgo, 90daysAgo)

  • yesterday, today

默认值:28daysAgo → yesterday。

验证

配置好凭据后:

node -e '
  import("./dist/tools/reports.js").then(m =>
    m.channelPerformance({ start_date: "7daysAgo", end_date: "yesterday", limit: 10 })
  ).then(r => console.log(JSON.stringify(r, null, 2)))
'

如果返回了行数据,则说明一切正常。预期错误:

  • GA4_PROPERTY_ID is not set — 检查 .env

  • PERMISSION_DENIED — 服务账号未被授予该媒体资源的查看者权限

  • 403 Google Analytics Data API has not been used — 在 GCP 项目中启用该 API

路线图

  • [ ] 管理 API 工具(列出自定义维度、转化事件、受众群体),按需添加

  • [ ] Search Console MCP 作为兄弟服务器,用于排名/展示次数/点击率数据(与此服务器互补)

  • [ ] 与 Demandbase MCP 配对,将 GA4 流量与公司画像(账户级归因)结合

Available Tools

10 tools
ga4_all_paid_performanceA

Paid: all paid channels (Search + Social + Video + Display + Other) broken down by channel × campaign × source × medium, with cost and ROAS.

Inputs: start_date, end_date, property_id, limit.

ga4_campaign_performanceB

Campaign × source × medium breakdown with sessions/conversions/revenue. Optional channel filter.

Inputs: start_date, end_date, property_id, limit, +1 more.

ga4_channel_performanceB

Default channel group rollup: sessions/users/conversions/revenue for Organic Search, Paid Search, Email, Direct, Referral, etc.

Inputs: start_date, end_date, property_id, limit.

ga4_conversions_by_sourceB

Conversions and key events broken down by session source × medium × channel group. Sorted by key events desc.

Inputs: start_date, end_date, property_id, limit.

ga4_google_ads_performanceA

Paid: Google Ads campaign × ad group × keyword performance (impressions, clicks, cost, CPC, conversions, ROAS). Requires Google Ads ↔ GA4 link.

Inputs: start_date, end_date, property_id, limit.

ga4_landing_page_performanceA

Top landing pages by sessions/engagement/conversions. Optional channel filter (e.g. 'Organic Search', 'Paid Search').

Inputs: start_date, end_date, property_id, limit, +1 more.

ga4_organic_search_performanceA

SEO: sessions/users/engagement/conversions for Organic Search traffic, broken down by landing page (default), source, country, or device.

Inputs: start_date, end_date, property_id, limit, +1 more.

ga4_paid_search_performanceB

Paid: sessions/conversions/cost/CPC for Paid Search traffic, broken down by campaign (default), landing page, keyword, or source.

Inputs: start_date, end_date, property_id, limit, +1 more.

ga4_realtime_active_usersB

Current active users (last 30 min) broken down by screen/page, country, device, or audience.

Inputs: property_id, breakdown, limit.

ga4_run_reportA

Flexible GA4 Data API runReport. Pass any dimensions + metrics. Use for custom reports the preset tools don't cover.

Inputs: dimensions (required), metrics (required), start_date, end_date, +4 more.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.7/5.0

    Scored across 10 tools

    Disambiguation5/5

    Each tool targets a distinct area of GA4 data: all paid, specific channels, campaigns, conversions, landing pages, organic/paid search, realtime, and a generic report. No two tools have overlapping purposes that would cause confusion.

    Naming Consistency5/5

    All tool names consistently follow the 'ga4_<descriptive_name>' format using snake_case. The naming pattern is predictable and uniform across the entire set.

    Tool Count5/5

    10 tools is well within the ideal 3-15 range for a focused analytics server. Each tool serves a clear purpose, and the count feels neither excessive nor insufficient for covering core GA4 queries.

    Completeness4/5

    The set covers acquisition, behavior, conversions, and realtime analytics well. Minor gaps exist (e.g., user demographics, event details), but the flexible ga4_run_report tool allows agents to fill these gaps for custom reports.

    Related MCP Connectors