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