|
| 1 | +# Google Analytics Tool |
| 2 | + |
| 3 | +Query GA4 website traffic and marketing performance data via the Data API v1. |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +Provides read-only access to Google Analytics 4 (GA4) properties. Use these tools to pull website traffic data, monitor real-time activity, and analyze marketing performance. |
| 8 | + |
| 9 | +Supports: |
| 10 | +- **Custom reports** with any combination of GA4 dimensions and metrics |
| 11 | +- **Real-time data** for current website activity |
| 12 | +- **Convenience wrappers** for common queries (top pages, traffic sources) |
| 13 | + |
| 14 | +## Tools |
| 15 | + |
| 16 | +### `ga_run_report` |
| 17 | + |
| 18 | +Run a custom GA4 report with flexible dimensions, metrics, and date ranges. |
| 19 | + |
| 20 | +| Argument | Type | Required | Default | Description | |
| 21 | +|----------|------|----------|---------|-------------| |
| 22 | +| `property_id` | str | Yes | - | GA4 property ID (e.g., `"properties/123456"`) | |
| 23 | +| `metrics` | list[str] | Yes | - | Metrics to retrieve (e.g., `["sessions", "totalUsers"]`) | |
| 24 | +| `dimensions` | list[str] | No | `None` | Dimensions to group by (e.g., `["pagePath", "sessionSource"]`) | |
| 25 | +| `start_date` | str | No | `"28daysAgo"` | Start date (e.g., `"2024-01-01"` or `"7daysAgo"`) | |
| 26 | +| `end_date` | str | No | `"today"` | End date | |
| 27 | +| `limit` | int | No | `100` | Max rows to return (1-10000) | |
| 28 | + |
| 29 | +### `ga_get_realtime` |
| 30 | + |
| 31 | +Get real-time analytics data (active users, current pages). |
| 32 | + |
| 33 | +| Argument | Type | Required | Default | Description | |
| 34 | +|----------|------|----------|---------|-------------| |
| 35 | +| `property_id` | str | Yes | - | GA4 property ID | |
| 36 | +| `metrics` | list[str] | No | `["activeUsers"]` | Metrics to retrieve | |
| 37 | + |
| 38 | +### `ga_get_top_pages` |
| 39 | + |
| 40 | +Get top pages by views and engagement (convenience wrapper). |
| 41 | + |
| 42 | +| Argument | Type | Required | Default | Description | |
| 43 | +|----------|------|----------|---------|-------------| |
| 44 | +| `property_id` | str | Yes | - | GA4 property ID | |
| 45 | +| `start_date` | str | No | `"28daysAgo"` | Start date | |
| 46 | +| `end_date` | str | No | `"today"` | End date | |
| 47 | +| `limit` | int | No | `10` | Max pages to return (1-10000) | |
| 48 | + |
| 49 | +Returns: `pagePath`, `pageTitle`, `screenPageViews`, `averageSessionDuration`, `bounceRate` |
| 50 | + |
| 51 | +### `ga_get_traffic_sources` |
| 52 | + |
| 53 | +Get traffic breakdown by source/medium (convenience wrapper). |
| 54 | + |
| 55 | +| Argument | Type | Required | Default | Description | |
| 56 | +|----------|------|----------|---------|-------------| |
| 57 | +| `property_id` | str | Yes | - | GA4 property ID | |
| 58 | +| `start_date` | str | No | `"28daysAgo"` | Start date | |
| 59 | +| `end_date` | str | No | `"today"` | End date | |
| 60 | +| `limit` | int | No | `10` | Max sources to return (1-10000) | |
| 61 | + |
| 62 | +Returns: `sessionSource`, `sessionMedium`, `sessions`, `totalUsers`, `conversions` |
| 63 | + |
| 64 | +## Environment Variables |
| 65 | + |
| 66 | +| Variable | Required | Description | |
| 67 | +|----------|----------|-------------| |
| 68 | +| `GOOGLE_APPLICATION_CREDENTIALS` | Yes | Path to Google Cloud service account JSON key file | |
| 69 | + |
| 70 | +## Setup |
| 71 | + |
| 72 | +1. Go to [Google Cloud Console](https://console.cloud.google.com/) > IAM & Admin > Service Accounts |
| 73 | +2. Create a service account (e.g., "hive-analytics-reader") |
| 74 | +3. Download the JSON key file |
| 75 | +4. Enable the **Google Analytics Data API** in your Google Cloud project |
| 76 | +5. In Google Analytics, go to Admin > Property > Property Access Management |
| 77 | +6. Add the service account email with **Viewer** role |
| 78 | +7. Set the environment variable: |
| 79 | + ```bash |
| 80 | + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json |
| 81 | + ``` |
| 82 | + |
| 83 | +## Common GA4 Metrics |
| 84 | + |
| 85 | +`sessions`, `totalUsers`, `newUsers`, `screenPageViews`, `conversions`, `bounceRate`, `averageSessionDuration`, `engagedSessions` |
| 86 | + |
| 87 | +## Common GA4 Dimensions |
| 88 | + |
| 89 | +`pagePath`, `pageTitle`, `sessionSource`, `sessionMedium`, `country`, `deviceCategory`, `date` |
| 90 | + |
| 91 | +## Example Usage |
| 92 | + |
| 93 | +```python |
| 94 | +# Custom report: sessions by page over the last 7 days |
| 95 | +result = ga_run_report( |
| 96 | + property_id="properties/123456", |
| 97 | + metrics=["sessions", "screenPageViews"], |
| 98 | + dimensions=["pagePath"], |
| 99 | + start_date="7daysAgo", |
| 100 | +) |
| 101 | + |
| 102 | +# Real-time active users |
| 103 | +result = ga_get_realtime(property_id="properties/123456") |
| 104 | + |
| 105 | +# Top 10 pages this month |
| 106 | +result = ga_get_top_pages( |
| 107 | + property_id="properties/123456", |
| 108 | + start_date="2024-01-01", |
| 109 | + end_date="2024-01-31", |
| 110 | +) |
| 111 | + |
| 112 | +# Traffic sources breakdown |
| 113 | +result = ga_get_traffic_sources(property_id="properties/123456") |
| 114 | +``` |
| 115 | + |
| 116 | +## Error Handling |
| 117 | + |
| 118 | +Returns error dicts for common issues: |
| 119 | +- `Google Analytics credentials not configured` - No credentials set |
| 120 | +- `property_id must start with 'properties/'` - Invalid property ID format |
| 121 | +- `metrics list must not be empty` - No metrics provided |
| 122 | +- `limit must be between 1 and 10000` - Limit out of bounds |
| 123 | +- `Failed to initialize Google Analytics client` - Bad credentials file |
| 124 | +- `Google Analytics API error: ...` - API-level errors (permissions, quota, etc.) |
0 commit comments