Skip to main content
Glama
Shin-sibainu

GA4 MCP Server

by Shin-sibainu

get_exit_pages

Retrieve exit page analysis from Google Analytics 4 to identify where users leave your website. Analyze final page views to optimize user retention and reduce bounce rates.

Instructions

離脱ページ(ユーザーが最後に見たページ)の分析結果を取得します。

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
propertyIdNoGA4プロパティID
periodYes集計期間
limitNo取得件数(デフォルト: 10)

Implementation Reference

  • The core handler function that executes the get_exit_pages tool logic. It queries GA4 for pagePath dimensions with screenPageViews and sessions metrics, approximates exit rates, and returns a list of exit pages.
    export async function getExitPages(
      input: GetExitPagesInput
    ): Promise<GetExitPagesOutput> {
      const propertyId = getPropertyId(input.propertyId);
      const property = formatPropertyPath(propertyId);
      const dateRange = periodToDateRange(input.period);
      const limit = input.limit || 10;
    
      // GA4では直接exitPageディメンションがないため、
      // pagePath と screenPageViews を使って近似する
      const response = await executeReport({
        property,
        dateRanges: [dateRange],
        dimensions: [{ name: "pagePath" }],
        metrics: [{ name: "screenPageViews" }, { name: "sessions" }],
        orderBys: [{ metric: { metricName: "screenPageViews" }, desc: true }],
        limit,
      });
    
      // 合計セッション数を取得
      const totalSessions =
        response.totals?.[0]?.metricValues?.[1]?.value
          ? parseFloat(response.totals[0].metricValues[1].value)
          : 0;
    
      const exitPages: ExitPage[] = [];
    
      for (const row of response.rows || []) {
        const dimensionValues = row.dimensionValues || [];
        const metricValues = row.metricValues || [];
    
        const pageViews = metricValues[0]?.value
          ? Math.round(parseFloat(metricValues[0].value))
          : 0;
    
        const sessions = metricValues[1]?.value
          ? Math.round(parseFloat(metricValues[1].value))
          : 0;
    
        // 離脱数をセッション数で近似(実際の離脱数とは異なる可能性あり)
        const exits = sessions;
    
        exitPages.push({
          pagePath: dimensionValues[0]?.value || "",
          exits,
          exitRate: calculatePercentage(exits, totalSessions),
        });
      }
    
      return { exitPages };
    }
  • TypeScript interfaces defining the input schema (GetExitPagesInput), data structure (ExitPage), and output schema (GetExitPagesOutput) for the get_exit_pages tool.
    // get_exit_pages
    export interface GetExitPagesInput extends PropertyId {
      period: ShortPeriod;
      limit?: number;
    }
    
    export interface ExitPage {
      pagePath: string;
      exits: number;
      exitRate: string;
    }
    
    export interface GetExitPagesOutput {
      exitPages: ExitPage[];
    }
  • src/server.ts:378-397 (registration)
    Registers the get_exit_pages tool in the MCP server's tools list, defining its name, description, and input schema.
    {
      name: "get_exit_pages",
      description: "離脱ページ(ユーザーが最後に見たページ)の分析結果を取得します。",
      inputSchema: {
        type: "object" as const,
        properties: {
          propertyId: { type: "string", description: "GA4プロパティID" },
          period: {
            type: "string",
            enum: ["7days", "28days", "30days"],
            description: "集計期間",
          },
          limit: {
            type: "number",
            description: "取得件数(デフォルト: 10)",
          },
        },
        required: ["period"],
      },
    },
  • src/server.ts:695-700 (registration)
    In the tool dispatch switch statement, handles calls to get_exit_pages by invoking the getExitPages handler with parsed arguments.
    case "get_exit_pages":
      return await getExitPages({
        propertyId: args.propertyId as string | undefined,
        period: args.period as "7days" | "28days" | "30days",
        limit: args.limit as number | undefined,
      });

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/Shin-sibainu/ga4-mcp-server'

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