diff --git a/src/common.js b/src/common.js index 355f722..d814d11 100644 --- a/src/common.js +++ b/src/common.js @@ -6,6 +6,7 @@ Common.prototype.exampleMethod = function () { } Common.prototype.logId5Id = function (id5Id) { + debugger; //Checks and saves ID5 ID if it is new if (id5Id != this.id5Id) { this.id5Id = id5Id; @@ -20,7 +21,7 @@ Common.prototype.logId5Id = function (id5Id) { currentUser.setUserAttribute('ID5ID', id5Id); this.id5IdSent = true; } -}; +}.bind(this); Common.prototype.buildPartnerData = function (mParticleUser) { var pdKeys = {}; @@ -46,7 +47,6 @@ Common.prototype.buildPartnerData = function (mParticleUser) { } Common.prototype.normalizeEmail = function(email) { - debugger; var parts = email.split("@") var charactersToRemove = ['+', '.'] diff --git a/src/identity-handler.js b/src/identity-handler.js index b8e29ff..6051819 100644 --- a/src/identity-handler.js +++ b/src/identity-handler.js @@ -27,13 +27,27 @@ IdentityHandler.prototype.onIdentifyComplete = function( identityApiRequest ) {}; IdentityHandler.prototype.onLoginComplete = function( - mParticleUser, - identityApiRequest -) {}; + mParticleUser +) { + var partnerData = this.common.buildPartnerData(mParticleUser); + var id5Instance = window.ID5.init({partnerId: this.common.partnerId, pd: partnerData}) + var logId5Id = this.common.logId5Id; + + id5Instance.onAvailable(function(status){ + logId5Id(status.getUserId()); + }.bind(logId5Id)); +}; IdentityHandler.prototype.onLogoutComplete = function( mParticleUser, identityApiRequest -) {}; +) { + var id5Instance = window.ID5.init({partnerId: this.common.partnerId}) + var logId5Id = this.common.logId5Id; + + id5Instance.onAvailable(function(status){ + logId5Id(status.getUserId()); + }.bind(logId5Id)); +}; IdentityHandler.prototype.onModifyComplete = function( mParticleUser, identityApiRequest diff --git a/src/initialization.js b/src/initialization.js index 6690e6f..bc54d91 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -27,6 +27,7 @@ var initialization = { id5Script.onload = function() { isInitialized = true; + var id5Instance = window.ID5.init({partnerId: common.partnerId}) id5Instance.onAvailable(function(status){ @@ -34,8 +35,13 @@ var initialization = { }.bind(common)); }; } else { - // For testing, you should fill out this section in order to ensure any required initialization calls are made, - // clientSDKObject.initialize(forwarderSettings.apiKey) + isInitialized = true; + + var id5Instance = window.ID5.init({partnerId: common.partnerId}) + + id5Instance.onAvailable(function(status){ + common.logId5Id(status.getUserId()); + }.bind(common)); } } }; diff --git a/test/src/tests.js b/test/src/tests.js index d186105..5b85e20 100644 --- a/test/src/tests.js +++ b/test/src/tests.js @@ -1,72 +1,7 @@ /* eslint-disable no-undef*/ describe('ID5 Forwarder', function () { // -------------------DO NOT EDIT ANYTHING BELOW THIS LINE----------------------- - var MessageType = { - SessionStart: 1, - SessionEnd: 2, - PageView: 3, - PageEvent: 4, - CrashReport: 5, - OptOut: 6, - AppStateTransition: 10, - Profile: 14, - Commerce: 16, - Media: 20, - UserAttributeChange: 17, - UserIdentityChange: 18, - }, - EventType = { - Unknown: 0, - Navigation: 1, - Location: 2, - Search: 3, - Transaction: 4, - UserContent: 5, - UserPreference: 6, - Social: 7, - Other: 8, - Media: 9, - getName: function() { - return 'blahblah'; - } - }, - ProductActionType = { - Unknown: 0, - AddToCart: 1, - RemoveFromCart: 2, - Checkout: 3, - CheckoutOption: 4, - Click: 5, - ViewDetail: 6, - Purchase: 7, - Refund: 8, - AddToWishlist: 9, - RemoveFromWishlist: 10 - }, - IdentityType = { - Other: 0, - CustomerId: 1, - Facebook: 2, - Twitter: 3, - Google: 4, - Microsoft: 5, - Yahoo: 6, - Email: 7, - FacebookCustomAudienceId: 9, - Other2: 10, - Other3: 11, - Other4: 12, - Other5: 13, - Other6: 14, - Other7: 15, - Other8: 16, - Other9: 17, - Other10: 18, - MobileNumber: 19, - PhoneNumber2: 20, - PhoneNumber3: 21, - }, - ReportingService = function () { + var ReportingService = function () { var self = this; this.id = null; @@ -84,7 +19,7 @@ describe('ID5 Forwarder', function () { }, reportService = new ReportingService(); -// -------------------DO NOT EDIT ANYTHING ABOVE THIS LINE----------------------- + // -------------------DO NOT EDIT ANYTHING ABOVE THIS LINE----------------------- // -------------------START EDITING BELOW:----------------------- // -------------------mParticle stubs - Add any additional stubbing to our methods as needed----------------------- var userAttributes = {}; @@ -106,7 +41,7 @@ describe('ID5 Forwarder', function () { } }; // -------------------START EDITING BELOW:----------------------- - var MockID5Forwarder = function() { + var MockID5 = function() { var self = this; // create properties for each type of event you want tracked, see below for examples @@ -114,6 +49,7 @@ describe('ID5 Forwarder', function () { this.logPurchaseEventCalled = false; this.isInitialized = false; this.initCalled = false; + this.numberOfInitsCalled = 0; this.getUserIdCalled = false; this.pd = null; @@ -123,19 +59,22 @@ describe('ID5 Forwarder', function () { // stub your different methods to ensure they are being called properly this.init = function(id5Options) { self.initCalled = true; + self.numberOfInitsCalled += 1; self.partnerId = id5Options.partnerId; self.pd = id5Options.pd return this; }; this.onAvailable = function(callback) { - return callback + return callback(self) }; this.getUserId = function() { self.getUserIdCalled = true; return 'ID5*testtesttesttest' } + + return self; }; before(function () { @@ -143,41 +82,63 @@ describe('ID5 Forwarder', function () { }); beforeEach(function() { - window.ID5 = new MockID5Forwarder(); + window.ID5 = new MockID5(); // Include any specific settings that is required for initializing your SDK here var sdkSettings = { partnerId: 1234, }; - // You may require userAttributes or userIdentities to be passed into initialization - var userAttributes = { - color: 'green' - }; - - var userIdentities = [{ - Identity: 'customerId', - Type: IdentityType.CustomerId - }, { - Identity: 'email', - Type: IdentityType.Email - }, { - Identity: 'mobile_number', - Type: IdentityType.MobileNumber - }]; - // The third argument here is a boolean to indicate that the integration is in test mode to avoid loading any third party scripts. Do not change this value. - mParticle.forwarder.init(sdkSettings, reportService.cb, true, null, userAttributes, userIdentities); + mParticle.forwarder.init(sdkSettings, reportService.cb, true); }); - it ('should load the script into the document during initialization', function(done) { - window.ID5 = new MockID5Forwarder(); - mParticle.forwarder.init({ - partnerId: 1234, + describe('Initialization', function() { + it('should call ID5.init', function(done) { + window.ID5.initCalled.should.equal(true); + done(); }); - document.scripts[0].src.should.equal('https://cdn.id5-sync.com/api/1.0/id5-api.js'); - done(); - }); + + it('should call getUserId', function(done) { + window.ID5.getUserIdCalled.should.equal(true); + done(); + }); + }) + + describe('Identity Handling', function() { + it('should call ID5.init twice on userLoginComplete', function(done){ + var user = { + getUserIdentities: function () { + return { + userIdentities: { + email: 'test@email.com', + }, + }; + }, + }; + mParticle.forwarder.onLoginComplete(user); + + window.ID5.numberOfInitsCalled.should.equal(2); + done(); + }); + + it('should call ID5.init twice on userLogoutComplete', function(done){ + var user = { + getUserIdentities: function () { + return { + userIdentities: { + email: 'test@email.com', + }, + }; + }, + }; + mParticle.forwarder.onLoginComplete(user); + + window.ID5.numberOfInitsCalled.should.equal(2); + done(); + }); + }) + describe('Common Functions', function() { it ('should log a user attribute when logId5 is called', function(done) {