@litemetrics/tracker
Lightweight browser analytics tracker for Litemetrics. ~3.5KB gzipped.
Installation
npm install @litemetrics/tracker
Or use the script tag (no build step needed):
<script src="https://your-server.com/litemetrics.js"></script>
<script>
Litemetrics.createTracker({
siteId: 'your-site-id',
endpoint: 'https://your-server.com/api/collect',
});
</script>
Usage
import { createTracker } from '@litemetrics/tracker';
const tracker = createTracker({
siteId: 'your-site-id',
endpoint: 'https://your-server.com/api/collect',
});
tracker.track('Signup', { plan: 'pro' });
tracker.identify('user-123', { name: 'John', email: 'john@example.com' });
tracker.page();
Auto-Tracking
When autoTrack is enabled (default), the tracker automatically captures:
- Pageviews - Initial page load
- SPA Navigation - Route changes via History API
- Data Attributes - Clicks on
data-litemetrics-event="EventName" elements.
A click on such an element, or anywhere inside one, is recorded only as the
declared event: the auto link/button click events below are suppressed for it.
An empty label (data-litemetrics-event="") counts as unlabelled and keeps auto capture.
- Link Clicks - Internal link clicks (path + link target path + text + selector)
- Outbound Links - Clicks on external links (destination host + path + query, text + selector)
- File Downloads - Clicks on links to
.pdf, .zip, .doc, .csv, etc. (text + selector)
- Button Clicks - Button clicks (text + selector)
- Scroll Depth - Milestones at 25%, 50%, 75%, 90%
- Rage Clicks - 3+ rapid clicks in the same area
Auto events are tagged with eventSource=auto and an eventSubtype - one of link_click, outbound_click, file_download, button_click, scroll_depth, rage_click. This makes them easy to filter in the dashboard and API.
Manual track() events default to eventSource=manual and eventSubtype=custom; data-attribute clicks are also eventSource=manual, with eventSubtype=attribute. Older events collected before this change may have eventSource set to null.
Configuration
createTracker({
siteId: 'your-site-id',
endpoint: '/api/collect',
autoTrack: true,
autoSpa: true,
autoOutbound: true,
autoLinkClicks: true,
autoFileDownloads: true,
autoButtonClicks: true,
autoScrollDepth: true,
autoRageClicks: true,
batchSize: 10,
flushInterval: 5000,
respectDnt: true,
debug: false,
});
Data Attribute Tracking
Track events declaratively with HTML attributes:
<button
data-litemetrics-event="Add to Cart"
data-litemetrics-event-product="T-Shirt"
data-litemetrics-event-price="29.99"
>
Add to Cart
</button>
Bot Short-Circuit
When navigator.webdriver === true, createTracker returns a no-op tracker —
track, page, identify, opt_in, and opt_out all become safe no-ops and
no network requests are made. This catches Selenium, Puppeteer, and Playwright
at the source so synthetic events never reach your collector. Server-side bot
filtering (@litemetrics/node) still handles bots that mask webdriver.
Privacy
- Respects
Do Not Track browser setting
- No cookies set - uses
localStorage for session/visitor IDs. The one cookie read (never set) is Meta's _fbp, and only for visitors who landed with an ad click ID (gclid/gbraid/wbraid/fbclid) — a visitor who never clicked an ad sends no cookie value
- Ad click IDs are kept for 90 days (conversion windows) and dropped by
reset()
opt_out() / opt_in() methods for user consent
- All data sent to your own server (self-hosted)
License
MIT