UNPKG

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