-
Notifications
You must be signed in to change notification settings - Fork 561
Expand file tree
/
Copy pathinfo.py
More file actions
100 lines (83 loc) · 3.29 KB
/
Copy pathinfo.py
File metadata and controls
100 lines (83 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright 2025 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tools for gathering Google Analytics account and property information."""
from typing import Any, Dict, List
from google.analytics import admin_v1beta
from analytics_mcp.coordinator import mcp
from analytics_mcp.tools.utils import (
construct_property_rn,
create_admin_api_client,
proto_to_dict,
)
@mcp.tool()
async def get_account_summaries() -> List[Dict[str, Any]]:
"""Retrieves information about the user's Google Analytics accounts and
properties.
"""
# Uses an async list comprehension so the pager returned by
# list_account_summaries retrieves all pages.
summary_pager = await create_admin_api_client().list_account_summaries()
summaries = []
async for summary_page in summary_pager:
# Extract just the ID from resource names
account_id = summary_page.account.split("/")[-1]
summaries.append(
{
"account_id": account_id,
"account_name": summary_page.display_name,
"properties": [
{
"id": ps.property.split("/")[-1],
"name": ps.display_name,
}
for ps in summary_page.property_summaries
],
}
)
return summaries
@mcp.tool(title="List links to Google Ads accounts")
async def list_google_ads_links(property_id: str) -> List[Dict[str, Any]]:
"""Returns a list of links to Google Ads accounts for a property.
Args:
property_id: The Google Analytics property ID as a string
(e.g., "213025502"). Get property IDs from
get_account_summaries().
"""
request = admin_v1beta.ListGoogleAdsLinksRequest(
parent=construct_property_rn(property_id)
)
# Uses an async list comprehension so the pager returned by
# list_google_ads_links retrieves all pages.
links_pager = await create_admin_api_client().list_google_ads_links(
request=request
)
all_pages = [proto_to_dict(link_page) async for link_page in links_pager]
return all_pages
@mcp.tool(title="Gets details about a property")
async def get_property_details(property_id: str) -> Dict[str, Any]:
"""Returns details about a property.
Args:
property_id: The Google Analytics property ID as a string
(e.g., "213025502"). Get property IDs from
get_account_summaries().
"""
client = create_admin_api_client()
request = admin_v1beta.GetPropertyRequest(
name=construct_property_rn(property_id)
)
response = await client.get_property(request=request)
# Convert to dict and remove redundant parent field
result = proto_to_dict(response)
result.pop("parent", None)
return result