Skip to main content
Glama
ruchernchong

mcp-server-google-analytics

by ruchernchong

runReport

Generate Google Analytics 4 reports by specifying date ranges, metrics, dimensions, and filters to analyze website performance data.

Instructions

Run a report to get analytics data

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dimensionFilterNoFilter for dimensions
dimensionsYesDimensions to group by (e.g., page, country)
endDateYesEnd date in YYYY-MM-DD format
metricsYesMetrics to include in the report
startDateYesStart date in YYYY-MM-DD format

Implementation Reference

  • Executes the 'runReport' tool by parsing input arguments, validating date range, and invoking fetchAnalyticsData to run the Google Analytics report.
    case "runReport": { const { startDate, endDate, dimensions = [], metrics = [], dimensionFilter, } = args as { startDate: string; endDate: string; dimensions?: { name: string }[]; metrics?: { name: string }[]; dimensionFilter?: object; }; validateDateRange(startDate, endDate); return fetchAnalyticsData({ dateRanges: [{ startDate, endDate }], dimensions, metrics, ...(dimensionFilter && { dimensionFilter }), }); }
  • Core function that performs the actual Google Analytics Data API runReport call, formats the response as text content, and handles API errors.
    async function fetchAnalyticsData( reportConfig: Partial<Omit<RunReportRequest, "property">> & { dateRanges: RunReportRequest["dateRanges"]; dimensions?: RunReportRequest["dimensions"]; metrics?: RunReportRequest["metrics"]; }, ) { try { const [response] = await analyticsDataClient.runReport({ property: `properties/${propertyId}`, ...reportConfig, }); return { content: [ { type: "text", text: JSON.stringify(response, null, 2), }, ], }; } catch (error) { // Handle Google Analytics API errors if (error instanceof Error) { throw new McpError( ErrorCode.InternalError, `Google Analytics API error: ${error.message}`, ); } throw new McpError(ErrorCode.InternalError, "An unexpected error occurred"); } }
  • src/index.ts:133-176 (registration)
    Registers the 'runReport' tool in the MCP server's listTools response, providing name, description, and full input schema.
    { name: "runReport", description: "Run a report to get analytics data", inputSchema: { type: "object", properties: { startDate: { type: "string", description: "Start date in YYYY-MM-DD format", }, endDate: { type: "string", description: "End date in YYYY-MM-DD format", }, dimensions: { type: "array", items: { type: "object", properties: { name: { type: "string" }, }, required: ["name"], }, description: "Dimensions to group by (e.g., page, country)", }, metrics: { type: "array", items: { type: "object", properties: { name: { type: "string" }, }, required: ["name"], }, description: "Metrics to include in the report", }, dimensionFilter: { type: "object", description: "Filter for dimensions", }, }, required: ["startDate", "endDate", "metrics", "dimensions"], }, },
  • Helper function to validate the startDate and endDate parameters used in the runReport tool.
    function validateDateRange(startDate: string, endDate: string): void { if (!validateDateFormat(startDate)) { throw new McpError( ErrorCode.InvalidParams, `Invalid startDate format. Expected YYYY-MM-DD, got: ${startDate}`, ); } if (!validateDateFormat(endDate)) { throw new McpError( ErrorCode.InvalidParams, `Invalid endDate format. Expected YYYY-MM-DD, got: ${endDate}`, ); } if (new Date(startDate) > new Date(endDate)) { throw new McpError( ErrorCode.InvalidParams, "startDate cannot be after endDate", ); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ruchernchong/mcp-server-google-analytics'

If you have feedback or need assistance with the MCP directory API, please join our Discord server