Skip to content

Instantly share code, notes, and snippets.

@nolanamy
Forked from ivarvong/gist:3763494
Last active August 29, 2015 14:20
Show Gist options
  • Save nolanamy/dc29283c2eb77e353c0c to your computer and use it in GitHub Desktop.
Save nolanamy/dc29283c2eb77e353c0c to your computer and use it in GitHub Desktop.
Mixpanel raw data export
var crypto = require('crypto');
var md5 = crypto.createHash('md5');
var request = require('request');
var api_key = 'your_api_key_here';
var api_secret = 'your_api_secret_here';
var api_endpoint = 'https://data.mixpanel.com/api/2.0/';
var current_time = parseInt((new Date().getTime())/1000);
var expire_time = current_time + 1000; // 1000 seconds in the future is the expire time.
var buildQuery = function(endpoint, params) {
params['format'] = 'json';
params['api_key'] = api_key;
params['expire'] = expire_time;
var paramKeys = Object.keys(params);
paramKeys.sort();
var paramString = "";
var paramStringToHash = "";
paramKeys.forEach(function (key) {
paramString += key + "=" + escape(params[key]) + "&";
paramStringToHash += key + "=" + params[key];
});
paramStringToHash += api_secret;
var signature = md5.update(paramStringToHash).digest("hex");
var url = api_endpoint + endpoint + '/?';
url += paramString;
url += "sig=" + signature; // no '&' because building paramString leaves one on the end
console.log(url);
request(url, function(err, res, body) {
if (err) {
console.error(err);
} else if (res.statusCode != 200) {
console.error('error: ' + JSON.parse(body).error);
} else {
var events = [];
var bodyParts = body.trim().split('\n');
bodyParts.forEach(function (bodyPart) {
events.push(JSON.parse(bodyPart));
});
processEvents(events);
}
});
}
function processEvents(events) {
console.log(events);
}
buildQuery(
'export',
{
'from_date': '2015-04-26',
'to_date': '2015-04-27',
'event': '["Sign in error"]'
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment