get-stats-summary
Access summarized statistics for views, visitors, likes, and comments on a WordPress site by providing site URL, ID, username, and application password.
Instructions
View a site's summarized views, visitors, likes and comments
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| password | Yes | WordPress application password | |
| siteId | Yes | WordPress site ID | |
| siteUrl | Yes | WordPress site URL | |
| username | Yes | WordPress username |
Implementation Reference
- src/index.ts:1169-1224 (handler)Full implementation of the get-stats-summary tool, including registration, input schema, and handler logic. Fetches summarized views, visitors, likes, and comments from the WordPress stats API endpoint `sites/{siteId}/stats/summary` and formats the data into a readable text response."get-stats-summary", "View a site's summarized views, visitors, likes and comments", { 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"), }, async ({ siteUrl, username, password, siteId }) => { try { const summary = await makeWPRequest<WPStatsSummary>({ siteUrl, endpoint: `sites/${siteId}/stats/summary`, auth: { username, password } }); const summaryText = ` Stats Summary for site #${siteId}: Views: Total: ${summary.views?.total || 0} ${summary.views?.fields?.map(f => `${f.period}: ${f.value}`).join('\n') || "No data"} Visitors: Total: ${summary.visitors?.total || 0} ${summary.visitors?.fields?.map(f => `${f.period}: ${f.value}`).join('\n') || "No data"} Likes: Total: ${summary.likes?.total || 0} ${summary.likes?.fields?.map(f => `${f.period}: ${f.value}`).join('\n') || "No data"} Comments: Total: ${summary.comments?.total || 0} ${summary.comments?.fields?.map(f => `${f.period}: ${f.value}`).join('\n') || "No data"} `.trim(); return { content: [ { type: "text", text: summaryText, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error retrieving stats summary: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/index.ts:97-114 (schema)TypeScript interface defining the structure of the WPStatsSummary response used by the get-stats-summary tool handler.interface WPStatsSummary { visitors: { total: number; fields: Array<{period: string; value: number}>; }; views: { total: number; fields: Array<{period: string; value: number}>; }; likes: { total: number; fields: Array<{period: string; value: number}>; }; comments: { total: number; fields: Array<{period: string; value: number}>; }; }
- src/index.ts:158-194 (helper)Helper function makeWPRequest used by the get-stats-summary handler to make authenticated requests to the WordPress REST API.async function makeWPRequest<T>({ siteUrl, endpoint, method = 'GET', auth, data = null, params = null }: { siteUrl: string; endpoint: string; method?: 'GET' | 'POST' | 'PUT' | 'DELETE'; auth: { username: string; password: string }; data?: any; params?: any; }): Promise<T> { const authString = Buffer.from(`${auth.username}:${auth.password}`).toString('base64'); try { const response = await axios({ method, url: `${siteUrl}/wp-json/wp/v2/${endpoint}`, headers: { 'Authorization': `Basic ${authString}`, 'Content-Type': 'application/json', }, data: data, params: params }); return response.data as T; } catch (error) { if (axios.isAxiosError(error) && error.response) { throw new Error(`WordPress API error: ${error.response.data?.message || error.message}`); } throw error; } }