Reading the stats in your app
Fast: straight from Cloudflare#
For anything your users wait on (a dashboard in your app, a stats card), read from Cloudflare directly; WordPress isn’t involved. Your server makes a short-lived read token for the sites a user may see, and the reads go to Cloudflare with it.
const t = await stats.readToken({ client: user.id, ttl: 600, origins: ['https://app.example.com'] });
// → { token, expires, endpoint, sites } import { createReader } from 'nahnu-analytics-sdk/read';
const reader = createReader({ endpoint: t.endpoint, token: async () => (await fetch('/api/stats-token')).json() });
await reader.overview(siteId, { period: '30d', compare: 'previous' }); // totals, chart, top lists, goals
await reader.overview(siteId, { period: '7d', only: 'aggregate,timeseries' }); // only what a card shows: faster
await reader.report(siteId, 'source', { period: '12m', limit: 100 });
await reader.realtime(siteId);
await reader.summary(); // every site on the token
await reader.funnel(siteId, [{ kind: 'page', match: '/pricing' }, { kind: 'event', match: 'Signup' }]);
await reader.journey(siteId, ['/']);
await reader.extra(siteId, 'errors'); // also speed, clicks, sessions, retention, conversions, notfound - A token reads only the sites it names:
sites: [refs], orclient: reffor every site of that client, including ones added later. It can only read, and ends afterttlseconds (default an hour, at most 7 days). originslimits a token to your app’s pages in a browser.- No preflight: the browser sends the token in a
text/plainbody, so each read is one request. - Renewal: with
tokenas a function, the reader gets a new token a minute before the old one ends. - On the server,
stats.reader({ client: user.id })does the same with tokens made and renewed for you. - Revoking:
stats.rotateReadSecret()ends every token made before, within a minute.
Use the token’s site ids: t.sites maps your refs to them, and sites.upsert returns site.id.
Through the API#
Simpler, slower, and it counts against the rate limit:
await stats.stats(website.id, { period: '30d', compare: 'previous' });
await stats.report(website.id, 'source', { period: '12m', limit: 100 });
await stats.realtime(website.id);
await stats.query({ site_id: 'example.com', metrics: ['visitors'], date_range: '7d', dimensions: ['visit:source'] }); Periods: today, yesterday, 24h, 7d, 28d, 30d, 91d, month, 6mo, year, 12m, all, or custom with from and to (YYYY-MM-DD). Goals and funnels: stats.request('POST', '/sites/<id>/goals', { kind: 'event', match: 'Signup' }).
The dashboard in an iframe#
const { url, iframe } = await stats.sites.embed(website.id); // the same address each time
await stats.sites.removeEmbed(website.id); // turn it off Read-only, no sign-in. Pass { password } (8+ characters) to ask for one. For a signed-in portal with every report, use a sign-in link.
Usage, for billing your users#
const { sites, clients } = await stats.usage('month'); // or '30d', '12m', { from: '2026-10-01', to: '2026-10-31' }
// clients: [{ ref: 'user_42', sites: 3, visitors: 5120, pageviews: 18200 }, …] A client’s visitors are its sites’ visitors added up, so one person who visits two sites counts twice. Revenue is per site, in the site’s currency.
Was this helpful?
Thanks for your feedback!
✓ Feedback received. Thank you!



