Browser script
The client-side tracker: SPA support, custom events, leave events.
The browser script is the default source created with every new site. It
targets under 1.5 kB gzipped, has zero dependencies, and uses no cookies,
localStorage or sessionStorage.
Install
<script
defer
src="https://cdn.metrikstack.com/js/script.js"
data-site="SITE_ID"
></script>See Quickstart for the full walkthrough, including proxying the script through your own domain.
What it tracks
On load, and on every SPA navigation, it sends:
path,referrer- screen size, language
utm_*query parameters (everything else is stripped)- the merge
request_id, if one is present (see How merging works) - the
navigator.globalPrivacyControlflag (gpc), recorded but never used to drop the event
On visibilitychange / pagehide it sends a leave event carrying time on
page and scroll depth, via sendBeacon so it survives the tab closing.
SPA support
The script patches history.pushState and listens for popstate, so
client-side route changes in React/Vue/Svelte-style SPAs are tracked as
pageviews without any extra integration code.
Custom events
<script>
window.metrikstack.track('signup', { plan: 'pro' });
</script>track(name, props?) sends a custom event with the given name and an
optional flat object of string properties, associated with the current page
and merge request_id.
Errors
A second, opt-in script tag adds uncaught-exception capture on top of this
one: data-errors plus a captureException helper, grouped into issues in
the dashboard. It ships as a separate file so sites that don't want it pay
nothing for it. See Errors for the tag, the API and what is
and isn't collected.
Local development
By default the script does nothing on localhost or file: origins, so
local testing doesn't create noise in production data. Opt back in with:
<script
defer
src="https://cdn.metrikstack.com/js/script.js"
data-site="SITE_ID"
data-allow-local
></script>Reading the merge request id
If your server or edge already sets a request id (see How merging
works), the script picks it up automatically: first from
<meta name="metrikstack-request-id">, falling back to
performance.getEntriesByType('navigation')[0].serverTiming (which survives
CDNs that strip response headers before they reach the browser but pass
Server-Timing through). No configuration is required on the script's side.