get_traffic_summary
Retrieve Google Analytics 4 traffic summaries including page views, users, sessions, and bounce rates for specified periods to monitor website performance.
Instructions
指定期間のトラフィック概要(PV数、ユーザー数、セッション数、直帰率など)を取得します。ダッシュボード的な使い方に最適です。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| propertyId | No | GA4プロパティID | |
| period | Yes | 集計期間 |
Implementation Reference
- The main handler function that executes the get_traffic_summary tool. It queries GA4 for key traffic metrics using executeReport and formats them into a summary output including page views, users, sessions, bounce rate, etc.export async function getTrafficSummary( input: GetTrafficSummaryInput ): Promise<TrafficSummaryOutput> { const propertyId = getPropertyId(input.propertyId); const property = formatPropertyPath(propertyId); const dateRange = periodToDateRange(input.period); const response = await executeReport({ property, dateRanges: [dateRange], metrics: [ { name: "screenPageViews" }, { name: "totalUsers" }, { name: "sessions" }, { name: "newUsers" }, { name: "averageSessionDuration" }, { name: "bounceRate" }, { name: "engagementRate" }, { name: "screenPageViewsPerSession" }, ], }); // 結果からメトリクス値を取得 const metricValues = response.totals?.[0]?.metricValues || []; const getValue = (index: number): number => { const value = metricValues[index]?.value; return value ? parseFloat(value) : 0; }; return { totalPageViews: Math.round(getValue(0)), totalUsers: Math.round(getValue(1)), totalSessions: Math.round(getValue(2)), newUsers: Math.round(getValue(3)), avgSessionDuration: formatDuration(getValue(4)), bounceRate: formatPercentageFromDecimal(getValue(5)), engagementRate: formatPercentageFromDecimal(getValue(6)), pagesPerSession: roundToDecimal(getValue(7)), period: dateRange, }; }
- src/types.ts:110-128 (schema)Type definitions for the tool's input (GetTrafficSummaryInput: propertyId optional, period required) and output (TrafficSummaryOutput: structured traffic metrics).// get_traffic_summary export interface GetTrafficSummaryInput extends PropertyId { period: Period; } export interface TrafficSummaryOutput { totalPageViews: number; totalUsers: number; totalSessions: number; newUsers: number; avgSessionDuration: string; bounceRate: string; engagementRate: string; pagesPerSession: number; period: { startDate: string; endDate: string; }; }
- src/server.ts:204-220 (registration)Registers the tool in the MCP server's tools array with name, description, and input schema definition.{ name: "get_traffic_summary", description: "指定期間のトラフィック概要(PV数、ユーザー数、セッション数、直帰率など)を取得します。ダッシュボード的な使い方に最適です。", inputSchema: { type: "object" as const, properties: { propertyId: { type: "string", description: "GA4プロパティID" }, period: { type: "string", enum: ["today", "yesterday", "7days", "28days", "30days", "90days"], description: "集計期間", }, }, required: ["period"], }, },
- src/server.ts:620-631 (registration)In the handleToolCall function's switch statement, dispatches calls to the getTrafficSummary handler with parsed arguments.case "get_traffic_summary": return await getTrafficSummary({ propertyId: args.propertyId as string | undefined, period: args.period as | "today" | "yesterday" | "7days" | "28days" | "30days" | "90days", });
- src/tools/analytics/index.ts:1-1 (helper)Re-exports the getTrafficSummary handler from its module for convenient import in server.ts.export { getTrafficSummary } from "./getTrafficSummary.js";