GA4 Analytics MCP
Enables querying Google Analytics 4 properties through natural language, supporting historical and real-time reports, metadata discovery, and property listing via a Vercel-deployed MCP server.
README
GA4 Analytics MCP
Personal Google Analytics 4 connector for Claude.ai Custom Connectors, hosted on Google Cloud Run.
Claude.ai Custom Connector
→ https://ga4-mcp-xxxxx-uc.a.run.app/ga4mcp
→ Cloud Run
→ Google Analytics Data API
→ your GA4 properties
There is no local stdio server, no npx requirement, and no claude_desktop_config.json.
Two authentication layers stay separate:
- Claude → MCP: MCP OAuth (CIMD / DCR)
- MCP → Google: Google OAuth refresh token stored in
GOOGLE_REFRESH_TOKEN
MCP tools
| Tool | Purpose |
|---|---|
ga4_list_properties |
Discover properties on the connected Google account |
ga4_get_metadata |
List valid dimensions and metrics |
ga4_run_report |
Historical GA4 reports |
ga4_run_realtime_report |
Last ~30 minutes |
Local development
npm install
copy .env.example .env.local
Fill in .env.local, then:
npm run dev
- App:
http://localhost:3000 - MCP:
http://localhost:3000/ga4mcp - Google OAuth:
http://localhost:3000/oauth/google - Health:
http://localhost:3000/health
npm test
npm run build
Claude.ai cannot reach localhost. Deploy to Cloud Run before adding the Custom Connector.
Google Cloud setup (one project)
Use the same Google Cloud project for APIs, OAuth, and Cloud Run.
1. Install and sign in
- Install the Google Cloud SDK.
- Run:
gcloud auth login
gcloud auth application-default login
- Create or select a project in Google Cloud Console.
gcloud config set project YOUR_PROJECT_ID
2. Enable APIs
.\scripts\cloud-run-setup.ps1 -ProjectId YOUR_PROJECT_ID -Region us-central1
This enables:
- Cloud Run
- Cloud Build
- Artifact Registry
- Google Analytics Data API
- Google Analytics Admin API
Or enable them in Console: APIs & Services → Library.
3. OAuth consent and web client
This Google OAuth client is only so Cloud Run can read your GA4 data. It is not the Claude.ai Advanced Settings client.
- Open APIs & Services → OAuth consent screen.
- User type: External for a personal Gmail account.
- App name:
GA4 MCP. - Add yourself as a test user if you stay in Testing.
- Publish to Production so refresh tokens do not expire after 7 days.
- Create OAuth client ID credentials.
- Application type: Web application.
- Authorized redirect URIs (add both):
http://localhost:3000/oauth/google/callbackhttps://ga4-mcp-XXXXXXXX-uc.a.run.app/oauth/google/callback
(use the real Cloud Run URL after the first deploy)
- Scope used by this app:
https://www.googleapis.com/auth/analytics.readonly
- Copy the client ID and client secret. Do not commit them.
The Google account you authorize must already have access to the GA4 properties Claude should query.
Deploy to Cloud Run
Default service name: ga4-mcp. Default region: us-central1.
.\scripts\cloud-run-deploy.ps1 -ProjectId YOUR_PROJECT_ID -Region us-central1
The script prints:
https://ga4-mcp-XXXXXXXX-uc.a.run.app
https://ga4-mcp-XXXXXXXX-uc.a.run.app/ga4mcp
https://ga4-mcp-XXXXXXXX-uc.a.run.app/health
https://ga4-mcp-XXXXXXXX-uc.a.run.app/oauth/google/callback
The service is deployed allow unauthenticated. That is required. Claude.ai connects from Anthropic (160.79.104.0/21). Auth is MCP_AUTH_TOKEN / MCP OAuth, not Cloud Run IAM.
Set environment variables
.\scripts\cloud-run-set-env.ps1 `
-ProjectId YOUR_PROJECT_ID `
-AppBaseUrl "https://ga4-mcp-XXXXXXXX-uc.a.run.app" `
-GoogleClientId "....apps.googleusercontent.com" `
-GoogleClientSecret "...." `
-McpAuthToken "a-long-random-string"
Then add the Cloud Run callback URL to the Google OAuth client if you have not already.
Connect Google
- Open
https://ga4-mcp-XXXXXXXX-uc.a.run.app/oauth/google - Enter
MCP_AUTH_TOKEN - Sign in with your Google account
- Copy
GOOGLE_REFRESH_TOKENfrom the success page - Set it and let Cloud Run start a new revision:
.\scripts\cloud-run-set-env.ps1 `
-ProjectId YOUR_PROJECT_ID `
-AppBaseUrl "https://ga4-mcp-XXXXXXXX-uc.a.run.app" `
-GoogleClientId "....apps.googleusercontent.com" `
-GoogleClientSecret "...." `
-McpAuthToken "a-long-random-string" `
-GoogleRefreshToken "1//...."
Cloud Run cannot write env vars from inside the container. Same rule as any serverless host.
Confirm the service
https://ga4-mcp-XXXXXXXX-uc.a.run.app/health
must return:
{"status":"ok"}
Environment variables
| Variable | Required | Purpose |
|---|---|---|
APP_BASE_URL |
Yes | Cloud Run origin, no trailing slash |
GOOGLE_CLIENT_ID |
Yes | Google OAuth web client |
GOOGLE_CLIENT_SECRET |
Yes | Google OAuth web client secret |
GOOGLE_REDIRECT_URI |
No | Defaults to ${APP_BASE_URL}/oauth/google/callback |
MCP_AUTH_TOKEN |
Yes | Operator setup token for Google OAuth and Claude MCP consent |
GOOGLE_REFRESH_TOKEN |
After Google OAuth | Long-lived Google token |
OAUTH_STATE_SECRET |
No | Signs Google OAuth state cookies |
MCP_TOKEN_SECRET |
No | Signs MCP JWTs. Defaults to MCP_AUTH_TOKEN |
MCP_OAUTH_CLIENT_ID |
No | Only for Claude.ai Advanced Settings confidential client |
MCP_OAUTH_CLIENT_SECRET |
No | Pair for that optional client |
Set these on the Cloud Run service. Do not put them in Git.
Optional Console path: Cloud Run → ga4-mcp → Edit & deploy new revision → Variables & secrets.
Claude.ai Custom Connector
- Confirm
/healthreturns{"status":"ok"}. - Finish Google OAuth and set
GOOGLE_REFRESH_TOKEN. - In Claude.ai open Customize → Connectors → Add custom connector.
- Name:
GA4 Analytics - URL:
https://ga4-mcp-XXXXXXXX-uc.a.run.app/ga4mcp
- Leave Advanced OAuth Client ID / Secret empty.
- Click Add.
- Enable the connector in + → Connectors.
- The first GA4 tool call shows Connect. Enter
MCP_AUTH_TOKENon this app’s consent page (not your Google password). - Ask: How many users did I have yesterday?
Manual gcloud (if you do not want the scripts)
gcloud artifacts repositories create ga4-mcp --repository-format=docker --location=us-central1
gcloud builds submit --config cloudbuild.yaml --substitutions=_REGION=us-central1
gcloud run services describe ga4-mcp --region us-central1 --format="value(status.url)"
gcloud run services update ga4-mcp --region us-central1 --update-env-vars APP_BASE_URL=https://...,GOOGLE_CLIENT_ID=...,GOOGLE_CLIENT_SECRET=...,GOOGLE_REDIRECT_URI=https://.../oauth/google/callback,MCP_AUTH_TOKEN=...
Security
- Never log Google tokens, authorization codes, client secrets, or MCP JWTs.
- MCP tools never return secrets.
- Only
GOOGLE_REFRESH_TOKENis persisted, as a Cloud Run env var. - Cloud Run ingress is public so Claude can connect. Do not also put a Cloud IAP / IAM login in front of
/ga4mcp. - Generate a long random
MCP_AUTH_TOKEN.
Dates
Passed to GA4 unchanged: today, yesterday, 7daysAgo, 30daysAgo, 90daysAgo, or YYYY-MM-DD. The date dimension comes back as YYYYMMDD.
Known limitations
- One Google account and one refresh token.
- Cloud Run cannot persist a file or mutate env vars at runtime; set
GOOGLE_REFRESH_TOKENand deploy a new revision. - Google Testing-mode refresh tokens expire after about 7 days.
- Realtime data is roughly the last 30 minutes.
- Report size is capped at 10,000 rows.
- Cold starts can add a few seconds when
min-instancesis 0.
Files added for Cloud Run
| File | Purpose |
|---|---|
Dockerfile |
Production Next.js standalone image |
cloudbuild.yaml |
Build image and deploy Cloud Run |
scripts/cloud-run-setup.ps1 |
Enable APIs and Artifact Registry |
scripts/cloud-run-deploy.ps1 |
Build and deploy |
scripts/cloud-run-set-env.ps1 |
Set Cloud Run env vars |
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。