This MCP server enables AI assistants to programmatically manage Google Tag Manager through OAuth2-authenticated API calls.
Authentication: Handle OAuth2 flow with tools to get authorization URLs, authenticate with codes, check status, reset credentials, and automatic token refresh stored in ~/.gtm-mcp-token.json.
Account Management: List, retrieve details, and update GTM accounts.
Container Management: Full CRUD operations for containers across web, Android, iOS, and AMP platforms.
Workspace Management: Complete CRUD capabilities plus synchronization and quick preview of changes.
Tag Management: Create, list, retrieve, update, and delete tags supporting GA4 Configuration (googtag), GA4 Events (gaawe), Google Ads Conversion Tracking (awct), Custom HTML (html), Custom Image (img), Facebook Pixel (fbq), and Universal Analytics (ua).
Trigger Management: Full CRUD operations for multiple trigger types including pageview, custom events, link clicks, element clicks, form submissions, scroll depth, element visibility, YouTube video events, and timers—with comprehensive filter support (standard filters, auto-event filters, wait for tags settings, and type-specific options).
Variable Management: Create, list, retrieve, update, and delete variables supporting constants (c), data layer (v), JavaScript (j), DOM element (d), cookie (k), URL (u), auto-event (ae), and built-in variables (b).
Version Management: Create versions from workspace changes, list all versions, retrieve version details, and publish versions to production.
Integration: Communicates via stdio using the MCP protocol, runs on macOS, and integrates with MCP clients like Cursor using environment variables or .env files.
Enables management of Google Tag Manager resources including accounts, containers, workspaces, tags, triggers, variables, and versions. Supports creating, updating, deleting, and listing GTM entities, as well as version creation for publishing.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Google Tag Manager MCP Servercreate a GA4 event tag for purchase tracking in container 12345"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
Google Tag Manager MCP Server
Google Tag Managerを操作するためのMCP(Model Context Protocol)サーバーです。Macローカル環境で動作します。
機能
このMCPサーバーは以下のGoogle Tag Manager操作を提供します:
アカウント管理: アカウント一覧の取得、詳細取得、更新
ユーザー権限管理: ユーザー権限の一覧取得、詳細取得、作成、更新、削除(tagmanager.manage.usersスコープ必要、要再認証)
コンテナ管理: コンテナの一覧取得、詳細取得、作成、更新、削除
ワークスペース管理: ワークスペースの一覧取得、詳細取得、作成、更新、削除、同期、クイックプレビュー
タグ管理: タグの一覧取得、詳細取得、作成、更新、削除
トリガー管理: トリガーの一覧取得、詳細取得、作成、更新、削除
変数管理: 変数の一覧取得、詳細取得、作成、更新、削除
バージョン管理: バージョンの一覧取得、詳細取得、作成(公開準備)、公開
セットアップ
1. 依存関係のインストール
npm install2. Google Cloud OAuth2認証情報の設定
Google Tag Manager APIを使用するには、OAuth2認証情報が必要です。
Google Cloud Consoleでプロジェクトを作成
APIとサービス > 認証情報 に移動
認証情報を作成 > OAuth クライアント ID を選択
アプリケーションの種類で デスクトップアプリ を選択
名前を入力して作成
クライアントID と クライアントシークレット をコピー
APIとサービス > ライブラリ で Tag Manager API を有効化
注意: アカウント管理機能(update_account)を使用するには、OAuth2認証情報の設定で以下のスコープを追加する必要があります:
https://www.googleapis.com/auth/tagmanager.manage.accounts
このスコープは、MCPサーバーの実装に既に含まれていますが、Google Cloud ConsoleでOAuth2認証情報を作成する際に、これらのスコープが有効になっていることを確認してください。
3. 環境変数の設定
.envファイルを作成し、OAuth2認証情報を設定します:
GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=your-client-secret
GOOGLE_REDIRECT_URI=http://localhost:3000/oauth2callback注意: GOOGLE_REDIRECT_URIは、Google Cloud ConsoleのOAuth2認証情報設定で「承認済みのリダイレクト URI」に追加する必要があります。
4. 初回認証・再認証
おすすめ(手間なし): プロジェクトのルートで次を実行し、開いたブラウザでGoogleにログインして権限を承認するだけです。コールバック用の一時サーバーが code を受け取り、トークン保存まで自動で行います。
node auth.js
# または
npm run auth認証情報は ~/.gtm-mcp-token.json に保存され、次回以降は自動的に使用されます。
ユーザー権限管理を使う場合: ユーザー権限の作成・更新には tagmanager.manage.users スコープが必要です。既存のトークンに含まれていない場合は、上記の node auth.js(または npm run auth)を実行して再認証すれば、新しいスコープ付きのトークンが取得できます。事前に reset_auth でトークンを消してから実行してもかまいません。
手動で行う場合: MCPの get_auth_url でURLを取得 → ブラウザで認証 → リダイレクト先の code= の値をコピー → authenticate に渡す、または node test-auth.js "認証コード" を実行。
使用方法
MCPクライアントとして設定
CursorなどのMCPクライアントで、このサーバーを設定します。
設定例(~/.cursor/mcp.json または適切な設定ファイル):
{
"mcpServers": {
"gtm": {
"command": "node",
"args": ["/Users/01035280/Documents/タグマネTテスト/GTM_MCP/src/index.js"],
"env": {
"GOOGLE_CLIENT_ID": "your-client-id.apps.googleusercontent.com",
"GOOGLE_CLIENT_SECRET": "your-client-secret",
"GOOGLE_REDIRECT_URI": "http://localhost:3000/oauth2callback"
}
}
}
}または、.envファイルを使用する場合は、環境変数の設定を省略できます。
MCPサーバーの再起動
MCPサーバーを再起動する方法は以下の通りです:
Cursorを再起動する(最も確実な方法)
Cursorを完全に終了(
Cmd + Q)してから再起動
MCP設定ファイルを編集して保存する
~/.cursor/mcp.jsonを開き、コメントやスペースを追加・削除して保存Cursorが設定変更を検知してMCPサーバーを再起動することがあります
コマンドパレットから再起動する(Cursorのバージョンによる)
Cmd + Shift + P(Mac)またはCtrl + Shift + P(Windows/Linux)でコマンドパレットを開く「MCP」や「reload」などのキーワードで検索し、MCPサーバー再起動コマンドを実行
注意: 現在のCursorのバージョンでは、MCP設定ファイルの編集による再起動が最も簡単な方法です。
利用可能なツール
認証操作
get_auth_url: OAuth2認証URLを取得authenticate: 認証コードを使用して認証を完了check_auth_status: 現在の認証状態を確認reset_auth: 保存された認証情報をリセット
アカウント操作
list_accounts: アカウント一覧を取得get_account: アカウントの詳細を取得update_account: アカウント情報を更新(名前など)
ユーザー権限操作(アカウント管理)
list_user_permissions: アカウントのユーザー権限一覧を取得get_user_permission: 指定ユーザー権限の詳細を取得create_user_permission: アカウント・コンテナへのユーザーアクセスを作成(メール、アカウント権限: noAccess/user/admin、コンテナ権限: noAccess/read/edit/approve/publish)update_user_permission: ユーザー権限を更新delete_user_permission: ユーザー権限を削除(アクセス取り消し)
※ ユーザー権限の作成・更新には tagmanager.manage.users スコープが必要です。既存トークンでは再認証してください。
コンテナ操作
list_containers: コンテナ一覧を取得get_container: コンテナの詳細を取得create_container: 新しいコンテナを作成update_container: コンテナ情報を更新delete_container: コンテナを削除
ワークスペース操作
list_workspaces: ワークスペース一覧を取得get_workspace: ワークスペースの詳細を取得create_workspace: 新しいワークスペースを作成update_workspace: ワークスペース情報を更新delete_workspace: ワークスペースを削除sync_workspace: ワークスペースを同期quick_preview: ワークスペースのクイックプレビューを取得
タグ操作
list_tags: タグ一覧を取得get_tag: タグの詳細を取得create_tag: 新しいタグを作成update_tag: 既存のタグを更新(すべてのパラメータを更新可能)delete_tag: タグを削除サポートする主要なタグタイプ:
googtag: GA4設定タグgaawe: GA4イベントタグawct: Google広告コンバージョントラッキングhtml: カスタムHTMLタグimg: カスタム画像タグ(ピクセルトラッキング)fbq: Facebookピクセルua: Universal Analytics(旧GA)
トリガー操作
list_triggers: トリガー一覧を取得get_trigger: トリガーの詳細を取得create_trigger: 新しいトリガーを作成update_trigger: 既存のトリガーを更新(filter、autoEventFilter、waitForTagsなどのすべての設定を更新可能)delete_trigger: トリガーを削除サポートするトリガータイプ:
pageview: ページビュートリガーcustomEvent: カスタムイベントトリガー(customEventFilterを使用)linkClick: リンククリックトリガー(filter、autoEventFilter、waitForTagsなどを使用)click: クリックトリガー(filterを使用)formSubmission: フォーム送信トリガー(formId、formClassesを使用)scrollDepth: スクロール深度トリガー(verticalThreshold、horizontalThresholdを使用)visible/elementVisibility: 要素の表示トリガー(selector、visiblePercentageThresholdを使用)youtubeVideo: YouTube動画トリガー(videoId、各種enableTriggerOnVideo*を使用)timer: タイマートリガー(interval、limit、startTimerOnを使用)
変数操作
list_variables: 変数一覧を取得get_variable: 変数の詳細を取得create_variable: 新しい変数を作成update_variable: 既存の変数を更新delete_variable: 変数を削除サポートする変数タイプ:
c: 定数変数v: データレイヤー変数j: JavaScript変数d: DOM要素変数k: 1st Party Cookie変数u: URL変数ae: 自動イベント変数b: 組み込み変数
バージョン操作
create_version: ワークスペースの変更をバージョンとして作成(公開準備)list_versions: コンテナのバージョン一覧を取得get_version: バージョンの詳細を取得publish_version: バージョンを公開
使用例
主要なタグタイプの作成
GA4設定タグ
{
"name": "GA4設定",
"type": "googtag",
"parameter": [
{
"type": "template",
"key": "tagId",
"value": "G-XXXXXXXXXX"
}
],
"firingTriggerId": ["2147479573"]
}GA4イベントタグ
{
"name": "GA4 イベント",
"type": "gaawe",
"parameter": [
{
"type": "template",
"key": "eventName",
"value": "custom_event"
},
{
"type": "boolean",
"key": "sendEcommerceData",
"value": "false"
},
{
"type": "template",
"key": "measurementIdOverride",
"value": "G-XXXXXXXXXX"
}
],
"firingTriggerId": ["トリガーID"]
}Google広告コンバージョントラッキング
{
"name": "Google広告コンバージョン",
"type": "awct",
"parameter": [
{
"type": "template",
"key": "conversionId",
"value": "1006772047"
},
{
"type": "template",
"key": "conversionLabel",
"value": "0L_dCLyI84sBEM--iOAD"
},
{
"type": "boolean",
"key": "enableConversionLinker",
"value": "true"
}
],
"firingTriggerId": ["トリガーID"]
}主要なトリガータイプの作成
linkClickトリガーの作成
{
"name": "リンククリック - test_click9",
"type": "linkClick",
"filter": [
{
"type": "contains",
"parameter": [
{
"type": "template",
"key": "arg0",
"value": "{{Click URL}}"
},
{
"type": "template",
"key": "arg1",
"value": "test_click9"
}
]
}
],
"autoEventFilter": [
{
"type": "contains",
"parameter": [
{
"type": "template",
"key": "arg0",
"value": "{{Page URL}}"
},
{
"type": "template",
"key": "arg1",
"value": "20251202cvtest/test1.html"
}
]
}
],
"waitForTags": true,
"checkValidation": false,
"waitForTagsTimeout": 2000
}linkClickトリガーの更新(filterとautoEventFilterを設定)
既存のトリガーにfilterとautoEventFilterを追加・更新する例:
{
"triggerId": "27",
"filter": [
{
"type": "contains",
"parameter": [
{
"type": "template",
"key": "arg0",
"value": "{{Click URL}}"
},
{
"type": "template",
"key": "arg1",
"value": "test_click9"
}
]
}
],
"autoEventFilter": [
{
"type": "contains",
"parameter": [
{
"type": "template",
"key": "arg0",
"value": "{{Page URL}}"
},
{
"type": "template",
"key": "arg1",
"value": "20251202cvtest/test1.html"
}
]
}
],
"waitForTags": true,
"waitForTagsTimeout": 2000
}formSubmissionトリガーの作成
{
"name": "フォーム送信",
"type": "formSubmission",
"formId": "contact-form",
"formClasses": "form-class"
}scrollDepthトリガーの作成
{
"name": "スクロール深度 50%",
"type": "scrollDepth",
"verticalThreshold": 50,
"horizontalThreshold": 75
}elementVisibilityトリガーの作成
{
"name": "要素の表示",
"type": "visible",
"selector": "#important-element",
"visiblePercentageThreshold": 50,
"continuousTimeMinMilliseconds": 1000
}youtubeVideoトリガーの作成
{
"name": "YouTube動画",
"type": "youtubeVideo",
"videoId": "dQw4w9WgXcQ",
"enableTriggerOnVideoStart": true,
"enableTriggerOnVideoComplete": true
}主要な変数タイプの作成
データレイヤー変数
{
"name": "データレイヤー変数",
"type": "v",
"parameter": [
{
"type": "template",
"key": "dataLayerVersion",
"value": "2"
},
{
"type": "template",
"key": "dataLayerVariable",
"value": "event"
}
]
}JavaScript変数
{
"name": "JavaScript変数",
"type": "j",
"parameter": [
{
"type": "template",
"key": "javascript",
"value": "function() {\n return document.title;\n}"
}
]
}DOM要素変数
{
"name": "DOM要素変数",
"type": "d",
"parameter": [
{
"type": "template",
"key": "selector",
"value": "#element-id"
},
{
"type": "template",
"key": "attributeName",
"value": "data-value"
}
]
}Cookie変数
{
"name": "Cookie変数",
"type": "k",
"parameter": [
{
"type": "template",
"key": "cookieName",
"value": "session_id"
}
]
}開発
開発モードで実行
npm run dev通常モードで実行
npm startテストの実行
すべてのテストを実行(リグレッションテスト):
npm test個別のテストを実行:
npm run test:auth # 認証テスト
npm run test:accounts # アカウント一覧取得テスト
npm run test:gtm # 基本GTMテスト
npm run test:details # 詳細情報取得テスト
npm run test:trigger # linkClickトリガー作成テスト
npm run test:filter # filter/autoEventFilterテスト
npm run test:detailed-filters # 詳細フィルタ設定テストテストファイルを直接実行することも可能です:
node run-all-tests.js # すべてのテストを実行
node test-auth.js # 認証テスト
node test-accounts.js # アカウント一覧取得テスト
# ... など注意事項
このサーバーは標準入出力(stdio)経由で通信します
Google Tag Manager APIのレート制限に注意してください
OAuth2認証情報(クライアントID、クライアントシークレット)は安全に管理してください
認証トークンは
~/.gtm-mcp-token.jsonに保存されます(.gitignoreに含まれています)リフレッシュトークンは自動的に使用され、アクセストークンが期限切れになる前に更新されます
ライセンス
MIT