@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
- Link Clicks - Internal link clicks (path + link target path)
- Outbound Links - Clicks on external links
- File Downloads - Clicks on links to
.pdf, .zip, .doc, .csv, etc.
- 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 (e.g. scroll_depth, button_click). This makes them easy to filter in the dashboard and API.
Manual track() events default to eventSource=manual and eventSubtype=custom. 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>
Privacy
- Respects
Do Not Track browser setting
- No cookies - uses
localStorage for session/visitor IDs
opt_out() / opt_in() methods for user consent
- All data sent to your own server (self-hosted)
License
MIT