Skip to main content
Glama

ga4-mcp-worker

一个只读的 Google Analytics 4 MCP 服务器,运行在 Cloudflare Workers 上,让团队成员无需在本地安装任何东西就能通过 Claude 查询 GA4 —— 不需要 Python,不需要 gcloud,不需要 ADC 凭据文件,而且从这个版本开始,也不需要共享密码。 接入方式就是点击“Connect”,用你自己的 Google 账户登录,然后以你自己的身份回答 GA4 问题。

它复刻了官方 googleanalytics/google-analytics-mcp stdio 服务器的工具表面,但直接调用 Google Analytics REST API,因为 Google 的客户端库无法在 Workers 运行时上运行。

请先阅读: docs/IMPLEMENTATION-NOTES.md § 已测试和未测试的内容。 实时端到端验证已经包含在内,但少数几块(v1alpha 端点、分页)仍只有 fixture 测试覆盖。

认证模式:每用户 OAuth

每位团队成员都用自己的 Google 账户登录。没有共享密钥,也没有大家共同依赖的单一 Google 凭据——完整设计以及它与已退役的共享密钥模式的区别,见 PRODUCTION.md § 1docs/PRODUCTION-PART2.md § 8。登录限制为 @zuddl.com 的 Google 账户(wrangler.toml 中的 ALLOWED_EMAIL_DOMAIN)。

GOOGLE_CLIENT_IDGOOGLE_CLIENT_SECRET 仍然必需——Google 要求每个请求用户数据的应用都必须完成注册,而这两个值就是注册本身,部署时设置一次即可。去掉的是手工签发的 refresh token,以及队友们曾经需要输入的密码。


文档

文件

内容

SETUP-GUIDE.md(+ 第 2 部分

从零开始讲解首次部署,不假设任何 OAuth 背景知识。包括创建 Google 应用、部署、测试、连接 Claude。从这里开始。

PRODUCTION.md(+ 第 2 部分

身份模型、监控、GA4 配额、密钥轮换、本地开发、runbook、安全清单、OAuth 内部机制。大规模部署前请阅读。

docs/IMPLEMENTATION-NOTES.md

这个实现合理的关键点、已经测试和未测试的内容,以及运行成本。

src/index.ts

MCP 服务器:工具、fetch 辅助函数、normalizer、router。注释详细。

src/google-oauth.ts

“sign in with Google”处理逻辑 —— /authorize/callback。注释详细。

wrangler.toml

Worker 配置。注意 nodejs_compat——见 gotchas

.dev.vars.example

本地开发模板。复制为 .dev.vars(已被 gitignore 忽略)。


Related MCP server: GA4 MCP Server

我们的 GA4 媒体资源

媒体资源 ID

名称

Measurement ID

状态

314138239

New zuddl website GA4 property

G-JWBQ2Z84QF

权威(canonical),除非另有说明,请使用这一个。

260479909

legacy

历史资源;只查看历史数据,勿用于当前报告。

328977581

legacy

历史资源;只查看历史数据,勿用于当前报告。

这个服务是媒体资源无感知的(property-agnostic):每个工具都接受 property_id,既可以传 314138239,也可以传 "properties/314138239"。如果不传,则回退到 wrangler.toml 里的 DEFAULT_PROPERTY_ID,默认指向上述权威媒体资源,这样队友不用记编号。调用 get_account_summaries 可以列出这些凭据实际能看到的所有内容。


工具(9 个)

工具

方法 + 端点

用途

run_report

POST analyticsdata.googleapis.com/v1beta/properties/{id}:runReport

核心工具。 历史数据报告。完整参数集。

run_realtime_report

POST .../v1beta/properties/{id}:runRealtimeReport

最近约 30 分钟。独立、较简洁的 schema。

run_funnel_report

POST .../v1alpha/properties/{id}:runFunnelReport

有序步骤序列与流失分析。

run_conversions_report

POST .../v1alpha/properties/{id}:runReport

转化、广告支出、ROAS、归因建模。受限字段列表。

get_custom_dimensions_and_metrics

GET .../v1beta/properties/{id}/metadata

自定义字段,以可查询的 apiName 返回。使用自定义字段前请先调用。

get_account_summaries

GET analyticsadmin.googleapis.com/v1beta/accountSummaries

列出这些凭据可以读取的所有内容。无需参数。

get_property_details

GET .../v1beta/properties/{id}

时区、货币、服务等级。时区可以解释日期不一致的问题。

list_property_annotations

GET .../v1alpha/properties/{id}/reportingDataAnnotations

带日期的注释,用于解释数据尖峰与下跌。

list_google_ads_links

GET .../v1beta/properties/{id}/googleAdsLinks

已关联的 Google Ads 账户;确认广告支出指标是否可能有数据。

run_report 接受完整的官方参数集: property_iddate_ranges列表,所以一次请求即可完成周期对比)、dimensionsmetricsdimension_filtermetric_filterorder_byslimitoffsetcurrency_codereturn_property_quotarun_realtime_report 使用相同结构,但不含 date_rangescurrency_code

工具描述是刻意写长的——它们包含了每种过滤器句式的可执行示例,也是模型学习请求格式的唯一来源。请把对这些内容的编辑视为行为变更。


端点

端点

认证

用途

POST /mcp

OAuth bearer token

MCP 连接(Streamable HTTP)。没有 /sse

GET /authorizePOST /tokenPOST /register

@cloudflare/workers-oauth-provider 实现的 OAuth 端点。

GET /callback

Google 登录后的跳转入口。不要人工直接打开。

GET /health

状态检查。200 {"status":"ok","auth":"oauth", ...}503 + 详情。见下方说明——它的证明力度远小于在 shared-secret 模式下。不会泄露数据。可以挂到 uptime monitor 上。

GET /

纯文本状态横幅,显示当前认证模式。

其他路径统一返回 404。

/health 在 OAuth 模式下能担保的更少。 没有单一的共享凭据可供检查,所以 200 只说明应用注册已配置好、授权存储(OAUTH_KV)可访问,并不代表某个特定用户的登录仍然有效。某个用户的授权可能已过期或被撤销,而 /health 仍是绿色。这属于 per-user 认证的固有特性,不是缺陷。

Worker 会为每个 MCP 请求、每次被拒绝的认证尝试、每个工具错误以及每次健康检查失败记录一行 JSON 日志,此外还有 OAuth 专用事件(oauth_authorize_redirectoauth_authorizedoauth_domain_rejected 等)。任何令牌(token)内容都不会被记录。所有 Google API 调用都会携带自定义 User-Agentga4-mcp-worker/1.0.0 (+cloudflare-workers)),以便跟踪配额来源。


只读保障

以下三层相互独立,且都保持启用:

  1. OAuth scope(授权范围) —— 每个 token,无论是旧共享的还是队友个人的,都只会请求 https://www.googleapis.com/auth/analytics.readonly,不会申请更多;OAuth 模式还会加上 openid/email(用于身份识别——见 src/google-oauth.ts,这两个 scope 不授予数据访问权限)。Google 会在服务端拒绝写操作。这才是真正的保证。 SETUP-GUIDE.md 里演示了如何通过 tokeninfo 验证这一点。

  2. 端点白名单 —— 两个 fetch 辅助函数会在发起请求检查锚定正则。如果将来的编辑引入了写端点,会直接抛出错误而不是发送请求。

  3. 没有修改性动词 —— 代码库中不存在 PATCHPUTDELETE。全项目只有三处 fetch 调用:token 刷新(POST)、gaGet(GET)和 gaPost(POST)。

:runReport:runRealtimeReport:runFunnelReport 虽然是 HTTP POST,但都是查询——请求体携带报表定义,是因为查询串放不下而已。它们在 GA4 中不创建也不修改任何数据。


快速命令

npm install
npm run typecheck && npm run dry-run
npm run deploy
npm run tail
curl -s https://ga4-mcp-worker.YOUR-SUBDOMAIN.workers.dev/health

部署两种认证模式都需要的那两个 secrets(请采用交互式粘贴——通过管道传入会改变值):

npx wrangler secret put GOOGLE_CLIENT_ID
npx wrangler secret put GOOGLE_CLIENT_SECRET

从 Claude Code 连接——无需 header,因为登录发生在浏览器中:

claude mcp add --transport http --scope user ga4 https://ga4-mcp-worker.YOUR-SUBDOMAIN.workers.dev/mcp

然后在交互式 claude 会话中,运行 /mcp,选择 ga4,用 Google 登录即可。


更多内容

F
license - not found
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    A
    quality
    B
    maintenance
    Connects Google Analytics 4 data to Claude, Cursor and other MCP clients, enabling natural language queries of website traffic, user behavior, and analytics data with access to 200+ GA4 dimensions and metrics.
    10
    235
    MIT
  • A
    license
    A
    quality
    B
    maintenance
    Connects MCP clients like Claude Desktop to Google Analytics 4 Data API, enabling natural language queries for reports, top pages, traffic sources, conversions, realtime users, and period comparisons.
    7
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    Enables querying Google Analytics 4 data using natural language through MCP clients like Claude and Cursor, supporting 200+ dimensions and metrics for traffic, user behavior, and e-commerce analysis.
    MIT

View all related MCP servers

Related MCP Connectors

  • WHOOP recovery, strain, sleep and workouts in Claude via official WHOOP OAuth. Free, open source.

  • GA4 conversion analyst inside Claude — funnel drops, traffic anomalies, device gaps, with numbers.

  • Garmin data in Claude & ChatGPT via the Garmin Health API. OAuth sign-in, no password sharing.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/prashantdasari-tech/ga4-mcp-worker'

If you have feedback or need assistance with the MCP directory API, please join our Discord server