A comprehensive JavaScript SDK for real-time user analytics and event tracking with Node.js backend support.
- Real-time Event Tracking: Track clicks, navigation, scroll, inputs, errors, and custom events
- Smart Element Detection: Automatically track elements with special attributes or classes - no JavaScript required!
- Automatic Session Management: Handles user sessions with intelligent timeout and persistence
- Privacy-First Design: Built-in GDPR compliance with consent management and data anonymization
- Offline Support: Queue events when offline and automatically sync when connection is restored
- Web Worker Support: Process events in background threads for better performance
- Batch Processing: Efficient event batching with configurable intervals and retry logic
- Real-time Dashboard: Live analytics dashboard with charts and metrics
- Multi-tenant Architecture: Support for multiple applications and organizations
- High Performance: Optimized for minimal impact on application performance
npm install @realtime/analytics-sdkOr include via CDN:
<script src="https://cdn.jsdelivr.net/npm/@realtime/analytics-sdk/dist/index.js"></script>git clone https://github.com/your-org/realtime-analytics.git
cd realtime-analytics/server
npm installimport { RealtimeAnalytics } from '@realtime/analytics-sdk';
// Initialize the SDK
const analytics = new RealtimeAnalytics({
apiKey: 'your-api-key',
apiEndpoint: 'http://localhost:3001/api/events',
enableAutoTracking: true,
enableSmartTracking: true, // π Enable smart tracking
batchSize: 10,
flushInterval: 5000
});
// Initialize and start tracking
await analytics.initialize();
// Track custom events (traditional way)
analytics.track('button_clicked', {
button_id: 'signup',
location: 'homepage'
});
// π Smart tracking - just add attributes to HTML!
// <button data-analytics="button_clicked" data-analytics-prop-location="homepage">Click Me</button>
// Identify users
analytics.identify('user-123', {
name: 'John Doe',
email: 'john@example.com'
});
// Track page views
analytics.page('/pricing', 'Pricing Page');# Start MongoDB
mongod
# Start Redis (optional)
redis-server
# Start the analytics server
npm startThe server will be available at http://localhost:3001 and the dashboard at http://localhost:3001/dashboard.
Access the real-time analytics dashboard at http://localhost:3001/dashboard to view:
- Overview: Key metrics and trends
- Events: Real-time event stream and breakdown
- Sessions: Active user sessions and behavior
- Users: User segments and analytics
- Performance: System health and response times
const analytics = new RealtimeAnalytics({
// Required
apiKey: 'your-api-key',
apiEndpoint: 'http://localhost:3001/api/events',
// Optional
userId: 'user-123', // User ID
sessionId: 'session-456', // Custom session ID
enableAutoTracking: true, // Enable automatic event tracking
batchSize: 10, // Events per batch
flushInterval: 5000, // Flush interval (ms)
maxRetries: 3, // Max retry attempts
retryDelay: 1000, // Retry delay (ms)
enableOfflineMode: true, // Enable offline queuing
enableWebWorker: false, // Use Web Worker for processing
debugMode: false, // Enable debug logging
respectDoNotTrack: true, // Respect browser DNT
domainWhitelist: [], // Allowed domains
customProperties: {} // Custom properties for all events
});Environment variables:
PORT=3001
MONGO_URI=mongodb://localhost:27017/realtime_analytics
REDIS_URL=redis://localhost:6379
NODE_ENV=developmentThe easiest way to track events - just add attributes to your HTML:
<!-- Basic tracking -->
<button data-analytics="button_clicked">Click Me</button>
<!-- With custom properties -->
<button data-analytics="signup_clicked"
data-analytics-prop-button-type="primary"
data-analytics-prop-location="header">
Sign Up
</button>
<!-- Using classes -->
<button class="analytics-purchase">Buy Now</button>
<!-- Using IDs -->
<button id="analytics-download">Download PDF</button>
<!-- Form tracking -->
<form data-analytics-event="contact_form" data-analytics-type="submit">
<input data-analytics-prop-field="email" type="email">
<button type="submit">Submit</button>
</form>Enable Smart Tracking:
const analytics = new RealtimeAnalytics({
// ... other config
enableSmartTracking: true
});π See SMART_TRACKING.md for complete documentation
The SDK automatically tracks:
- Click Events: User clicks on elements
- Navigation Events: Page views and route changes
- Scroll Events: Scroll depth and behavior
- Input Events: Form interactions
- Error Events: JavaScript errors and exceptions
// Track custom events with properties
analytics.track('purchase_completed', {
order_id: 'ORD-123',
amount: 99.99,
currency: 'USD',
product: 'Premium Plan'
});
// Track user interactions
analytics.track('feature_used', {
feature: 'export_data',
method: 'csv'
});// Identify users with traits
analytics.identify('user-123', {
name: 'John Doe',
email: 'john@example.com',
plan: 'premium',
signup_date: '2023-01-15'
});
// Update user traits
analytics.identify('user-123', {
last_login: new Date().toISOString()
});// Opt-out users
analytics.optOut();
// Opt-in users
analytics.optIn();
// Export user data (GDPR Article 20)
const userData = await analytics.exportUserData();
// Delete user data (GDPR Article 17)
await analytics.deleteUserData();const analytics = new RealtimeAnalytics({
// Privacy options
respectDoNotTrack: true,
anonymizeIp: true,
maskSensitiveInputs: true,
excludeLocalhost: true,
cookieConsentRequired: true,
dataRetentionDays: 365
});// Create a custom plugin
const customPlugin = {
name: 'custom-plugin',
initialize: (analytics) => {
console.log('Plugin initialized');
},
track: (event) => {
// Modify events before sending
event.properties.custom_field = 'added_by_plugin';
return event;
},
beforeSend: (events) => {
// Modify batch before sending
return events;
}
};
// Add plugin
analytics.addPlugin(customPlugin);const analytics = new RealtimeAnalytics({
enableWebWorker: true,
// ... other config
});
// Check if worker is being used
const stats = analytics.getStats();
console.log('Using Web Worker:', stats.worker.isUsingWorker);const analytics = new RealtimeAnalytics({
enableOfflineMode: true,
// Events will be queued when offline
// and automatically sent when connection is restored
});
// Check offline queue size
const stats = analytics.getStats();
console.log('Offline events:', stats.batching.offlineQueueSize);| Method | Description | Parameters |
|---|---|---|
initialize() |
Initialize the SDK | None |
track(eventName, properties) |
Track custom event | eventName: string, properties: object |
identify(userId, traits) |
Identify user | userId: string, traits: object |
page(url, title) |
Track page view | url: string, title: string |
reset() |
Reset session | None |
optOut() |
Opt-out tracking | None |
optIn() |
Opt-in tracking | None |
flush() |
Force send events | None |
| Method | Description |
|---|---|
getSession() |
Get current session data |
getUserIdentity() |
Get user identity data |
getPrivacyStatus() |
Get privacy settings status |
getPerformanceMetrics() |
Get SDK performance metrics |
getStats() |
Get comprehensive statistics |
| Endpoint | Method | Description |
|---|---|---|
/api/events |
POST | Submit single event |
/api/events/batch |
POST | Submit batch of events |
/api/events |
GET | Retrieve events with filters |
/api/events/:id |
GET | Get specific event |
/api/events/types |
GET | Get event types |
/api/events/names |
GET | Get event names |
| Endpoint | Method | Description |
|---|---|---|
/api/analytics/overview |
GET | Analytics overview |
/api/analytics/timeline |
GET | Timeline data |
/api/analytics/funnel |
GET | Funnel analysis |
/api/analytics/retention |
GET | Retention analysis |
/api/analytics/segments |
GET | User segments |
| Endpoint | Method | Description |
|---|---|---|
/api/dashboard |
GET | Dashboard data |
/api/dashboard/realtime |
GET | Real-time data |
/api/dashboard/performance |
GET | Performance metrics |
/api/dashboard/alerts |
GET | System alerts |
/api/dashboard/export |
GET | Export data |
npm test
npm run test:watchcd server
npm test
npm run test:watchnpm run test:integrationimport { useEffect } from 'react';
import { RealtimeAnalytics } from '@realtime/analytics-sdk';
let analytics = null;
export function useAnalytics() {
useEffect(() => {
if (!analytics) {
analytics = new RealtimeAnalytics({
apiKey: process.env.REACT_APP_ANALYTICS_KEY,
apiEndpoint: process.env.REACT_APP_ANALYTICS_ENDPOINT,
enableAutoTracking: true
});
analytics.initialize();
}
}, []);
const track = (eventName, properties) => {
analytics?.track(eventName, properties);
};
const identify = (userId, traits) => {
analytics?.identify(userId, traits);
};
return { track, identify };
}// plugins/analytics.js
import { RealtimeAnalytics } from '@realtime/analytics-sdk';
const analytics = new RealtimeAnalytics({
apiKey: process.env.VUE_APP_ANALYTICS_KEY,
apiEndpoint: process.env.VUE_APP_ANALYTICS_ENDPOINT,
enableAutoTracking: true
});
export default async ({ app }) => {
await analytics.initialize();
app.config.globalProperties.$analytics = analytics;
};// lib/analytics.js
import { RealtimeAnalytics } from '@realtime/analytics-sdk';
const analytics = new RealtimeAnalytics({
apiKey: process.env.NEXT_PUBLIC_ANALYTICS_KEY,
apiEndpoint: process.env.NEXT_PUBLIC_ANALYTICS_ENDPOINT,
enableAutoTracking: true
});
export { analytics };
// pages/_app.js
import { analytics } from '../lib/analytics';
import { useEffect } from 'react';
function MyApp({ Component, pageProps }) {
useEffect(() => {
analytics.initialize();
}, []);
return <Component {...pageProps} />;
}- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- π Documentation
- π Issue Tracker
- π¬ Discord Community
- π§ Email Support
- Mobile SDK (React Native, Flutter)
- Advanced funnel analysis
- A/B testing integration
- Heatmap visualization
- Session replay
- Custom alerting
- Advanced user segmentation
- Export to BI tools
- Multi-region deployment
- Edge computing support
Made with β€οΈ by the Realtime Analytics Team