Query Traffic With Mcp¶
π The Legend Behind This Quest¶
Your agent can reach Google Analytics now β but a connection without good questions is just a faster way to be confused. In this quest you'll learn the five tools the MCP server exposes, the GA4 vocabulary of dimensions and metrics, and how to turn "how's traffic?" into a precise, repeatable report.
π― Quest Objectives¶
Primary Objectives (Required for Quest Completion)¶
- Run a top-pages report for a date range
- Segment sessions by channel
- Apply a dimension filter to narrow a report
Secondary Objectives (Bonus Achievements)¶
- Compare two date ranges in one report (week-over-week)
- Ask your agent a plain-English question and read its query back
Mastery Indicators¶
- You can name the five tools and pick the right one quickly
- You can build a
runReportcall from a question in your head
πΊοΈ Quest Prerequisites¶
π Knowledge Requirements¶
- Completed Connect Google Analytics to Your AI Agent
- Basic GA4 concepts
π οΈ System Requirements¶
- The
google-analyticsMCP server connected to your agent
π§° Chapter 1: Know Your Five Tools¶
The MCP server exposes five tools. Four are convenience wrappers; one is the workhorse.
| Tool | Use it for |
|---|---|
getActiveUsers |
quick active-user count |
getPageViews |
page views, optionally by a dimension |
getEvents |
event counts |
getUserBehavior |
session duration, bounce/engagement |
runReport |
anything β custom dimensions + metrics + filters |
When in doubt, reach for runReport. The others are shortcuts for common questions.
π Chapter 2: Dimensions vs. Metrics¶
Every report is metrics (numbers) grouped by dimensions (attributes).
- Metrics:
activeUsers,sessions,screenPageViews,engagementRate,averageSessionDuration - Dimensions:
pagePath,pageTitle,sessionDefaultChannelGroup,country,deviceCategory,hostName,date
A report = "give me these metrics, broken down by these dimensions, for this date range."
π Chapter 3: Build Real Reports¶
Just ask your agent: "Top 10 pages by views over the last 28 days." Behind the scenes it calls runReport:
runReport({
dateRanges: [{ startDate: "28daysAgo", endDate: "today" }],
dimensions: [{ name: "pagePath" }],
metrics: [{ name: "screenPageViews" }, { name: "activeUsers" }],
orderBys: [{ metric: { metricName: "screenPageViews" }, desc: true }],
limit: 10,
})
Traffic by channel β where visitors come from:
runReport({
dateRanges: [{ startDate: "28daysAgo", endDate: "today" }],
dimensions: [{ name: "sessionDefaultChannelGroup" }],
metrics: [{ name: "sessions" }, { name: "engagementRate" }],
})
Week-over-week β two date ranges in one report:
dateRanges: [
{ startDate: "7daysAgo", endDate: "today", name: "cur" },
{ startDate: "14daysAgo", endDate: "8daysAgo", name: "prev" },
]
Filter to one slice β e.g. only one section of the site:
dimensionFilter: { filter: { fieldName: "pagePath",
stringFilter: { matchType: "BEGINS_WITH", value: "/quests/" } } }
π§ͺ Chapter 4: Sanity-Check Before You Trust¶
One habit pays for itself forever: before believing a headline number, group by hostName once. It instantly reveals whether localhost, Docker, or another site is mixed into your data β so you analyze production traffic, not your own dev machine. Add this filter to keep reports honest:
dimensionFilter: { filter: { fieldName: "hostName",
stringFilter: { matchType: "EXACT", value: "your-site.dev" } } }
β Validation¶
- You produced a top-pages report and a channel report
- You filtered a report by a dimension value
- You can explain when to use
runReportvs. the wrappers
π Rewards¶
- π Report Runner β you build reports on demand
- π Dimension Wrangler β dimensions and metrics are second nature
β‘οΈ Next Quest¶
Running reports by hand gets old. Put it on autopilot in Automate a Weekly Traffic Report.