Skip to content
metryo

05Custom events

Custom events: track the actions that matter

Pageviews are automatic on every plan. For everything else — signups, purchases, downloads — call metryo.event() from anywhere in your code. Available on all plans, including Free.

From JavaScript

anywhere.js
// Track any action with one call — from an onclick,
// a form submit, or anywhere in your code.
metryo.event("Signup completed");

// Attach properties: strings, numbers and booleans only.
// Up to 20 per event, names trimmed to 60 characters and
// values to 300. Arrays, nested objects, NaN and Infinity
// are dropped before the request is sent.
metryo.event("Checkout started", {
  plan: "growth",
  value: 15,
});

From HTML

index.html
<button onclick="metryo.event('Pricing CTA clicked')">
  Start for free
</button>

Single-page apps

spa.js
// Mostly nothing to do. The script hooks pushState and
// popstate, so router.push() and the back button are
// tracked automatically — query string included, so
// ?page=2 counts as its own pageview.
//
// history.replaceState is treated differently on purpose:
// it counts only when the PATH changes. Apps also use it
// to write filters and tabs into the query on every
// keystroke, and counting those would put a pageview on
// each letter typed into a search box.
//
// So if you paginate or filter with router.replace(),
// call this yourself at the point you know it is a real
// navigation:
metryo.pageview();

Calling before the script loads

index.html
<!-- Only needed if you call metryo.event() before the
     script has finished loading — with defer, async, or a
     tag manager. Put this above the script tag. -->
<script>
  window.metryo = window.metryo || {
    q: [],
    event: function (n, p) { this.q.push(["event", n, p]); },
    pageview: function () { this.q.push(["pageview"]); }
  };
</script>