A Model Context Protocol (MCP) server for Google Analytics 4, providing comprehensive integration with both the Google Analytics Data API (for reading reports) and Measurement Protocol v2 (for sending events).
IMPORTANT: Google Analytics reports can return large datasets that consume significant tokens. This server is designed with token optimization in mind:
- All read tools default to 10 results - Adjust the
limitparameter as needed - Use specific date ranges - Avoid querying years of data at once
- Select only needed dimensions/metrics - Don't request everything
- Check TOKEN_OPTIMIZATION.md for detailed best practices
See the dedicated Token Optimization Guide for strategies to minimize token usage.
See QUICKSTART.md for a 5-minute setup guide, or follow the installation steps below.
npm install -g mcp-google-analyticsnpx mcp-google-analyticsThis server requires different credentials for reading data vs sending events:
You need a Service Account with access to your GA4 property:
- Go to Google Cloud Console
- Create or select a project
- Enable the Google Analytics Data API
- Create a Service Account:
- Go to "IAM & Admin" > "Service Accounts"
- Click "Create Service Account"
- Give it a name (e.g., "GA4 MCP Reader")
- Grant the "Viewer" role
- Create a JSON key and download it
- Add the service account email to your GA4 property:
- Go to GA4 Admin > Property Access Management
- Add the service account email with "Viewer" role
- Get your Property ID:
- Go to GA4 Admin > Property Settings
- Copy the Property ID (numeric, e.g., "123456789")
You need a Measurement ID and API Secret:
- Go to GA4 Admin > Data Streams
- Select your data stream (web, iOS, or Android)
- Copy the Measurement ID (format:
G-XXXXXXXXXX) - Click "Measurement Protocol API secrets"
- Click "Create" to generate a new API secret
- Copy the secret value
Set these environment variables:
# For Data API (reading)
export GA_SERVICE_ACCOUNT_JSON=/path/to/service-account.json
# Or provide JSON directly:
# export GA_SERVICE_ACCOUNT_JSON='{"type":"service_account","project_id":"..."}'
export GA_PROPERTY_ID=123456789
# For Measurement Protocol (writing)
export GA_MEASUREMENT_ID=G-XXXXXXXXXX
export GA_API_SECRET=your-api-secret-hereAdd to your Claude Desktop configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%/Claude/claude_desktop_config.json
{
"mcpServers": {
"google-analytics": {
"command": "npx",
"args": ["-y", "mcp-google-analytics"],
"env": {
"GA_SERVICE_ACCOUNT_JSON": "/path/to/service-account.json",
"GA_PROPERTY_ID": "123456789",
"GA_MEASUREMENT_ID": "G-XXXXXXXXXX",
"GA_API_SECRET": "your-api-secret"
}
}
}
}Or if installed globally:
{
"mcpServers": {
"google-analytics": {
"command": "mcp-google-analytics",
"env": {
"GA_SERVICE_ACCOUNT_JSON": "/path/to/service-account.json",
"GA_PROPERTY_ID": "123456789",
"GA_MEASUREMENT_ID": "G-XXXXXXXXXX",
"GA_API_SECRET": "your-api-secret"
}
}
}
}Restart Claude Desktop after updating the configuration.
Add to your Cursor MCP settings file:
macOS/Linux: ~/.cursor/mcp.json
Windows: %USERPROFILE%\.cursor\mcp.json
{
"mcpServers": {
"google-analytics": {
"command": "npx",
"args": ["-y", "mcp-google-analytics"],
"env": {
"GA_SERVICE_ACCOUNT_JSON": "/path/to/service-account.json",
"GA_PROPERTY_ID": "123456789",
"GA_MEASUREMENT_ID": "G-XXXXXXXXXX",
"GA_API_SECRET": "your-api-secret"
}
}
}
}Restart Cursor after updating the configuration.
Run custom reports with dimensions and metrics.
Common Dimensions: date, city, country, deviceCategory, browser, pagePath, eventName, sessionSource, sessionMedium, sessionCampaignName
Common Metrics: activeUsers, sessions, screenPageViews, conversions, totalRevenue, engagementRate, averageSessionDuration
Example:
{
"dateRanges": [{"startDate": "7daysAgo", "endDate": "today"}],
"dimensions": [{"name": "city"}],
"metrics": [{"name": "activeUsers"}],
"limit": 10
}Get real-time data (last 30 minutes).
Example:
{
"metrics": [{"name": "activeUsers"}],
"dimensions": [{"name": "country"}],
"limit": 10
}Get all available dimensions and metrics for your property.
Warning: Returns 500+ items. Use sparingly.
List all GA accounts accessible to the service account.
List GA4 properties, optionally filtered by account ID.
Get details about the configured property.
List data streams for the configured property.
Run pivot table reports with row/column dimensions.
Example:
{
"dateRanges": [{"startDate": "7daysAgo", "endDate": "today"}],
"dimensions": [{"name": "country"}, {"name": "deviceCategory"}],
"metrics": [{"name": "activeUsers"}],
"pivots": [{"fieldNames": ["deviceCategory"], "limit": 5}]
}Run funnel analysis to track user progression.
Example:
{
"dateRanges": [{"startDate": "7daysAgo", "endDate": "today"}],
"funnelSteps": [
{"name": "page_view"},
{"name": "add_to_cart"},
{"name": "begin_checkout"},
{"name": "purchase"}
]
}Run multiple reports in a single request.
Warning: Can return large datasets. Limit to 2-5 reports per batch.
Send custom events to GA4.
Example:
{
"events": [{
"name": "button_click",
"params": {
"button_id": "cta_signup",
"page": "/landing"
}
}],
"user_id": "user123"
}Validate events before sending (uses debug endpoint).
Send page view events.
Example:
{
"page_location": "https://example.com/products",
"page_title": "Products",
"user_id": "user123"
}Send ecommerce purchase events.
Example:
{
"transaction_id": "T12345",
"value": 99.99,
"currency": "USD",
"items": [{
"item_id": "SKU123",
"item_name": "Product Name",
"price": 99.99,
"quantity": 1
}]
}Send login events.
Send user registration events.
Send add-to-cart events.
Send checkout initiation events.
See EXAMPLES.md for practical usage examples in Spanish.
Show me active users by country for the last 7 days
Claude will use ga_run_report:
{
"dateRanges": [{"startDate": "7daysAgo", "endDate": "today"}],
"dimensions": [{"name": "country"}],
"metrics": [{"name": "activeUsers"}],
"limit": 10,
"orderBys": [{"metric": {"metricName": "activeUsers"}, "desc": true}]
}Send a purchase event for order #12345, $99.99 USD
Claude will use ga_send_purchase:
{
"transaction_id": "12345",
"value": 99.99,
"currency": "USD",
"items": [{
"item_id": "product_1",
"item_name": "Example Product",
"price": 99.99,
"quantity": 1
}]
}Enable debug logging by setting:
export DEBUG=mcp-google-analytics:*For Measurement Protocol, use ga_validate_event to check events before sending them live.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see LICENSE file for details.
- GitHub Repository
- npm Package
- MCP Documentation
- GA4 Data API Documentation
- Measurement Protocol Documentation
For issues and questions:
See CHANGELOG.md for version history.
Made with β€οΈ by GoMakers.ai