Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

new import method to handle importation of old events

  • Loading branch information...
commit c323213f61ec88c8b0208f99e890e691616ad6d9 1 parent 30441f5
@carlsverre carlsverre authored
Showing with 113 additions and 24 deletions.
  1. +14 −5 example.js
  2. +46 −10 lib/mixpanel-node.js
  3. +14 −5 readme.md
  4. +39 −4 test/import.js
View
19 example.js
@@ -14,11 +14,6 @@ mixpanel.track("my event", {
});
mixpanel.track("played_game");
-// import old event
-mixpanel.track("my event", {
- time: new Date(2012, 4, 20, 12, 34, 56)
-});
-
// create or update a user in Mixpanel Engage
mixpanel.people.set("billybob", {
$first_name: "Billy",
@@ -46,3 +41,17 @@ mixpanel.people.delete_user("billybob");
// all functions that send data to mixpanel take an optional
// callback as the last argument
mixpanel.track("test", function(err) { if (err) throw err; });
+
+// import an old event
+var mixpanel_importer = Mixpanel.init('valid mixpanel token', {
+ key: "valid api key for project"
+});
+mixpanel_importer.set_config({ debug: true });
+
+// 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'
+});
View
56 lib/mixpanel-node.js
@@ -15,7 +15,7 @@ var create_client = function(token, config) {
var metrics = {};
if(!token) {
- throw new Error("The Mixpanel Client needs a Mixpanel token");
+ throw new Error("The Mixpanel Client needs a Mixpanel token: `init(token)`");
}
metrics.config = {
@@ -43,10 +43,11 @@ var create_client = function(token, config) {
};
if (endpoint === '/import') {
- var key = metrics.config.key;
- if (!key)
- throw new Error("The Mixpanel Client needs a Mixpanel api key when importing old events: `init(token, { key: ... })`");
- request_data.api_key = key;
+ var key = metrics.config.key;
+ if (!key) {
+ throw new Error("The Mixpanel Client needs a Mixpanel api key when importing old events: `init(token, { key: ... })`");
+ }
+ request_data.api_key = key;
}
var request_options = {
@@ -83,8 +84,6 @@ var create_client = function(token, config) {
track(event, properties, callback)
---
this function sends an event to mixpanel.
-
- If specifying the `time` property, the /import endpoint will be used.
event:string the event name
properties:object additional event properties to send
@@ -97,8 +96,8 @@ var create_client = function(token, config) {
properties = {};
}
- // if an old event was given, use import endpoint
- var endpoint = properties.time ? '/import' : '/track';
+ // if properties.time exists, use import endpoint
+ var endpoint = (typeof(properties.time) === 'number') ? '/import' : '/track';
properties.token = metrics.token;
properties.mp_lib = "node";
@@ -108,7 +107,7 @@ var create_client = function(token, config) {
'properties' : properties
};
- if(metrics.config.debug) {
+ if (metrics.config.debug) {
console.log("Sending the following event to Mixpanel:");
console.log(data);
}
@@ -116,6 +115,43 @@ var create_client = function(token, config) {
metrics.send_request(endpoint, data, callback);
};
+ /**
+ import(event, properties, callback)
+ ---
+ This function sends an event to mixpanel using the import
+ endpoint. The time argument should be either a Date or Number,
+ and should signify the time the event occurred.
+
+ It is highly recommended that you specify the distinct_id
+ property for each event you import, otherwise the events will be
+ tied to the IP address of the sending machine.
+
+ For more information look at:
+ https://mixpanel.com/docs/api-documentation/importing-events-older-than-31-days
+
+ event:string the event name
+ time:date|number the time of the event
+ properties:object additional event properties to send
+ callback:function(err:Error) callback is called when the request is
+ finished or an error occurs
+ */
+ metrics.import = function(event, time, properties, callback) {
+ if (typeof(properties) === 'function' || !properties) {
+ callback = properties;
+ properties = {};
+ }
+
+ if (time === void 0) {
+ throw new Error("The import method requires you to specify the time of the event");
+ } else if (Object.prototype.toString.call(time) === '[object Date]') {
+ time = Math.floor(time.getTime() / 1000);
+ }
+
+ properties.time = time;
+
+ metrics.track(event, properties, callback);
+ };
+
metrics.people = {
/**
people.set(distinct_id, prop, to, callback)
View
19 readme.md
@@ -27,11 +27,6 @@ Quick Start
});
mixpanel.track("played_game");
- // import old event
- mixpanel.track("my event", {
- time: new Date(2012, 4, 20, 12, 34, 56)
- });
-
// create or update a user in Mixpanel Engage
mixpanel.people.set("billybob", {
$first_name: "Billy",
@@ -60,6 +55,19 @@ Quick Start
// callback as the last argument
mixpanel.track("test", function(err) { if (err) throw err; });
+ // import an old event
+ var mixpanel_importer = Mixpanel.init('valid mixpanel token', {
+ key: "valid api key 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'
+ });
+
Tests
-----
@@ -79,6 +87,7 @@ Contributions from:
- [Andres Gottlieb](https://github.com/andresgottlieb)
- [Ken Perkins](https://github.com/kenperkins)
- [Nathan Rajlich](https://github.com/TooTallNate)
+ - [Thomas Watson Steen](https://github.com/watson)
License
-------------------
View
43 test/import.js
@@ -1,7 +1,7 @@
var Mixpanel = require('../lib/mixpanel-node'),
Sinon = require('sinon');
-exports.track = {
+exports.import = {
setUp: function(next) {
this.mixpanel = Mixpanel.init('token', { key: 'key' });
this.clock = Sinon.useFakeTimers();
@@ -20,7 +20,32 @@ exports.track = {
"calls send_request with correct endpoint and data": function(test) {
var event = "test",
- props = { key1: 'val1', time: '0' },
+ time = 500,
+ props = { key1: 'val1' },
+ expected_endpoint = "/import",
+ expected_data = {
+ event: 'test',
+ properties: {
+ key1: 'val1',
+ token: 'token',
+ time: 500
+ }
+ };
+
+ this.mixpanel.import(event, time, props);
+
+ test.ok(
+ this.mixpanel.send_request.calledWithMatch(expected_endpoint, expected_data),
+ "import didn't call send_request with correct arguments"
+ );
+
+ test.done();
+ },
+
+ "supports a Date instance": function(test) {
+ var event = "test",
+ time = new Date,
+ props = { key1: 'val1' },
expected_endpoint = "/import",
expected_data = {
event: 'test',
@@ -31,11 +56,21 @@ exports.track = {
}
};
- this.mixpanel.track(event, props);
+ this.mixpanel.import(event, time, props);
test.ok(
this.mixpanel.send_request.calledWithMatch(expected_endpoint, expected_data),
- "track didn't call send_request with correct arguments"
+ "import didn't call send_request with correct arguments"
+ );
+
+ test.done();
+ },
+
+ "requires the time argument": function(test) {
+ test.throws(
+ function() { this.mixpanel.import('test'); },
+ "The import method requires you to specify the time of the event",
+ "import didn't throw an error when time wasn't specified"
);
test.done();

0 comments on commit c323213

Please sign in to comment.
Something went wrong with that request. Please try again.