Skip to content

Commit

Permalink
Replace userIdentity if type exists; remove userIdentity of specified…
Browse files Browse the repository at this point in the history
… type by passing null for id
  • Loading branch information
rmi22186 authored Apr 14, 2017
1 parent 87824bb commit e6df72e
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 10 deletions.
71 changes: 62 additions & 9 deletions mparticle.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
// forEach polyfill
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
//

if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
Expand Down Expand Up @@ -84,6 +84,7 @@
};
}

// map polyfill
// Production steps of ECMA-262, Edition 5, 15.4.4.19
// Reference: http://es5.github.io/#x15.4.4.19
if (!Array.prototype.map) {
Expand Down Expand Up @@ -123,6 +124,38 @@
};
}

// filter polyfill
// Prodcution steps of ECMA-262, Edition 5
// Reference: http://es5.github.io/#x15.4.4.20
if (!Array.prototype.filter) {
Array.prototype.filter = function(fun/*, thisArg*/) {
'use strict';

if (this === void 0 || this === null) {
throw new TypeError();
}

var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}

var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i];
if (fun.call(thisArg, val, i, t)) {
res.push(val);
}
}
}

return res;
};
}

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
if (!Array.isArray) {
Array.prototype.isArray = function (arg) {
Expand Down Expand Up @@ -2252,14 +2285,22 @@
}
},
setUserIdentity: function(id, type) {
if (!type) {
logDebug('You must include a type to set a user identity');
return;
}

if (canLog()) {
if (IdentityType.isValid(type)) {
var userIdentity = {
Identity: id,
Type: type
};

mParticle.removeUserIdentity(id);
mParticle.removeUserIdentity(id, type);
if (id === null) {
return;
}
userIdentities.push(userIdentity);

if (!tryNativeSdk(NativeSdkPaths.SetUserIdentity, JSON.stringify(userIdentity))) {
Expand Down Expand Up @@ -2297,15 +2338,27 @@

return foundIdentity;
},
removeUserIdentity: function(id) {
userIdentities.forEach(function(userIdentity, i) {
if (userIdentity.Identity === id) {
userIdentities.splice(i, 1);
i--;
}
});
removeUserIdentity: function(id, type) {
if (id === undefined) {
logDebug('Please include an id to remove');
return;
} else if (id) {
userIdentities = userIdentities.filter(function(userIdentity) {
return userIdentity.Identity !== id;
});
}

// Below includes times where id === null
if (type && IdentityType.isValid(type)) {
userIdentities = userIdentities.filter(function(userIdentity) {
return userIdentity.Type !== type;
});
} else {
logDebug('IdentityType is not valid. Please ensure you are using a valid IdentityType from http://docs.mparticle.com/#user-identity');
}

tryNativeSdk(NativeSdkPaths.RemoveUserIdentity, id);

setCookie();
},
startNewSession: function() {
Expand Down
73 changes: 72 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ describe('mParticle Core SDK', function() {
';expires=' + expires +
';path=/';
},
expireCookie = function() {
var date = new Date(),
key = 'mprtcl-api',
value = {expire: 'me'},
expires = new Date(date.getTime() +
(-365 * 24 * 60 * 60 * 1000)).toGMTString();

window.document.cookie =
encodeURIComponent(key) + '=' + encodeURIComponent(JSON.stringify(value)) +
';expires=' + expires +
';path=/';
},
getEvent = function(eventName, isForwarding) {
var requests = getRequests(isForwarding ? 'Forwarding' : 'Events'),
matchedEvent = null;
Expand Down Expand Up @@ -268,6 +280,7 @@ describe('mParticle Core SDK', function() {
};

mParticle.reset();
expireCookie();
mParticle.init(apiKey);
window.mParticleAndroid = null;
});
Expand Down Expand Up @@ -701,7 +714,7 @@ describe('mParticle Core SDK', function() {
done();
});

it('should remove user identities', function(done) {
it('should remove user identities by id', function(done) {
mParticle.setUserIdentity('test@mparticle.com', mParticle.IdentityType.CustomerId);
mParticle.removeUserIdentity('test@mparticle.com');

Expand All @@ -712,6 +725,64 @@ describe('mParticle Core SDK', function() {
done();
});

it('should replace existing userIdentities of the same type', function(done) {
mParticle.reset();

setCookie({
ui: [{Identity: 123, Type: 0}, {Identity:123, Type: 2}]
});

mParticle.init(apiKey);

mParticle.setUserIdentity(123, mParticle.IdentityType.CustomerId);

var identity = mParticle.getUserIdentity(123);

identity.should.have.property('Type', 1);
identity.should.have.property('Identity', 123);

done();
});

it('should replace previous userIdentities when setting multiple identities of the same type', function(done) {
mParticle.setUserIdentity('user1@mparticle.com', mParticle.IdentityType.CustomerId);
mParticle.setUserIdentity('user2@mparticle.com', mParticle.IdentityType.CustomerId);

var identity1 = mParticle.getUserIdentity('user1@mparticle.com');
var identity2 = mParticle.getUserIdentity('user2@mparticle.com');

Should(identity1).not.be.ok();
Should(identity2).be.ok();

done();
});

it('should remove userIdentity of specified type when null is passed in as id', function(done) {
mParticle.reset();

mParticle.init(apiKey);

mParticle.setUserIdentity('facebookid', mParticle.IdentityType.Facebook);
var identity1 = mParticle.getUserIdentity('facebook');

mParticle.setUserIdentity(null, mParticle.IdentityType.Facebook);
var identity2 = mParticle.getUserIdentity('facebook');

Should(identity2).not.be.ok();
Should(identity1).not.be.ok();

done();
});

it('should not create a userIdentity when only an id is passed', function(done) {
mParticle.setUserIdentity('test@mparticle.com');
var identity = mParticle.getUserIdentity('test@mparticle.com');

Should(identity).not.be.ok();

done();
});

it('should invoke forwarder setIdentity', function(done) {
mParticle.reset();
var mockForwarder = new MockForwarder();
Expand Down

0 comments on commit e6df72e

Please sign in to comment.