Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/export endpoint -v2 API #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion lib/mixpanel-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,53 @@ var create_client = function(token, config) {

metrics.token = token;

function calc_sig(params, secret) {
var hash_string = '';

// Sort by keys
Object.keys(params).sort().forEach(function(key) {
// Concatenate each subpart for form a string like 'a=bc=d...'
hash_string += key + '=' + params[key];
});

return require('crypto')
.createHash('md5')
.update(hash_string + secret, 'utf8')
.digest('hex');
}

/**
send_request_v2(endpoint, params, callback)
---
this function sends an async GET request to mixpanel using the V2 API
(needed for /export endpoint).

endpoint:string endpoint to use
params:object query string params to send in the request
callback:function(err:Error) callback is called when the request is
finished or an error occurs
*/
metrics.send_request_v2 = function (endpoint, params, callback) {
params = params || {};

if (!metrics.config.key || !metrics.config.secret) {
throw new Error("The Mixpanel Client needs a Mixpanel api key and secret for this request: `init(token, { key: ..., secret: ... })`");
}

// Add in auth stuff
params.api_key = metrics.config.key;
params.expire = Math.floor(new Date().getTime() / 1000) + 60; // add one minute for the request to run.
params.sig = calc_sig(params, metrics.config.secret);

var request_options = {
host: endpoint !== '/export' ? 'mixpanel.com' : 'data.mixpanel.com',
headers: {},
path: ['/api/2.0', endpoint, "?", querystring.stringify(params)].join("")
};

do_request(request_options, callback);
};

/**
send_request(data)
---
Expand Down Expand Up @@ -60,10 +107,15 @@ var create_client = function(token, config) {

if (metrics.config.test) { request_data.test = 1; }


var query = querystring.stringify(request_data);

request_options.path = [endpoint,"?",query].join("");

do_request(request_options, callback);
};

function do_request(request_options, callback) {
http.get(request_options, function(res) {
var data = "";
res.on('data', function(chunk) {
Expand Down Expand Up @@ -95,7 +147,7 @@ var create_client = function(token, config) {
}
callback(e);
});
};
}

/**
track(event, properties, callback)
Expand Down Expand Up @@ -169,6 +221,19 @@ var create_client = function(token, config) {
metrics.track(event, properties, callback);
};

metrics.export = function(from_date, to_date, properties, callback) {
if (typeof(properties) === 'function') {
callback = properties;
properties = undefined;
}

properties = properties || {};
properties.from_date = from_date.toISOString().slice(0,10); // convert to 'yyyy-mm-dd' format
properties.to_date = to_date.toISOString().slice(0,10);

metrics.send_request_v2('/export', properties, callback);
};

/**
alias(distinct_id, alias)
---
Expand Down Expand Up @@ -540,3 +605,5 @@ module.exports = {
},
init: create_client
};

// vim: set et sw=4 ts=4:
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"name": "mixpanel",
"description": "A simple API for mixpanel",
"keywords": ["mixpanel", "analytics", "api", "stats"],
"keywords": [
"mixpanel",
"analytics",
"api",
"stats"
],
"version": "0.0.20",
"homepage": "https://github.com/carlsverre/mixpanel-node",
"author": "Carl Sverre",
Expand All @@ -10,8 +15,8 @@
"lib": "lib"
},
"repository": {
"type": "git",
"url": "http://github.com/carlsverre/mixpanel-node.git"
"type": "git",
"url": "http://github.com/carlsverre/mixpanel-node.git"
},
"engines": {
"node": ">=0.6.0"
Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ Quick Start
gender: 'male'
});

// export events
var mixpanel_exporter = Mixpanel.init('valid mixpanel token', {
key: "valid api key for project"
secret: "api secret for project"
});

mixpanel_exporter.export(new Date(2012, 4, 20), new Date(2012, 4, 23), {
event: 'thing'
}, callback);

Tests
-----

Expand Down
44 changes: 44 additions & 0 deletions test/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var Mixpanel = require('../lib/mixpanel-node'),
Sinon = require('sinon');

exports.import = {
setUp: function(next) {
this.mixpanel = Mixpanel.init('token', { key: 'key' });
this.clock = Sinon.useFakeTimers();

Sinon.stub(this.mixpanel, 'send_request_v2');

next();
},

tearDown: function(next) {
this.mixpanel.send_request_v2.restore();
this.clock.restore();

next();
},

"calls send_request with correct endpoint and data": function(test) {
var event = "test",
fromDate = new Date('2014-01-01'),
toDate = new Date('2014-02-01'),
props = { event: 'myEvent' },
expected_endpoint = "/export",
expected_params = {
event: 'myEvent',
from_date: '2014-01-01',
to_date: '2014-02-01'
};

this.mixpanel.export(fromDate, toDate, props);

test.ok(
this.mixpanel.send_request_v2.calledWithMatch(expected_endpoint, expected_params),
"export didn't call send_request with correct arguments"
);

test.done();
}
};

// vim: set et sw=4 tw=4:
51 changes: 51 additions & 0 deletions test/send_request_v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var Mixpanel = require('../lib/mixpanel-node'),
Sinon = require('sinon'),
http = require('http'),
events = require('events');

var clock;
exports.send_request_v2 = {
setUp: function(next) {
this.mixpanel = Mixpanel.init('token', {key: 'pretty', secret: 'really'});

Sinon.stub(http, 'get');
clock = Sinon.useFakeTimers();

this.http_emitter = new events.EventEmitter();
this.res = new events.EventEmitter();

http.get.returns(this.http_emitter);
http.get.callsArgWith(1, this.res);

next();
},

tearDown: function(next) {
http.get.restore();
clock.restore();

next();
},

"sends correct data": function(test) {
var endpoint = "/export",
data = {
one: 'two',
three: 'four'
};

var expected_http_get = {
host: 'data.mixpanel.com',
headers: {},
path: '/api/2.0/export?one=two&three=four&api_key=pretty&expire=60&sig=25ce6fe76d7d1250165f58b0e30432ce'
};

this.mixpanel.send_request_v2(endpoint, data);

test.ok(http.get.calledWithMatch(expected_http_get), "send_request_v2 didn't call http.get with correct arguments, got:" + JSON.stringify(http.get.getCall(0).args[0]));

test.done();
}
};

// vim: set et sw=4 ts=4: