Docs
← Back to site

Sales and signups from your server

1 min readUpdated November 11, 2026

Send the event from inside the buyer’s request (a route handler, a server action or an API route) so it joins their visit and the source that brought them gets the credit.

app/api/checkout/route.ts
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' },
  });
}

track() never throws: a stats hiccup can’t break a checkout or a signup. It returns false and passes the error to onError.

After the response#

With Next.js 15’s after(), the page never waits:

Route handler
import { trackAfter } from 'nahnu-analytics-sdk/next/server';
trackAfter(stats, request, website.id, 'Signup', { props: { plan: 'trial' } });

Not tied to a request#

A cron job or an import sends one event or a list (up to 50):

Cron job
await stats.events.send(website.id, {
  name: 'Renewal',
  id: invoice.id, // the same id within three days counts once
  revenue: { amount: 49, currency: 'USD' },
  // optional: props, url, ip, user_agent
});

The answer says how many were accepted, rejected and duplicates.

Stripe#

Checkout and webhook
import { stripeMetadata, fromStripe } from 'nahnu-analytics-sdk';

// Creating the Checkout Session, inside the buyer's request
await stripe.checkout.sessions.create({ /* … */ metadata: { ...stripeMetadata(request, website.id) } });

// Your Stripe webhook: paid checkouts → Purchase, renewals → Subscription renewed
const sale = fromStripe(event);
if (sale) await stats.events.send(sale.site ?? website.id, sale.event);

Amounts are converted from Stripe’s cents for each currency, including those with no decimals or three.

Shopify, and any other app#

Shopify orders/paid webhook
import { fromShopify } from 'nahnu-analytics-sdk';

const sale = fromShopify(order); // uses the buyer's IP and browser Shopify keeps; test orders → null
if (sale) await stats.events.send(website.id, sale);

For any other app, send { name, revenue, ip, user_agent }. With the buyer’s address and browser the sale joins their visit; without them it still counts, as its own visitor.

Tip

Give every event an id (your order number, a webhook’s event id). A webhook that is sent again is then not a second sale, and the SDK can safely retry it. fromStripe and fromShopify set it for you.

Was this helpful?