UNPKG

2.71 kBJavaScriptView Raw
1// grab the Mixpanel factory
2var Mixpanel = require('./lib/mixpanel-node');
3
4// create an instance of the mixpanel client
5var mixpanel = Mixpanel.init('962dbca1bbc54701d402c94d65b4a20e');
6mixpanel.set_config({ debug: true });
7
8// track an event with optional properties
9mixpanel.track("my event", {
10 distinct_id: "some unique client id",
11 as: "many",
12 properties: "as",
13 you: "want"
14});
15mixpanel.track("played_game");
16
17// create or update a user in Mixpanel Engage
18mixpanel.people.set("billybob", {
19 $first_name: "Billy",
20 $last_name: "Bob",
21 $created: (new Date('jan 1 2013')).toISOString(),
22 plan: "premium",
23 games_played: 1,
24 points: 0
25});
26
27// set a single property on a user
28mixpanel.people.set("billybob", "plan", "free");
29
30// set a single property on a user, don't override
31mixpanel.people.set_once("billybob", "first_game_play", (new Date('jan 1 2013')).toISOString());
32
33// increment a numeric property
34mixpanel.people.increment("billybob", "games_played");
35
36// increment a numeric property by a different amount
37mixpanel.people.increment("billybob", "points", 15);
38
39// increment multiple properties
40mixpanel.people.increment("billybob", {"points": 10, "games_played": 1});
41
42// append value to a list
43mixpanel.people.append("billybob", "awards", "Great Player");
44
45// append multiple values to a list
46mixpanel.people.append("billybob", {"awards": "Great Player", "levels_finished": "Level 4"});
47
48// record a transaction for revenue analytics
49mixpanel.people.track_charge("billybob", 39.99);
50
51// clear a users transaction history
52mixpanel.people.clear_charges("billybob");
53
54// delete a user
55mixpanel.people.delete_user("billybob");
56
57// all functions that send data to mixpanel take an optional
58// callback as the last argument
59mixpanel.track("test", function(err) { if (err) { throw err; } });
60
61// import an old event
62var mixpanel_importer = Mixpanel.init('valid mixpanel token', {
63 key: "valid api key for project"
64});
65mixpanel_importer.set_config({ debug: true });
66
67// needs to be in the system once for it to show up in the interface
68mixpanel_importer.track('old event', { gender: '' });
69
70mixpanel_importer.import("old event", new Date(2012, 4, 20, 12, 34, 56), {
71 distinct_id: 'billybob',
72 gender: 'male'
73});
74
75// import multiple events at once
76mixpanel_importer.import_batch([
77 {
78 event: 'old event',
79 properties: {
80 time: new Date(2012, 4, 20, 12, 34, 56),
81 distinct_id: 'billybob',
82 gender: 'male'
83 }
84 },
85 {
86 event: 'another old event',
87 properties: {
88 time: new Date(2012, 4, 21, 11, 33, 55),
89 distinct_id: 'billybob',
90 color: 'red'
91 }
92 }
93]);