Installation
Requirements and install commands
Requirements
- Node.js
>=22.14.0 - An ESM project (
"type": "module") or a bundler. The package ships ESM only. CommonJS consumers need a dynamicimport().
Install
pnpm add tracklaneThat is everything the library needs. If you also want the visitor's consent answer to decide
which destinations run, @tracklane/consent is a separate install and the
library never learns it is there.
Two entry points
The browser and the server are two libraries. They share no code and no state, only the
vocabulary, which is what makes purchase mean the same thing on both sides. Import from the one
you are in:
| Import from | Where |
|---|---|
tracklane/browser | In the browser. Calls the vendor tags already on your page. |
tracklane/server | On your server. Calls each vendor's conversion API. |
tracklane | Types only, for writing your own provider. Has no runtime. |
Importing the server entry point into client code would pull Node built-ins into your bundle, which is why they are separate.
Vendor tags go on the page
This library does not install vendor tags. Each tag belongs on your page, put there the way its vendor documents: a snippet or a tag manager. We talk to the tag that is already there.
That is deliberate: injecting a third-party script is a decision about your content-security policy and your page weight, and a vendor's own configuration command emits a page view, so issuing a second one would quietly double your traffic.
Each provider page says exactly what its vendor expects on the page.
Verify
import { createTracking, ga4 } from 'tracklane/browser';
const { track } = createTracking({
providers: [ga4('G-XXXXXXX')],
onError: console.error,
});
track('page_view');If a tag is missing, the call fails and reaches onError rather than disappearing.
Open your browser's network tab and look for a request to google-analytics.com/g/collect. If it
is there with a 204, the tag is wired up.
A 204 means Google received the request, not that the event will appear in a report. Google
answers 204 to payloads it discards, including anything sent from a headless browser, which
it treats as a bot. Confirm in the Realtime report, not in the network tab.
Where to put it
createTracking returns plain functions and holds no state beyond the standing identity in the
browser. Create it once, at module scope, and import the functions where you need them:
// lib/tracking.ts
import { createTracking, ga4 } from 'tracklane/browser';
export const { track, identify, consent } = createTracking({
providers: [ga4('G-XXXXXXX')],
onError: (error) => console.error(error),
});On the server, do the same, but never carry user data across calls. A server instance is shared
by every request, so identity travels with each call, never in the instance. The server's
identify takes the person in its context, exactly as track does.
Errors
Nothing throws at the call site. One vendor failing must not break a checkout. Everything arrives
through onError:
onError: (error) => {
// error.provider which vendor
// error.event which event
// error.severity 'error': it did not arrive
// 'warning': it arrived, but something is worth knowing
// error.message what happened. Never the payload.
// error.cause the original throw, where there was one
report(error);
};The warning level exists because some vendors accept an event and then do nothing useful with
it. Those are the cases you want to see; without them, the only symptom is a report that stays
empty.
error.message never carries event contents. Events hold personal data by design, and that one
line is the surface you can pipe straight to a logger.
Why the vendor refused
A status code tells you a send failed. The vendor's own body tells you why, and that is what you
need at 3am. It travels on error.cause:
import { isVendorResponseError } from 'tracklane';
onError: (error) => {
if (isVendorResponseError(error.cause)) {
// error.cause.provider 'meta'
// error.cause.status 400
// error.cause.body what the vendor said, verbatim
}
report(error);
};Use isVendorResponseError rather than instanceof. Two copies of this package in one
node_modules produce two classes and one silent false; the guard reads a brand that survives
both.
body is the vendor's words, not ours, and some vendors quote the parameter they refused back
at you. Meta's Graph API does. So cause.body can contain event data where message never
does, and provider, status and body are ordinary properties: a logger you hand the whole
error to, and that serialises what it is given, takes the body with it. That is the decision
being handed to you rather than made for you. If you want the guarantee back, log
error.message and the fields you named.
Only a vendor's HTTP refusal carries one. A missing prerequisite the library cannot invent, like
Meta with no identifier at all to build user_data from, throws before any request exists, so
there is no response to carry.
Splitting one error into several in Sentry
Every refusal in a provider throws from the same line, so Sentry groups them all into one issue: its default grouping uses the stack trace, and a stack that never varies never splits. Two unrelated Meta failures land under one title, and the title shows whichever arrived last.
Put the vendor's own reason into the fingerprint and they separate:
// A refusal is not always JSON: a gateway between you and the vendor answers
// HTML, and a parse that throws here would take out your reporter.
const metaErrorCode = (body: string): string => {
try {
return String(JSON.parse(body)?.error?.code ?? 'unknown');
} catch {
return 'unparseable';
}
};
onError: (error) => {
const cause = error.cause;
if (!isVendorResponseError(cause)) return Sentry.captureException(error);
Sentry.captureException(cause, {
fingerprint: ['tracklane', cause.provider, String(cause.status), metaErrorCode(cause.body)],
});
};Sentry drops non-standard properties from an Error unless you add
extraErrorDataIntegration, so body does not reach it by accident. Add that integration if you
want the body on the issue, having read the warning above.