Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@sandfox/mixpanel

mixpanel60.3.3

A simple server-side API for mixpanel

mixpanel, analytics, api, stats

readme

Mixpanel-node

Build Status

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 at https://github.com/mixpanel/mixpanel-js.

Installation

npm install mixpanel

Quick Start

// grab the Mixpanel factory
var Mixpanel = require('mixpanel');

// create an instance of the mixpanel client
var mixpanel = Mixpanel.init('6fd9434dba686db2d1ab66b4462a3a67');

// track an event with optional properties
mixpanel.track("my event", {
    distinct_id: "some unique client id",
    as: "many",
    properties: "as",
    you: "want"
});
mixpanel.track("played_game");

// create or update a user in Mixpanel Engage
mixpanel.people.set("billybob", {
    $first_name: "Billy",
    $last_name: "Bob",
    $created: (new Date('jan 1 2013')).toISOString(),
    plan: "premium",
    games_played: 1,
    points: 0
});

// create or update a user in Mixpanel Engage without altering $last_seen
// - pass option `$ignore_time: true` to prevent the $last_seen property from being updated
mixpanel.people.set("billybob", {
    plan: "premium",
    games_played: 1,
    $ignore_time: true
});

// set a single property on a user
mixpanel.people.set("billybob", "plan", "free");

// increment a numeric property
mixpanel.people.increment("billybob", "games_played");

// increment a numeric property by a different amount
mixpanel.people.increment("billybob", "points", 15);

// increment multiple properties
mixpanel.people.increment("billybob", {"points": 10, "games_played": 1});

// append value to a list
mixpanel.people.append("billybob", "awards", "Great Player");

// append multiple values to a list
mixpanel.people.append("billybob", {"awards": "Great Player", "levels_finished": "Level 4"});

// merge value to a list (ignoring duplicates)
mixpanel.people.union("billybob", {"browsers": "ie"});

// merge multiple values to a list (ignoring duplicates)
mixpanel.people.union("billybob", {"browsers": ["ie", "chrome"]});


// record a transaction for revenue analytics
mixpanel.people.track_charge("billybob", 39.99);

// clear a users transaction history
mixpanel.people.clear_charges("billybob");

// delete a user
mixpanel.people.delete_user("billybob");

// Create an alias for an existing distinct id
mixpanel.alias("distinct_id", "your_alias");

// all functions that send data to mixpanel take an optional
// callback as the last argument
mixpanel.track("test", function(err) { if (err) throw err; });

// import an old event
var mixpanel_importer = Mixpanel.init('valid mixpanel token', {
    key: "valid api key for project"
});

// needs to be in the system once for it to show up in the interface
mixpanel_importer.track('old event', { gender: '' });

mixpanel_importer.import("old event", new Date(2012, 4, 20, 12, 34, 56), {
    distinct_id: 'billybob',
    gender: 'male'
});

// import multiple events at once
mixpanel_importer.import_batch([
    {
        event: 'old event',
        properties: {
            time: new Date(2012, 4, 20, 12, 34, 56),
            distinct_id: 'billybob',
            gender: 'male'
        }
    },
    {
        event: 'another old event',
        properties: {
            time: new Date(2012, 4, 21, 11, 33, 55),
            distinct_id: 'billybob',
            color: 'red'
        }
    }
]);

FAQ

Where is mixpanel.identify()?

mixpanel-node is a server-side library, optimized for stateless shared usage; e.g., in a web application, the same mixpanel instance is used across requests for all users. Rather than setting a distinct_id through identify() calls like Mixpanel client-side libraries (where a single Mixpanel instance is tied to a single user), this library requires you to pass the distinct_id with every tracking call. See https://github.com/mixpanel/mixpanel-node/issues/13.

How do I get or set superproperties?

See the previous answer: the library does not maintain user state internally and so has no concept of superproperties for individual users. If you wish to preserve properties for users between requests, you will need to load these properties from a source specific to your app (e.g., your session store or database) and pass them explicitly with each tracking call.

Tests

# in the mixpanel directory
npm install
npm test

Attribution/Credits

Heavily inspired by the original js library copyright Mixpanel, Inc. (http://mixpanel.com/)

Copyright (c) 2014-15 Mixpanel Original Library Copyright (c) 2012-14 Carl Sverre

Contributions from:

License

Released under the MIT license. See file called LICENSE for more details.

changelog

0.3.2 / 2015-12-10

  • correct $delete field in people.delete_user request (thanks godspeedelbow)

0.3.1 / 2015-08-06

  • added config option for API host (thanks gmichael225)

0.3.0 / 2015-08-06

  • added people.union support (thanks maeldur)

0.2.0 / 2015-04-14

  • added batch import support

0.1.1 / 2015-03-27

  • fixed callback behavior in track_charges when no properties supplied (thanks sorribas)

0.1.0 / 2015-03-20

  • updated URL metadata (thanks freeall)
  • updated dev dependencies
  • added builds for iojs, node 0.12, dropped support for node <0.10

0.0.20 / 2014-05-11

  • removed hardcoded port 80 for more flexibility (thanks zeevl)

0.0.19 / 2014.04.03

  • added people.append (thanks jylauril)

0.0.18 / 2013-08-23

  • added callback to alias (thanks to sandinmyjoints)
  • added verbose config option (thanks to sandinmyjoints)
  • added unset method (thanks to lukapril)

0.0.17 / 2013-08-12

  • added alias method (thanks to PierrickP)

0.0.16 / 2013-06-29

  • allow special key "ip" to be 0 in people.set (thanks to wwlinx)

0.0.15 / 2013-05-24

  • adds set once functionality to people (thanks to avoid3d)
  • $ignore_time in people.set (thanks to Rick Cotter)

0.0.14 / 2013-03-28

  • revert Randal's http only patch since Mixpanel indeed supports https.
  • handles the ip property in a property object properly for people calls

0.0.13 / 2013-03-25

  • force requests to go over http [reverted in 0.0.14]

0.0.12 / 2013-01-24

  • track_charge() no longer includes $time by default, rather it lets Mixpanel's servers set the time when they receive the transaction. This doesn't modify the ability for the user to pass in their own $time (for importing transactions).

0.0.11 / 2013-01-11

  • added track_charge() method which provides the ability to record user transactions for revenue analytics.
  • added clear_charges() method which provides the ability to remove a users transactions from Mixpanel
  • added tests for delete_user()

0.0.10 / 2012-11-26

  • added import() method which provides the ability to import events older than 5 days. Contributions from Thomas Watson Steen.

0.0.9 / 2012-11-15

  • removed time from properties sent to server. This is to ensure that UTC is always used. Mixpanel will set the correct time as soon as they receive the event.

0.0.8 / 2012-10-24

  • added mp_lib property, so people can segment by library

0.0.7 / 2012-01-05

  • added unit tests
  • people.increment() only prints error message if debug is true

0.0.6 / 2012-01-01

  • added engage support
    • people.set()
    • people.increment()
    • people.delete_user()
  • deprecated old constructor: require("mixpanel").Client(token)
  • added new constructor: require("mixpanel").init(token)