Skip to content

Commit ad3cec2

Browse files
authored
Merge pull request #4239 from Ttian18/feat/tina-google-analytics-tool
[Integration]: Google Analytics - Website Traffic & Marketing Performance #3727
2 parents bc836db + 890b906 commit ad3cec2

File tree

13 files changed

+1366
-1
lines changed

13 files changed

+1366
-1
lines changed

tools/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies = [
3131
"litellm>=1.81.0",
3232
"dnspython>=2.4.0",
3333
"resend>=2.0.0",
34+
"google-analytics-data>=0.18.0",
3435
"framework",
3536
"stripe>=14.3.0",
3637
"arxiv>=2.1.0",

tools/src/aden_tools/credentials/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- github.py: GitHub API credentials
4242
- hubspot.py: HubSpot CRM credentials
4343
- slack.py: Slack workspace credentials
44+
- google_analytics.py: Google Analytics credentials
4445
- google_maps.py: Google Maps Platform credentials
4546
- calcom.py: Cal.com scheduling API credentials
4647
@@ -63,6 +64,7 @@
6364
from .email import EMAIL_CREDENTIALS
6465
from .gcp_vision import GCP_VISION_CREDENTIALS
6566
from .github import GITHUB_CREDENTIALS
67+
from .google_analytics import GOOGLE_ANALYTICS_CREDENTIALS
6668
from .google_calendar import GOOGLE_CALENDAR_CREDENTIALS
6769
from .google_docs import GOOGLE_DOCS_CREDENTIALS
6870
from .google_maps import GOOGLE_MAPS_CREDENTIALS
@@ -100,6 +102,7 @@
100102
**APOLLO_CREDENTIALS,
101103
**DISCORD_CREDENTIALS,
102104
**GITHUB_CREDENTIALS,
105+
**GOOGLE_ANALYTICS_CREDENTIALS,
103106
**GOOGLE_DOCS_CREDENTIALS,
104107
**GOOGLE_MAPS_CREDENTIALS,
105108
**HUBSPOT_CREDENTIALS,
@@ -145,6 +148,7 @@
145148
"EMAIL_CREDENTIALS",
146149
"GCP_VISION_CREDENTIALS",
147150
"GITHUB_CREDENTIALS",
151+
"GOOGLE_ANALYTICS_CREDENTIALS",
148152
"GOOGLE_DOCS_CREDENTIALS",
149153
"GOOGLE_MAPS_CREDENTIALS",
150154
"HUBSPOT_CREDENTIALS",

tools/src/aden_tools/credentials/bigquery.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
BIGQUERY_CREDENTIALS = {
1010
"bigquery": CredentialSpec(
1111
env_var="GOOGLE_APPLICATION_CREDENTIALS",
12+
credential_group="google_cloud",
1213
tools=["run_bigquery_query", "describe_dataset"],
1314
required=False, # Falls back to ADC if not set
1415
startup_required=False,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Google Analytics credentials.
3+
4+
Contains credentials for Google Analytics 4 Data API integration.
5+
"""
6+
7+
from .base import CredentialSpec
8+
9+
GOOGLE_ANALYTICS_CREDENTIALS = {
10+
"google_analytics": CredentialSpec(
11+
env_var="GOOGLE_APPLICATION_CREDENTIALS",
12+
credential_group="google_cloud",
13+
tools=[
14+
"ga_run_report",
15+
"ga_get_realtime",
16+
"ga_get_top_pages",
17+
"ga_get_traffic_sources",
18+
],
19+
required=True,
20+
startup_required=False,
21+
help_url="https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries",
22+
description="Path to Google Cloud service account JSON key with Analytics read access",
23+
# Auth method support
24+
aden_supported=False,
25+
direct_api_key_supported=True,
26+
api_key_instructions="""To set up Google Analytics credentials:
27+
1. Go to Google Cloud Console > IAM & Admin > Service Accounts
28+
2. Create a service account (e.g., "hive-analytics-reader")
29+
3. Download the JSON key file
30+
4. In Google Analytics, go to Admin > Property > Property Access Management
31+
5. Add the service account email with "Viewer" role
32+
6. Set the env var to the path of the JSON key file:
33+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json""",
34+
# Health check - GA4 Data API doesn't have a simple health endpoint
35+
health_check_endpoint="",
36+
health_check_method="GET",
37+
# Credential store mapping
38+
credential_id="google_analytics",
39+
credential_key="service_account_key_path",
40+
),
41+
}

tools/src/aden_tools/tools/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from .file_system_toolkits.write_to_file import register_tools as register_write_to_file
5555
from .github_tool import register_tools as register_github
5656
from .gmail_tool import register_tools as register_gmail
57+
from .google_analytics_tool import register_tools as register_google_analytics
5758
from .google_docs_tool import register_tools as register_google_docs
5859
from .google_maps_tool import register_tools as register_google_maps
5960
from .http_headers_scanner import register_tools as register_http_headers_scanner
@@ -126,6 +127,7 @@ def register_all_tools(
126127
register_slack(mcp, credentials=credentials)
127128
register_telegram(mcp, credentials=credentials)
128129
register_vision(mcp, credentials=credentials)
130+
register_google_analytics(mcp, credentials=credentials)
129131
register_google_docs(mcp, credentials=credentials)
130132
register_google_maps(mcp, credentials=credentials)
131133
register_account_info(mcp, credentials=credentials)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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.)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Google Analytics Tool - Query GA4 website traffic and marketing data."""
2+
3+
from .google_analytics_tool import register_tools
4+
5+
__all__ = ["register_tools"]

0 commit comments

Comments
 (0)