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
| Name | Required | Description | Default |
|---|---|---|---|
| endDate | Yes | End date in YYYY-MM-DD format | |
| startDate | Yes | Start 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"], }, },
- src/index.ts:352-369 (handler)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" }], }); }
- src/index.ts:97-127 (helper)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"); } }
- src/index.ts:47-68 (helper)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", ); } }