loading…
Search for a command to run...
loading…
Enables interaction with Google Analytics 4 to manage accounts, run standard or realtime reports, and validate event parameters. It supports dual authentication
Enables interaction with Google Analytics 4 to manage accounts, run standard or realtime reports, and validate event parameters. It supports dual authentication methods and integrates with GTM for end-to-end data collection orchestration.
A Model Context Protocol (MCP) server for Google Analytics 4 (GA4) that provides tools for querying GA4 data through the Admin API and Data API.
npm install
npm run build
# Set your OAuth credentials
export GA4_CLIENT_ID="your-client-id.apps.googleusercontent.com"
export GA4_CLIENT_SECRET="your-client-secret"
# Run setup to authenticate with Google
npm run setup
This will:
~/.ga4-mcp/tokens.jsonOnce authenticated, you can use the MCP server with Claude Desktop or other clients.
The server supports two authentication methods, checked in this order:
Set these environment variables:
GA4_ACCESS_TOKEN - OAuth access tokenGA4_REFRESH_TOKEN - OAuth refresh tokenGA4_CLIENT_ID - OAuth client IDGA4_CLIENT_SECRET - OAuth client secretOr create ~/.ga4-mcp/tokens.json:
{
"access_token": "your-access-token",
"refresh_token": "your-refresh-token",
"client_id": "your-client-id",
"client_secret": "your-client-secret"
}
Options (in priority order):
GA4_SERVICE_ACCOUNT_JSON environment variable with JSON stringGOOGLE_APPLICATION_CREDENTIALS to the JSON file path~/.ga4-mcp/credentials.json./Credential/ folderAdd to your Claude Desktop configuration (~/.config/claude-desktop/config.json):
{
"mcpServers": {
"ga4": {
"command": "node",
"args": ["/path/to/ga4-mcp-server/dist/index.js"],
"env": {
"GA4_ACCESS_TOKEN": "your-access-token",
"GA4_REFRESH_TOKEN": "your-refresh-token",
"GA4_CLIENT_ID": "your-client-id",
"GA4_CLIENT_SECRET": "your-client-secret"
}
}
}
}
# With OAuth environment variables
GA4_ACCESS_TOKEN="..." GA4_REFRESH_TOKEN="..." GA4_CLIENT_ID="..." GA4_CLIENT_SECRET="..." node dist/index.js
# With Service Account
GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json" node dist/index.js
| Tool | Description |
|---|---|
ga4_account_summaries |
List all GA4 accounts and properties the user has access to |
ga4_property_details |
Get detailed information about a specific property |
ga4_google_ads_links |
List Google Ads accounts linked to a property |
ga4_property_annotations |
List annotations for a property (limited support) |
| Tool | Description |
|---|---|
ga4_run_report |
Run a standard GA4 report with dimensions, metrics, and date ranges |
ga4_run_realtime_report |
Run a realtime report for the last 30 minutes |
ga4_custom_dimensions_metrics |
Get custom dimensions and metrics defined for a property |
| Tool | Description |
|---|---|
ga4_validate_gtm_params |
Validate GTM event parameters against GA4 custom dimensions and data collection |
{
"tool": "ga4_account_summaries"
}
{
"tool": "ga4_run_report",
"arguments": {
"propertyId": "123456789",
"dateRanges": [
{"startDate": "30daysAgo", "endDate": "yesterday"}
],
"dimensions": ["country", "deviceCategory"],
"metrics": ["activeUsers", "sessions"]
}
}
{
"tool": "ga4_run_realtime_report",
"arguments": {
"propertyId": "123456789",
"dimensions": ["country"],
"metrics": ["activeUsers"]
}
}
{
"tool": "ga4_validate_gtm_params",
"arguments": {
"propertyId": "123456789",
"gtmEvents": [
{
"eventName": "purchase",
"parameters": ["transaction_id", "value", "currency"]
}
],
"startDate": "7daysAgo",
"endDate": "yesterday"
}
}
Or with GTM Export JSON:
{
"tool": "ga4_validate_gtm_params",
"arguments": {
"propertyId": "123456789",
"gtmExportJson": { "...GTM container export data..." }
}
}
This server works with the GTM MCP Server to provide end-to-end validation of GTM event parameters in GA4.
┌─────────────────────────────────────────────────────────────────────┐
│ Claude Agent │
│ (Orchestrator Role) │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ ① GTM MCP Server ② GA4 MCP Server │
│ (gtmAgent/mcp-server) (ga4-mcp-server) │
│ │
│ gtm_export_full() ga4_validate_gtm_params() │
│ gtm_tag().list() ga4_custom_dimensions_metrics() │
│ │ ga4_run_report() │
│ │ ▲ │
│ └───────────────────────────────────┘ │
│ Agent relays data │
│ │
└─────────────────────────────────────────────────────────────────────┘
gtm_export_full() → Get GA4 Event tags and parametersga4_validate_gtm_params() → Check registration and collection{
"mcpServers": {
"gtm": {
"command": "node",
"args": ["/path/to/gtmAgent/mcp-server/dist/index.js"],
"env": {}
},
"ga4": {
"command": "node",
"args": ["/path/to/ga4-mcp-server/dist/index.js"],
"env": {
"GA4_ACCESS_TOKEN": "your-access-token",
"GA4_REFRESH_TOKEN": "your-refresh-token",
"GA4_CLIENT_ID": "your-client-id",
"GA4_CLIENT_SECRET": "your-client-secret"
}
}
}
}
| Scenario | Traditional | Optimized | Reduction |
|---|---|---|---|
| 5 events × 21 params | 105 calls | 22 calls | 79% |
| 14 events × 32 params | 448 calls | 33 calls | 93% |
The validation tool queries metadata once, then makes one API call per unique parameter instead of per event-parameter combination.
The server uses the following scope:
https://www.googleapis.com/auth/analytics.readonlyMIT
Добавь это в claude_desktop_config.json и перезапусти Claude Desktop.
{
"mcpServers": {
"ga4-mcp-server": {
"command": "npx",
"args": []
}
}
}