UNPKG

5.8 kBMarkdownView Raw
1Mixpanel-node
2=============
3[![Build Status](https://travis-ci.org/mixpanel/mixpanel-node.svg?branch=master)](https://travis-ci.org/mixpanel/mixpanel-node)
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('6fd9434dba686db2d1ab66b4462a3a67');
22
23// track an event with optional properties
24mixpanel.track("my event", {
25 distinct_id: "some unique client id",
26 as: "many",
27 properties: "as",
28 you: "want"
29});
30mixpanel.track("played_game");
31
32// create or update a user in Mixpanel Engage
33mixpanel.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
44mixpanel.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
52mixpanel.people.set("billybob", "plan", "free");
53
54// increment a numeric property
55mixpanel.people.increment("billybob", "games_played");
56
57// increment a numeric property by a different amount
58mixpanel.people.increment("billybob", "points", 15);
59
60// increment multiple properties
61mixpanel.people.increment("billybob", {"points": 10, "games_played": 1});
62
63// append value to a list
64mixpanel.people.append("billybob", "awards", "Great Player");
65
66// append multiple values to a list
67mixpanel.people.append("billybob", {"awards": "Great Player", "levels_finished": "Level 4"});
68
69// merge value to a list (ignoring duplicates)
70mixpanel.people.union("billybob", {"browsers": "ie"});
71
72// merge multiple values to a list (ignoring duplicates)
73mixpanel.people.union("billybob", {"browsers": ["ie", "chrome"]});
74
75
76// record a transaction for revenue analytics
77mixpanel.people.track_charge("billybob", 39.99);
78
79// clear a users transaction history
80mixpanel.people.clear_charges("billybob");
81
82// delete a user
83mixpanel.people.delete_user("billybob");
84
85// Create an alias for an existing distinct id
86mixpanel.alias("distinct_id", "your_alias");
87
88// all functions that send data to mixpanel take an optional
89// callback as the last argument
90mixpanel.track("test", function(err) { if (err) throw err; });
91
92// import an old event
93var 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
98mixpanel_importer.track('old event', { gender: '' });
99
100mixpanel_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
106mixpanel_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
126FAQ
127---
128**Where is `mixpanel.identify()`?**
129
130`mixpanel-node` is a server-side library, optimized for stateless shared usage; e.g.,
131in a web application, the same mixpanel instance is used across requests for all users.
132Rather than setting a `distinct_id` through `identify()` calls like Mixpanel client-side
133libraries (where a single Mixpanel instance is tied to a single user), this library
134requires you to pass the `distinct_id` with every tracking call. See
135https://github.com/mixpanel/mixpanel-node/issues/13.
136
137**How do I get or set superproperties?**
138
139See the previous answer: the library does not maintain user state internally and so has
140no concept of superproperties for individual users. If you wish to preserve properties
141for users between requests, you will need to load these properties from a source specific
142to your app (e.g., your session store or database) and pass them explicitly with each
143tracking call.
144
145
146Tests
147-----
148
149 # in the mixpanel directory
150 npm install
151 npm test
152
153Attribution/Credits
154-------------------
155
156Heavily inspired by the original js library copyright Mixpanel, Inc.
157(http://mixpanel.com/)
158
159Copyright (c) 2014-15 Mixpanel
160Original Library Copyright (c) 2012-14 Carl Sverre
161
162Contributions 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
181License
182-------------------
183
184Released under the MIT license. See file called LICENSE for more
185details.