介绍
# Google Analytics
使用托管的 OAuth 身份验证访问 Google Analytics。此技能涵盖管理 API(管理帐户、媒体资源、数据流)和数据 API(运行指标报告)。
## 快速开始
```bash # List account summaries (Admin API) python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://gateway.maton.ai/google-analytics-admin/v1beta/accountSummaries') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF
# Run a report (Data API) python <<'EOF' import urllib.request, os, json data = json.dumps({'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}], 'dimensions': [{'name': 'city'}], 'metrics': [{'name': 'activeUsers'}]}).encode() req = urllib.request.Request('https://gateway.maton.ai/google-analytics-data/v1beta/properties/{propertyId}:runReport', data=data, method='POST') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') req.add_header('Content-Type', 'application/json') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```
## 基础 URL
**Admin API**(管理帐户、媒体资源、数据流): ``` https://gateway.maton.ai/google-analytics-admin/{native-api-path} ```
**Data API**(运行报告): ``` https://gateway.maton.ai/google-analytics-data/{native-api-path} ```
将 `{native-api-path}` 替换为实际的 Google Analytics API 端点路径。网关将请求代理到 `analyticsadmin.googleapis.com` 和 `analyticsdata.googleapis.com`,并自动注入您的 OAuth 令牌。
## 身份验证
所有请求都需要在 Authorization 头中包含 Maton API 密钥:
``` Authorization: Bearer $MATON_API_KEY ```
**环境变量:** 将您的 API 密钥设置为 `MATON_API_KEY`:
```bash export MATON_API_KEY="YOUR_API_KEY" ```
### 获取 API 密钥
1. 登录或在 [maton.ai](https://maton.ai) 创建帐户 2. 访问 [maton.ai/settings](https://maton.ai/settings) 3. 复制您的 API 密钥
## 连接管理
在 `https://ctrl.maton.ai` 管理您的 Google OAuth 连接。
**重要提示:** Admin API 和 Data API 使用单独的连接: - `google-analytics-admin` - Admin API 端点所需(管理帐户、媒体资源、数据流) - `google-analytics-data` - Data API 端点所需(运行报告)
根据您要使用的 API 创建所需的连接。
### 列出连接
```bash # List Admin API connections python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://ctrl.maton.ai/connections?app=google-analytics-admin&status=ACTIVE') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF
# List Data API connections python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://ctrl.maton.ai/connections?app=google-analytics-data&status=ACTIVE') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```
### 创建连接
```bash # Create Admin API connection (for managing accounts, properties, data streams) python <<'EOF' import urllib.request, os, json data = json.dumps({'app': 'google-analytics-admin'}).encode() req = urllib.request.Request('https://ctrl.maton.ai/connections', data=data, method='POST') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') req.add_header('Content-Type', 'application/json') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF
# Create Data API connection (for running reports) python <<'EOF' import urllib.request, os, json data = json.dumps({'app': 'google-analytics-data'}).encode() req = urllib.request.Request('https://ctrl.maton.ai/connections', data=data, method='POST') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') req.add_header('Content-Type', 'application/json') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```
### 获取连接
```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```
**响应:** ```json { "connection": { "connection_id": "21fd90f9-5935-43cd-b6c8-bde9d915ca80", "status": "ACTIVE", "creation_time": "2025-12-08T07:20:53.488460Z", "last_updated_time": "2026-01-31T20:03:32.593153Z", "url": "https://connect.maton.ai/?session_token=...", "app": "google-analytics-admin", "metadata": {} } } ```
在浏览器中打开返回的 `url` 以完成 OAuth 授权。
### 删除连接
```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}', method='DELETE') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```
### 指定连接
如果您有多个 Google Analytics 连接,请使用 `Maton-Connection` 头指定要使用的连接:
```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://gateway.maton.ai/google-analytics-admin/v1beta/accountSummaries') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') req.add_header('Maton-Connection', '21fd90f9-5935-43cd-b6c8-bde9d915ca80') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```
如果省略,网关将使用默认(最旧的)活动连接。
## Admin API 参考
### 帐户
```bash GET /google-analytics-admin/v1beta/accounts GET /google-analytics-admin/v1beta/accounts/{accountId} GET /google-analytics-admin/v1beta/accountSummaries ```
### 媒体资源
```bash GET /google-analytics-admin/v1beta/properties?filter=parent:accounts/{accountId} GET /google-analytics-admin/v1beta/properties/{propertyId} ```
#### 创建媒体资源
```bash POST /google-analytics-admin/v1beta/properties Content-Type: application/json
{ "parent": "accounts/{accountId}", "displayName": "My New Property", "timeZone": "America/Los_Angeles", "currencyCode": "USD" } ```
### 数据流
```bash GET /google-analytics-admin/v1beta/properties/{propertyId}/dataStreams ```
#### 创建网络数据流
```bash POST /google-analytics-admin/v1beta/properties/{propertyId}/dataStreams Content-Type: application/json
{ "type": "WEB_DATA_STREAM", "displayName": "My Website", "webStreamData": {"defaultUri": "https://example.com"} } ```
### 自定义维度
```bash GET /google-analytics-admin/v1beta/properties/{propertyId}/customDimensions ```
#### 创建自定义维度
```bash POST /google-analytics-admin/v1beta/properties/{propertyId}/customDimensions Content-Type: application/json
{ "parameterName": "user_type", "displayName": "User Type", "scope": "USER" } ```
### 转化事件
```bash GET /google-analytics-admin/v1beta/properties/{propertyId}/conversionEvents POST /google-analytics-admin/v1beta/properties/{propertyId}/conversionEvents ```
## Data API 参考
### 运行报告
```bash POST /google-analytics-data/v1beta/properties/{propertyId}:runReport Content-Type: application/json
{ "dateRanges": [{"startDate": "30daysAgo", "endDate": "today"}], "dimensions": [{"name": "city"}], "metrics": [{"name": "activeUsers"}] } ```
### 运行实时报告
```bash POST /google-analytics-data/v1beta/properties/{propertyId}:runRealtimeReport Content-Type: application/json
{ "dimensions": [{"name": "country"}], "metrics": [{"name": "activeUsers"}] } ```
### 批量运行报告
```bash POST /google-analytics-data/v1beta/properties/{propertyId}:batchRunReports Content-Type: application/json
{ "requests": [ { "dateRanges": [{"startDate": "7daysAgo", "endDate": "today"}], "dimensions": [{"name": "country"}], "metrics": [{"name": "sessions"}] } ] } ```
### 获取元数据
```bash GET /google-analytics-data/v1beta/properties/{propertyId}/metadata ```
## 常用报告示例
### 按页面查看页面浏览量
```json { "dateRanges": [{"startDate": "30daysAgo", "endDate": "today"}], "dimensions": [{"name": "pagePath"}], "metrics": [{"name": "screenPageViews"}], "orderBys": [{"metric": {"metricName": "screenPageViews"}, "desc": true}], "limit": 10 } ```
### 按国家/地区查看用户
```json { "dateRanges": [{"startDate": "30daysAgo", "endDate": "today"}], "dimensions": [{"name": "country"}], "metrics": [{"name": "activeUsers"}, {"name": "sessions"}] } ```
### 流量来源
```json { "dateRanges": [{"startDate": "30daysAgo", "endDate": "today"}], "dimensions": [{"name": "sessionSource"}, {"name": "sessionMedium"}], "metrics": [{"name": "sessions"}, {"name": "conversions"}] } ```
## 常用维度
- `date`, `country`, `city`, `deviceCategory` - `pagePath`, `pageTitle`, `landingPage` - `sessionSource`, `sessionMedium`, `sessionCampaignName`
## 常用指标
- `activeUsers`, `newUsers`, `sessions` - `screenPageViews`, `bounceRate`, `averageSessionDuration` - `conversions`, `eventCount`
## 日期格式
- 相对日期:`today`, `yesterday`, `7daysAgo`, `30daysAgo` - 绝对日期:`2026-01-01`
## 代码示例
### JavaScript
```javascript // List account summaries (Admin API) const accounts = await fetch( 'https://gateway.maton.ai/google-analytics-admin/v1beta/accountSummaries', { headers: { 'Authorization': `Bearer ${process.env.MATON_API_KEY}` } } );
// Run a report (Data API) const report = await fetch( 'https://gateway.maton.ai/google-analytics-data/v1beta/properties/123456:runReport', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.MATON_API_KEY}` }, body: JSON.stringify({ dateRanges: [{ startDate: '30daysAgo', endDate: 'today' }], dimensions: [{ name: 'country' }], metrics: [{ name: 'activeUsers' }] }) } ); ```
### Python
```python import os import requests
# List account summaries (Admin API) accounts = requests.get( 'https://gateway.maton.ai/google-analytics-admin/v1beta/accountSummaries', headers={'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}'} )
# Run a report (Data API) report = requests.post( 'https://gateway.maton.ai/google-analytics-data/v1beta/properties/123456:runReport', headers={'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}'}, json={ 'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}], 'dimensions': [{'name': 'country'}], 'metrics': [{'name': 'activeUsers'}] } ) ```
## 注意事项
- 仅支持 GA4 媒体资源(不支持 Universal Analytics) - 媒体资源 ID 为数字(例如 `properties/521310447`) - 使用 `accountSummaries` 快速列出所有可访问的媒体资源 - 在 Admin API 的 PATCH 请求中使用 `updateMask` - 使用元数据端点发现可用的维度/指标 - 重要:使用 curl 命令时,如果 URL 包含括号(`fields[]`, `sort[]`, `records[]`),请使用 `curl -g` 以禁用 glob 解析 - 重要:将 curl 输出通过管道传输到 `jq` 或其他命令时,在某些 Shell 环境中,`$MATON_API_KEY` 等环境变量可能无法正确展开。通过管道传输时,您可能会收到“Invalid API key”错误。
## 错误处理
| 状态 | 含义 | |--------|---------| | 400 | 缺少 Google Analytics 连接 | | 401 | Maton API 密钥无效或缺失 | | 429 | 请求频率受限(每个帐户每秒 10 次) | | 4xx/5xx | 来自 Google Analytics API 的透传错误 |
### 故障排除:无效的 API 密钥
**当您收到“Invalid API key”错误时,在断定存在问题之前,请务必执行以下步骤:**
1. 检查是否设置了 `MATON_API_KEY` 环境变量:
```bash echo $MATON_API_KEY ```
2. 通过列出连接来验证 API 密钥是否有效:
```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://ctrl.maton.ai/connections') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```
### 故障排除:无效的应用名称
1. 确保您的 URL 路径以正确的应用名称开头: - 对于 Admin API:使用 `google-analytics-admin` - 对于 Data API:使用 `google-analytics-data`
示例: - 正确:`https://gateway.maton.ai/google-analytics-admin/v1beta/accountSummaries` - 正确:`https://gateway.maton.ai/google-analytics-data/v1beta/properties/123456:runReport` - 错误:`https://gateway.maton.ai/analytics/v1beta/accountSummaries`
## 资源
- [Admin API 概述](https://developers.google.com/analytics/devguides/config/admin/v1) - [帐户](https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1beta/accounts) - [媒体资源](https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1beta/properties) - [数据流](https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1beta/properties.dataStreams) - [Data API 概述](https://developers.google.com/analytics/devguides/reporting/data/v1) - [运行报告](https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport) - [实时报告](https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport) - [Maton 社区](https://discord.com/invite/dBfFAcefs2) - [Maton 支持](mailto:[email protected])