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 merge Identities functionality #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/mixpanel-node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ declare namespace mixpanel {

alias(distinctId: string, alias: string, callback?: Callback): void;

merge(distinctIds: string, distinctId2: string, callback?: Callback): void;

people: People;

groups: Groups;
Expand Down
19 changes: 19 additions & 0 deletions lib/mixpanel-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,25 @@ var create_client = function(token, config) {
metrics.track('$create_alias', properties, callback);
};

/**
merge(distinct_id_1, distinct_id_2)
---
This function merges 2 distinct_id s

For more information look at:
https://developer.mixpanel.com/reference/identity-merge

distinct_id_1:string one distinct id
distinct_id_2:string a second distinct id
*/
metrics.merge = function(distinct_id_1, distinct_id_2, callback) {
var properties = {
distinct_ids: [distinct_id_1, distinct_id_2]
};

metrics.import('$merge', new Date().getDate(), properties, callback);
};

metrics.groups = new MixpanelGroups(metrics);
metrics.people = new MixpanelPeople(metrics);

Expand Down
39 changes: 39 additions & 0 deletions test/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var Mixpanel = require('../lib/mixpanel-node'),
Sinon = require('sinon');

exports.merge = {
setUp: function(next) {
this.mixpanel = Mixpanel.init('token', { key: 'key' });

Sinon.stub(this.mixpanel, 'send_request');

next();
},

tearDown: function(next) {
this.mixpanel.send_request.restore();

next();
},

"calls send_request with correct endpoint and data": function(test) {
var distinct_id_1 = "distinct_id1",
distinct_id_2 = "distinct_id2",
expected_endpoint = "/import",
expected_data = {
event: '$merge',
properties: {
distinct_ids: [distinct_id_1, distinct_id_2],
token: 'token'
}
};

this.mixpanel.merge(distinct_id_1, distinct_id_2);
test.ok(
this.mixpanel.send_request.calledWithMatch({ endpoint: expected_endpoint, data: expected_data }),
"merge didn't call send_request with correct arguments"
);

test.done();
}
};