GO

Google Analytics 4 MCP Server — Reports & Events

Access GA4 reports and send events using an MCP server with the Google Analytics Data API and Measurement Protocol.

Quick Install
npx -y @gomakers-ai/mcp-google-analytics

Overview

This MCP (Model Context Protocol) server provides a simple bridge between AI agents or other server-side tools and Google Analytics 4 (GA4). It exposes endpoints that let you query GA4 reports through the Google Analytics Data API and send server-side events through GA4’s Measurement Protocol. By packaging these actions as an MCP-compatible service, models and apps can request analytics data or emit events without embedding Google SDKs or secrets directly in multiple places.

The server is useful when you want programmatic access to GA4 from an AI toolchain or microservice architecture: run queries (user metrics, funnels, time series) and record events (purchases, signups, conversions) in a consistent, auditable way. It centralizes authentication (service account + API secret) and request validation, and returns GA4-formatted responses suitable for downstream processing.

Features

  • Query GA4 reports using the Google Analytics Data API (dimension/metric requests, date ranges, filters).
  • Send server-side events to GA4 via the Measurement Protocol (supports event parameters and user/pseudo-id).
  • Single configuration point for GA credentials (service account JSON and Measurement Protocol API secret).
  • Simple HTTP endpoints designed for MCP-style integrations or direct server-to-server calls.
  • Example request/response payloads and error handling to simplify integration with agents and services.

Installation / Configuration

Prerequisites:

  • A Google Cloud project with a Google Analytics 4 property.
  • The Google Analytics Data API enabled.
  • A service account with the analytics.data permission (or appropriate role).
  • A Measurement Protocol API secret created in the GA4 property.

Quick start (clone, configure, run):

# clone the repo
git clone https://github.com/gomakers-ai/mcp-google-analytics.git
cd mcp-google-analytics

# create .env (example below), then run with Docker or locally

Example .env file:

# path to the Google service account JSON file
GOOGLE_APPLICATION_CREDENTIALS=/app/creds/service-account.json

# GA4 property id (numeric)
GA4_PROPERTY_ID=123456789

# measurement protocol settings
MEASUREMENT_ID=G-XXXXXXX
API_SECRET=your-measurement-protocol-secret

# server port
PORT=8080

Run locally (example):

# if project uses Node.js
npm install
npm start

# or using Docker
docker build -t mcp-ga4 .
docker run -p 8080:8080 \
  -e GOOGLE_APPLICATION_CREDENTIALS="/app/creds/service-account.json" \
  -e GA4_PROPERTY_ID=123456789 \
  -e MEASUREMENT_ID=G-XXXXXXX \
  -e API_SECRET=your-secret \
  mcp-ga4

Environment variables summary:

VariableDescription
GOOGLE_APPLICATION_CREDENTIALSPath to service account JSON used for Data API
GA4_PROPERTY_IDNumeric GA4 property id (used by Data API)
MEASUREMENT_IDGA4 measurement id (for Measurement Protocol)
API_SECRETMeasurement Protocol API secret created in GA UI
PORTListening port for the MCP server

Available Tools / Resources

  • GitHub repository: https://github.com/gomakers-ai/mcp-google-analytics
  • Google Analytics Data API docs: https://developers.google.com/analytics/devguides/reporting/data/v1
  • Measurement Protocol (GA4) docs: https://developers.google.com/analytics/devguides/collection/protocol/ga4
  • Google Cloud service accounts: https://cloud.google.com/iam/docs/service-accounts

API examples

Below are representative request payloads you can send to the MCP server. Endpoint paths may vary slightly depending on deployment; adapt to your server base URL.

  1. Fetch a report (Data API style)

Request:

POST /v1/data/report
Content-Type: application/json

{
  "property": "properties/123456789",
  "dateRanges": [{"startDate": "2023-03-01", "endDate": "2023-03-31"}],
  "dimensions": [{"name": "city"}, {"name": "country"}],
  "metrics": [{"name": "activeUsers"}, {"name": "newUsers"}],
  "limit": 100
}

Response (abbreviated):

{
  "rows": [
    { "dimensionValues": [...], "metricValues": [...] },
    ...
  ],
  "rowCount": 12
}
  1. Send an event (Measurement Protocol)

Request:

POST /v1/measurement/event
Content-Type: application/json

{
  "client_id": "555.12345",
  "events": [
    {
      "name": "purchase",
      "params": {
        "currency": "USD",
        "value": 99.99,
        "transaction_id": "T-12345"
      }
    }
  ]
}

Response:

{
  "status": "ok",
  "measurement_response": { ... } // raw GA4 Measurement Protocol response
}

Notes:

  • Use the provided service account and GA4 property id for Data API calls.
  • For Measurement Protocol, include either client_id or user_id; the server signs requests with API_SECRET.

Use Cases

  • Periodic reports for an AI agent: An agent can request a daily active-users time series and use the result to generate insights or summaries.

    • Example: Ask the MCP server for activeUsers by date for the last 7 days, then feed the series into a summarization model.
  • Server-side event tracking for sensitive flows: Emit purchase or subscription events from backend services without exposing measurement secrets in client code.

    • Example: On successful checkout,