UNPKG

8.64 kBMarkdownView Raw
1Mixpanel-node
2=============
3![Build Status](https://github.com/mixpanel/mixpanel-node/actions/workflows/tests.yml/badge.svg)
4
5This library provides many of the features in the official JavaScript mixpanel library. It is easy to use, and fully async. It is intended to be used on the server (it is not a client module). The in-browser client library is available
6at [https://github.com/mixpanel/mixpanel-js](https://github.com/mixpanel/mixpanel-js).
7
8Installation
9------------
10
11 npm install mixpanel
12
13Quick Start
14-----------
15
16```javascript
17// grab the Mixpanel factory
18var Mixpanel = require('mixpanel');
19
20// create an instance of the mixpanel client
21var mixpanel = Mixpanel.init('<YOUR_TOKEN>');
22
23// initialize mixpanel client configured to communicate over http instead of https
24var mixpanel = Mixpanel.init('<YOUR_TOKEN>', {
25 protocol: 'http',
26});
27
28// turn off keepAlive (reestablish connection on each request)
29var mixpanel = Mixpanel.init('<YOUR_TOKEN>', {
30 keepAlive: false,
31});
32
33// track an event with optional properties
34mixpanel.track('my event', {
35 distinct_id: 'some unique client id',
36 as: 'many',
37 properties: 'as',
38 you: 'want'
39});
40mixpanel.track('played_game');
41
42// set an IP address to get automatic geolocation info
43mixpanel.track('my event', {ip: '127.0.0.1'});
44
45// track an event with a specific timestamp (up to 5 days old;
46// use mixpanel.import() for older events)
47mixpanel.track('timed event', {time: new Date()});
48
49// create or update a user in Mixpanel Engage
50mixpanel.people.set('billybob', {
51 $first_name: 'Billy',
52 $last_name: 'Bob',
53 $created: (new Date('jan 1 2013')).toISOString(),
54 plan: 'premium',
55 games_played: 1,
56 points: 0
57});
58
59// create or update a user in Mixpanel Engage without altering $last_seen
60// - pass option $ignore_time: true to prevent the $last_seen property from being updated
61mixpanel.people.set('billybob', {
62 plan: 'premium',
63 games_played: 1
64}, {
65 $ignore_time: true
66});
67
68// set a user profile's IP address to get automatic geolocation info
69mixpanel.people.set('billybob', {
70 plan: 'premium',
71 games_played: 1
72}, {
73 $ip: '127.0.0.1'
74});
75
76// set a user profile's latitude and longitude to get automatic geolocation info
77mixpanel.people.set('billybob', {
78 plan: 'premium',
79 games_played: 1
80}, {
81 $latitude: 40.7127753,
82 $longitude: -74.0059728
83});
84
85// set a single property on a user
86mixpanel.people.set('billybob', 'plan', 'free');
87
88// set a single property on a user, don't override
89mixpanel.people.set_once('billybob', 'first_game_play', (new Date('jan 1 2013')).toISOString());
90
91// increment a numeric property
92mixpanel.people.increment('billybob', 'games_played');
93
94// increment a numeric property by a different amount
95mixpanel.people.increment('billybob', 'points', 15);
96
97// increment multiple properties
98mixpanel.people.increment('billybob', {'points': 10, 'games_played': 1});
99
100// append value to a list
101mixpanel.people.append('billybob', 'awards', 'Great Player');
102
103// append multiple values to a list
104mixpanel.people.append('billybob', {'awards': 'Great Player', 'levels_finished': 'Level 4'});
105
106// merge value to a list (ignoring duplicates)
107mixpanel.people.union('billybob', {'browsers': 'ie'});
108
109// merge multiple values to a list (ignoring duplicates)
110mixpanel.people.union('billybob', {'browsers': ['ie', 'chrome']});
111
112
113// record a transaction for revenue analytics
114mixpanel.people.track_charge('billybob', 39.99);
115
116// clear a users transaction history
117mixpanel.people.clear_charges('billybob');
118
119// delete a user
120mixpanel.people.delete_user('billybob');
121
122// delete a user in Mixpanel Engage without altering $last_seen or resolving aliases
123// - pass option $ignore_time: true to prevent the $last_seen property from being updated
124// (useful if you subsequently re-import data for the same distinct ID)
125mixpanel.people.delete_user('billybob', {$ignore_time: true, $ignore_alias: true});
126
127// Create an alias for an existing distinct id
128mixpanel.alias('distinct_id', 'your_alias');
129
130// all functions that send data to mixpanel take an optional
131// callback as the last argument
132mixpanel.track('test', function(err) { if (err) throw err; });
133
134// track multiple events at once
135mixpanel.track_batch([
136 {
137 event: 'recent event',
138 properties: {
139 time: new Date(),
140 distinct_id: 'billybob',
141 gender: 'male'
142 }
143 },
144 {
145 event: 'another recent event',
146 properties: {
147 distinct_id: 'billybob',
148 color: 'red'
149 }
150 }
151]);
152
153// import an old event
154var mixpanel_importer = Mixpanel.init('valid mixpanel token', {
155 secret: 'valid api secret for project'
156});
157
158// needs to be in the system once for it to show up in the interface
159mixpanel_importer.track('old event', { gender: '' });
160
161mixpanel_importer.import('old event', new Date(2012, 4, 20, 12, 34, 56), {
162 distinct_id: 'billybob',
163 gender: 'male'
164});
165
166// import multiple events at once
167mixpanel_importer.import_batch([
168 {
169 event: 'old event',
170 properties: {
171 time: new Date(2012, 4, 20, 12, 34, 56),
172 distinct_id: 'billybob',
173 gender: 'male'
174 }
175 },
176 {
177 event: 'another old event',
178 properties: {
179 time: new Date(2012, 4, 21, 11, 33, 55),
180 distinct_id: 'billybob',
181 color: 'red'
182 }
183 }
184]);
185```
186
187FAQ
188---
189**Where is `mixpanel.identify()`?**
190
191`mixpanel-node` is a server-side library, optimized for stateless shared usage; e.g.,
192in a web application, the same mixpanel instance is used across requests for all users.
193Rather than setting a `distinct_id` through `identify()` calls like Mixpanel client-side
194libraries (where a single Mixpanel instance is tied to a single user), this library
195requires you to pass the `distinct_id` with every tracking call. See
196https://github.com/mixpanel/mixpanel-node/issues/13.
197
198**How do I get or set superproperties?**
199
200See the previous answer: the library does not maintain user state internally and so has
201no concept of superproperties for individual users. If you wish to preserve properties
202for users between requests, you will need to load these properties from a source specific
203to your app (e.g., your session store or database) and pass them explicitly with each
204tracking call.
205
206
207Tests
208-----
209
210 # in the mixpanel directory
211 npm install
212 npm test
213
214Alternative Clients and Related Tools
215-------------------------------------
216
217- [Mixpanel-CLI](https://github.com/FGRibreau/mixpanel-cli) - CLI for Mixpanel API (currently only supports tracking functions)
218- [Mixpanel Data Export](https://github.com/michaelcarter/mixpanel-data-export-js) - Supports various query and data-management APIs; runs in both Node.js and browser
219- [Mixpanel Data Export (strawbrary)](https://github.com/strawbrary/mixpanel-data-export-js) - Fork of previous library, optimized for Node.js with support for streaming large raw exports
220
221Attribution/Credits
222-------------------
223
224Heavily inspired by the original js library copyright Mixpanel, Inc.
225(http://mixpanel.com/)
226
227Copyright (c) 2014-21 Mixpanel
228Original Library Copyright (c) 2012-14 Carl Sverre
229
230Contributions from:
231 - [Andres Gottlieb](https://github.com/andresgottlieb)
232 - [Ken Perkins](https://github.com/kenperkins)
233 - [Nathan Rajlich](https://github.com/TooTallNate)
234 - [Thomas Watson Steen](https://github.com/watson)
235 - [Gabor Ratky](https://github.com/rgabo)
236 - [wwlinx](https://github.com/wwlinx)
237 - [PierrickP](https://github.com/PierrickP)
238 - [lukapril](https://github.com/lukapril)
239 - [sandinmyjoints](https://github.com/sandinmyjoints)
240 - [Jyrki Laurila](https://github.com/jylauril)
241 - [Zeevl](https://github.com/zeevl)
242 - [Tobias Baunbæk](https://github.com/freeall)
243 - [Eduardo Sorribas](https://github.com/sorribas)
244 - [Nick Chang](https://github.com/maeldur)
245 - [Michael G](https://github.com/gmichael225)
246 - [Tejas Manohar](https://github.com/tejasmanohar)
247 - [Eelke Boezeman](https://github.com/godspeedelbow)
248 - [Jim Thomas](https://github.com/Left47)
249 - [Frank Chiang](https://github.com/chiangf)
250 - [Morgan Croney](https://github.com/cruzanmo)
251 - [Cole Furfaro-Strode](https://github.com/colestrode)
252 - [Jonas Hermsmeier](https://github.com/jhermsmeier)
253 - [Marko Klopets](https://github.com/mklopets)
254 - [Cameron Diver](https://github.com/CameronDiver)
255 - [veerabio](https://github.com/veerabio)
256 - [Will Neild](https://github.com/wneild)
257 - [Elijah Insua](https://github.com/tmpvar)
258 - [Arsal Imam](https://github.com/ArsalImam)
259
260License
261-------------------
262
263Released under the MIT license. See file called LICENSE for more
264details.