> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/mixpanel/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js SDK

> Track events from Node.js servers with the Mixpanel server-side SDK

<Note>
  This is the server-side Node.js library. For browser tracking, use the [JavaScript SDK](/sdks/javascript).
</Note>

## Installation

```bash theme={null}
npm install mixpanel
```

## Initialize

```javascript theme={null}
const Mixpanel = require('mixpanel');

const mp = Mixpanel.init('YOUR_PROJECT_TOKEN');
```

### Configuration

```javascript theme={null}
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN', {
  verbose: true,
  protocol: 'https',
  keepAlive: true
});
```

## Track Events

<Warning>
  You must provide a `distinct_id` for all events.
</Warning>

```javascript theme={null}
mp.track('Purchase', {
  distinct_id: '12345',
  product: 'Premium Subscription',
  amount: 49.99,
  currency: 'USD'
});
```

### Async/Await

```javascript theme={null}
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:

```javascript theme={null}
mp.import('Old Event', 1698023982, {
  distinct_id: '12345',
  product: 'Premium'
});
```

## User Profiles

```javascript theme={null}
mp.people.set('12345', {
  name: 'John Doe',
  email: 'john@example.com',
  plan: 'Premium',
  ip: '0' // Disable geolocation
});
```

<Tabs>
  <Tab title="set_once()">
    ```javascript theme={null}
    mp.people.set_once('12345', {
      'First Purchase': new Date().toISOString(),
      ip: '0'
    });
    ```
  </Tab>

  <Tab title="increment()">
    ```javascript theme={null}
    mp.people.increment('12345', 'login_count', 1);
    mp.people.increment('12345', 'age', 1);
    ```
  </Tab>

  <Tab title="append()">
    ```javascript theme={null}
    mp.people.append('12345', 'transactions', {
      amount: 50,
      date: new Date()
    });
    ```
  </Tab>

  <Tab title="union()">
    ```javascript theme={null}
    mp.people.union('12345', {
      'skills': ['Node.js', 'JavaScript']
    });
    ```
  </Tab>
</Tabs>

## Group Analytics

### Send Events

```javascript theme={null}
mp.track('Feature Used', {
  distinct_id: '12345',
  company: 'Acme Inc',
  feature: 'Reports'
});
```

### Set Group Properties

```javascript theme={null}
mp.groups.set('company', 'Acme Inc', {
  name: 'Acme Inc',
  industry: 'Technology',
  employees: 500
});
```

<Tabs>
  <Tab title="set_once()">
    ```javascript theme={null}
    mp.groups.set_once('company', 'Acme Inc', {
      founded: '2010-01-01'
    });
    ```
  </Tab>

  <Tab title="unset()">
    ```javascript theme={null}
    mp.groups.unset('company', 'Acme Inc', 'temp_property');
    ```
  </Tab>

  <Tab title="union()">
    ```javascript theme={null}
    mp.groups.union('company', 'Acme Inc', {
      features: ['Reports', 'Analytics']
    });
    ```
  </Tab>

  <Tab title="remove()">
    ```javascript theme={null}
    mp.groups.remove('company', 'Acme Inc', {
      features: ['Beta Feature']
    });
    ```
  </Tab>
</Tabs>

## Privacy Controls

### EU Data Residency

```javascript theme={null}
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN', {
  host: 'api-eu.mixpanel.com'
});
```

### India Data Residency

```javascript theme={null}
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN', {
  host: 'api-in.mixpanel.com'
});
```

### Disable Geolocation

```javascript theme={null}
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN', {
  geolocate: false
});

// Or per-request
mp.track('event', {
  distinct_id: '12345',
  ip: '0'
});
```

## Debug Mode

```javascript theme={null}
const mp = Mixpanel.init('YOUR_PROJECT_TOKEN', {
  debug: true
});
```

## Platform Considerations

<Warning>
  All server-side calls originate from your server's IP, which affects geolocation. Set `ip: 0` or configure custom IP addresses.
</Warning>

* 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

* [GitHub Repository](https://github.com/mixpanel/mixpanel-node)
* [Example Application](https://github.com/mixpanel/mixpanel-node/blob/master/example.js)
