• Stars
    star
    463
  • Rank 94,661 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 14 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A node.js API for mixpanel

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('<YOUR_TOKEN>');

// initialize mixpanel client configured to communicate over http instead of https
var mixpanel = Mixpanel.init('<YOUR_TOKEN>', {
    protocol: 'http',
});

// turn off keepAlive (reestablish connection on each request)
var mixpanel = Mixpanel.init('<YOUR_TOKEN>', {
    keepAlive: false,
});

// 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');

// set an IP address to get automatic geolocation info
mixpanel.track('my event', {ip: '127.0.0.1'});

// track an event with a specific timestamp (up to 5 days old;
// use mixpanel.import() for older events)
mixpanel.track('timed event', {time: new Date()});

// 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 user profile's IP address to get automatic geolocation info
mixpanel.people.set('billybob', {
    plan: 'premium',
    games_played: 1
}, {
    $ip: '127.0.0.1'
});

// set a user profile's latitude and longitude to get automatic geolocation info
mixpanel.people.set('billybob', {
    plan: 'premium',
    games_played: 1
}, {
    $latitude: 40.7127753,
    $longitude: -74.0059728
});

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

// set a single property on a user, don't override
mixpanel.people.set_once('billybob', 'first_game_play', (new Date('jan 1 2013')).toISOString());

// 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');

// delete a user in Mixpanel Engage without altering $last_seen or resolving aliases
// - pass option $ignore_time: true to prevent the $last_seen property from being updated
// (useful if you subsequently re-import data for the same distinct ID)
mixpanel.people.delete_user('billybob', {$ignore_time: true, $ignore_alias: true});

// 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; });

// track multiple events at once
mixpanel.track_batch([
    {
        event: 'recent event',
        properties: {
            time: new Date(),
            distinct_id: 'billybob',
            gender: 'male'
        }
    },
    {
        event: 'another recent event',
        properties: {
            distinct_id: 'billybob',
            color: 'red'
        }
    }
]);

// import an old event
var mixpanel_importer = Mixpanel.init('valid mixpanel token', {
    secret: 'valid api secret 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 #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

Alternative Clients and Related Tools

Attribution/Credits

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

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

Contributions from:

License

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

More Repositories

1

mixpanel-iphone

Official iOS (Objective-C) Tracking Library for Mixpanel Analytics
Objective-C
1,027
star
2

mixpanel-android

Official Android Tracking Library for Mixpanel Analytics
Java
1,017
star
3

mixpanel-js

Official Mixpanel JavaScript Client Library
JavaScript
823
star
4

mixpanel-swift

Official iOS (Swift) Tracking Library for Mixpanel Analytics
Swift
395
star
5

panel

Web Components + Virtual DOM: web standards for powerful UIs
JavaScript
273
star
6

mixpanel-ruby

Ruby
170
star
7

mixpanel-php

PHP
134
star
8

mixpanel-react-native

Official React Native Tracking Library for Mixpanel Analytics
JavaScript
103
star
9

mixpanel-python

Official Mixpanel Python library.
Python
101
star
10

mixpanel-utils

Python
80
star
11

mixpanel-flutter

Official Flutter Tracking Library for Mixpanel Analytics
Dart
66
star
12

sample-android-mixpanel-integration

Java
55
star
13

tracking-proxy

One-click configuration to proxy tracking requests to Mixpanel's API
Dockerfile
49
star
14

mixpanel-java

Java
48
star
15

mixpanel-unity

Official Unity Tracking Library for Mixpanel Analytics
C#
37
star
16

docs

Mixpanel's Official Documentation
MDX
35
star
17

fuzzbunny

Fast fuzzy string searching/matching/highlighting
JavaScript
19
star
18

panel-farm

Manage cute animals with Web Components and Virtual DOM very wow
JavaScript
10
star
19

snabbdom-jsx-lite

Write snabbdom templates in .jsx or .tsx (JSX for TypeScript)
TypeScript
10
star
20

flask-tracking-proxy

Example Python Flask application to proxy tracking requests to Mixpanel's API
Python
10
star
21

configmanager

A library for adding dynamic configuration to your code
Go
9
star
22

webcomponent

Lightweight utilities for constructing web components
JavaScript
8
star
23

webpack-dev-server-status-bar

Unobtrusive HTML status indicator for Webpack compilation status
JavaScript
7
star
24

mixpanel-gtm-template

A GitHub project created under the Mixpanel organization to store the Mixpanel GTM template
Smarty
6
star
25

obs

Opinionated observability package that combines logging, tracing and metrics.
Go
6
star
26

mixpanel-go

Mixpanel Official Go SK
Go
4
star
27

framesg

request/response communication to/from iframes
JavaScript
4
star
28

sheets

🔄 integrate mixpanel with google sheets
JavaScript
3
star
29

mixpanel-chrome-extension

This extension will inject the Mixpanel library into every page you visit
JavaScript
3
star
30

domsuite

JavaScript browser testing/automation utilities with async/await
JavaScript
2
star
31

mixpanel-js-wrapper

A GitHub project created under the Mixpanel organization to store the Mixpanel JS wrapper
JavaScript
2
star
32

django-prefetch-example

Python
1
star
33

dedupe-chunks-webpack-plugin

(make public later)
JavaScript
1
star