The Lightweight Tracking Script is a minimal (~2KB gzipped), dependency-free JavaScript library that enables web analytics collection on your websites. It automatically tracks pageviews and allows custom event tracking with automatic batching for optimal performance.
<head> or <body> tagpm-abc123-1704067200000)<script>
window.TRACKER_CODE = 'your-tracking-code-here';
</script>
<script src="https://your-analytics-domain.com/tracker.js" async></script>
Replace your-tracking-code-here with your actual tracking code from the dashboard.
If you need more control over the tracker initialization, you can specify additional options:
<script>
window.TRACKER_CODE = 'pm-abc123-1704067200000';
window.TRACKER_API = 'https://your-analytics-domain.com/api/v1/events';
window.TRACKER_DEBUG = true; // Enable debug logging
</script>
<script src="https://your-analytics-domain.com/tracker.js" async></script>
You can also use data attributes instead of window variables:
<script
src="https://your-analytics-domain.com/tracker.js"
async
data-tracking-code="pm-abc123-1704067200000"
data-api-url="https://your-analytics-domain.com/api/v1/events">
</script>
| Option | Type | Default | Description |
|---|---|---|---|
TRACKER_CODE |
string | (required) | Your unique tracking code from the dashboard |
TRACKER_API |
string | /api/v1/events |
API endpoint for sending events |
TRACKER_DEBUG |
boolean | false |
Enable debug logging in browser console |
<script>
window.TRACKER_CODE = 'pm-abc123-1704067200000';
</script>
<script src="https://analytics.example.com/tracker.js" async></script>
<script>
window.TRACKER_CODE = 'pm-abc123-1704067200000';
window.TRACKER_DEBUG = true;
</script>
<script src="https://analytics.example.com/tracker.js" async></script>
<script>
window.TRACKER_CODE = 'pm-abc123-1704067200000';
window.TRACKER_API = 'https://custom-api.example.com/events';
</script>
<script src="https://analytics.example.com/tracker.js" async></script>
Once installed, the script automatically tracks:
pushState and replaceState changes<a> tagsNo additional code is needed for basic tracking.
<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
<script>
window.TRACKER_CODE = 'pm-abc123-1704067200000';
</script>
<script src="https://analytics.example.com/tracker.js" async></script>
</head>
<body>
<h1>My Blog</h1>
<article>...</article>
<!-- Pageview is automatically tracked on load -->
<!-- Link clicks are automatically tracked -->
<a href="/about">About</a>
</body>
</html>
Use the PivotMetrics.track() function to send custom events:
// Simple custom event
PivotMetrics.track('custom', {
action: 'video_play',
videoId: '123',
duration: 45
});
PivotMetrics.track('custom', {
type: 'product_view',
productId: 'SKU-12345',
productName: 'Blue Widget',
category: 'Electronics',
price: 99.99
});
PivotMetrics.track('custom', {
type: 'add_to_cart',
productId: 'SKU-12345',
quantity: 2,
price: 199.98
});
PivotMetrics.track('custom', {
type: 'feature_used',
featureName: 'export_report',
userId: 'user-123',
duration: 30000 // milliseconds
});
// Video started
PivotMetrics.track('custom', {
type: 'video_event',
event: 'start',
videoId: 'video-123',
videoTitle: 'Tutorial: Getting Started'
});
// Video played 50%
PivotMetrics.track('custom', {
type: 'video_event',
event: 'progress',
videoId: 'video-123',
progress: 50
});
// Video completed
PivotMetrics.track('custom', {
type: 'video_event',
event: 'complete',
videoId: 'video-123',
watchTime: 600 // seconds
});
// Form submission
document.getElementById('contact-form').addEventListener('submit', function(e) {
e.preventDefault();
PivotMetrics.track('custom', {
type: 'form_submit',
formName: 'contact',
fields: ['name', 'email', 'message']
});
// Then submit the form
this.submit();
});
document.getElementById('signup-btn').addEventListener('click', function() {
PivotMetrics.track('custom', {
type: 'button_click',
buttonId: 'signup-btn',
buttonText: 'Sign Up',
location: 'hero_section'
});
});
The tracker automatically batches events for optimal performance:
This means you can call PivotMetrics.track() frequently without worrying about performance.
Force-send all queued events immediately:
// Send all queued events
PivotMetrics.flush();
Use cases:
The tracker automatically manages:
sessionStoragelocalStorageThese IDs are automatically sent with every event.
The tracker handles offline scenarios gracefully:
Enable detailed logging in the browser console:
<script>
window.TRACKER_CODE = 'pm-abc123-1704067200000';
window.TRACKER_DEBUG = true;
</script>
<script src="https://analytics.example.com/tracker.js" async></script>
Then open browser DevTools console (F12) to see:
[PivotMetrics] Tracker loaded { trackingCode: '...', sessionId: '...', visitorId: '...' }
[PivotMetrics] Event queued { eventType: 'pageview', url: 'https://...', ... }
[PivotMetrics] Events flushed { count: 2 }
Problem: Tracking code appears in HTML but no events are recorded
Solutions:
Problem: Script loads but events don’t appear in analytics dashboard
Solutions:
TRACKER_API)Problem: Events appear in dashboard after significant delay
Possible causes:
Solutions:
PivotMetrics.flush() to send events immediatelyProblem: Events appear multiple times in dashboard
Possible causes:
.track() calls plus automatic trackingSolutions:
TRACKER_CODE is set to single correct codeProblem: Browser memory increases significantly after installing tracker
Possible causes:
Solutions:
PivotMetrics.flush() periodically in long-running apps<!-- Good: Script loads asynchronously -->
<script src="https://analytics.example.com/tracker.js" async></script>
<!-- Avoid: Script blocks page load -->
<script src="https://analytics.example.com/tracker.js"></script>
// Good: Only necessary data
PivotMetrics.track('custom', {
type: 'button_click',
buttonId: 'signup'
});
// Avoid: Excessive nested data
PivotMetrics.track('custom', {
type: 'button_click',
user: { id: '123', name: 'John', email: 'john@example.com', ... },
page: { title: '...', url: '...', referrer: '...', ... },
device: { ... },
browserData: { ... }
});
// Good: Single track call with aggregated data
PivotMetrics.track('custom', {
type: 'page_metrics',
scrollDepth: 75,
timeOnPage: 45,
clicks: 3
});
// Avoid: Multiple track calls for related data
PivotMetrics.track('custom', { scrollDepth: 75 });
PivotMetrics.track('custom', { timeOnPage: 45 });
PivotMetrics.track('custom', { clicks: 3 });
// For SPAs that may not reload for hours
setInterval(() => {
PivotMetrics.flush();
}, 30000); // Flush every 30 seconds
By default, the tracker collects:
The tracker does NOT collect:
<script>
// Don't track users with DNT preference
if (navigator.doNotTrack === '1') {
console.log('User has DNT enabled, not tracking');
} else {
window.TRACKER_CODE = 'pm-abc123-1704067200000';
}
</script>
<script src="https://analytics.example.com/tracker.js" async></script>
// Don't track events on admin/settings pages
if (window.location.pathname.startsWith('/admin')) {
// Script is loaded but you can choose not to use it
window.SKIP_TRACKING = true;
}
Never send PII in custom events:
// Good: No PII
PivotMetrics.track('custom', {
type: 'user_action',
userId: 'user_hash_123' // Use hashed/anonymized ID
});
// Bad: Contains PII
PivotMetrics.track('custom', {
userName: 'John Doe',
userEmail: 'john@example.com',
userPhone: '555-1234'
});
The lightweight tracker is designed to be privacy-focused:
To ensure full compliance:
Example: Consent-based tracking
<script>
// Only load tracker if user consents
if (localStorage.getItem('analytics-consent') === 'true') {
window.TRACKER_CODE = 'pm-abc123-1704067200000';
}
</script>
<script src="https://analytics.example.com/tracker.js" async></script>
PivotMetrics.track(eventType, properties)Queue a custom event for sending.
Parameters:
eventType (string): Type of event (e.g., ‘custom’, ‘click’, ‘scroll’)properties (object, optional): Custom event dataExample:
PivotMetrics.track('custom', {
action: 'signup',
plan: 'premium'
});
PivotMetrics.flush()Immediately send all queued events.
Parameters: None
Example:
// Send events immediately
PivotMetrics.flush();
import { useEffect } from 'react';
export default function App() {
useEffect(() => {
// Initialize tracker on app load
window.TRACKER_CODE = process.env.NEXT_PUBLIC_TRACKING_CODE;
const script = document.createElement('script');
script.src = process.env.NEXT_PUBLIC_TRACKER_URL;
script.async = true;
document.head.appendChild(script);
}, []);
const trackEvent = (eventType, properties) => {
window.PivotMetrics?.track(eventType, properties);
};
return (
<button onClick={() => trackEvent('custom', { action: 'click' })}>
Track Event
</button>
);
}
<template>
<button @click="trackEvent">Track Event</button>
</template>
<script>
export default {
mounted() {
window.TRACKER_CODE = 'pm-abc123-1704067200000';
const script = document.createElement('script');
script.src = 'https://analytics.example.com/tracker.js';
script.async = true;
document.head.appendChild(script);
},
methods: {
trackEvent() {
window.PivotMetrics?.track('custom', { action: 'button_click' });
}
}
};
</script>
Add to theme’s functions.php:
add_action('wp_head', function() {
$tracking_code = 'pm-abc123-1704067200000'; // Get from settings
echo '<script>';
echo 'window.TRACKER_CODE = "' . esc_js($tracking_code) . '";';
echo '</script>';
echo '<script src="https://analytics.example.com/tracker.js" async></script>';
});
Or add directly to theme’s header.php:
<script>
window.TRACKER_CODE = "pm-abc123-1704067200000";
</script>
<script src="https://analytics.example.com/tracker.js" async></script>
For questions or issues, please contact support through the dashboard.