Skip to main content
Glama
ruchernchong

mcp-server-google-analytics

by ruchernchong

getUserBehavior

Retrieve user behavior metrics including session duration and bounce rate from Google Analytics 4 data for specified date ranges to analyze website engagement patterns.

Instructions

Get user behavior metrics like session duration and bounce rate

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
endDateYesEnd date in YYYY-MM-DD format
startDateYesStart date in YYYY-MM-DD format

Implementation Reference

  • src/index.ts:240-258 (registration)
    Registration of the 'getUserBehavior' tool, including name, description, and input schema.
    { name: "getUserBehavior", description: "Get user behavior metrics like session duration and bounce rate", 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", }, }, required: ["startDate", "endDate"], }, },
  • Handler implementation for 'getUserBehavior' tool. Extracts date range from arguments, validates it, and calls fetchAnalyticsData with specific metrics for user behavior (averageSessionDuration, bounceRate, sessionsPerUser) grouped by date.
    case "getUserBehavior": { const { startDate, endDate } = args as { startDate: string; endDate: string; }; validateDateRange(startDate, endDate); return fetchAnalyticsData({ dateRanges: [{ startDate, endDate }], metrics: [ { name: "averageSessionDuration" }, { name: "bounceRate" }, { name: "sessionsPerUser" }, ], dimensions: [{ name: "date" }], }); }
  • Shared helper function fetchAnalyticsData used by the getUserBehavior handler to execute the Google Analytics report request and return formatted results.
    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"); } }
  • Helper function validateDateRange used in the getUserBehavior handler to validate the input date range.
    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