Gtm Ga4

io.github.mharnett由 GitHub 账号 mharnett 发布v1.0.1更新于 Oct 25, 2026

GTM + GA4 MCP: tag management, consent auditing, workspace versioning, reports.

已验证STDIO仅桌面数据与分析商业与电商

概览

AI 生成的概览

本地 MCP 服务器,让助手管理 Google Tag Manager 的代码、触发器和变量,审计同意配置,并运行 GA4 报表。

可以做什么
列出、读取、创建和更新 GTM 代码,并列出工作区中的触发器和变量。审计所有代码的同意配置合规性。预览当前工作区并据此创建容器版本。运行 GA4 报表和实时报表,列出或创建 GA4 自定义维度。
适用场景
适合在使用 Google Tag Manager 和 GA4 时,让助手检查或修改容器配置、核查同意设置,或直接获取分析报表,而无需切换到网页控制台。
运行要求
通过 npm(npx mcp-gtm-ga4)或构建后的克隆在本地运行,需要 Node.js。需在 GOOGLE_APPLICATION_CREDENTIALS 中提供 Google 凭据 JSON 密钥文件路径(服务账号或授权用户 OAuth),并设置 GTM_ACCOUNT_ID、GTM_CONTAINER_ID 和 GA4_PROPERTY_ID。可选 GTM_SANDBOX_WORKSPACE_ID 和 MCP_SERVER_NAME。需要访问 Google API 的网络。
安装前请注意
凭据文件拥有 GTM 容器和 GA4 属性的编辑权限,请勿将其和 .mcp.json 配置提交到 git 或写入共享日志。写入类工具会创建和更新代码与变量、创建容器版本和自定义维度,可能影响线上容器;服务器会阻止写入已解析工作区之外的目标,但本身不会发布。OAuth 辅助脚本会把刷新令牌打印到标准输出。

安装

在 SourceWeft 中

  1. 打开 控制台中的 Gtm Ga4,将其添加到工作区。
  2. 为需要使用其工具的对话启用该服务。

Desktop only,通过 STDIO。 STDIO 服务会启动本地进程,因此需要 SourceWeft 桌面宿主。

其他 MCP 客户端

参照 仓库 中的启动说明。

README

mcp-gtm-ga4

MCP server for Google Tag Manager and GA4 -- tag management, consent auditing, workspace versioning, and analytics reporting via Claude.

Features

  • 14 tools spanning GTM workspace management (tags, triggers, variables), consent compliance auditing, workspace preview/versioning, and GA4 reporting
  • Sandbox safety -- all write operations verify they target the resolved workspace, refusing to write to non-sandbox workspaces
  • Multi-client support -- configure per-client via environment variables
  • Auto-detects Default Workspace ID -- no need to manually specify workspace IDs unless using a custom sandbox

Installation

bash
npm install mcp-gtm-ga4

Or clone and build:

bash
git clone https://github.com/mharnett/mcp-gtm-ga4.gitcd mcp-gtm-ga4npm installnpm run build

Configuration

Security: Never share your .mcp.json file or commit it to git -- it may contain API credentials. Add .mcp.json to your .gitignore.

Runtime configuration is via environment variables.

VariableRequiredDescription
GOOGLE_APPLICATION_CREDENTIALSYesPath to a Google credential JSON key file (service account or authorized-user — see Authentication below)
GTM_ACCOUNT_IDYesGTM account ID
GTM_CONTAINER_IDYesGTM container ID
GA4_PROPERTY_IDYesGA4 property ID
GTM_SANDBOX_WORKSPACE_IDNoOverride workspace ID (auto-detects Default Workspace if omitted)
MCP_SERVER_NAMENoServer name (defaults to package name mcp-gtm-ga4)

See config.example.json for a reference template. The only value read from disk is oauth.scope (in an optional config.json) — the single source of truth for the OAuth scope the onboarding paths request. If no config.json is present, the committed minimum scope is used.

Authentication

This MCP supports two auth models. Both feed the same GOOGLE_APPLICATION_CREDENTIALS runtime path.

Precedence & mechanism

There is no runtime service-account-vs-OAuth toggle. Both models converge on a single slot — GOOGLE_APPLICATION_CREDENTIALS — which the runtime hands to GoogleAuth({ keyFile }). Whichever file you point that env var at is the credential:

  • a service-account JSON key (option 1 below), or
  • the authorized_user keyfile the auth subcommand writes (option 2) — a file that plugs into the exact same slot.

So the only real precedence rule is failure handling: an explicitly-configured keyfile is used; when GOOGLE_APPLICATION_CREDENTIALS is unset the server fails loudly at startup with an onboarding error naming both the service-account path and the auth OAuth helper. It deliberately does not fall back to Google's Application Default Credentials (gcloud user creds / GCE metadata server) — no silent machine-local default, no silent runtime failover.

1. Service account (primary, recommended for unattended/server use)

Create a service account in your GCP project, download its JSON key, grant it the GTM container role (on the target GTM container) and GA4 property access, and point GOOGLE_APPLICATION_CREDENTIALS at the key file. No OAuth flow, no browser, no refresh token. This is the recommended path for headless/server/unattended deployments.

2. User OAuth (interactive, for users without a service account)

If you can't use a service account, mint a user credential with your own Google OAuth client (a "Desktop app" OAuth 2.0 Client ID created in your own GCP project — enable the Tag Manager API and the Google Analytics Admin + Data APIs). Two equivalent onboarding commands, both hardened with PKCE (RFC 7636, S256) and both requesting the scope from config.json (oauth.scope) so they never drift:

