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 | // track an event with optional properties
|
24 | mixpanel.track("my event", {
|
25 | distinct_id: "some unique client id",
|
26 | as: "many",
|
27 | properties: "as",
|
28 | you: "want"
|
29 | });
|
30 | mixpanel.track("played_game");
|
31 |
|
32 | // create or update a user in Mixpanel Engage
|
33 | mixpanel.people.set("billybob", {
|
34 | $first_name: "Billy",
|
35 | $last_name: "Bob",
|
36 | $created: (new Date('jan 1 2013')).toISOString(),
|
37 | plan: "premium",
|
38 | games_played: 1,
|
39 | points: 0
|
40 | });
|
41 |
|
42 | // create or update a user in Mixpanel Engage without altering $last_seen
|
43 | // - pass option `$ignore_time: true` to prevent the $last_seen property from being updated
|
44 | mixpanel.people.set("billybob", {
|
45 | plan: "premium",
|
46 | games_played: 1
|
47 | }, {
|
48 | $ignore_time: true
|
49 | });
|
50 |
|
51 | // set a single property on a user
|
52 | mixpanel.people.set("billybob", "plan", "free");
|
53 |
|
54 | // increment a numeric property
|
55 | mixpanel.people.increment("billybob", "games_played");
|
56 |
|
57 | // increment a numeric property by a different amount
|
58 | mixpanel.people.increment("billybob", "points", 15);
|
59 |
|
60 | // increment multiple properties
|
61 | mixpanel.people.increment("billybob", {"points": 10, "games_played": 1});
|
62 |
|
63 | // append value to a list
|
64 | mixpanel.people.append("billybob", "awards", "Great Player");
|
65 |
|
66 | // append multiple values to a list
|
67 | mixpanel.people.append("billybob", {"awards": "Great Player", "levels_finished": "Level 4"});
|
68 |
|
69 | // merge value to a list (ignoring duplicates)
|
70 | mixpanel.people.union("billybob", {"browsers": "ie"});
|
71 |
|
72 | // merge multiple values to a list (ignoring duplicates)
|
73 | mixpanel.people.union("billybob", {"browsers": ["ie", "chrome"]});
|
74 |
|
75 |
|
76 | // record a transaction for revenue analytics
|
77 | mixpanel.people.track_charge("billybob", 39.99);
|
78 |
|
79 | // clear a users transaction history
|
80 | mixpanel.people.clear_charges("billybob");
|
81 |
|
82 | // delete a user
|
83 | mixpanel.people.delete_user("billybob");
|
84 |
|
85 | // Create an alias for an existing distinct id
|
86 | mixpanel.alias("distinct_id", "your_alias");
|
87 |
|
88 | // all functions that send data to mixpanel take an optional
|
89 | // callback as the last argument
|
90 | mixpanel.track("test", function(err) { if (err) throw err; });
|
91 |
|
92 | // import an old event
|
93 | var mixpanel_importer = Mixpanel.init('valid mixpanel token', {
|
94 | key: "valid api key for project"
|
95 | });
|
96 |
|
97 | // needs to be in the system once for it to show up in the interface
|
98 | mixpanel_importer.track('old event', { gender: '' });
|
99 |
|
100 | mixpanel_importer.import("old event", new Date(2012, 4, 20, 12, 34, 56), {
|
101 | distinct_id: 'billybob',
|
102 | gender: 'male'
|
103 | });
|
104 |
|
105 | // import multiple events at once
|
106 | mixpanel_importer.import_batch([
|
107 | {
|
108 | event: 'old event',
|
109 | properties: {
|
110 | time: new Date(2012, 4, 20, 12, 34, 56),
|
111 | distinct_id: 'billybob',
|
112 | gender: 'male'
|
113 | }
|
114 | },
|
115 | {
|
116 | event: 'another old event',
|
117 | properties: {
|
118 | time: new Date(2012, 4, 21, 11, 33, 55),
|
119 | distinct_id: 'billybob',
|
120 | color: 'red'
|
121 | }
|
122 | }
|
123 | ]);
|
124 | ```
|
125 |
|
126 | FAQ
|
127 | ---
|
128 | **Where is `mixpanel.identify()`?**
|
129 |
|
130 | `mixpanel-node` is a server-side library, optimized for stateless shared usage; e.g.,
|
131 | in a web application, the same mixpanel instance is used across requests for all users.
|
132 | Rather than setting a `distinct_id` through `identify()` calls like Mixpanel client-side
|
133 | libraries (where a single Mixpanel instance is tied to a single user), this library
|
134 | requires you to pass the `distinct_id` with every tracking call. See
|
135 | https://github.com/mixpanel/mixpanel-node/issues/13.
|
136 |
|
137 | **How do I get or set superproperties?**
|
138 |
|
139 | See the previous answer: the library does not maintain user state internally and so has
|
140 | no concept of superproperties for individual users. If you wish to preserve properties
|
141 | for users between requests, you will need to load these properties from a source specific
|
142 | to your app (e.g., your session store or database) and pass them explicitly with each
|
143 | tracking call.
|
144 |
|
145 |
|
146 | Tests
|
147 | -----
|
148 |
|
149 | # in the mixpanel directory
|
150 | npm install
|
151 | npm test
|
152 |
|
153 | Attribution/Credits
|
154 | -------------------
|
155 |
|
156 | Heavily inspired by the original js library copyright Mixpanel, Inc.
|
157 | (http://mixpanel.com/)
|
158 |
|
159 | Copyright (c) 2014-15 Mixpanel
|
160 | Original Library Copyright (c) 2012-14 Carl Sverre
|
161 |
|
162 | Contributions from:
|
163 | - [Andres Gottlieb](https://github.com/andresgottlieb)
|
164 | - [Ken Perkins](https://github.com/kenperkins)
|
165 | - [Nathan Rajlich](https://github.com/TooTallNate)
|
166 | - [Thomas Watson Steen](https://github.com/watson)
|
167 | - [Gabor Ratky](https://github.com/rgabo)
|
168 | - [wwlinx](https://github.com/wwlinx)
|
169 | - [PierrickP](https://github.com/PierrickP)
|
170 | - [lukapril](https://github.com/lukapril)
|
171 | - [sandinmyjoints](https://github.com/sandinmyjoints)
|
172 | - [Jyrki Laurila](https://github.com/jylauril)
|
173 | - [Zeevl](https://github.com/zeevl)
|
174 | - [Tobias Baunbæk](https://github.com/freeall)
|
175 | - [Eduardo Sorribas](https://github.com/sorribas)
|
176 | - [Nick Chang](https://github.com/maeldur)
|
177 | - [Michael G](https://github.com/gmichael225)
|
178 | - [Tejas Manohar](https://github.com/tejasmanohar)
|
179 | - [Eelke Boezeman](https://github.com/godspeedelbow)
|
180 |
|
181 | License
|
182 | -------------------
|
183 |
|
184 | Released under the MIT license. See file called LICENSE for more
|
185 | details.
|