Track events

Outbound links & downloads

See which external links and downloadable files your visitors click. One delegated listener covers your whole site.

Outbound link clicks

Listen for clicks on any link, and fire an event when the destination is a different host:

document.addEventListener('click', function (e) {
  const a = e.target.closest('a');
  if (!a || !a.href) return;
  const url = new URL(a.href, location.href);
  if (url.host !== location.host) {
    window.servoki.track('outbound', { host: url.host });
  }
});

Because the click and the navigation happen together, the event is sent with sendBeacon and reliably leaves the page even as the browser navigates away.

File downloads

Match links that point at downloadable files by extension:

const FILE_RE = /\.(pdf|zip|dmg|csv|xlsx?|docx?|pptx?|mp4|mp3)($|\?)/i;
document.addEventListener('click', function (e) {
  const a = e.target.closest('a');
  if (a && a.href && FILE_RE.test(a.href)) {
    const name = new URL(a.href, location.href).pathname.split('/').pop();
    window.servoki.track('download', { file: name });
  }
});

Combine both

You can keep them in one listener — check the file pattern first, then fall back to the outbound check. Both are cheap and run only on actual clicks.

Tip: the host and file properties let you rank your top outbound destinations and most-downloaded files directly in the dashboard.