bash
export GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.comexport GOOGLE_CLIENT_SECRET=your-client-secret
# Option A: write an authorized_user credential file directlynode dist/index.js auth --output ./gtm-ga4-credentials.json# then set GOOGLE_APPLICATION_CREDENTIALS=./gtm-ga4-credentials.json
# Option B: the standalone helper (prints GOOGLE_REFRESH_TOKEN + a ready-to-save# authorized_user JSON you can write to a file for GOOGLE_APPLICATION_CREDENTIALS)node get-refresh-token.cjs

The refresh token / credential is written by you and read from your environment only. Nothing is shared and no OAuth client keyfile is bundled. Do not run the helper with stdout redirected to a shared log — the refresh token is printed to stdout by design.

OAuth scopes requested

The onboarding paths request exactly the scopes this MCP's tools use (from config.example.json → oauth.scope):

ScopeNeeded by
tagmanager.edit.containersgtm_create_tag, gtm_update_tag, gtm_create_variable, gtm_update_variable, gtm_delete_variable, tag/trigger/variable reads
tagmanager.edit.containerversionsgtm_create_version
tagmanager.publishgtm_create_version, gtm_preview (quick preview)
analytics.readonlygtm_ga4_run_report, gtm_ga4_realtime_report, gtm_ga4_list_custom_dimensions
analytics.editgtm_ga4_create_custom_dimension

tagmanager.readonly is intentionally not requested — the edit scopes already grant read access.

Usage

Claude Code (.mcp.json)

json
{  "mcpServers": {    "gtm-ga4": {      "command": "node",      "args": ["/path/to/mcp-gtm-ga4/dist/index.js"],      "env": {        "GOOGLE_APPLICATION_CREDENTIALS": "/path/to/service-account.json",        "GTM_ACCOUNT_ID": "1234567890",        "GTM_CONTAINER_ID": "9876543",        "GA4_PROPERTY_ID": "331956119"      }    }  }}

npx

bash
GTM_ACCOUNT_ID=1234567890 \GTM_CONTAINER_ID=9876543 \GA4_PROPERTY_ID=331956119 \GOOGLE_APPLICATION_CREDENTIALS=/path/to/sa.json \npx mcp-gtm-ga4

Claude Desktop: Add to ~/Library/Application Support/Claude/claude_desktop_config.json (macOS) or %APPDATA%\Claude\claude_desktop_config.json (Windows).

Safety

Workspace sandbox protection

All write operations (gtm_update_tag, gtm_create_tag, gtm_preview, gtm_create_version) verify they target the resolved workspace before executing. If a request attempts to write to a different workspace, the server returns a SafetyError and blocks the operation.

This prevents accidental production changes when the MCP server is configured against a sandbox workspace.

Tools

GTM Tags

ToolDescription
gtm_list_tagsList all tags in the workspace with consent status and firing triggers
gtm_get_tagGet full tag configuration by tag ID
gtm_update_tagUpdate an existing tag (merge patch via JSON)
gtm_create_tagCreate a new tag from a JSON definition

GTM Structure

ToolDescription
gtm_list_triggersList all triggers (ID, name, type)
gtm_list_variablesList all variables (ID, name, type)

GTM Consent

ToolDescription
gtm_audit_consentAudit all tags for consent configuration compliance

GTM Workspace

ToolDescription
gtm_previewGenerate a quick preview of the current workspace
gtm_create_versionCreate a new container version from the workspace

GA4 Reports

ToolDescription
gtm_ga4_run_reportRun a GA4 report with dimensions, metrics, date range, and filters
gtm_ga4_realtime_reportRun a GA4 realtime report (last 30 minutes)

GA4 Admin

ToolDescription
gtm_ga4_list_custom_dimensionsList all custom dimensions for the property
gtm_ga4_create_custom_dimensionCreate a new custom dimension

Architecture

  • GTM API: googleapis (Tag Manager v2)
  • GA4 Data: @google-analytics/data (BetaAnalyticsDataClient)
  • GA4 Admin: @google-analytics/admin (AnalyticsAdminServiceClient)
  • Resilience: cockatiel (retry, circuit breaker, timeout policies)
  • Logging: pino with pino-pretty
  • Transport: MCP SDK stdio transport

License

MIT

Author

Built by Mark Harnett / drak-marketing

来源:README.md,提交 539cc67

工具

0
SourceWeft 尚未在沙箱中启动过该软件包,因此其工具未知。

版本历史

1
  1. v1.0.1最新Sep 16, 2026

评分

已满足适用检查项的 50%

该评分衡量此收录条目的完整程度,依据注册中心条目、端点与公开来源计算得出。它不是对服务器本身的评价,也不表示服务器安全或当前可用:可连通性是带日期的检查结果,评分也不包含安装量或用户评价。

  • 近 30 天内可连通25 分

    请保持端点能够响应 MCP 握手;SourceWeft 每 30 天探测一次。

  • 已声明认证方式10 分

    请在注册中心条目中声明凭据请求头,或保持端点能够响应探测,以便判定其认证方式。

  • 已列出工具15 分

    请让服务器在无凭据时响应 tools/list,或发布可在沙箱中无凭据启动的软件包。

  • 描述必需15 分
  • 图标10 分
  • 文档15 分
  • 许可证10 分