get-country-views
Analyze a WordPress site's traffic by country to gain geographic insights. Specify the site URL, credentials, and time period to retrieve detailed view statistics.
Instructions
View a site's views by country
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Maximum number of countries to return | |
| password | Yes | WordPress application password | |
| period | No | Time period for stats | |
| siteId | Yes | WordPress site ID | |
| siteUrl | Yes | WordPress site URL | |
| username | Yes | WordPress username |
Implementation Reference
- src/index.ts:1343-1379 (handler)The handler function that executes the 'get-country-views' tool. It makes an authenticated API request to the WordPress Jetpack Stats endpoint `/sites/{siteId}/stats/country-views`, processes the response data typed as WPCountryView[], formats it into a readable text summary, and returns it in the MCP content format. Handles errors gracefully.async ({ siteUrl, username, password, siteId, period = "week", limit = 10 }) => { try { const countryData = await makeWPRequest<{country_views: WPCountryView[]}>({ siteUrl, endpoint: `sites/${siteId}/stats/country-views`, auth: { username, password }, params: { period, limit } }); const countriesText = Array.isArray(countryData.country_views) && countryData.country_views.length > 0 ? countryData.country_views.map((country) => `${country.country_name || "Unknown"} (${country.country_code || "??"}) Views: ${country.views || 0} Percentage: ${country.views_percent ? Math.round(country.views_percent * 100) / 100 + '%' : "0%"} ---` ).join("\n") : "No country data found"; return { content: [ { type: "text", text: `Views by Country for site #${siteId} (${period}):\n\n${countriesText}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error retrieving country views: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }
- src/index.ts:1335-1342 (schema)Zod input schema defining parameters for the 'get-country-views' tool: siteUrl (required URL), username and password (auth), siteId (required number), optional period (enum: day/week/month/year), and limit (1-100).{ siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), siteId: z.number().describe("WordPress site ID"), period: z.enum(["day", "week", "month", "year"]).optional().describe("Time period for stats"), limit: z.number().min(1).max(100).optional().describe("Maximum number of countries to return"), },
- src/index.ts:1332-1334 (registration)Registration of the 'get-country-views' tool using McpServer.tool() method, specifying the tool name, description, input schema, and handler function.server.tool( "get-country-views", "View a site's views by country",
- src/index.ts:133-138 (helper)TypeScript interface WPCountryView used to type the country views data returned from the API in the handler.interface WPCountryView { country_code: string; country_name: string; views: number; views_percent: number; }