Google Analytics MCP Server: Query GA4 Data With Gemini CLI

7月23日 Published inMCP Services

The Google Analytics MCP Server bridges your GA4 data and a large language model. It runs locally as a Model Context Protocol server. Gemini CLI and Code Assist can then talk to it. The server wraps the Google Analytics Admin API and Data API. Your LLM gains a set of tools: fetch account summaries, pull property details, run core reports, and stream real-time metrics. Ask questions in plain English. Get answers without clicking through the GA4 interface.

The server exposes several tools for LLM consumption.

Account and Property Lookup

  • get_account_summaries — Lists all GA accounts and properties you can access.
  • get_property_details — Returns full configuration details for a specific property.
  • list_google_ads_links — Shows Google Ads accounts linked to a property.

Core Reporting

  • run_report — Executes a GA4 report via the Data API.
  • get_dimensions — Returns available dimensions for a property, custom ones included.
  • get_metrics — Returns available metrics for a property, custom ones included.
  • get_standard_dimensions — Lists all standard dimensions.
  • get_standard_metrics — Lists all standard metrics.
  • run_report_date_ranges_hints — Suggests valid date range formats.
  • run_report_metric_filter_hints — Suggests valid metric filter syntax.
  • run_report_dimension_filter_hints — Suggests valid dimension filter syntax.

Real-Time Reporting

  • run_realtime_report — Queries real-time GA4 data.
  • get_realtime_dimensions — Lists dimensions available for real-time reports.
  • get_realtime_metrics — Lists metrics available for real-time reports.

Setting Up the Google Analytics MCP Server

Python Setup

Install pipx. It isolates the server environment.

Enable APIs

In your Google Cloud project, turn on:

  • Google Analytics Admin API
  • Google Analytics Data API

Configure Credentials

Set up Application Default Credentials (ADC). The authenticated user needs read access to the target GA accounts.

The credentials must include the read-only scope:

https://www.googleapis.com/auth/analytics.readonly

Example gcloud commands:

After downloading your OAuth client JSON file, run:

gcloud auth application-default login \
  --scopes https://www.googleapis.com/auth/analytics.readonly,https://www.googleapis.com/auth/cloud-platform \
  --client-id-file=YOUR_CLIENT_JSON_FILE

For service account impersonation:

gcloud auth application-default login \
  --impersonate-service-account=SERVICE_ACCOUNT_EMAIL \
  --scopes=https://www.googleapis.com/auth/analytics.readonly,https://www.googleapis.com/auth/cloud-platform

Configure Gemini

Install Gemini CLI or Gemini Code Assist.

Edit ~/.gemini/settings.json. Add the server to the mcpServers list:

{
  "mcpServers": {
    "analytics-mcp": {
      "command": "pipx",
      "args": [
        "run",
        "--spec",
        "git+https://github.com/googleanalytics/google-analytics-mcp.git",
        "google-analytics-mcp"
      ]
    }
  }
}

You can also pin a specific ADC file by setting an environment variable:

{
  "mcpServers": {
    "analytics-mcp": {
      "command": "pipx",
      "args": [
        "run",
        "--spec",
        "git+https://github.com/googleanalytics/google-analytics-mcp.git",
        "google-analytics-mcp"
      ],
      "env": {
        "GOOGLE_APPLICATION_CREDENTIALS": "PATH_TO_ADC_JSON"
      }
    }
  }
}

Replace PATH_TO_ADC_JSON with the full path to your credentials file.

Using the Server

Start Gemini Code Assist or Gemini CLI. Type /mcp. You should see analytics-mcp listed.

Try these prompts:

Ask what the server can do:

what can the analytics-mcp server do?

Find a property by name fragment:

Give me details about my Google Analytics property with 'xyz' in the name

Analyze event popularity:

what are the most popular events in my Google Analytics property in the last 180 days?

Check login status of users:

were most of my users in the last 6 months logged in?

Inspect custom definitions:

what are the custom dimensions and custom metrics in my property?

The LLM translates your question into the right tool calls, fetches the data, and returns a readable summary. No more drilling through GA4 reports by hand.