Install and authenticate
Install#
Terminal
npm install ./nahnu-analytics-sdk-1.4.0.tgz It needs Node 18 or later, or any runtime with fetch and Web Crypto (browsers, Deno, Bun, edge runtimes).
Make a platform key#
In wp-admin: Nahnu Analytics › Data & API › API keys › new key with Can: Platform. It works for all clients and allows 20,000 requests an hour.
Warning
Keep the key on your server only, never in a browser bundle or an app.
.env
NAHNU_ANALYTICS_URL=https://stats.example.com # the WordPress site with the plugin
NAHNU_ANALYTICS_KEY=nak_… Create a client#
lib/stats.ts
import { createClient } from 'nahnu-analytics-sdk';
export const stats = createClient(); // reads the two variables above
// Or pass everything yourself:
export const statsEu = createClient({
url: 'https://stats.example.com',
key: process.env.STATS_KEY,
timeout: 15000, // ms before a request gives up
retries: 2, // after a network error, 429 or 5xx, when it can't double anything
onError: (e) => logger.warn(e), // where track() reports failures
}); | Option | Default | What it does |
|---|---|---|
url | NAHNU_ANALYTICS_URL | The WordPress site with the plugin |
key | NAHNU_ANALYTICS_KEY | A platform key (nak_…) |
restBase | url + '/wp-json' | Without pretty permalinks: https://example.com/?rest_route= |
timeout | 15000 | Milliseconds before a request gives up |
retries | 2 | Tries again only when that can’t double anything |
trust | from the environment | Which header carries the visitor’s address, for track(request) |
onError | console.warn | Where track() reports a failure |
fetch | global fetch | Your own fetch |
Check the setup#
For a health check or your settings screen:
Health check
const { ok, problem, version } = await stats.check();
// problem says what is wrong: the key, a key that is not a platform key, or the address Next: Your users and their sites.
Was this helpful?
Thanks for your feedback!
✓ Feedback received. Thank you!



