-
-
Save nolanamy/dc29283c2eb77e353c0c to your computer and use it in GitHub Desktop.
Mixpanel raw data export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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