hs_get_email_campaign_stats
Retrieve opens, clicks, bounces, and unsubscribes for a HubSpot marketing email using its ID.
Instructions
Get performance statistics (opens, clicks, bounces, unsubscribes) for a specific marketing email.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| emailId | Yes | HubSpot marketing email ID |
Implementation Reference
- src/tools/emails.ts:22-28 (handler)The actual handler function that executes the tool logic: fetches email details and statistics via HubSpot API, then returns combined result.
export async function getEmailCampaignStats(args: z.infer<typeof GetEmailCampaignStatsSchema>) { const [details, stats] = await Promise.all([ hubspot(`/marketing-emails/v1/emails/${args.emailId}`), hubspot(`/marketing-emails/v1/emails/${args.emailId}/statistics/summary`), ]); return { details, stats }; } - src/tools/emails.ts:18-20 (schema)Input schema for the tool, requiring a single 'emailId' (number) describing the HubSpot marketing email ID.
export const GetEmailCampaignStatsSchema = z.object({ emailId: z.number().int().describe("HubSpot marketing email ID"), }); - src/index.ts:225-230 (registration)Registration of the tool with the MCP server: name 'hs_get_email_campaign_stats', description, schema reference, and async handler calling getEmailCampaignStats.
server.tool( "hs_get_email_campaign_stats", "Get performance statistics (opens, clicks, bounces, unsubscribes) for a specific marketing email.", GetEmailCampaignStatsSchema.shape, async (args) => { try { return ok(await getEmailCampaignStats(args)); } catch (e) { return err(e); } }, ); - src/client.ts:16-48 (helper)Generic HTTP client ('hubspot') used by the handler to make GET requests to the HubSpot API.
export async function hubspot<T = unknown>( path: string, method: "GET" | "POST" | "PATCH" | "DELETE" = "GET", body?: unknown, params?: Record<string, string | number | boolean>, ): Promise<T> { const token = getToken(); let url = `${BASE_URL}${path}`; if (params && method === "GET") { const qs = new URLSearchParams( Object.entries(params).map(([k, v]) => [k, String(v)]), ).toString(); if (qs) url += `?${qs}`; } const res = await fetch(url, { method, headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, ...(body && method !== "GET" ? { body: JSON.stringify(body) } : {}), }); if (!res.ok) { const text = await res.text().catch(() => res.statusText); throw new HubSpotError(`HubSpot API error (${res.status}): ${text}`, res.status); } if (res.status === 204) return undefined as T; return (await res.json()) as T; }