UNPKG

3 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// create or update a user in Mixpanel Engage without altering $last_seen
28// - pass option `$ignore_time: true` to prevent the $last_seen property from being updated
29mixpanel.people.set("billybob", {
30 plan: "premium",
31 games_played: 1
32}, {
33 $ignore_time: true
34});
35
36// set a single property on a user
37mixpanel.people.set("billybob", "plan", "free");
38
39// set a single property on a user, don't override
40mixpanel.people.set_once("billybob", "first_game_play", (new Date('jan 1 2013')).toISOString());
41
42// increment a numeric property
43mixpanel.people.increment("billybob", "games_played");
44
45// increment a numeric property by a different amount
46mixpanel.people.increment("billybob", "points", 15);
47
48// increment multiple properties
49mixpanel.people.increment("billybob", {"points": 10, "games_played": 1});
50
51// append value to a list
52mixpanel.people.append("billybob", "awards", "Great Player");
53
54// append multiple values to a list
55mixpanel.people.append("billybob", {"awards": "Great Player", "levels_finished": "Level 4"});
56
57// record a transaction for revenue analytics
58mixpanel.people.track_charge("billybob", 39.99);
59
60// clear a users transaction history
61mixpanel.people.clear_charges("billybob");
62
63// delete a user
64mixpanel.people.delete_user("billybob");
65
66// all functions that send data to mixpanel take an optional
67// callback as the last argument
68mixpanel.track("test", function(err) { if (err) { throw err; } });
69
70// import an old event
71var mixpanel_importer = Mixpanel.init('valid mixpanel token', {
72 secret: "valid api secret for project"
73});
74mixpanel_importer.set_config({ debug: true });
75
76// needs to be in the system once for it to show up in the interface
77mixpanel_importer.track('old event', { gender: '' });
78
79mixpanel_importer.import("old event", new Date(2012, 4, 20, 12, 34, 56), {
80 distinct_id: 'billybob',
81 gender: 'male'
82});
83
84// import multiple events at once
85mixpanel_importer.import_batch([
86 {
87 event: 'old event',
88 properties: {
89 time: new Date(2012, 4, 20, 12, 34, 56),
90 distinct_id: 'billybob',
91 gender: 'male'
92 }
93 },
94 {
95 event: 'another old event',
96 properties: {
97 time: new Date(2012, 4, 21, 11, 33, 55),
98 distinct_id: 'billybob',
99 color: 'red'
100 }
101 }
102]);