Skip to main content
This is the server-side Node.js library. For browser tracking, use the JavaScript SDK.

Installation

npm install mixpanel

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
});
mp.people.set_once('12345', {
  'First Purchase': new Date().toISOString(),
  ip: '0'
});

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
});
mp.groups.set_once('company', 'Acme Inc', {
  founded: '2010-01-01'
});

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
});

Platform Considerations

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

Build docs developers (and LLMs) love