GA4 Automation MCP Server

GA4 Automation MCP Server

This project is a Google Analytics 4 (GA4) automation server implemented as a Model Context Protocol (MCP) tool. It lets AI agents query GA4 data (reports, traffic sources, demographics, etc.) and also includes a simple connection test script.

Category
访问服务器

README

GA4 Automation MCP Server

This project is a Google Analytics 4 (GA4) automation server implemented as a Model Context Protocol (MCP) tool. It lets AI agents query GA4 data (reports, traffic sources, demographics, etc.) and also includes a simple connection test script.


1. Prerequisites

  • Node.js (recommended: a recent LTS, e.g. via nvm)
  • npm (comes with Node)
  • A GA4 property you have access to
  • A Google Cloud service account with:
    • The Analytics Data API enabled
    • Access to your GA4 property (Viewer or higher)

2. Clone the project and install dependencies

cd /<path-to-your-projects>/ga4-automation
git clone https://github.com/mattwilkerson1121/ga-ai-mcp-server/ # or copy the folder another way
cd ga4-automation

npm install

(If the repo is already on disk, just cd /<path-to-your-project>/ga4-automation and run npm install.)


3. Create your GA4 service account key

  1. Go to Google Cloud Console → IAM & Admin → Service Accounts.
  2. Create or select a service account.
  3. Under Keys, create a new JSON key and download it.
  4. Save the JSON key file into the project root as:
/<path-to-project-folder>/ga4-automation/credentials.json
  1. In GA4, grant this service account email access to your property:
    • Admin → Property Access Management → Add user → paste the service account email → give at least Viewer.

4. Configure environment and test script

The test script (test-connection.js) uses environment variables (and .env) to locate credentials and GA4 properties.

Create a .env file in the project root:

cd /<path-to-project-folder>/ga4-automation
cat > .env << 'EOF'
GOOGLE_APPLICATION_CREDENTIALS=/<path-to-project-folder>/ga4-automation/credentials.json
GA_PROPERTY_ID=<your-ga4-property-id>   # for multiple properties use comma‑separated list of your GA4 property IDs
EOF

Key variables:

  • GOOGLE_APPLICATION_CREDENTIALS: absolute path to your service account JSON.
  • GA_PROPERTY_ID: one or more GA4 property IDs, separated by commas.

Run the GA4 connection test

From the project root:

cd /<path-to-project-folder>/ga4-automation
npm run test:connection

Expected behavior:

  • For each property ID you configured, it will:
    • Run a small GA4 report
    • Print metrics like activeUsers, sessions, screenPageViews, newUsers
    • Show any permission or configuration errors with helpful hints

If you see a message like:

  • Credentials file not found → check GOOGLE_APPLICATION_CREDENTIALS and file path.
  • PERMISSION_DENIED → ensure the service account has access to the GA4 property.
  • NOT_FOUND → the property ID is probably incorrect.

5. MCP server overview (src/index.js)

The main GA4 automation server is implemented in src/index.js as an MCP server over stdio (not an HTTP server).

  • It:
    • Reads the credentials file at credentials.json in the project root.
    • Creates a GoogleAuth and BetaAnalyticsDataClient.
    • Exposes several tools such as:
      • query_analytics
      • get_realtime_data
      • get_traffic_sources
      • get_user_demographics
      • get_page_performance
      • get_conversion_data
      • get_custom_report
    • Connects to an MCP client via stdin/stdout (StdioServerTransport).

You don’t hit this server via a browser or curl; instead, an MCP‑aware client (like Claude Desktop) launches and talks to it.


6. Running the MCP server manually (for sanity checks)

From the project root:

cd /<path-to-project-folder>/ga4-automation

# Quick syntax check (no execution)
node --check src/index.js

# Start the server (will wait for MCP messages on stdin)
npm start

The npm start script is defined in package.json as:

NODE_OPTIONS='--no-deprecation' node src/index.js

If you run npm start in a normal terminal, it will just wait because no MCP client is connected to its stdin/stdout. That’s expected.


7. Using the server from Claude (MCP)

This project already includes an example Claude MCP configuration. You will need to update the contents with the appropriate paths and then copy the json and add it to your Claude Desktop Configuration file (you can find the path to the file in the Claude AI Desktop App by going to settings > developer > and clicking the edit config button):

claude-config.json:

{
  "mcpServers": {
    "ga4-analytics": {
      "command": "/Users/<your-user-name>/.nvm/versions/node/<your-node-version>/bin/node",
      "args": [
        "/<path-to-project-folder>/ga4-automation/src/index.js"
      ],
      "env": {
        "GOOGLE_APPLICATION_CREDENTIALS": "/<path-to-project-folder>/ga4-automation/credentials.json",
        "GA4_PROPERTY_ID": "<your-ga4-property-id>",
        "NODE_OPTIONS": "--no-deprecation"
      }
    }
  }
}

Steps to use with Claude Desktop

  1. Copy claude-config.json into (or merge it with) Claude’s MCP config file location (varies by OS; see Claude documentation).
  2. Adjust all absolute paths to match your environment:
    • command (your Node binary path)
    • args[0] (path to src/index.js)
    • GOOGLE_APPLICATION_CREDENTIALS
    • GA4_PROPERTY_ID value(s)
  3. Restart Claude Desktop so it picks up the new MCP server.
  4. In Claude, ask something like:
    • “Use the ga4-analytics tool to get page performance data for the last 7 days.”
    • "Query Analytics and show me the number of new users for 01/01/2025 to 01/31/2025"
    • "Create a table with columns containing the metric name in the header and show me the number of sessions, new users, and the percentage of new users vs total users for the last 30 days in a row corresponding to the correct columns."

Claude will:

  • Spawn the Node MCP server using the command/args from claude-desktop-config.json.
  • Call the tools you defined (get_page_performance, etc.) to run GA4 reports.

8. Example tool payloads (conceptual)

These examples show the shape of the tool inputs the MCP server expects. The actual wiring is handled by the MCP client (Claude); you normally do not send this JSON manually, but this should give you an idea on how to structure your prompts in the Claude UI.

Example: query_analytics

{
  "propertyId": "<your-ga4-property-id>",
  "startDate": "2025-01-01",
  "endDate": "2025-01-31",
  "dimensions": ["country", "city"],
  "metrics": ["sessions", "activeUsers"]
}

Example: get_page_performance

{
  "propertyId": "<your-ga4-property-id>",
  "startDate": "2025-01-01",
  "endDate": "2025-01-31",
  "limit": 50
}

The responses are normalized into a JSON structure containing:

  • rows: an array of objects (dimension/metric name → value)
  • rowCount: number of rows
  • totals: any totals provided by GA4

9. Troubleshooting

  • node: command not found

    • Install Node (e.g. brew install node or nvm install --lts) and open a new terminal.
  • ENOENT: no such file or directory, open 'credentials.json'

    • Ensure a valid JSON key exists at:
      • /<path-to-project-folder>/ga4-automation/credentials.json, or
      • Update src/index.js to point at credentials.json.
  • PERMISSION_DENIED when running npm run test:connection

    • Confirm the service account has access to the GA4 property in GA4 Admin.
  • NOT_FOUND: Property ID

    • Check for typos in GA_PROPERTY_ID / GA4_PROPERTY_ID.

If you hit an error that isn’t covered here, capture the full stack trace and logs from npm run test:connection or the MCP client and adjust credentials, env vars, or GA4 access as needed.

If you continually run into errors you may need to clear the cache for the Claude Desktop App.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选