Google Analytics MCP Server

repository·main·Indexed 25 days ago

https://github.com/googleanalytics/google-analytics-mcp

An experimental Model Context Protocol (MCP) server that allows LLMs to interact with Google Analytics data via the Admin and Data APIs. It provides tools to retrieve account and property information, run core, realtime, and funnel reports, and inspect custom dimensions and metrics. Compatible with MCP clients such as Gemini and Claude Code.

Tokens
1.8K
Snippets
6
Records
13
Agent score
84%

What's inside google-analytics-mcp

  1. Overview of Google Analytics MCP Server Tools

    main

    The Google Analytics MCP server provides tools for LLMs to interact with Google Analytics via the Admin and Data APIs. The available tools are categorized into three functional groups:

    Account and Property Information

    • get_account_summaries: Retrieves information about the user's Google Analytics accounts and properties.
    • get_property_details: Returns details about a specific property.
    • list_google_ads_links: Returns a list of links to Google Ads accounts for a property.

    Core Reports

    • run_report: Runs a Google Analytics report using the Data API.
    • run_funnel_report: Runs a Google Analytics funnel report using the Data API.
    • get_custom_dimensions_and_metrics: Retrieves custom dimensions and metrics for a specific property.

    Realtime Reports

    • run_realtime_report: Runs a Google Analytics realtime report using the Data API.
  2. Configure Google Analytics MCP for Gemini

    main

    To use the server with Gemini CLI or Gemini Code Assist, add the analytics-mcp server to your ~/.gemini/settings.json file under the mcpServers key.

    Replace PATH_TO_CREDENTIALS_JSON with the path obtained during the ADC setup and YOUR_PROJECT_ID with your Google Cloud project ID.

    {
      "mcpServers": {
        "analytics-mcp": {
          "command": "pipx",
          "args": ["run", "analytics-mcp"],
          "env": {
            "GOOGLE_APPLICATION_CREDENTIALS": "PATH_TO_CREDENTIALS_JSON",
            "GOOGLE_PROJECT_ID": "YOUR_PROJECT_ID"
          }
        }
      }
    }
  3. Configure Google Analytics Credentials (ADC)

    main

    You must configure Application Default Credentials (ADC) with the https://www.googleapis.com/auth/analytics.readonly scope. Ensure the credentials belong to a user with access to your Google Analytics accounts.

    Option 1: Using User Credentials and an OAuth Client

    Use this if you have downloaded a client JSON file (YOUR_CLIENT_JSON_FILE).

    Option 2: Using Service Account Impersonation

    Use this to impersonate a specific service account.

    Note: After running the command, copy the PATH_TO_CREDENTIALS_JSON printed to the console; you will need this for the Gemini or Claude configuration steps.

    # Set up ADC using user credentials and an OAuth desktop or web client
    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
    
    # Set up ADC using 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
  4. Configure Google Analytics MCP for Claude Code

    main

    Add the MCP server to Claude Code using the claude mcp add command. Replace PATH_TO_CREDENTIALS_JSON with your credentials path and YOUR_PROJECT_ID with your Google Cloud project ID.

    claude mcp add analytics-mcp \
      --scope user \
      -e "GOOGLE_APPLICATION_CREDENTIALS=PATH_TO_CREDENTIALS_JSON" \
      -e "GOOGLE_PROJECT_ID=YOUR_PROJECT_ID" \
      -- pipx run analytics-mcp
  5. Run the Google Analytics MCP server

    main

    The Google Analytics MCP server can be executed as a standalone process using standard I/O (stdio). This is the primary way to interface the server with MCP clients like Claude Code or Gemini.

    To run the server, execute the server.py script directly using a Python interpreter. The server communicates via standard input and output streams.

  6. Sample Prompts for Google Analytics MCP

    main

    Once configured, you can interact with your Google Analytics data using natural language. Examples include:

    • Capability check: what can the analytics-mcp server do?
    • Property details: Give me details about my Google Analytics property with 'xyz' in the name
    • Data analysis: what are the most popular events in my Google Analytics property in the last 180 days?
    • User analysis: were most of my users in the last 6 months logged in?
    • Configuration check: what are the custom dimensions and custom metrics in my property?
  7. Call an MCP tool

    main

    The call_mcp_tool function is an asynchronous MCP server handler used to execute a specific tool by name with provided arguments. It handles tool execution via the internal tool_map, serializes the response to JSON, and returns it as a list of mcp_types.TextContent. If execution fails, it returns a JSON-formatted error message.

    @app.call_tool()
    async def call_mcp_tool(name: str, arguments: dict) -> list[mcp_types.Content]:
        # ... implementation details ...
  8. List available Google Analytics MCP tools

    main

    The list_tools function is an asynchronous MCP server handler that returns a list of available mcp_types.Tool objects. These tools include account summaries, property details, and various reporting capabilities (core, realtime, funnel, and conversions).

    @app.list_tools()
    async def list_tools() -> list[mcp_types.Tool]:
        return mcp_tools
  9. Sanitize MCP schema properties

    main

    The sanitize_mcp_schema_properties function ensures that the JSON schema for MCP tools is compatible with strict clients (like Claude Desktop). It recursively traverses a schema node and ensures that additionalProperties is set to a boolean value (True) rather than a schema object, which can cause client-side failures.

    def sanitize_mcp_schema_properties(node: dict) -> None: