From 5cc536233f18c2da3ad836ca4c4bfe4e7f21f883 Mon Sep 17 00:00:00 2001 From: Robert Ing Date: Fri, 3 May 2024 12:25:52 -0400 Subject: [PATCH] fix: Allow any userIdentificationType to be used when initializing kit (#48) --- src/BrazeKit-dev.js | 18 +++++++++++++----- test/tests.js | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/BrazeKit-dev.js b/src/BrazeKit-dev.js index 55305b3..3a3d23a 100644 --- a/src/BrazeKit-dev.js +++ b/src/BrazeKit-dev.js @@ -632,9 +632,11 @@ var constructor = function () { ]; } - kitLogger('braze.changeUser', brazeUserIDType); + if (brazeUserIDType) { + kitLogger('braze.changeUser', brazeUserIDType); - braze.changeUser(brazeUserIDType); + braze.changeUser(brazeUserIDType); + } if (userIdentities.email) { kitLogger('braze.getUser().setEmail', userIdentities.email); @@ -792,10 +794,16 @@ var constructor = function () { braze.addSdkMetadata(['mp']); primeBrazeWebPush(); - if (forwarderSettings.userIdentificationType === 'MPID' && mParticle.Identity != null && mParticle.Identity.getCurrentUser().getMPID() != null) { - onUserIdentified(mParticle.Identity.getCurrentUser()) + const currentUser = + mParticle.Identity !== null + ? mParticle.Identity.getCurrentUser() + : null; + const mpid = currentUser ? currentUser.getMPID() : null; + + if (currentUser && mpid) { + onUserIdentified(currentUser); } - + openSession(forwarderSettings); } diff --git a/test/tests.js b/test/tests.js index fcb69db..2098679 100644 --- a/test/tests.js +++ b/test/tests.js @@ -254,6 +254,7 @@ describe('Braze Forwarder', function() { return { userIdentities: { customerid: 'abc', + email: 'email@gmail.com' }, }; }, @@ -970,6 +971,25 @@ describe('Braze Forwarder', function() { delete mParticle.getVersion; }); + it('should set an identity on the user upon kit initialization when userIdentificationType is email', function() { + mParticle.forwarder.init({ + apiKey: '123456', + userIdentificationType: 'Email', + }); + + window.braze.userId.should.equal('email@gmail.com'); + window.braze.getUser().emailSet.should.equal('email@gmail.com'); + }); + + it('should not attempt to set an identity on braze if the userIdentificationType does not exist on the customer', function() { + mParticle.forwarder.init({ + apiKey: '123456', + userIdentificationType: 'other2', + }); + + Should(window.braze.userId).equal(null); + }); + it('should set main braze user identity from userIdentificationType ', function() { mParticle.forwarder.init({ apiKey: '123456',