4 min read

JavaScript Tracker API Reference

Complete client-side reference for window.trimstats commands, attribution snapshots, command queueing, and browser storage keys.

Imtiaz Rayhan
Imtiaz RayhanCo-Founder & Lead Engineer, DotCamp & TrimStats
Verified against live TrimStats APIs
Quick reference

What methods are exposed on the window.trimstats JavaScript tracker API?

TrimStats exposes window.trimstats("event", name, props) for custom events, window.trimstats("identify", userId) for linking logged-in users (up to 128 chars), window.trimstats("pageview") for manual virtual hits, and window.trimstats.attribution() returning the current AttributionSnapshot for checkout metadata.

window.trimstats commands

Once t.js initializes, it exposes a single global function at window.trimstats along with a helper method window.trimstats.attribution(). Always use optional chaining (window.trimstats?.(...)) so your application never throws if the script is blocked or not loaded in local development.

window.trimstats('event', name, props?)voidOptional

Dispatches a custom event beacon (t: "custom"). name is truncated to 120 characters. props is an optional flat object with string | number | boolean | null values.

window.trimstats('identify', userId)voidOptional

Associates subsequent events in the current page session with userId (truncated to 128 characters). Updates the uid field returned by window.trimstats.attribution().

window.trimstats.attribution()() => AttributionSnapshotOptional

Returns a shallow copy of the current attribution snapshot: { vid, uid, ref, lp, utm_source, utm_medium, utm_campaign }.

AttributionSnapshot return object

window.trimstats.attribution()json
1{
2 "vid": "f4c9a1e2-8b3d-4c11-9d2e-1a2b3c4d5e6f",
3 "uid": "user_12345",
4 "ref": "https://news.ycombinator.com/",
5 "lp": "/pricing",
6 "utm_source": "hackernews",
7 "utm_medium": "social",
8 "utm_campaign": "launch_2026"
9}

In default cookie-less mode, vid is an empty string (""), while ref, lp, and UTM fields reflect the initial landing page of the current document load. In first-party mode (data-mode="first-party"), vid and the first-touch snapshot persist across visits in localStorage.

Pre-load command queue (window.trimstats.q)

Because <script defer> downloads in the background without blocking HTML paint, a fast user or hydration effect can invoke identify() or event milliseconds before t.js finishes executing. Pushing calls onto window.trimstats.q eliminates that race condition: on initialization, t.js replays every queued call in FIFO order before sending the initial pageview.

Site analytics / Overview / Event Properties
Every flat key passed in window.trimstats('event', name, props) becomes an inspectable property tab in your dashboard automatically.
Inline queue stub (optional)html
1<script>
2 window.trimstats = window.trimstats || function () {
3 (window.trimstats.q = window.trimstats.q || []).push(arguments);
4 };
5 window.trimstats('identify', 'user_12345');
6</script>
7<script defer src="https://collect.trimstats.com/t.js" data-site="pk_live_your_key"></script>

Browser localStorage keys

KeyModePurpose
_ts_vidFirst-party mode onlyRandom UUID generated client-side to persist visitor identity across days
_ts_attrFirst-party mode onlyJSON-serialized first-touch attribution snapshot (ref, lp, utm_*)
trimstats_ignoreAny mode (manual)Set to "1" in DevTools to disable tracking in your own browser

Common technical questions

What fields does window.trimstats.attribution() return?
It returns an AttributionSnapshot containing the visitor ID (vid, when first-party mode is active), external user ID (uid, after identify()), initial referrer (ref), landing page (lp), and captured UTM parameters.
Which localStorage keys can TrimStats write when opt-in features are active?
Default cookie-less mode writes zero keys. Opt-in First-Party ID mode uses only _ts_vid and _ts_attr on your own origin, and developers can optionally set trimstats_ignore="1" to exclude their own browser.

Related guides