Gists

Abstract
This report introduces a powerful method for automating Google Analytics tasks using the Gemini CLI and a custom MCP (Model Context Protocol) server built with Google Apps Script. This integration enables streamlined web page analysis through simple natural language commands, simplifying authorization and complex data retrieval workflows.
Introduction
Accessing and interpreting web analytics data often involves navigating complex interfaces and manual report generation. However, the emergence of natural language interfaces is changing this paradigm. Gemini CLI, when paired with MCP servers, allows users to orchestrate sophisticated, multi-step workflows using conversational commands. This creates a more intuitive and efficient way to interact with powerful services like Google Analytics.
Gists
This is a sample script for retrieving “Users”, “Sessions” and “PageViews” of User Summary Report from Google Analytics using Google Apps Script. When you use this, please enable Analytics Reporting API at Advanced Google services.
Sample script
function myFunction() {
const viewId = "###";
const startDate = "2020-01-01";
const endDate = "2020-06-01";
const resource = {
reportRequests: [
{
viewId: viewId,
dateRanges: [{ startDate: startDate, endDate: endDate }],
metrics: [
{ expression: "ga:users" },
{ expression: "ga:sessions" },
{ expression: "ga:pageviews" },
],
},
],
};
const res = AnalyticsReporting.Reports.batchGet(resource, { fields: "*" });
console.log(res);
}
Result
{
"reports": [
{
"columnHeader": {
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:users",
"type": "INTEGER"
},
{
"type": "INTEGER",
"name": "ga:sessions"
},
{
"type": "INTEGER",
"name": "ga:pageviews"
}
]
}
},
"data": {
"maximums": [
{
"values": ["###", "###", "###"]
}
],
"rowCount": 1,
"totals": [
{
"values": ["###", "###", "###"]
}
],
"minimums": [
{
"values": ["###", "###", "###"]
}
],
"isDataGolden": true,
"rows": [
{
"metrics": [
{
"values": ["###", "###", "###"]
}
]
}
]
}
}
]
}
Reference