Getting Started
This Mixpanel Node.js library provides many of the features in available in the JavaScript SDK. It is fully async, and intended to be used in Javascript server-side outside of the browser (such as importing past events with a script). For the in-browser client-side library, please use the JavaScript SDK. The Library Source Code and an Example Application is documented in our GitHub repo.Installing the Library
The Javascript library is named
mixpanel-browser in NPM. This is to distinguish it from this server-side Node.js library, which is available as mixpanel..init() method with your project token.
Javascript
Library Configuration
The Mixpanel object can be initialized with different configurations. See a complete list of the configuration optionshere. You can override the default configuration using theconfig argument when initializing the library.
Example Usage
Javascript
Sending Events
Usetrack() to send an event by providing the distinct_id, event name, and any event properties. This will trigger a request to the /track API endpoint to ingest the event into your project.
The /track endpoint will only validate events with timestamps within the last 5 days of the request. Events with timestamps older than 5 days will not be ingested. See below on best practices for historical imports.
$city, $region, mp_country_code) using the IP address on the incoming request. As all server-side calls will likely originate from the same IP (that is, the IP of your server), this can have the unintended effect of setting the location of all of your users to the location of your data center. Learn more about best practices for geolocation..
Note that tracking with Node in an async serverless implementation requires you to wait for the Mixpanel request to complete. The easiest way to do this would be to pass in in a callback as a 3rd parameter into
track and return a promise that is resolved when the request is sent.Importing Historical Events
Thetrack() function is designed for real-time tracking in a server-side environment and will trigger request to the /track API endpoint, which will validate for events with a time stamp that is within the last 5 days of the request. Events older than 5 days will not be ingested.
Use the import() function to import events that occurred more than 5 days in the past. The import() function is based on the /import API endpoint.
Example Usage
Managing User Identity
Since the Node.js SDK is a server-side library, IDs are not generated by the SDK. Instead, you will need to generate and manage the distinct_id yourself and include it in your events and profile data. Learn more about server-side identity management.Storing User Profiles
Create user profiles by setting profile properties to describe them. Example profile properties include “name”, “email”, “company”, and any other demographic details about the user. The Mixpanel object contains apeople property, that exposes the MixpanelPeople class which contain methods for managing user profile properties. These methods will trigger requests to the /engage API endpoint.
Mixpanel determines default geolocation data ($city, $region, mp_country_code) using the IP address on the incoming request. As all server-side calls will likely originate from the same IP (that is, the IP of your server), this can have the unintended effect of setting the location of all of your users to the location of your data center. Learn more about best practices for geolocation..
Setting Profile Properties
Set profile properties on a user profile by callingpeople.set().
If a profile property already exists, it will be overwritten with the latest value provided in the method. If a profile property does not exist, it will be added to the profile.
Example Usage
Other Types of Profile Updates
There are a few other methods for setting profile properties. See a complete reference of the available methods in theMixpanelPeople class here.
A few commonly used people methods are highlighted below:
- set_once()
- append()
- union()
- increment()
The
people.set_once() method set profile properties only if they do not exist yet. If it is setting a profile property that already exists, it will be ignored.Use this method if you want to set profile properties without the risk of overwriting existing data.Example UsageGroup Analytics
Read more about Group Analytics before proceeding. You will need to have the group key defined in your project settings first.
group_key and group_id.
group_keyis the event property that connects event data to a group. (e.g.company)group_idis the identifier for a specific group. (e.g.mixpanel,company_a,company_b, etc.)
Sending Group Identifiers With Events
All events must have the group key as an event property in order to be attributed to a group. Without the group key, an event cannot be attributed to a group. To send group identifiers with your events, set thegroup_key as an event property with the group_id as the value.
Example Usage
group_key value as a list of multiple group_id values.
Example Usage
Adding Group Identifiers to User Profiles
To connect group information to a user profile, include thegroup_key and group_id as a user profile property using the people.set() call.
Example Usage
Setting Group Profile Properties
Create a group profile by setting group properties, similar to a user profile. For example, you may want to describe a company group with properties such as “ARR”, “employee_count”, and “subscription”. The Mixpanel object contains agroups property, that exposes the MixpanelGroups class which contain methods for managing group profile properties.
To set group profile properties, use the groups.set() method, which will trigger a request to the /groups API endpoint.
Example Usage
JavaScript
Other Group Profile Methods
See all of the methods under theMixpanelGroups class here.
A few commonly used group methods are highlighted below:
- set_once()
- unset()
- union()
- remove()
The
groups.set_once() method set group profile properties only if they do not exist yet. If it is setting a profile property that already exists, it will be ignored.Use this method if you want to set group profile properties without the risk of overwriting existing data.Example UsageDebug Mode
To enable debug mode, set thedebug configuration option to true during the library initialization.
Example Usage
Privacy-Friendly Tracking
You have control over the data you send to Mixpanel. The Node.js SDK have a few configurations to help you protect user data. Since this is a server-side tracking library where you have control of the servers, your server is responsible for determining whether to send data about a particular user or not.EU Data Residency
Route data to Mixpanel’s EU servers by setting thehost config property during the library initialization.
Example Usage
India Data Residency
Route data to Mixpanel’s EU servers by setting thehost config property during the library initialization.
Example Usage
Disable Geolocation
The Node.js SDK parse the request IP address to generate geolocation properties for events and profiles. You may want to disable them to prevent the unintentional setting of your data’s geolocation to the location of your server that is sending the request, or to prevent geolocation data from being tracked entirely. To disable geolocation, set thegeolocate config property to false during the library initialization.
You can also disable geolocation for individual requests by setting the ip to 0.
Example Usage