Comparing version 0.2.1 to 0.3.0
@@ -579,2 +579,61 @@ /* | ||
/** | ||
people.union(distinct_id, data, callback) | ||
--- | ||
merge value(s) into a list-valued people analytics property. | ||
usage: | ||
mixpanel.people.union('bob', {'browsers': 'firefox'}); | ||
mixpanel.people.union('bob', {'browsers', ['chrome'], os: ['linux']}); | ||
*/ | ||
union: function(distinct_id, data, callback) { | ||
var $union = {}; | ||
if (typeof(data) !== 'object' || util.isArray(data)) { | ||
if (metrics.config.debug) { | ||
console.error("Invalid value passed to mixpanel.people.union - data must be an object with array values"); | ||
} | ||
return; | ||
} | ||
Object.keys(data).forEach(function(key) { | ||
var val = data[key]; | ||
if (util.isArray(val)) { | ||
var merge_values = val.filter(function(v) { | ||
return typeof(v) === 'string' || typeof(v) === 'number'; | ||
}); | ||
if (merge_values.length > 0) { | ||
$union[key] = merge_values; | ||
} | ||
} else if (typeof(val) === 'string' || typeof(val) === 'number') { | ||
$union[key] = [val]; | ||
} else { | ||
if (metrics.config.debug) { | ||
console.error("Invalid argument passed to mixpanel.people.union - values must be a scalar value or array"); | ||
console.error("Passed " + key + ':', val); | ||
} | ||
return; | ||
} | ||
}); | ||
if (Object.keys($union).length === 0) { | ||
return; | ||
} | ||
data = { | ||
'$union': $union, | ||
'$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); | ||
}, | ||
/** | ||
people.unset(distinct_id, prop, callback) | ||
@@ -581,0 +640,0 @@ --- |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/mixpanel/mixpanel-node", | ||
@@ -13,0 +13,0 @@ "author": "Carl Sverre", |
@@ -60,2 +60,9 @@ Mixpanel-node | ||
// merge value to a list (ignoring duplicates) | ||
mixpanel.people.union("billybob", {"browsers": "ie"}); | ||
// merge multiple values to a list (ignoring duplicates) | ||
mixpanel.people.union("billybob", {"browsers": ["ie", "chrome"]}); | ||
// record a transaction for revenue analytics | ||
@@ -141,2 +148,3 @@ mixpanel.people.track_charge("billybob", 39.99); | ||
- [Eduardo Sorribas](https://github.com/sorribas) | ||
- [Nick Chang](https://github.com/maeldur) | ||
@@ -143,0 +151,0 @@ License |
@@ -364,2 +364,62 @@ var Mixpanel = require('../lib/mixpanel-node'), | ||
union: { | ||
"calls send_request with correct endpoint and data": function(test) { | ||
var expected_data = { | ||
$union: {'key1': ['value1', 'value2']}, | ||
$token: this.token, | ||
$distinct_id: this.distinct_id | ||
}; | ||
this.mixpanel.people.union(this.distinct_id, { | ||
'key1': ['value1', 'value2'] | ||
}); | ||
test.ok( | ||
this.mixpanel.send_request.calledWithMatch(this.endpoint, expected_data), | ||
"people.union didn't call send_request with correct arguments" | ||
); | ||
test.done(); | ||
}, | ||
"supports being called with a scalar value": function(test) { | ||
var data = { | ||
'key1': 'value1' | ||
}, | ||
expected_data = { | ||
$union: { | ||
'key1': ['value1'] | ||
}, | ||
$token: this.token, | ||
$distinct_id: this.distinct_id | ||
}; | ||
this.mixpanel.people.union(this.distinct_id, data); | ||
test.ok( | ||
this.mixpanel.send_request.calledWithMatch(this.endpoint, expected_data), | ||
"people.union didn't call send_request with correct arguments" | ||
); | ||
test.done(); | ||
}, | ||
"errors on other argument types": function(test) { | ||
this.mixpanel.people.union(this.distinct_id, {key1: {key: 'val'}}); | ||
this.mixpanel.people.union(this.distinct_id, 1231241.123); | ||
this.mixpanel.people.union(this.distinct_id, [5]); | ||
this.mixpanel.people.union(this.distinct_id, {key1: function() {}}); | ||
this.mixpanel.people.union(this.distinct_id, {key1: [function() {}]}); | ||
test.ok( | ||
!this.mixpanel.send_request.called, | ||
"people.union shouldn't call send_request on invalid arguments" | ||
); | ||
test.done(); | ||
} | ||
}, | ||
unset: { | ||
@@ -366,0 +426,0 @@ "calls send_request with correct endpoint and data": function(test) { |
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
65593
1424
154