get-stats-highlights
Retrieve key performance metrics for a WordPress site from the past seven days using site URL, username, password, and site ID for analysis and insights.
Instructions
Get highlight metrics for a WordPress site from the last seven days
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:1118-1165 (registration)Registration of the 'get-stats-highlights' tool using server.tool, including description, input schema, and handler function.server.tool( "get-stats-highlights", "Get highlight metrics for a WordPress site from the last seven days", { 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 highlights = await makeWPRequest<WPStatsHighlights>({ siteUrl, endpoint: `sites/${siteId}/stats/highlights`, auth: { username, password } }); const highlightsText = ` Stats Highlights for site #${siteId}: Period: ${highlights.period || "Last 7 days"} Views: ${highlights.views || 0} Visitors: ${highlights.visitors || 0} Likes: ${highlights.likes || 0} Comments: ${highlights.comments || 0} Followers: ${highlights.followers || 0} Posts: ${highlights.posts || 0} `.trim(); return { content: [ { type: "text", text: highlightsText, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error retrieving stats highlights: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/index.ts:1127-1164 (handler)Handler function that makes API request to Jetpack stats endpoint `/sites/{siteId}/stats/highlights`, formats the WPStatsHighlights data, and returns formatted text response.async ({ siteUrl, username, password, siteId }) => { try { const highlights = await makeWPRequest<WPStatsHighlights>({ siteUrl, endpoint: `sites/${siteId}/stats/highlights`, auth: { username, password } }); const highlightsText = ` Stats Highlights for site #${siteId}: Period: ${highlights.period || "Last 7 days"} Views: ${highlights.views || 0} Visitors: ${highlights.visitors || 0} Likes: ${highlights.likes || 0} Comments: ${highlights.comments || 0} Followers: ${highlights.followers || 0} Posts: ${highlights.posts || 0} `.trim(); return { content: [ { type: "text", text: highlightsText, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error retrieving stats highlights: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }
- src/index.ts:1121-1126 (schema)Zod input schema defining parameters for the tool.{ 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"), },
- src/index.ts:87-95 (schema)TypeScript interface defining the structure of stats highlights response data.interface WPStatsHighlights { views: number; visitors: number; likes: number; comments: number; followers: number; posts: number; period: string; }
- src/index.ts:158-194 (helper)Helper function used by the tool to make authenticated requests to WordPress REST API endpoints.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; } }