A lightweight, privacy-focused web analytics solution that provides real-time tracking of page views, clicks, form submissions, and user engagement without relying on external services.
- Page View Tracking - Monitor visitor traffic across your website
- Click Event Tracking - Track button clicks, link clicks, and user interactions
- Form Submission Tracking - Monitor contact forms and user submissions
- Time on Page - Measure user engagement and session duration
- Real-time Dashboard - Live analytics with charts and statistics
- Privacy-First - All data stored locally, no external dependencies
- CORS Support - Works with local development and production environments
- Cookie-based User IDs - Track unique visitors while respecting privacy
- Node.js (v14 or higher)
- npm or yarn
-
Clone or download this repository
git clone <your-repo-url> cd tiny-tracker
-
Install dependencies
npm install
-
Start the server
npm start
-
Visit your analytics dashboard
- Open http://localhost:8080 in your browser
- Navigate between pages to generate tracking data
- View analytics at http://localhost:8080/stats
tiny-tracker/
βββ server.js # Main server application
βββ events.db # SQLite database (auto-created)
βββ package.json # Node.js dependencies
βββ website/ # Demo website with tracking
β βββ index.html # Homepage
β βββ about.html # About page
β βββ products.html # Products page
β βββ contact.html # Contact page
β βββ tracker.js # Tracking JavaScript library
βββ README.md # This file
- Express Server - Serves your website and handles tracking requests
- SQLite Database - Stores all analytics data locally
- CORS Configuration - Allows cross-origin requests for local development
- Static File Serving - Serves your website files from the
/website
directory
- Automatic Page Views - Tracks when users visit pages
- Click Tracking - Monitors interactions with tracked elements
- User Identification - Uses cookies to identify unique visitors
- Real-time Data - Events are captured and stored immediately
Add this to your HTML pages:
<!-- Add before closing </body> tag -->
<script src="http://localhost:8080/tracker.js"></script>
<script>
(function() {
// Generate or retrieve user ID
function getUid() {
const match = document.cookie.match(/(?:^|;)\s*tt_uid=([^;]+)/);
if (match) return match[1];
const uid = Math.random().toString(36).slice(2, 18);
document.cookie = `tt_uid=${uid};path=/;max-age=${60*60*24*365*2}`;
return uid;
}
// Send tracking event
function trackEvent(data = {}) {
const payload = {
url: location.href,
ref: document.referrer || "",
uid: getUid(),
...data
};
fetch('http://localhost:8080/event', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
credentials: 'include'
});
}
// Track page view
trackEvent({ event_type: 'page_view' });
})();
</script>
Add data-track-event
attributes to any element:
<!-- Track button clicks -->
<button data-track-event="hero-signup">Sign Up Now</button>
<!-- Track link clicks -->
<a href="/pricing" data-track-event="pricing-link">View Pricing</a>
<!-- Track form submissions -->
<form data-track-event="contact-form">
<!-- form fields -->
<button type="submit">Submit</button>
</form>
// Track custom events
window.tracker.trackEvent({
event_type: 'click',
event_name: 'custom-button-click',
element_tag: 'button',
element_text: 'Custom Action'
});
Visit http://localhost:8080/stats
to view:
- Total Events - Overall activity count
- Unique Visitors - Number of unique users
- Event Types - Breakdown by page views, clicks, etc.
- Top Click Events - Most popular interactions
- Top URLs - Most visited pages
- Hourly Chart - Activity timeline
- Time Filters - View data for 1h, 24h, or 7 days
- Real-time Updates - Refresh to see new data
- Event Breakdown - Detailed analytics by type
- Visual Charts - Interactive timeline visualization
Edit server.js
to customize:
// Change server port
const PORT = process.env.PORT || 3000;
// Update CORS origins for production
origin: function (origin, callback) {
// Add your production domain
if (origin === 'https://yourdomain.com') {
return callback(null, true);
}
// ... existing logic
}
Edit website/tracker.js
to customize:
const TRACKER_CONFIG = {
endpoint: 'http://your-domain.com/event', // Your tracking endpoint
cookieName: 'your_uid', // Custom cookie name
cookieMaxAge: 60 * 60 * 24 * 365 * 2 // Cookie expiration
};
The SQLite database stores events with the following fields:
Field | Type | Description |
---|---|---|
id |
INTEGER | Primary key |
ts |
INTEGER | Unix timestamp |
ip |
TEXT | Client IP address |
ua |
TEXT | User agent |
url |
TEXT | Page URL |
ref |
TEXT | Referrer URL |
uid |
TEXT | User ID (cookie) |
kind |
TEXT | 'pixel' or 'beacon' |
event_type |
TEXT | 'page_view', 'click', 'form_submit' |
event_name |
TEXT | Custom event name |
element_tag |
TEXT | HTML tag name |
element_text |
TEXT | Element text content |
link_url |
TEXT | Link destination |
button_type |
TEXT | Button type |
form_id |
TEXT | Form identifier |
duration |
INTEGER | Time on page (ms) |
client_timestamp |
INTEGER | Client-side timestamp |
- Local Data Storage - All data stays on your server
- No External Tracking - No third-party analytics services
- Minimal Data Collection - Only essential metrics
- Cookie Consent Ready - Easy to integrate with consent systems
- GDPR Friendly - Data stays under your control
# Set production port
export PORT=8080
# Start with PM2 for production
npm install -g pm2
pm2 start server.js --name tiny-tracker
// In server.js, restrict origins for production
origin: function (origin, callback) {
const allowedOrigins = [
'https://yourdomain.com',
'https://www.yourdomain.com'
];
if (!origin || allowedOrigins.includes(origin)) {
return callback(null, true);
}
return callback(new Error('Not allowed by CORS'));
}
// In website/tracker.js
const TRACKER_CONFIG = {
endpoint: 'https://analytics.yourdomain.com/event',
// ... other config
};
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
ISC License - feel free to use this project for personal or commercial purposes.
CORS Errors
- Ensure your domain is allowed in the CORS configuration
- Check that credentials are properly configured
No Data Appearing
- Verify the server is running on the correct port
- Check browser console for JavaScript errors
- Ensure the tracking script is loaded properly
Database Issues
- The SQLite database is created automatically
- Check file permissions in the project directory
- Check the browser console for error messages
- Verify network requests are reaching
/event
endpoint - Test the tracking with simple page loads first
Happy Tracking! πβ¨