@@ -4,3 +4,219 @@ This repo contains the source code for running a local
44[ MCP] ( https://modelcontextprotocol.io ) server that interacts with APIs for
55[ Google Analytics] ( https://support.google.com/analytics ) .
66
7+ ## Tools :hammer_and_wrench :
8+
9+ The server uses the
10+ [ Google Analytics Admin API] ( https://developers.google.com/analytics/devguides/config/admin/v1 )
11+ and
12+ [ Google Analytics Data API] ( https://developers.google.com/analytics/devguides/reporting/data/v1 )
13+ to provide several
14+ [ Tools] ( https://modelcontextprotocol.io/docs/concepts/tools ) for use with LLMs.
15+
16+ ### Retrieve account and property information :orange_circle :
17+
18+ * ` get_account_summaries ` : Retrieves information about the user's Google
19+ Analytics accounts and properties.
20+ * ` get_property_details ` : Returns details about a property.
21+ * ` list_google_ads_links ` : Returns a list of links to Google Ads accounts for
22+ a property.
23+
24+ ### Run core reports :orange_book :
25+
26+ * ` run_report ` : Run a Google Analytics report using the Data API.
27+ * ` get_dimensions ` : Retrieves Core Reporting Dimensions for a specific
28+ property, including its custom dimensions.
29+ * ` get_metrics ` : Retrieves Core Reporting Metrics for a specific property,
30+ including its custom dimensions.
31+ * ` get_standard_dimensions ` : Returns a list of standard dimensions.
32+ * ` get_standard_metrics ` : Returns a list of standard metrics.
33+ * ` run_report_date_ranges_hints ` : Provides hints about the expected values
34+ for the ` date_ranges ` argument for the ` run_report ` tool.
35+ * ` run_report_metric_filter_hints ` : Provides hints about the expected values
36+ for the metric_filter argument for the ` run_report ` and
37+ ` run_realtime_report ` tools.
38+ * ` run_report_dimension_filter_hints ` : Provides hints about the expected
39+ values for the dimension_filter argument for the ` run_report ` and
40+ ` run_realtime_report ` tools.
41+
42+ ### Run realtime reports :hourglass_flowing_sand :
43+
44+ * ` run_realtime_report ` : Run a Google Analytics realtime report using the
45+ Data API.
46+ * ` get_realtime_dimensions ` : Retrieves the list of realtime reporting
47+ dimensions.
48+ * ` get_realtime_metrics ` : Retrieves the list of realtime metrics.
49+
50+ ## Setup instructions
51+
52+ Setup involves the following steps:
53+
54+ 1 . Configure Python and install dependencies.
55+ 1 . Configure credentials for Google Analytics.
56+ 1 . Configure Gemini.
57+
58+ ### Configure Python :snake :
59+
60+ Navigate to the ` analytics-mcp ` directory, then complete the following steps.
61+
62+ 1 . Create a Python virtual environment in the ` env ` directory.
63+
64+ ``` shell
65+ python3 -m venv .venv
66+ ```
67+
68+ 1. Activate the virtual environment.
69+
70+ ` ` ` shell
71+ source .venv/bin/activate
72+ ` ` `
73+
74+ 1. Setup the project and its dependencies.
75+
76+ ` ` ` shell
77+ pip install .
78+ ` ` `
79+
80+
81+ # ## Configure credentials :key:
82+
83+ Configure your [Application Default Credentials
84+ (ADC)](https://cloud.google.com/docs/authentication/provide-credentials-adc).
85+ Make sure the credentials are for a user with access to your Google Analytics
86+ accounts or properties.
87+
88+ Credentials must include the Google Analytics read-only scope:
89+
90+ ```
91+ https://www.googleapis.com/auth/analytics.readonly
92+ ```
93+
94+ Here are some sample `gcloud` commands you might find useful:
95+
96+ * Set up ADC using user credentials and an OAuth web client after
97+ downloading the client JSON to `YOUR_WEB_CLIENT_JSON_FILE`.
98+
99+ ```shell
100+ gcloud auth application-default login \
101+ --scopes https://www.googleapis.com/auth/analytics.readonly,https://www.googleapis.com/auth/cloud-platform \
102+ --client-id-file=YOUR_WEB_CLIENT_JSON_FILE
103+ ```
104+
105+ * Set up ADC using service account impersonation.
106+
107+ ``` shell
108+ gcloud auth application-default login \
109+ --impersonate-service-account=SERVICE_ACCOUNT_EMAIL \
110+ --scopes=https://www.googleapis.com/auth/analytics.readonly,https://www.googleapis.com/auth/cloud-platform
111+ ```
112+
113+ ### Configure Gemini
114+
115+ 1 . Install [ Gemini
116+ CLI] ( https://github.com/google-gemini/gemini-cli/blob/main/docs/cli/index.md )
117+ or [ Gemini Code
118+ Assist] ( https://marketplace.visualstudio.com/items?itemName=Google.geminicodeassist )
119+
120+ 1 . Get a Gemini API key and set the ` GEMINI_API_KEY ` environment variable.
121+
122+ ``` shell
123+ export GEMINI_API_KEY=YOUR_GEMINI_API_KEY
124+ ```
125+
126+ 1. Create or edit the file at ` ~/.gemini/settings.json` , adding your server
127+ to the ` mcpServers` list.
128+
129+ Replace ` PATH_TO_SERVER` with the complete path to the directory where you
130+ cloned this repo.
131+
132+ ```
133+ {
134+ " mcpServers" : {
135+ " analytics-mcp" : {
136+ " command" : " PATH_TO_SERVER/env/bin/python" ,
137+ " args" : [
138+ " PATH_TO_SERVER/server.py"
139+ ],
140+ " env" : {
141+ " MCP_DEBUG" : " true"
142+ }
143+ }
144+ },
145+ " selectedAuthType" : " gemini-api-key" ,
146+ " fileFiltering" : {
147+ " enableRecursiveFileSearch" : false
148+ }
149+ }
150+ ```
151+
152+ 1. ** Optional:** Configure the ` GOOGLE_APPLICATION_CREDENTIALS` environment
153+ variable in Gemini settings. You may want to do this if you always want to
154+ use a specific set of credentials, regardless of which Application Default
155+ Credentials are selected in your current environment.
156+
157+ In ` ~/.gemini/settings.json` , add a ` GOOGLE_APPLICATION_CREDENTIALS`
158+ attribute to the ` env` object. Replace ` PATH_TO_ADC_JSON` in the following
159+ example with the full path to the ADC JSON file you always want to use for
160+ your MCP server.
161+
162+ ` ` `
163+ {
164+ " mcpServers" : {
165+ " analytics-mcp" : {
166+ " command" : " PATH_TO_SERVER/.venv/bin/python" ,
167+ " args" : [
168+ " PATH_TO_SERVER/server.py"
169+ ],
170+ " env" : {
171+ " MCP_DEBUG" : " true" ,
172+ " GOOGLE_APPLICATION_CREDENTIALS" : " PATH_TO_ADC_JSON"
173+ }
174+ }
175+ },
176+ " selectedAuthType" : " gemini-api-key" ,
177+ " fileFiltering" : {
178+ " enableRecursiveFileSearch" : false
179+ }
180+ }
181+ ` ` `
182+
183+ # # Try it out :lab_coat:
184+
185+ Launch Gemini Code Assist or Gemini CLI and type ` /mcp` . You should see
186+ ` analytics-mcp` listed in the results.
187+
188+ Here are some sample prompts to get you started:
189+
190+ * Ask what the server can do:
191+
192+ ` ` `
193+ what can the analytics-mcp server do?
194+ ` ` `
195+
196+ * Ask about a Google Analytics property
197+
198+ ` ` `
199+ Give me details about my Google Analytics property with ' xyz' in the name
200+ ` ` `
201+
202+ * Prompt for analysis:
203+
204+ ` ` `
205+ what are the most popular events in my Google Analytics property in the last 180 days?
206+ ` ` `
207+
208+ * Ask about signed-in users:
209+
210+ ` ` `
211+ were most of my users in the last 6 months logged in?
212+ ` ` `
213+
214+ * Ask about property configuration:
215+
216+ ` ` `
217+ what are the custom dimensions and custom metrics in my property?
218+ ` ` `
219+
220+ # # Contributing
221+
222+ Contributions welcome! See the [Contributing Guide](CONTRIBUTING.md).
0 commit comments