Comparing version 0.0.14 to 0.0.15
@@ -30,2 +30,5 @@ // grab the Mixpanel factory | ||
// set a single property on a user, don't override | ||
mixpanel.people.set_once("billybob", "first_game_play", (new Date('jan 1 2013')).toISOString()); | ||
// increment a numeric property | ||
@@ -32,0 +35,0 @@ mixpanel.people.increment("billybob", "games_played"); |
@@ -156,2 +156,27 @@ /* | ||
metrics.people = { | ||
/** people.set_once(distinct_id, prop, to, callback) | ||
--- | ||
The same as people.set but in the words of mixpanel: | ||
mixpanel.people.set_once | ||
" This method allows you to set a user attribute, only if | ||
it is not currently set. It can be called multiple times | ||
safely, so is perfect for storing things like the first date | ||
you saw a user, or the referrer that brought them to your | ||
website for the first time. " | ||
*/ | ||
set_once: function(distinct_id, prop, to, callback) { | ||
var $set = {}, data = {}; | ||
if (typeof(prop) === 'object') { | ||
callback = to; | ||
$set = prop; | ||
} else { | ||
$set[prop] = to; | ||
} | ||
this._set(distinct_id, $set, callback, { set_once: true }) | ||
}, | ||
/** | ||
@@ -181,7 +206,14 @@ people.set(distinct_id, prop, to, callback) | ||
this._set(distinct_id, $set, callback) | ||
}, | ||
// used internally by set and set_once | ||
_set: function(distinct_id, $set, callback, options) { | ||
var set_key = (options && options["set_once"]) ? "$set_once" : "$set"; | ||
var data = { | ||
'$set': $set, | ||
'$token': metrics.token, | ||
'$distinct_id': distinct_id | ||
} | ||
data[set_key] = $set; | ||
@@ -193,2 +225,7 @@ if($set['ip']) { | ||
if ($set['$ignore_time']) { | ||
data['$ignore_time'] = $set['$ignore_time']; | ||
delete $set['$ignore_time']; | ||
} | ||
if(metrics.config.debug) { | ||
@@ -195,0 +232,0 @@ console.log("Sending the following data to Mixpanel (Engage):"); |
@@ -5,3 +5,3 @@ { | ||
"keywords": ["mixpanel", "analytics", "api", "stats"], | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"homepage": "https://github.com/carlsverre/mixpanel-node", | ||
@@ -8,0 +8,0 @@ "author": "Carl Sverre", |
@@ -23,3 +23,20 @@ var Mixpanel = require('../lib/mixpanel-node'), | ||
set: { | ||
_set: { | ||
"handles set_once correctly": function(test){ | ||
var expected_data = { | ||
$set_once: { key1: 'val1' }, | ||
$token: this.token, | ||
$distinct_id: this.distinct_id | ||
}; | ||
this.mixpanel.people.set_once(this.distinct_id, 'key1', 'val1'); | ||
test.ok( | ||
this.mixpanel.send_request.calledWithMatch(this.endpoint, expected_data), | ||
"people.set_once calls send request with correct arguments" | ||
); | ||
test.done(); | ||
}, | ||
"calls send_request with correct endpoint and data": function(test) { | ||
@@ -77,2 +94,47 @@ var expected_data = { | ||
test.done(); | ||
}, | ||
"handles the $ignore_time property in a property object properly": function(test) { | ||
var prop = { $ignore_time: true, key1: 'val1', key2: 'val2' }, | ||
expected_data = { | ||
$set: { key1: 'val1', key2: 'val2' }, | ||
$token: this.token, | ||
$distinct_id: this.distinct_id, | ||
$ignore_time: true | ||
}; | ||
this.mixpanel.people.set(this.distinct_id, prop); | ||
test.ok( | ||
this.mixpanel.send_request.calledWithMatch(this.endpoint, expected_data), | ||
"people.set didn't call send_request with correct arguments" | ||
); | ||
test.done(); | ||
}, | ||
"supports a callback function": function(test) { | ||
var prop = { a: 'b' }, callback = function() { }; | ||
this.mixpanel.people.set(this.distinct_id, prop, callback); | ||
test.ok( | ||
this.mixpanel.send_request.args[0][2] === callback, | ||
"people.set didn't call send_request with a callback" | ||
); | ||
test.done(); | ||
}, | ||
"supports a callback function (set_once)": function(test) { | ||
var prop = { a: 'b' }, callback = function() { }; | ||
this.mixpanel.people.set_once(this.distinct_id, prop, callback); | ||
test.ok( | ||
this.mixpanel.send_request.args[0][2] === callback, | ||
"people.set_once didn't call send_request with a callback" | ||
); | ||
test.done(); | ||
} | ||
@@ -79,0 +141,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
39186
818