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

Add group support on Mixpanel #4

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions lib/index.js
Expand Up @@ -280,6 +280,62 @@ Mixpanel.prototype.increment = function(track, fn){
}
};

/**
* Group support for Mixpanel by adding users with a property called
* isGroup set to True and forcing the prefix 'group.' on $distinct_id
*
* @param {Group} group
* @param {Function} fn
* @api public
*/

Mixpanel.prototype.group = function(group, fn){
if (!this.settings.people) return tick(fn);
var json = group.json();
var traits = json.traits || {};
var self = this;
var groupId = 'group.' + group.groupId();

traits['$name'] = traits['name'];
traits['isGroup'] = true;
object.del(traits,'name');

var group_payload = {
$distinct_id: groupId, // the primary group id
$token: this.settings.token,
$time: group.timestamp().getTime(),
$set: stringifyValues(traits), // set all the traits on group
$ip: 0,
$ignore_time: false, //enable Last Seen for groups
mp_lib: 'Segment: ' + identify.library().name
};

this
.get('/engage')
.query({ ip: 0 })
.query({ verbose: 1 })
.query({ data: b64encode(group_payload) })
.end(this.handle(function(err){
if (err) return fn(err);

var user_payload = {
$distinct_id: group.userId(), // the primary id
$token: self.settings.token,
$union: stringifyValues({ "groups" : [groupId]})
};

self
.get('/engage')
.query({ ip: 0 })
.query({ verbose: 1 })
.query({ data: b64encode(user_payload) })
.end(self._parseResponse(fn));

})
);

};

/**
* Common function for parsing the response from a mixpanel call.
*
Expand Down