logly_stats
Retrieve traffic totals for a site: pageviews, sessions, visitors, bounce rate, and average duration. Includes daily series and comparison with previous period.
Instructions
Traffic totals for a site — pageviews, sessions, visitors, bounce rate, average duration — plus a daily series and a comparison against the previous period.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| site | Yes | Logly site ID (slug). Call logly_list_sites to discover it. | |
| days | No | Days to look back: 7, 30 or 90. Defaults to 30. Ignored when 'from'/'to' are set. | |
| from | No | Range start, YYYY-MM-DD. Use together with 'to'. | |
| to | No | Range end, YYYY-MM-DD. Use together with 'from'. |
Implementation Reference
- index.js:60-66 (handler)The handler for the logly_stats tool calls loglyApi with the /api/sites/{site}/stats endpoint, passing the date range parameters (days, from, to) to retrieve traffic totals.
tool( "logly_stats", "Traffic totals for a site — pageviews, sessions, visitors, bounce rate, average duration — plus a daily series and a comparison against the previous period.", { site: siteArg, days: daysArg, from: fromArg, to: toArg }, ({ site, days, from, to }) => loglyApi(`/api/sites/${encodeURIComponent(site)}/stats`, range({ days, from, to })) ); - index.js:47-51 (schema)Zod schema definitions for the input parameters used by logly_stats: site (required string), days (optional positive int), from (optional string), to (optional string).
const siteArg = z.string().describe("Logly site ID (slug). Call logly_list_sites to discover it."); const daysArg = z.number().int().positive().optional() .describe("Days to look back: 7, 30 or 90. Defaults to 30. Ignored when 'from'/'to' are set."); const fromArg = z.string().optional().describe("Range start, YYYY-MM-DD. Use together with 'to'."); const toArg = z.string().optional().describe("Range end, YYYY-MM-DD. Use together with 'from'."); - index.js:37-45 (registration)Registration helper that wraps server.tool() with error handling. Invoked on line 60 to register 'logly_stats'.
function tool(name, description, shape, fn) { server.tool(name, description, shape, async (args) => { try { return { content: [{ type: "text", text: await fn(args || {}) }] }; } catch (e) { return { content: [{ type: "text", text: "Error: " + e.message }], isError: true }; } }); } - index.js:8-27 (helper)Helper function that makes authenticated API calls to Logly. Called by logly_stats handler to fetch statistics data.
async function loglyApi(path, params) { const key = process.env.LOGLY_API_KEY; if (!key) { throw new Error( "LOGLY_API_KEY is not set. Create one in Logly → Settings → API keys." ); } const url = new URL(BASE + path); for (const [k, v] of Object.entries(params || {})) { if (v !== undefined && v !== null && v !== "") url.searchParams.set(k, String(v)); } const res = await fetch(url, { headers: { Authorization: `Bearer ${key}`, Accept: "application/json" }, }); const text = await res.text(); if (!res.ok) { throw new Error(`Logly API ${res.status} on ${path}: ${text.slice(0, 300)}`); } return text; } - index.js:30-33 (helper)Helper function that normalizes date range parameters. Used by logly_stats to build query params for the API call.
function range({ days, from, to }) { if (from || to) return { from, to }; return { days: days ?? 30 }; }