This is the server-side Node.js library. For browser tracking, use the JavaScript SDK.
Installation
Initialize
const Mixpanel = require('mixpanel');
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN');
Configuration
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN', {
verbose: true,
protocol: 'https',
keepAlive: true
});
Track Events
You must provide a distinct_id for all events.
mp.track('Purchase', {
distinct_id: '12345',
product: 'Premium Subscription',
amount: 49.99,
currency: 'USD'
});
Async/Await
const result = await new Promise((resolve, reject) => {
mp.track('Purchase', {
distinct_id: '12345',
product: 'Premium'
}, (err) => {
if (err) reject(err);
else resolve();
});
});
Import Historical Events
For events older than 5 days:
mp.import('Old Event', 1698023982, {
distinct_id: '12345',
product: 'Premium'
});
User Profiles
mp.people.set('12345', {
name: 'John Doe',
email: '[email protected]',
plan: 'Premium',
ip: '0' // Disable geolocation
});
set_once()
increment()
append()
union()
mp.people.set_once('12345', {
'First Purchase': new Date().toISOString(),
ip: '0'
});
mp.people.increment('12345', 'login_count', 1);
mp.people.increment('12345', 'age', 1);
mp.people.append('12345', 'transactions', {
amount: 50,
date: new Date()
});
mp.people.union('12345', {
'skills': ['Node.js', 'JavaScript']
});
Group Analytics
Send Events
mp.track('Feature Used', {
distinct_id: '12345',
company: 'Acme Inc',
feature: 'Reports'
});
Set Group Properties
mp.groups.set('company', 'Acme Inc', {
name: 'Acme Inc',
industry: 'Technology',
employees: 500
});
set_once()
unset()
union()
remove()
mp.groups.set_once('company', 'Acme Inc', {
founded: '2010-01-01'
});
mp.groups.unset('company', 'Acme Inc', 'temp_property');
mp.groups.union('company', 'Acme Inc', {
features: ['Reports', 'Analytics']
});
mp.groups.remove('company', 'Acme Inc', {
features: ['Beta Feature']
});
Privacy Controls
EU Data Residency
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN', {
host: 'api-eu.mixpanel.com'
});
India Data Residency
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN', {
host: 'api-in.mixpanel.com'
});
Disable Geolocation
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN', {
geolocate: false
});
// Or per-request
mp.track('event', {
distinct_id: '12345',
ip: '0'
});
Debug Mode
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN', {
debug: true
});
All server-side calls originate from your server’s IP, which affects geolocation. Set ip: 0 or configure custom IP addresses.
- You manage
distinct_id yourself
- No automatic identity management
- Synchronous and asynchronous APIs available
- Use
.import() for historical data (>5 days old)
- No client-side features (cookies, localStorage)
Resources