Instantly share code, notes, and snippets.

What would you like to do?
Mixpanel Library CheatSheet -->
// Mixpanel Cheatsheet
// This requires the mixpanel javascript library to be embedded on your site
// 1. API Methods.
mixpanel.init('new token', { your: 'config' }, 'library_name'); // initialize a new instance of the Mixpanel tracking object
mixpanel.push(['register', { a: 'b' }]); // push() keeps the standard async-array-push behavior around after the lib is loaded. This is only useful for external integrations that do not wish to rely on our convenience methods (created in the snippet).
mixpanel.disable('eventToDisable'); // Disable events on the Mixpanel object. If passed no arguments, this function disables tracking of any event. If passed an array of event names, those events will be disabled, but other events will continue to be tracked.
mixpanel.track('Registered', {'Gender': 'Male', 'Age': 21}, callback); // Track an event.
mixpanel.track_links('#nav', 'Clicked Nav Link', {'Gender': 'Male', 'Age': 21}); // Track clicks on a set of document elements.
mixpanel.track_forms('#register', 'Created Account', {'Gender': 'Male', 'Age': 21}); // Tracks form submissions.
mixpanel.register({'MySuperProperties': 'Value'}, 2); // Register a set of super properties, which are included with all events. This will overwrite previous super property values.
mixpanel.register_once({'MySuperProperties': 'Value'}, 'None', 2); // Register a set of super properties only once. This will not overwrite previous super property values, unlike register().
mixpanel.unregister('MySuperProperties'); // Delete a super property stored with the current user.
mixpanel.identify("13483"); // Identify a user with a unique id. All subsequent actions caused by this user will be tied to this identity.
mixpanel.get_distinct_id(); // Returns the current distinct id of the user. This is either the id automatically generated by the library or the id that has been passed by a call to mixpanel.identify.
mixpanel.alias("new_id", "existing_id"); // Create an alias, which Mixpanel will use to link two distinct_ids going forward (not retroactively).
mixpanel.get_property('property_name'); // Returns the value of the super property named property_name. If no such property is set, get_property will return the undefined value.
mixpanel.people.set('gender', 'm', callback); // Set properties on a user record.
mixpanel.people.set_once('First Login Date', new Date(), callback); // Set properties on a user record, only if they do not yet exist.
mixpanel.people.increment('points', 5); // Increment/decrement numeric people analytics properties.
mixpanel.people.append('pages_visited', 'homepage', callback); // Append a value to a list-valued people analytics property.
mixpanel.people.delete_user(); // Permanently deletes the current people analytics profile from Mixpanel (using the current distinct_id).
mixpanel.people.track_charge(30.50, { // Record that you have charged the current user a certain amount of money. Charges recorded with track_charge will appear in the Mixpanel revenue report.
'$time': new Date('jan 1 2012') // Charge a user $30.50 on the 2nd of january
}, callback);
mixpanel.people.clear_charges(callback); // Permanently clear all revenue report transactions from the current user's people analytics profile.
// 2. Library Configuration.
mixpanel.get_config(); // Returns the current config object for the library.
mixpanel.config({ // Update the configuration of a mixpanel library instance. The default config is:
// super properties span subdomains
cross_subdomain_cookie: true,
// super properties cookie name
cookie_name: "",
// super properties cookie expiration (in days)
cookie_expiration: 365,
// should we track a page view on page load
track_pageview: true,
// the amount of time track_links will
// wait for Mixpanel's servers to respond
track_links_timeout: 300,
// if this is true, the mixpanel cookie will be deleted,
// and no user persistence will take place
disable_cookie: false,
// if this is true, the mixpanel cookie will be marked as
// secure, meaning it will only be transmitted over https
secure_cookie: false,
// if you set upgrade to be true, the library will check for a
// cookie from our old js library and import super
// properties from it, then the old cookie is deleted
// The upgrade config option only works in the initialization,
// so make sure you set it when you create the library.
upgrade: false

This comment has been minimized.

facundofarias commented Jul 7, 2015

Based on the docs, config is now called set_config:
Thanks 👍


This comment has been minimized.

sylvaincha commented Aug 24, 2016

New way to configure it since 24/08/2016 without coding, copy/past your personal tracking code in the head section of your website and in the header file if you use cms like prestashop and follow the instructions here : to track custom events, remember to disable your adblock ;)


This comment has been minimized.

rugarte commented May 11, 2017

Thanks for the great resource! Mixpanel surprisingly hasn't changed much over the last couple of years. There are a few additions for anyone who is reading this now: mixpanel.people.union, and mixpanel.time_event (handy for tracking duration of events).

Small plug but if you're struggling to get Mixpanel implemented, you might find these short videos helpful. They cover all the core concepts (for the Javascript library) and how to use the basic reports:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment