JavaScript Tracker API Reference
Complete client-side reference for window.trimstats commands, attribution snapshots, command queueing, and browser storage keys.
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?)voidOptionalDispatches 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)voidOptionalAssociates 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()() => AttributionSnapshotOptionalReturns a shallow copy of the current attribution snapshot: { vid, uid, ref, lp, utm_source, utm_medium, utm_campaign }.
AttributionSnapshot return object
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.
window.trimstats('event', name, props) becomes an inspectable property tab in your dashboard automatically.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
| Key | Mode | Purpose |
|---|---|---|
_ts_vid | First-party mode only | Random UUID generated client-side to persist visitor identity across days |
_ts_attr | First-party mode only | JSON-serialized first-touch attribution snapshot (ref, lp, utm_*) |
trimstats_ignore | Any mode (manual) | Set to "1" in DevTools to disable tracking in your own browser |
Common technical questions
What fields does window.trimstats.attribution() return?
Which localStorage keys can TrimStats write when opt-in features are active?
Related guides
Learn when to use custom events instead of pageviews, how to avoid event explosion with flat properties, and how to inspect property breakdowns.
Choose per site between zero-storage cookie-less analytics and persistent first-party visitor IDs, plus optional logged-in user identification.
HTTP specification for POST /v1/collect, payload validation rules, rate/quota responses, and server-side event ingestion.