Comparing version 0.0.17 to 0.0.18
@@ -12,16 +12,18 @@ /* | ||
querystring = require('querystring'), | ||
Buffer = require('buffer').Buffer; | ||
Buffer = require('buffer').Buffer, | ||
util = require('util'); | ||
var create_client = function(token, config) { | ||
var metrics = {}; | ||
if(!token) { | ||
throw new Error("The Mixpanel Client needs a Mixpanel token: `init(token)`"); | ||
} | ||
metrics.config = { | ||
test: false, | ||
debug: false | ||
debug: false, | ||
verbose: false | ||
}; | ||
metrics.token = token; | ||
@@ -33,3 +35,3 @@ | ||
this function sends an async GET request to mixpanel | ||
data:object the data to send in the request | ||
@@ -44,3 +46,4 @@ callback:function(err:Error) callback is called when the request is | ||
'data': event_data.toString('base64'), | ||
'ip': 0 | ||
'ip': 0, | ||
'verbose': metrics.config.verbose ? 1 : 0 | ||
}; | ||
@@ -55,3 +58,3 @@ | ||
} | ||
var request_options = { | ||
@@ -62,9 +65,9 @@ host: 'api.mixpanel.com', | ||
}; | ||
if (metrics.config.test) { request_data.test = 1; } | ||
var query = querystring.stringify(request_data); | ||
request_options.path = [endpoint,"?",query].join(""); | ||
http.get(request_options, function(res) { | ||
@@ -75,5 +78,20 @@ var data = ""; | ||
}); | ||
res.on('end', function() { | ||
var e = (data !== '1') ? new Error("Mixpanel Server Error: " + data) : undefined; | ||
var e; | ||
if(metrics.config.verbose) { | ||
try { | ||
var result = JSON.parse(data); | ||
if(result.status != 1) { | ||
e = new Error("Mixpanel Server Error: " + result.error); | ||
} | ||
} | ||
catch(ex) { | ||
e = new Error("Could not parse response from Mixpanel"); | ||
} | ||
} | ||
else { | ||
e = (data !== '1') ? new Error("Mixpanel Server Error: " + data) : undefined; | ||
} | ||
callback(e); | ||
@@ -88,3 +106,3 @@ }); | ||
}; | ||
/** | ||
@@ -94,3 +112,3 @@ track(event, properties, callback) | ||
this function sends an event to mixpanel. | ||
event:string the event name | ||
@@ -117,3 +135,3 @@ properties:object additional event properties to send | ||
}; | ||
if (metrics.config.debug) { | ||
@@ -123,3 +141,3 @@ console.log("Sending the following event to Mixpanel:"); | ||
} | ||
metrics.send_request(endpoint, data, callback); | ||
@@ -176,3 +194,3 @@ }; | ||
*/ | ||
metrics.alias = function(distinct_id, alias) { | ||
metrics.alias = function(distinct_id, alias, callback) { | ||
var properties = { | ||
@@ -183,3 +201,3 @@ distinct_id: distinct_id, | ||
metrics.track('$create_alias', properties); | ||
metrics.track('$create_alias', properties, callback); | ||
}; | ||
@@ -217,5 +235,5 @@ | ||
set properties on an user record in engage | ||
usage: | ||
mixpanel.people.set('bob', 'gender', 'm'); | ||
@@ -265,3 +283,3 @@ | ||
} | ||
metrics.send_request('/engage', data, callback); | ||
@@ -274,3 +292,3 @@ }, | ||
increment/decrement properties on an user record in engage | ||
usage: | ||
@@ -327,3 +345,3 @@ | ||
} | ||
metrics.send_request('/engage', data, callback); | ||
@@ -409,3 +427,3 @@ }, | ||
delete an user record in engage | ||
usage: | ||
@@ -425,7 +443,47 @@ | ||
} | ||
metrics.send_request('/engage', data, callback); | ||
}, | ||
/** | ||
people.unset(distinct_id, prop, callback) | ||
--- | ||
delete a property on an user record in engage | ||
usage: | ||
mixpanel.people.unset('bob', 'page_views'); | ||
mixpanel.people.unset('bob', ['page_views', 'last_login']); | ||
*/ | ||
unset: function(distinct_id, prop, callback) { | ||
var $unset = []; | ||
if (util.isArray(prop)) { | ||
$unset = prop; | ||
} else if (typeof(prop) === 'string') { | ||
$unset = [prop]; | ||
} else { | ||
if (metrics.config.debug) { | ||
console.error("Invalid argument passed to mixpanel.people.unset - must be a string or array"); | ||
console.error("Passed: " + prop); | ||
} | ||
return; | ||
} | ||
data = { | ||
'$unset': $unset, | ||
'$token': metrics.token, | ||
'$distinct_id': distinct_id | ||
}; | ||
if(metrics.config.debug) { | ||
console.log("Sending the following data to Mixpanel (Engage):"); | ||
console.log(data); | ||
} | ||
metrics.send_request('/engage', data, callback); | ||
} | ||
}; | ||
/** | ||
@@ -435,3 +493,3 @@ set_config(config) | ||
Modifies the mixpanel config | ||
config:object an object with properties to override in the | ||
@@ -451,3 +509,3 @@ mixpanel client config | ||
} | ||
return metrics; | ||
@@ -454,0 +512,0 @@ }; |
@@ -5,3 +5,3 @@ { | ||
"keywords": ["mixpanel", "analytics", "api", "stats"], | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"homepage": "https://github.com/carlsverre/mixpanel-node", | ||
@@ -8,0 +8,0 @@ "author": "Carl Sverre", |
@@ -101,2 +101,4 @@ Mixpanel-node | ||
- [PierrickP](https://github.com/PierrickP) | ||
- [lukapril](https://github.com/lukapril) | ||
- [sandinmyjoints](https://github.com/sandinmyjoints) | ||
@@ -103,0 +105,0 @@ License |
@@ -11,3 +11,3 @@ var Mixpanel = require('../lib/mixpanel-node'); | ||
test.deepEqual(this.mixpanel.config, | ||
{ test: false, debug: false }, | ||
{ test: false, debug: false, verbose: false }, | ||
"default config is incorrect"); | ||
@@ -14,0 +14,0 @@ test.done(); |
@@ -290,2 +290,52 @@ var Mixpanel = require('../lib/mixpanel-node'), | ||
unset: { | ||
"calls send_request with correct endpoint and data": function(test) { | ||
var expected_data = { | ||
$unset: ['key1'], | ||
$token: this.token, | ||
$distinct_id: this.distinct_id | ||
}; | ||
this.mixpanel.people.unset(this.distinct_id, 'key1'); | ||
test.ok( | ||
this.mixpanel.send_request.calledWithMatch(this.endpoint, expected_data), | ||
"people.unset didn't call send_request with correct arguments" | ||
); | ||
test.done(); | ||
}, | ||
"supports being called with a property array": function(test) { | ||
var prop = ['key1', 'key2'], | ||
expected_data = { | ||
$unset: prop, | ||
$token: this.token, | ||
$distinct_id: this.distinct_id | ||
}; | ||
this.mixpanel.people.unset(this.distinct_id, prop); | ||
test.ok( | ||
this.mixpanel.send_request.calledWithMatch(this.endpoint, expected_data), | ||
"people.unset didn't call send_request with correct arguments" | ||
); | ||
test.done(); | ||
}, | ||
"errors on other argument types": function(test) { | ||
this.mixpanel.people.unset(this.distinct_id, { key1:'val1', key2:'val2' }); | ||
this.mixpanel.people.unset(this.distinct_id, 1231241.123); | ||
test.ok( | ||
!this.mixpanel.send_request.called, | ||
"people.unset shouldn't call send_request on invalid arguments" | ||
); | ||
test.done(); | ||
} | ||
} | ||
}; |
@@ -42,3 +42,3 @@ var Mixpanel = require('../lib/mixpanel-node'), | ||
headers: {}, | ||
path: '/track?data=eyJldmVudCI6InRlc3QiLCJwcm9wZXJ0aWVzIjp7ImtleTEiOiJ2YWwxIiwidG9rZW4iOiJ0b2tlbiIsInRpbWUiOjEzNDY4NzY2MjF9fQ%3D%3D&ip=0' | ||
path: '/track?data=eyJldmVudCI6InRlc3QiLCJwcm9wZXJ0aWVzIjp7ImtleTEiOiJ2YWwxIiwidG9rZW4iOiJ0b2tlbiIsInRpbWUiOjEzNDY4NzY2MjF9fQ%3D%3D&ip=0&verbose=0' | ||
}; | ||
@@ -45,0 +45,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44694
953
109