Skip to main content
highseat/server records events from your own backend: a signup in a route handler, a payment in a webhook handler, a renewal in a job. It sends with the site secret, so the event can name the visitor it belongs to. Events are batched, sent in the background and retried when Highseat is busy or unreachable. Nothing in it throws.

Set it up

Install the highseat package, then set two variables on the server, both from your site’s Installation settings:
The site id starts with site_. The secret starts with hs_secret_ and lets its holder name any visitor, so keep it on the server and out of browser code. Without either variable, the tracker warns once and sends nothing.

Send an event

Pass the request you are handling. The event takes the visitor’s IP, user agent and hostname from it, so it gets their country and device:
In a Next.js server action, pass the headers instead:
track() takes the same names and properties as the browser’s, checked by the same Register declarations (see Track events). It returns at once. Events wait up to a second to share a request, at most 25 to one.

Visitors and users

An event counts as a visitor through its ip and userAgent, read from request or headers, or passed yourself. An explicit ip or userAgent wins over the request’s. userId is a user id from your own system, as identify() sets in the browser. It is stored as a string. An event no visitor caused, such as one from a job or a webhook, leaves the request out. With a userId it counts as that user, and without one as a visitor of its own:
Never forward the visitor of one request on an event about someone else, such as an admin acting on a customer’s account: the event would count the admin’s browser as the customer’s. The visitor’s IP sits in a different header on every host. The tracker reads cf-connecting-ip in a Cloudflare Worker, x-real-ip on Vercel and x-nf-client-connection-ip on Netlify. Anywhere else, set clientIp ("nginx", or a function that returns the IP) or pass ip with each event. Without either, an event carries no IP and no country, and the tracker warns once.

Hostnames

Highseat stores a server event only when its hostname is the site’s domain, one of its subdomains, or one of its Allowed hostnames in Site settings › General. The hostname comes from the event’s hostname, then the tracker’s hostname, then the request’s host. A job has no request, so give the tracker a hostname. An event under any other hostname, such as a preview deploy’s, is not stored and reaches onError as dropped with the reason foreign_hostname.

Properties are strings

Highseat stores every property value as a string, cut to 500 characters. A number arrives as "1" and a boolean as "false". A property set to undefined or null is left out. revenue is the exception: it keeps its integer amount in the currency’s smallest unit, so 4999 is $49.99.

Your own tracker

createTracker() returns a tracker with its own track(), flush() and close(). Use it for credentials or a fetch of your own, a logger for failures, or several sites in one process:
Each event takes request or headers, then ip, userAgent, userId, hostname, url, referrer, eventId, timestamp and waitUntil. A pageview also takes its url and referrer from the request. eventId is any id your system already holds, such as a job id or a webhook event id: a retry of the same event within three days is stored once. timestamp dates an event that happened earlier, up to a day ago.

Turn it off, or log instead

createTracker() never throws, so a bad setting cannot stop your server from starting. A site id or secret that cannot be right, such as a secret without its hs_secret_ prefix, turns the tracker off. It reaches onError once as an invalid error, with no events. An unknown clientIp preset and an upstream that is not a URL do the same. enabled: false turns the tracker off on purpose. It never reads HIGHSEAT_SITE or HIGHSEAT_SECRET, so a staging server stays quiet even with production credentials in its environment. dryRun sends nothing and logs each event as it would be sent, with its properties already turned into strings. true logs through console.info, and a function receives each event, for your own logger. A dry run needs no credentials and never reads the environment. It wins over enabled: false, so one setup covers both production and a development server. A setting you pass that cannot be right turns a dry run off too, with the same invalid error:
Each logged event holds name, eventId, properties and timestamp, plus revenue, userId, hostname, ip, userAgent, url and referrer when they are set.

Serverless functions and shutdown

A serverless function can stop as soon as it answers, before a batch leaves. Pass waitUntil, and the event is sent at once while the function stays alive until it lands:
In a Cloudflare Worker, pass waitUntil: promise => ctx.waitUntil(promise), and in a Netlify function context.waitUntil. A script that exits on its own awaits flush() first. A long-running server calls close() at shutdown, which sends what is queued. During an outage that can take minutes of retries, so give it a deadline inside your shutdown’s grace period:
Past the deadline, close() stops sending, reports the events not confirmed sent to onError, and resolves. A track() after close() sends nothing.

Failures

A batch Highseat answers with 429 or 5xx, or never answers, is retried with backoff, honoring Retry-After. Batches go one at a time, so the events behind a retry wait. Up to 1000 events wait during an outage, and a newer one is reported and not kept. onError receives each failure with a type: A retry Highseat recognises as already stored is not a failure.

Testing

highseat/testing gives your tests a tracker that keeps every event in memory and sends nothing, so a test checks what an action tracked without faking fetch or parsing requests. Write the code under test to take a Tracker from highseat/server, and pass it createTestTracker() in the test:
Each record holds the event’s name, its properties as you passed them, before they are turned into strings, and its options, an empty object when the call passed none. events lists every record, oldest first, getEvents(name) the records with one name, and reset() forgets them all between tests. The test tracker is typed by your Register, so in strict mode a misspelt event name fails to compile in the test as it does in your code. As with the real tracker, a track() after close() records nothing, until reset().

Without the package

Any language can send the same request:
The body is a JSON array of up to 25 events. Each event names event_name and hostname (or a page_url), and optionally event_id, user_id, ip, user_agent, page_url, referrer, properties and revenue. properties holds string values only, up to 30 keys. age is the milliseconds since the event happened, up to a day. The answer lists each refused event in rejections and each event it did not store in dropped, by its index in the batch.