Developers · SDK

Analytics for your own product, in a few lines of TypeScript.

nahnu-analytics-sdk sets up your users and their websites, records sales and signups from your server, counts your app, and shows the stats inside your product. Version 1.4.0, with TypeScript types included.

Terminalbash
npm install ./nahnu-analytics-sdk-1.4.0.tgz
lib/stats.tsTypeScript
import { createClient } from 'nahnu-analytics-sdk';

export const stats = createClient(); // reads NAHNU_ANALYTICS_URL and NAHNU_ANALYTICS_KEY

await stats.clients.upsert(user.id, { name: user.company });
const { site } = await stats.sites.upsert(website.id, {
  name: website.name, domains: [website.domain], client: user.id,
});
// site.snippet: the tracking code for your user's website

Install and authenticate

Two environment variables.

Make a platform key in wp-admin, set two variables on your server, and check the setup from your health check or settings screen.

  • Node 18 or later, or any runtime with fetch and Web Crypto: browsers, Deno, Bun and edge runtimes
  • A platform key works for all clients
  • 20,000 requests an hour per platform key
  • Keep the key on your server, never in a browser bundle or an app
Install and authenticate →
.envbash
NAHNU_ANALYTICS_URL=https://stats.example.com   # the WordPress site with the plugin
NAHNU_ANALYTICS_KEY=nak_…
Health checkTypeScript
const { ok, problem, version } = await stats.check();
// problem says what is wrong: the key, its type, or the address

Server-side events

Credit the source that sold.

Send a purchase from inside the buyer’s request and it joins their visit, so the ad or search that brought them gets the credit. Stripe and Shopify webhooks have helpers.

  • track() never throws, so a stats hiccup can’t break a checkout
  • fromStripe() and fromShopify() turn webhooks into sales
  • The same event id within 3 days counts once
  • Up to 50 events in one call from a cron job or import
More about server-side events →
app/api/checkout/route.tsTypeScript
import { stats } from '@/lib/stats';

export async function POST(request: Request) {
  // … create the order …
  await stats.track(request, website.id, 'Purchase', {
    revenue: { amount: 49, currency: 'USD' },
    props: { plan: 'pro' },
  });
}

Stats in your app

Fast reads, straight from Cloudflare.

For a dashboard or a stats card your users wait on, your server makes a short-lived read token and the reads go to Cloudflare with it. WordPress isn’t involved.

  • A token reads only the sites it names, and can only read
  • Ends after the time you set, at most 7 days
  • Limited to your app’s pages with origins
  • Reads with a token don’t count against the rate limit
Reading the stats in your app →
Your serverTypeScript
const t = await stats.readToken({ client: user.id, ttl: 600 });
BrowserTypeScript
import { createReader } from 'nahnu-analytics-sdk/read';

const reader = createReader({ endpoint: t.endpoint, token: t.token });
await reader.overview(siteId, { period: '30d', compare: 'previous' });
await reader.realtime(siteId);

Safe by design

Built so stats can’t break your product.

  • No broken checkouts

    track() never throws. It returns false and reports the error to onError.

  • Safe retries

    After a network error, a 429 or a 5xx, it tries again up to 2 times, only when nothing can be doubled.

  • No double sales

    The same event id within 3 days counts once, so a webhook sent again isn’t a second sale.

  • Clear errors

    Every other method throws an ApiError with the status, a code and a message.

  • Timeouts

    A request gives up after 15 seconds, or the time you set.

  • Rate limits

    Every answer carries X-RateLimit-Limit, -Remaining and -Reset.

Errors, retries and limits →

Entry points

Import only what you use.

nahnu-analytics-sdkcreateClient(): clients, users, sites, events, stats, read tokens and usage, plus stripeMetadata, fromStripe, fromShopify and ApiError
nahnu-analytics-sdk/next<Analytics siteKey />, track(), identify() and view() for Next.js
nahnu-analytics-sdk/next/servercreateProxy() to serve tracking from your own domain, and trackAfter()
nahnu-analytics-sdk/appcreateAppClient() for mobile and desktop apps, with an offline queue
nahnu-analytics-sdk/readcreateReader() for fast reads with a read token
Every method, in the docs →

Questions

SDK questions

Something else? Ask us.

Who is the SDK for?

A SaaS, website builder, host or app that gives its own users analytics. Agencies working in WordPress use the plugin and the free connector instead.

Which runtimes does it support?

Node 18 or later, and any runtime with fetch and Web Crypto: browsers, Deno, Bun and edge runtimes.

Does it cost extra?

No. The SDK comes with your license.

Can I use another language?

Yes. Everything the SDK does is plain HTTP, described in an OpenAPI 3.1 file in the package.

Where does the data live?

In your own Cloudflare account, like every other site on Nahnu Analytics.

Add analytics to your product.

Unlimited websites, every feature, the SDK included.