@pipeworx/mcp-google_analytics 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +38 -0
- package/package.json +20 -0
- package/server.json +18 -0
- package/src/index.ts +196 -0
- package/tsconfig.json +14 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pipeworx
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# mcp-google_analytics
|
|
2
|
+
|
|
3
|
+
Google Analytics MCP Pack
|
|
4
|
+
|
|
5
|
+
Part of the [Pipeworx](https://pipeworx.io) open MCP gateway.
|
|
6
|
+
|
|
7
|
+
## Tools
|
|
8
|
+
|
|
9
|
+
| Tool | Description |
|
|
10
|
+
|------|-------------|
|
|
11
|
+
| `ga_run_report` | Run a report on a Google Analytics 4 property. Specify dimensions (e.g., "city", "pagePath"), metrics (e.g., "activeUsers", "sessions"), and date ranges to retrieve analytics data. |
|
|
12
|
+
| `ga_list_properties` | List all Google Analytics 4 properties accessible by the authenticated user. Uses the Admin API to fetch account summaries with property details. |
|
|
13
|
+
| `ga_get_realtime` | Get a realtime report for a Google Analytics 4 property. Shows currently active users and realtime metrics. |
|
|
14
|
+
| `ga_get_metadata` | List all available dimensions and metrics for a Google Analytics 4 property. Useful for discovering what fields can be used in reports. |
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
Add to your MCP client config:
|
|
19
|
+
|
|
20
|
+
```json
|
|
21
|
+
{
|
|
22
|
+
"mcpServers": {
|
|
23
|
+
"google_analytics": {
|
|
24
|
+
"url": "https://gateway.pipeworx.io/google_analytics/mcp"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Or use the CLI:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npx pipeworx use google_analytics
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pipeworx/mcp-google_analytics",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Google Analytics MCP Pack",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.ts",
|
|
7
|
+
"types": "src/index.ts",
|
|
8
|
+
"keywords": ["mcp", "mcp-server", "model-context-protocol", "pipeworx", "google_analytics"],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/pipeworx-io/mcp-google_analytics"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"typecheck": "tsc --noEmit"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"typescript": "^5.7.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/server.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
|
|
3
|
+
"name": "io.github.pipeworx-io/google_analytics",
|
|
4
|
+
"title": "google_analytics",
|
|
5
|
+
"description": "Google Analytics MCP Pack",
|
|
6
|
+
"version": "0.1.0",
|
|
7
|
+
"websiteUrl": "https://pipeworx.io/packs/google_analytics",
|
|
8
|
+
"repository": {
|
|
9
|
+
"url": "https://github.com/pipeworx-io/mcp-google_analytics",
|
|
10
|
+
"source": "github"
|
|
11
|
+
},
|
|
12
|
+
"remotes": [
|
|
13
|
+
{
|
|
14
|
+
"type": "streamable-http",
|
|
15
|
+
"url": "https://gateway.pipeworx.io/google_analytics/mcp"
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
interface McpToolDefinition {
|
|
2
|
+
name: string;
|
|
3
|
+
description: string;
|
|
4
|
+
inputSchema: {
|
|
5
|
+
type: 'object';
|
|
6
|
+
properties: Record<string, unknown>;
|
|
7
|
+
required?: string[];
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface McpToolExport {
|
|
12
|
+
tools: McpToolDefinition[];
|
|
13
|
+
callTool: (name: string, args: Record<string, unknown>) => Promise<unknown>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Google Analytics MCP Pack
|
|
18
|
+
*
|
|
19
|
+
* Requires OAuth connection — gateway injects credentials via _context.google_analytics.
|
|
20
|
+
* Tools: run report, list properties, get realtime data, get metadata.
|
|
21
|
+
*
|
|
22
|
+
* Uses the Google Analytics Data API (v1beta) for reports and the
|
|
23
|
+
* Google Analytics Admin API (v1beta) for property listing.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
interface AnalyticsContext {
|
|
28
|
+
google_analytics?: { accessToken: string };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DATA_API = 'https://analyticsdata.googleapis.com/v1beta';
|
|
32
|
+
const ADMIN_API = 'https://analyticsadmin.googleapis.com/v1beta';
|
|
33
|
+
|
|
34
|
+
async function gFetch(ctx: AnalyticsContext, url: string, options: RequestInit = {}) {
|
|
35
|
+
if (!ctx.google_analytics) {
|
|
36
|
+
return { error: 'connection_required', message: 'Connect your Google account at https://pipeworx.io/account' };
|
|
37
|
+
}
|
|
38
|
+
const res = await fetch(url, {
|
|
39
|
+
...options,
|
|
40
|
+
headers: {
|
|
41
|
+
Authorization: `Bearer ${ctx.google_analytics.accessToken}`,
|
|
42
|
+
'Content-Type': 'application/json',
|
|
43
|
+
...(options.headers ?? {}),
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
if (!res.ok) {
|
|
47
|
+
const text = await res.text();
|
|
48
|
+
throw new Error(`Google Analytics API error (${res.status}): ${text}`);
|
|
49
|
+
}
|
|
50
|
+
return res.json();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const tools: McpToolExport['tools'] = [
|
|
54
|
+
{
|
|
55
|
+
name: 'ga_run_report',
|
|
56
|
+
description: 'Run a report on a Google Analytics 4 property. Specify dimensions (e.g., "city", "pagePath"), metrics (e.g., "activeUsers", "sessions"), and date ranges to retrieve analytics data.',
|
|
57
|
+
inputSchema: {
|
|
58
|
+
type: 'object' as const,
|
|
59
|
+
properties: {
|
|
60
|
+
property_id: { type: 'string', description: 'GA4 property ID (numeric, e.g., "123456789")' },
|
|
61
|
+
dimensions: {
|
|
62
|
+
type: 'array',
|
|
63
|
+
description: 'List of dimension names (e.g., ["city", "pagePath", "date"])',
|
|
64
|
+
items: { type: 'string' },
|
|
65
|
+
},
|
|
66
|
+
metrics: {
|
|
67
|
+
type: 'array',
|
|
68
|
+
description: 'List of metric names (e.g., ["activeUsers", "sessions", "screenPageViews"])',
|
|
69
|
+
items: { type: 'string' },
|
|
70
|
+
},
|
|
71
|
+
start_date: { type: 'string', description: 'Start date (YYYY-MM-DD or relative: "today", "yesterday", "7daysAgo", "30daysAgo")' },
|
|
72
|
+
end_date: { type: 'string', description: 'End date (YYYY-MM-DD or relative: "today", "yesterday")' },
|
|
73
|
+
limit: { type: 'number', description: 'Maximum number of rows to return (default 100, max 10000)' },
|
|
74
|
+
dimension_filter: {
|
|
75
|
+
type: 'object',
|
|
76
|
+
description: 'Optional dimension filter object (GA4 FilterExpression format)',
|
|
77
|
+
},
|
|
78
|
+
order_bys: {
|
|
79
|
+
type: 'array',
|
|
80
|
+
description: 'Optional ordering. Each item: { dimension: { dimensionName, orderType? } } or { metric: { metricName }, desc? }',
|
|
81
|
+
items: { type: 'object' },
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
required: ['property_id', 'metrics', 'start_date', 'end_date'],
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
name: 'ga_list_properties',
|
|
89
|
+
description: 'List all Google Analytics 4 properties accessible by the authenticated user. Uses the Admin API to fetch account summaries with property details.',
|
|
90
|
+
inputSchema: {
|
|
91
|
+
type: 'object' as const,
|
|
92
|
+
properties: {
|
|
93
|
+
page_size: { type: 'number', description: 'Maximum number of account summaries to return (default 50)' },
|
|
94
|
+
page_token: { type: 'string', description: 'Token for fetching the next page of results' },
|
|
95
|
+
},
|
|
96
|
+
required: [],
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'ga_get_realtime',
|
|
101
|
+
description: 'Get a realtime report for a Google Analytics 4 property. Shows currently active users and realtime metrics.',
|
|
102
|
+
inputSchema: {
|
|
103
|
+
type: 'object' as const,
|
|
104
|
+
properties: {
|
|
105
|
+
property_id: { type: 'string', description: 'GA4 property ID (numeric)' },
|
|
106
|
+
dimensions: {
|
|
107
|
+
type: 'array',
|
|
108
|
+
description: 'Realtime dimensions (e.g., ["city", "unifiedScreenName", "platform"])',
|
|
109
|
+
items: { type: 'string' },
|
|
110
|
+
},
|
|
111
|
+
metrics: {
|
|
112
|
+
type: 'array',
|
|
113
|
+
description: 'Realtime metrics (e.g., ["activeUsers", "screenPageViews"]). Defaults to ["activeUsers"].',
|
|
114
|
+
items: { type: 'string' },
|
|
115
|
+
},
|
|
116
|
+
limit: { type: 'number', description: 'Maximum number of rows (default 100)' },
|
|
117
|
+
},
|
|
118
|
+
required: ['property_id'],
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
name: 'ga_get_metadata',
|
|
123
|
+
description: 'List all available dimensions and metrics for a Google Analytics 4 property. Useful for discovering what fields can be used in reports.',
|
|
124
|
+
inputSchema: {
|
|
125
|
+
type: 'object' as const,
|
|
126
|
+
properties: {
|
|
127
|
+
property_id: { type: 'string', description: 'GA4 property ID (numeric)' },
|
|
128
|
+
},
|
|
129
|
+
required: ['property_id'],
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
async function callTool(name: string, args: Record<string, unknown>): Promise<unknown> {
|
|
135
|
+
const context = (args._context ?? {}) as AnalyticsContext;
|
|
136
|
+
delete args._context;
|
|
137
|
+
|
|
138
|
+
switch (name) {
|
|
139
|
+
case 'ga_run_report': {
|
|
140
|
+
const propertyId = args.property_id as string;
|
|
141
|
+
const metrics = (args.metrics as string[]).map((m) => ({ name: m }));
|
|
142
|
+
const dimensions = args.dimensions
|
|
143
|
+
? (args.dimensions as string[]).map((d) => ({ name: d }))
|
|
144
|
+
: undefined;
|
|
145
|
+
const startDate = args.start_date as string;
|
|
146
|
+
const endDate = args.end_date as string;
|
|
147
|
+
const limit = Math.min(10000, Math.max(1, (args.limit as number) ?? 100));
|
|
148
|
+
|
|
149
|
+
const body: Record<string, unknown> = {
|
|
150
|
+
metrics,
|
|
151
|
+
dateRanges: [{ startDate, endDate }],
|
|
152
|
+
limit,
|
|
153
|
+
};
|
|
154
|
+
if (dimensions) body.dimensions = dimensions;
|
|
155
|
+
if (args.dimension_filter) body.dimensionFilter = args.dimension_filter;
|
|
156
|
+
if (args.order_bys) body.orderBys = args.order_bys;
|
|
157
|
+
|
|
158
|
+
return gFetch(context, `${DATA_API}/properties/${propertyId}:runReport`, {
|
|
159
|
+
method: 'POST',
|
|
160
|
+
body: JSON.stringify(body),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
case 'ga_list_properties': {
|
|
164
|
+
const pageSize = Math.min(200, Math.max(1, (args.page_size as number) ?? 50));
|
|
165
|
+
const params = new URLSearchParams({ pageSize: String(pageSize) });
|
|
166
|
+
if (args.page_token) params.set('pageToken', args.page_token as string);
|
|
167
|
+
return gFetch(context, `${ADMIN_API}/accountSummaries?${params}`);
|
|
168
|
+
}
|
|
169
|
+
case 'ga_get_realtime': {
|
|
170
|
+
const propertyId = args.property_id as string;
|
|
171
|
+
const metrics = args.metrics
|
|
172
|
+
? (args.metrics as string[]).map((m) => ({ name: m }))
|
|
173
|
+
: [{ name: 'activeUsers' }];
|
|
174
|
+
const dimensions = args.dimensions
|
|
175
|
+
? (args.dimensions as string[]).map((d) => ({ name: d }))
|
|
176
|
+
: undefined;
|
|
177
|
+
const limit = Math.min(10000, Math.max(1, (args.limit as number) ?? 100));
|
|
178
|
+
|
|
179
|
+
const body: Record<string, unknown> = { metrics, limit };
|
|
180
|
+
if (dimensions) body.dimensions = dimensions;
|
|
181
|
+
|
|
182
|
+
return gFetch(context, `${DATA_API}/properties/${propertyId}:runRealtimeReport`, {
|
|
183
|
+
method: 'POST',
|
|
184
|
+
body: JSON.stringify(body),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
case 'ga_get_metadata': {
|
|
188
|
+
const propertyId = args.property_id as string;
|
|
189
|
+
return gFetch(context, `${DATA_API}/properties/${propertyId}/metadata`);
|
|
190
|
+
}
|
|
191
|
+
default:
|
|
192
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export default { tools, callTool, meter: { credits: 15 }, provider: 'google_analytics' } satisfies McpToolExport;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"outDir": "dist",
|
|
10
|
+
"rootDir": "src",
|
|
11
|
+
"declaration": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["src"]
|
|
14
|
+
}
|