From 7ec551a6355458db406220873272f7b2ebc39231 Mon Sep 17 00:00:00 2001 From: SbDove <100377552+SbDove@users.noreply.github.com> Date: Thu, 20 Jun 2024 13:38:54 -0700 Subject: [PATCH 1/8] Add UA, identity, and custom event processing with unit tests --- src/event-handler.js | 5 +- src/identity-handler.js | 19 ++- src/initialization.js | 2 + src/user-attribute-handler.js | 14 +- test/end-to-end-testapp/build/compilation.js | 149 ++++++++++++------- test/end-to-end-testapp/settings.js | 3 +- test/tests.js | 149 ++++++++++++++++--- 7 files changed, 263 insertions(+), 78 deletions(-) diff --git a/src/event-handler.js b/src/event-handler.js index e6bb018..bc91a61 100644 --- a/src/event-handler.js +++ b/src/event-handler.js @@ -16,7 +16,10 @@ A non-ecommerce event has the following schema: function EventHandler(common) { this.common = common || {}; } -EventHandler.prototype.logEvent = function(event) {}; +EventHandler.prototype.logEvent = function(event) { + if (event.EventName == "click" || event.EventName == "change" || event.EventName == "submit") return; + window.heap.track(event.EventName, event.EventAttributes); +}; EventHandler.prototype.logError = function(event) { // The schema for a logError event is the same, but noteworthy differences are as follows: // { diff --git a/src/identity-handler.js b/src/identity-handler.js index b8e29ff..1db0978 100644 --- a/src/identity-handler.js +++ b/src/identity-handler.js @@ -21,7 +21,14 @@ For more userIdentity types, see https://docs.mparticle.com/developers/sdk/web/i function IdentityHandler(common) { this.common = common || {}; } -IdentityHandler.prototype.onUserIdentified = function(mParticleUser) {}; +IdentityHandler.prototype.onUserIdentified = function(mParticleUser) { + var identitiesObject = mParticleUser.getUserIdentities(); + var identity = identitiesObject.userIdentities[this.common.userIdentificationType]; + + if (identity) { + window.heap.identify(identity); + } +}; IdentityHandler.prototype.onIdentifyComplete = function( mParticleUser, identityApiRequest @@ -33,11 +40,17 @@ IdentityHandler.prototype.onLoginComplete = function( IdentityHandler.prototype.onLogoutComplete = function( mParticleUser, identityApiRequest -) {}; +) { + window.heap.resetIdentity(); +}; IdentityHandler.prototype.onModifyComplete = function( mParticleUser, identityApiRequest -) {}; +) { + if (forwarderSettings.userIdentificationType == type) { + heap.identify(id); + } +}; /* In previous versions of the mParticle web SDK, setting user identities on kits is only reachable via the onSetUserIdentity method below. We recommend diff --git a/src/initialization.js b/src/initialization.js index 2756473..851f98b 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -27,6 +27,8 @@ var initialization = { /* Load your Web SDK here using a variant of your snippet from your readme that your customers would generally put into their tags Generally, our integrations create script tags and append them to the . Please follow the following format as a guide: */ + common.userIdentificationType = forwarderSettings.userIdentificationType + var forwardWebRequestsServerSide = forwarderSettings.forwardWebRequestsServerSide === 'True'; common.forwardWebRequestsServerSide = forwardWebRequestsServerSide; if (!forwardWebRequestsServerSide) { diff --git a/src/user-attribute-handler.js b/src/user-attribute-handler.js index 59c55f3..d00a122 100644 --- a/src/user-attribute-handler.js +++ b/src/user-attribute-handler.js @@ -13,12 +13,22 @@ function UserAttributeHandler(common) { UserAttributeHandler.prototype.onRemoveUserAttribute = function( key, mParticleUser -) {}; +) { + delete this.common.userAttributes[key]; + window.heap.addUserProperties(this.common.userAttributes); +}; UserAttributeHandler.prototype.onSetUserAttribute = function( key, value, mParticleUser -) {}; +) { + if (!this.common.userAttributes) { + this.common.userAttributes = {}; + } + + this.common.userAttributes[key] = value; + window.heap.addUserProperties(this.common.userAttributes); +}; UserAttributeHandler.prototype.onConsentStateUpdated = function( oldState, newState, diff --git a/test/end-to-end-testapp/build/compilation.js b/test/end-to-end-testapp/build/compilation.js index 9f1e91e..5867831 100644 --- a/test/end-to-end-testapp/build/compilation.js +++ b/test/end-to-end-testapp/build/compilation.js @@ -94,7 +94,10 @@ var HeapKit = (function (exports) { function EventHandler(common) { this.common = common || {}; } - EventHandler.prototype.logEvent = function(event) {}; + EventHandler.prototype.logEvent = function(event) { + if (event.EventName == "click" || event.EventName == "change" || event.EventName == "submit") return; + window.heap.track(event.EventName, event.EventAttributes); + }; EventHandler.prototype.logError = function(event) { // The schema for a logError event is the same, but noteworthy differences are as follows: // { @@ -135,7 +138,14 @@ var HeapKit = (function (exports) { function IdentityHandler(common) { this.common = common || {}; } - IdentityHandler.prototype.onUserIdentified = function(mParticleUser) {}; + IdentityHandler.prototype.onUserIdentified = function(mParticleUser) { + var identitiesObject = mParticleUser.getUserIdentities(); + var identity = identitiesObject.userIdentities[this.common.userIdentificationType]; + + if (identity) { + window.heap.identify(identity); + } + }; IdentityHandler.prototype.onIdentifyComplete = function( mParticleUser, identityApiRequest @@ -147,11 +157,17 @@ var HeapKit = (function (exports) { IdentityHandler.prototype.onLogoutComplete = function( mParticleUser, identityApiRequest - ) {}; + ) { + window.heap.resetIdentity(); + }; IdentityHandler.prototype.onModifyComplete = function( mParticleUser, identityApiRequest - ) {}; + ) { + if (forwarderSettings.userIdentificationType == type) { + heap.identify(id); + } + }; /* In previous versions of the mParticle web SDK, setting user identities on kits is only reachable via the onSetUserIdentity method below. We recommend @@ -167,45 +183,70 @@ var HeapKit = (function (exports) { var initialization = { name: 'Heap', - /* ****** Fill out initForwarder to load your SDK ****** - Note that not all arguments may apply to your SDK initialization. - These are passed from mParticle, but leave them even if they are not being used. - forwarderSettings contain settings that your SDK requires in order to initialize - userAttributes example: {gender: 'male', age: 25} - userIdentities example: { 1: 'customerId', 2: 'facebookId', 7: 'emailid@email.com' } - additional identityTypes can be found at https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101 - */ - initForwarder: function(forwarderSettings, testMode, userAttributes, userIdentities, processEvent, eventQueue, isInitialized, common, appVersion, appName, customFlags, clientId) { + /* ****** Fill out initForwarder to load your SDK ****** + Note that not all arguments may apply to your SDK initialization. + These are passed from mParticle, but leave them even if they are not being used. + forwarderSettings contain settings that your SDK requires in order to initialize + userAttributes example: {gender: 'male', age: 25} + userIdentities example: { 1: 'customerId', 2: 'facebookId', 7: 'emailid@email.com' } + additional identityTypes can be found at https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101 + */ + initForwarder: function ( + forwarderSettings, + testMode, + userAttributes, + userIdentities, + processEvent, + eventQueue, + isInitialized, + common, + appVersion, + appName, + customFlags, + clientId + ) { /* `forwarderSettings` contains your SDK specific settings such as apiKey that your customer needs in order to initialize your SDK properly */ if (!testMode) { /* Load your Web SDK here using a variant of your snippet from your readme that your customers would generally put into their tags Generally, our integrations create script tags and append them to the . Please follow the following format as a guide: */ - - if (!window.heap) { - var heapScript = document.createElement('script'); - heapScript.type = 'text/javascript'; - heapScript.async = true; - heapScript.src = 'https://cdn.heapanalytics.com/js/heap-' + forwarderSettings.appId + '.js'; - (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(heapScript); - heapScript.onload = function() { - - if (window.heap && eventQueue.length > 0) { - // Process any events that may have been queued up while forwarder was being initialized. - for (var i = 0; i < eventQueue.length; i++) { - processEvent(eventQueue[i]); - } - // now that each queued event is processed, we empty the eventQueue - eventQueue = []; - } - window.heap.load(forwarderSettings.appId); - }; + common.userIdentificationType = forwarderSettings.userIdentificationType; + + var forwardWebRequestsServerSide = forwarderSettings.forwardWebRequestsServerSide === 'True'; + common.forwardWebRequestsServerSide = forwardWebRequestsServerSide; + if (!forwardWebRequestsServerSide) { + if (!window.heap) { + window.heap = window.heap || []; + heap.load = function (e, t) { + (window.heap.appid = e), (window.heap.config = t = t || {}); + var heapScript = document.createElement('script'); + heapScript.type = 'text/javascript'; + heapScript.async = !0; + heapScript.src = + 'https://cdn.heapanalytics.com/js/heap-' + e + '.js'; + ( + document.getElementsByTagName('head')[0] || + document.getElementsByTagName('body')[0] + ).appendChild(heapScript); + + heapScript.onload = function () { + + if (window.heap && eventQueue.length > 0) { + // Process any events that may have been queued up while forwarder was being initialized. + for (var i = 0; i < eventQueue.length; i++) { + processEvent(eventQueue[i]); + } + // now that each queued event is processed, we empty the eventQueue + eventQueue = []; + } + }; + }; + + window.heap.load(forwarderSettings.appId); + } } - - - } - } + }, }; var initialization_1 = initialization; @@ -236,12 +277,22 @@ var HeapKit = (function (exports) { UserAttributeHandler.prototype.onRemoveUserAttribute = function( key, mParticleUser - ) {}; + ) { + delete this.common.userAttributes[key]; + window.heap.addUserProperties(this.common.userAttributes); + }; UserAttributeHandler.prototype.onSetUserAttribute = function( key, value, mParticleUser - ) {}; + ) { + if (!this.common.userAttributes) { + this.common.userAttributes = {}; + } + + this.common.userAttributes[key] = value; + window.heap.addUserProperties(this.common.userAttributes); + }; UserAttributeHandler.prototype.onConsentStateUpdated = function( oldState, newState, @@ -391,8 +442,7 @@ var HeapKit = (function (exports) { function logSessionStart(event) { try { - sessionHandler_1.onSessionStart(event); - return true; + return sessionHandler_1.onSessionStart(event); } catch (e) { return { error: 'Error starting session on forwarder ' + name + '; ' + e, @@ -402,8 +452,7 @@ var HeapKit = (function (exports) { function logSessionEnd(event) { try { - sessionHandler_1.onSessionEnd(event); - return true; + return sessionHandler_1.onSessionEnd(event); } catch (e) { return { error: 'Error ending session on forwarder ' + name + '; ' + e, @@ -413,8 +462,7 @@ var HeapKit = (function (exports) { function logError(event) { try { - self.eventHandler.logError(event); - return true; + return self.eventHandler.logError(event); } catch (e) { return { error: 'Error logging error on forwarder ' + name + '; ' + e, @@ -424,8 +472,7 @@ var HeapKit = (function (exports) { function logPageView(event) { try { - self.eventHandler.logPageView(event); - return true; + return self.eventHandler.logPageView(event); } catch (e) { return { error: @@ -436,8 +483,7 @@ var HeapKit = (function (exports) { function logEvent(event) { try { - self.eventHandler.logEvent(event); - return true; + return self.eventHandler.logEvent(event); } catch (e) { return { error: 'Error logging event on forwarder ' + name + '; ' + e, @@ -447,8 +493,7 @@ var HeapKit = (function (exports) { function logEcommerceEvent(event) { try { - self.commerceHandler.logCommerceEvent(event); - return true; + return self.commerceHandler.logCommerceEvent(event); } catch (e) { return { error: @@ -734,11 +779,13 @@ var HeapKit = (function (exports) { } var SDKsettings = { - apiKey: 'testAPIKey' + apiKey: 'testAPIKey', /* fill in SDKsettings with any particular settings or options your sdk requires in order to initialize, this may be apiKey, projectId, primaryCustomerType, etc. These are passed into the src/initialization.js file as the */ + appId: '1518360323', + userIdentificationType: 'customerid', }; // Do not edit below: diff --git a/test/end-to-end-testapp/settings.js b/test/end-to-end-testapp/settings.js index 7ce53ef..c0bb5c1 100644 --- a/test/end-to-end-testapp/settings.js +++ b/test/end-to-end-testapp/settings.js @@ -4,7 +4,8 @@ var SDKsettings = { initialize, this may be apiKey, projectId, primaryCustomerType, etc. These are passed into the src/initialization.js file as the */ - appId: 'testAppId', + appId: '1518360323', + userIdentificationType: 'customerid', }; // Do not edit below: diff --git a/test/tests.js b/test/tests.js index 4c9713a..05b0a07 100644 --- a/test/tests.js +++ b/test/tests.js @@ -1,5 +1,5 @@ /* eslint-disable no-undef*/ -describe('XYZ Forwarder', function () { +describe('Heap Forwarder', function () { // -------------------DO NOT EDIT ANYTHING BELOW THIS LINE----------------------- var MessageType = { SessionStart: 1, @@ -83,22 +83,25 @@ describe('XYZ Forwarder', function () { }, }; // -------------------START EDITING BELOW:----------------------- - var MockXYZForwarder = function () { + var MockHeapForwarder = function () { var self = this; // create properties for each type of event you want tracked, see below for examples - this.trackCustomEventCalled = false; + this.trackCalled = false; this.logPurchaseEventCalled = false; + this.addUserPropertiesCalled = false; + this.loadCalled = false; + this.identifyCalled = false; this.initializeCalled = false; this.trackCustomName = null; this.logPurchaseName = null; - this.apiKey = null; - this.appId = null; - this.userId = null; + this.appid = null; + this.identity = null; this.userAttributes = {}; this.userIdField = null; + this.events = []; this.eventProperties = []; this.purchaseEventProperties = []; @@ -109,6 +112,27 @@ describe('XYZ Forwarder', function () { self.appId = appId; }; + this.load = function (appId) { + self.loadCalled = true; + self.appid = appId; + }; + + this.identify = function (identity) { + self.identity = identity; + self.identifyCalled = true; + }; + + this.addUserProperties = function(properties) { + self.addUserPropertiesCalled = true; + self.userAttributes = properties; + }; + + this.track = function (eventName, eventAttributes) { + this.trackCalled = true; + this.events.push(eventName); + this.eventProperties.push(eventAttributes); + }; + this.stubbedTrackingMethod = function (name, eventProperties) { self.trackCustomEventCalled = true; self.trackCustomName = name; @@ -139,11 +163,12 @@ describe('XYZ Forwarder', function () { before(function () {}); beforeEach(function () { - window.MockXYZForwarder = new MockXYZForwarder(); + window.MockHeapForwarder = new MockHeapForwarder(); // Include any specific settings that is required for initializing your SDK here var sdkSettings = { clientKey: '123456', appId: 'test-app-id', + userIdentificationType: 'customerid', }; // You may require userAttributes or userIdentities to be passed into initialization @@ -186,21 +211,105 @@ describe('XYZ Forwarder', function () { done(); }); - it('should log event', function (done) { - // mParticle.forwarder.process({ - // EventDataType: MessageType.PageEvent, - // EventName: 'Test Event', - // EventAttributes: { - // label: 'label', - // value: 200, - // category: 'category' - // } - // }); + describe('UserIdentification', function () { + it('should log the correct identity to heap based on the forwarder settings', function (done) { + window.heap = new MockHeapForwarder(); - // window.MockXYZForwarder.eventProperties[0].label.should.equal('label'); - // window.MockXYZForwarder.eventProperties[0].value.should.equal(200); + mParticle.forwarder.init({ + appId: 'test-app-id', + userIdentificationType: 'customerid' + }); - done(); + var user = { + getUserIdentities: function () { + return { + userIdentities: { + customerid: 'cid123' + } + } + } + } + mParticle.forwarder.onUserIdentified(user); + + window.heap.should.be.defined; + window.heap.identity.should.equal('cid123'); + done(); + }); + + it('should return a null identity on logout', function (done) { + mParticle.forwarder.init({ + appId: 'test-app-id', + userIdentificationType: 'customerid' + }); + + var user = { + getUserIdentities: function () { + return { + userIdentities: { + customerid: 'cid123' + } + } + } + } + mParticle.forwarder.onUserIdentified(user); + + window.heap.should.be.defined; + window.heap.identity.should.equal('cid123'); + + mParticle.forwarder.onLogoutComplete(); + + window.heap.identity.should.not.exist; + done(); + }); + }); + + describe('UserAttributeProcessing', function () { + it('Should log all user attributes when one is added', function (done) { + mParticle.forwarder.setUserAttribute("newKey", "newValue"); + + window.heap.addUserPropertiesCalled.should.equal(true); + done(); + }); + + it('Should log user attributes when one is removed', function(done){ + mParticle.forwarder.setUserAttribute("newKey2", "newValue2"); + + window.heap.userAttributes.newKey2.should.exist; + window.heap.userAttributes.newKey.should.exist; + + mParticle.forwarder.removeUserAttribute("newKey"); + + Object.keys(window.heap.userAttributes).length.should.equal(1); + window.heap.addUserPropertiesCalled.should.equal(true); + done(); + }); + }); + + describe('EventProcessing', function () { + it('should log event', function (done) { + window.heap = new MockHeapForwarder(); + mParticle.forwarder.init({ + appId: 'test-app-id' + }); + + mParticle.forwarder.process({ + EventDataType: MessageType.PageEvent, + EventName: 'Test Event', + EventAttributes: { + label: 'label', + value: 200, + category: 'category' + } + }); + + window.heap.trackCalled.should.equal(true); + window.heap.events.length.should.equal(1); + window.heap.events[0].should.equal('Test Event'); + window.heap.eventProperties[0].label.should.equal('label'); + window.heap.eventProperties[0].value.should.equal(200); + window.heap.eventProperties[0].category.should.equal('category'); + done(); + }); }); it('should log page view', function (done) { From e2a0e463c9a17863fae3ee64ed7c641b5b3c83fd Mon Sep 17 00:00:00 2001 From: SbDove <100377552+SbDove@users.noreply.github.com> Date: Fri, 21 Jun 2024 09:11:23 -0700 Subject: [PATCH 2/8] Update method of checking ignored events for readability --- src/event-handler.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/event-handler.js b/src/event-handler.js index bc91a61..aea6b0a 100644 --- a/src/event-handler.js +++ b/src/event-handler.js @@ -17,7 +17,16 @@ function EventHandler(common) { this.common = common || {}; } EventHandler.prototype.logEvent = function(event) { - if (event.EventName == "click" || event.EventName == "change" || event.EventName == "submit") return; + var ignoredEvents = [ + 'click', + 'change', + 'submit' + ]; + + if (ignoredEvents.includes(event.EventName.toLowerCase())) { + return; + } + window.heap.track(event.EventName, event.EventAttributes); }; EventHandler.prototype.logError = function(event) { From be6891c4a3a868ce697171d14cb6d34a5910f2c8 Mon Sep 17 00:00:00 2001 From: SbDove <100377552+SbDove@users.noreply.github.com> Date: Fri, 21 Jun 2024 09:14:53 -0700 Subject: [PATCH 3/8] Update onSetUserIdentity for increased compatibility --- src/identity-handler.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/identity-handler.js b/src/identity-handler.js index 1db0978..62f3b99 100644 --- a/src/identity-handler.js +++ b/src/identity-handler.js @@ -22,6 +22,10 @@ function IdentityHandler(common) { this.common = common || {}; } IdentityHandler.prototype.onUserIdentified = function(mParticleUser) { + if (!mParticleUser && !mParticleUser.getUserIdentities()) { + return; + } + var identitiesObject = mParticleUser.getUserIdentities(); var identity = identitiesObject.userIdentities[this.common.userIdentificationType]; @@ -46,11 +50,7 @@ IdentityHandler.prototype.onLogoutComplete = function( IdentityHandler.prototype.onModifyComplete = function( mParticleUser, identityApiRequest -) { - if (forwarderSettings.userIdentificationType == type) { - heap.identify(id); - } -}; +) {}; /* In previous versions of the mParticle web SDK, setting user identities on kits is only reachable via the onSetUserIdentity method below. We recommend @@ -60,6 +60,10 @@ IdentityHandler.prototype.onSetUserIdentity = function( forwarderSettings, id, type -) {}; +) { + if (this.common.userIdentificationType === type) { + window.heap.identify(id); + } +}; module.exports = IdentityHandler; From 1b03900b6e1286fd22068ac1d1297d3787178a1e Mon Sep 17 00:00:00 2001 From: SbDove <100377552+SbDove@users.noreply.github.com> Date: Fri, 21 Jun 2024 09:16:39 -0700 Subject: [PATCH 4/8] Restore ignored filed --- test/end-to-end-testapp/build/compilation.js | 149 +++++++------------ 1 file changed, 51 insertions(+), 98 deletions(-) diff --git a/test/end-to-end-testapp/build/compilation.js b/test/end-to-end-testapp/build/compilation.js index 5867831..9f1e91e 100644 --- a/test/end-to-end-testapp/build/compilation.js +++ b/test/end-to-end-testapp/build/compilation.js @@ -94,10 +94,7 @@ var HeapKit = (function (exports) { function EventHandler(common) { this.common = common || {}; } - EventHandler.prototype.logEvent = function(event) { - if (event.EventName == "click" || event.EventName == "change" || event.EventName == "submit") return; - window.heap.track(event.EventName, event.EventAttributes); - }; + EventHandler.prototype.logEvent = function(event) {}; EventHandler.prototype.logError = function(event) { // The schema for a logError event is the same, but noteworthy differences are as follows: // { @@ -138,14 +135,7 @@ var HeapKit = (function (exports) { function IdentityHandler(common) { this.common = common || {}; } - IdentityHandler.prototype.onUserIdentified = function(mParticleUser) { - var identitiesObject = mParticleUser.getUserIdentities(); - var identity = identitiesObject.userIdentities[this.common.userIdentificationType]; - - if (identity) { - window.heap.identify(identity); - } - }; + IdentityHandler.prototype.onUserIdentified = function(mParticleUser) {}; IdentityHandler.prototype.onIdentifyComplete = function( mParticleUser, identityApiRequest @@ -157,17 +147,11 @@ var HeapKit = (function (exports) { IdentityHandler.prototype.onLogoutComplete = function( mParticleUser, identityApiRequest - ) { - window.heap.resetIdentity(); - }; + ) {}; IdentityHandler.prototype.onModifyComplete = function( mParticleUser, identityApiRequest - ) { - if (forwarderSettings.userIdentificationType == type) { - heap.identify(id); - } - }; + ) {}; /* In previous versions of the mParticle web SDK, setting user identities on kits is only reachable via the onSetUserIdentity method below. We recommend @@ -183,70 +167,45 @@ var HeapKit = (function (exports) { var initialization = { name: 'Heap', - /* ****** Fill out initForwarder to load your SDK ****** - Note that not all arguments may apply to your SDK initialization. - These are passed from mParticle, but leave them even if they are not being used. - forwarderSettings contain settings that your SDK requires in order to initialize - userAttributes example: {gender: 'male', age: 25} - userIdentities example: { 1: 'customerId', 2: 'facebookId', 7: 'emailid@email.com' } - additional identityTypes can be found at https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101 - */ - initForwarder: function ( - forwarderSettings, - testMode, - userAttributes, - userIdentities, - processEvent, - eventQueue, - isInitialized, - common, - appVersion, - appName, - customFlags, - clientId - ) { + /* ****** Fill out initForwarder to load your SDK ****** + Note that not all arguments may apply to your SDK initialization. + These are passed from mParticle, but leave them even if they are not being used. + forwarderSettings contain settings that your SDK requires in order to initialize + userAttributes example: {gender: 'male', age: 25} + userIdentities example: { 1: 'customerId', 2: 'facebookId', 7: 'emailid@email.com' } + additional identityTypes can be found at https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101 + */ + initForwarder: function(forwarderSettings, testMode, userAttributes, userIdentities, processEvent, eventQueue, isInitialized, common, appVersion, appName, customFlags, clientId) { /* `forwarderSettings` contains your SDK specific settings such as apiKey that your customer needs in order to initialize your SDK properly */ if (!testMode) { /* Load your Web SDK here using a variant of your snippet from your readme that your customers would generally put into their tags Generally, our integrations create script tags and append them to the . Please follow the following format as a guide: */ - common.userIdentificationType = forwarderSettings.userIdentificationType; - - var forwardWebRequestsServerSide = forwarderSettings.forwardWebRequestsServerSide === 'True'; - common.forwardWebRequestsServerSide = forwardWebRequestsServerSide; - if (!forwardWebRequestsServerSide) { - if (!window.heap) { - window.heap = window.heap || []; - heap.load = function (e, t) { - (window.heap.appid = e), (window.heap.config = t = t || {}); - var heapScript = document.createElement('script'); - heapScript.type = 'text/javascript'; - heapScript.async = !0; - heapScript.src = - 'https://cdn.heapanalytics.com/js/heap-' + e + '.js'; - ( - document.getElementsByTagName('head')[0] || - document.getElementsByTagName('body')[0] - ).appendChild(heapScript); - - heapScript.onload = function () { - - if (window.heap && eventQueue.length > 0) { - // Process any events that may have been queued up while forwarder was being initialized. - for (var i = 0; i < eventQueue.length; i++) { - processEvent(eventQueue[i]); - } - // now that each queued event is processed, we empty the eventQueue - eventQueue = []; - } - }; - }; - - window.heap.load(forwarderSettings.appId); - } + + if (!window.heap) { + var heapScript = document.createElement('script'); + heapScript.type = 'text/javascript'; + heapScript.async = true; + heapScript.src = 'https://cdn.heapanalytics.com/js/heap-' + forwarderSettings.appId + '.js'; + (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(heapScript); + heapScript.onload = function() { + + if (window.heap && eventQueue.length > 0) { + // Process any events that may have been queued up while forwarder was being initialized. + for (var i = 0; i < eventQueue.length; i++) { + processEvent(eventQueue[i]); + } + // now that each queued event is processed, we empty the eventQueue + eventQueue = []; + } + window.heap.load(forwarderSettings.appId); + }; } + + + } - }, + } }; var initialization_1 = initialization; @@ -277,22 +236,12 @@ var HeapKit = (function (exports) { UserAttributeHandler.prototype.onRemoveUserAttribute = function( key, mParticleUser - ) { - delete this.common.userAttributes[key]; - window.heap.addUserProperties(this.common.userAttributes); - }; + ) {}; UserAttributeHandler.prototype.onSetUserAttribute = function( key, value, mParticleUser - ) { - if (!this.common.userAttributes) { - this.common.userAttributes = {}; - } - - this.common.userAttributes[key] = value; - window.heap.addUserProperties(this.common.userAttributes); - }; + ) {}; UserAttributeHandler.prototype.onConsentStateUpdated = function( oldState, newState, @@ -442,7 +391,8 @@ var HeapKit = (function (exports) { function logSessionStart(event) { try { - return sessionHandler_1.onSessionStart(event); + sessionHandler_1.onSessionStart(event); + return true; } catch (e) { return { error: 'Error starting session on forwarder ' + name + '; ' + e, @@ -452,7 +402,8 @@ var HeapKit = (function (exports) { function logSessionEnd(event) { try { - return sessionHandler_1.onSessionEnd(event); + sessionHandler_1.onSessionEnd(event); + return true; } catch (e) { return { error: 'Error ending session on forwarder ' + name + '; ' + e, @@ -462,7 +413,8 @@ var HeapKit = (function (exports) { function logError(event) { try { - return self.eventHandler.logError(event); + self.eventHandler.logError(event); + return true; } catch (e) { return { error: 'Error logging error on forwarder ' + name + '; ' + e, @@ -472,7 +424,8 @@ var HeapKit = (function (exports) { function logPageView(event) { try { - return self.eventHandler.logPageView(event); + self.eventHandler.logPageView(event); + return true; } catch (e) { return { error: @@ -483,7 +436,8 @@ var HeapKit = (function (exports) { function logEvent(event) { try { - return self.eventHandler.logEvent(event); + self.eventHandler.logEvent(event); + return true; } catch (e) { return { error: 'Error logging event on forwarder ' + name + '; ' + e, @@ -493,7 +447,8 @@ var HeapKit = (function (exports) { function logEcommerceEvent(event) { try { - return self.commerceHandler.logCommerceEvent(event); + self.commerceHandler.logCommerceEvent(event); + return true; } catch (e) { return { error: @@ -779,13 +734,11 @@ var HeapKit = (function (exports) { } var SDKsettings = { - apiKey: 'testAPIKey', + apiKey: 'testAPIKey' /* fill in SDKsettings with any particular settings or options your sdk requires in order to initialize, this may be apiKey, projectId, primaryCustomerType, etc. These are passed into the src/initialization.js file as the */ - appId: '1518360323', - userIdentificationType: 'customerid', }; // Do not edit below: From 4403d65dfc911e9a97e224a7ab6cb8d48e01bcae Mon Sep 17 00:00:00 2001 From: SbDove <100377552+SbDove@users.noreply.github.com> Date: Fri, 21 Jun 2024 09:18:33 -0700 Subject: [PATCH 5/8] Restore settings file to previous --- test/end-to-end-testapp/settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/end-to-end-testapp/settings.js b/test/end-to-end-testapp/settings.js index c0bb5c1..b5bd719 100644 --- a/test/end-to-end-testapp/settings.js +++ b/test/end-to-end-testapp/settings.js @@ -4,7 +4,7 @@ var SDKsettings = { initialize, this may be apiKey, projectId, primaryCustomerType, etc. These are passed into the src/initialization.js file as the */ - appId: '1518360323', + appId: 'testAppId', userIdentificationType: 'customerid', }; From b697e906e74c28054fceff362e408e60d53ec98b Mon Sep 17 00:00:00 2001 From: SbDove <100377552+SbDove@users.noreply.github.com> Date: Fri, 21 Jun 2024 09:25:41 -0700 Subject: [PATCH 6/8] Update to include all user attributes from mParticleUser --- src/user-attribute-handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user-attribute-handler.js b/src/user-attribute-handler.js index d00a122..ac271df 100644 --- a/src/user-attribute-handler.js +++ b/src/user-attribute-handler.js @@ -23,7 +23,7 @@ UserAttributeHandler.prototype.onSetUserAttribute = function( mParticleUser ) { if (!this.common.userAttributes) { - this.common.userAttributes = {}; + this.common.userAttributes = mParticleUser.getAllUserAttributes(); } this.common.userAttributes[key] = value; From 4943c8282703ebf4e24cc13d21d34956512aa21b Mon Sep 17 00:00:00 2001 From: SbDove <100377552+SbDove@users.noreply.github.com> Date: Fri, 21 Jun 2024 10:58:53 -0700 Subject: [PATCH 7/8] remove cached dist/ files --- dist/HeapKit.common.js | 779 ---------------------------------------- dist/HeapKit.iife.js | 782 ----------------------------------------- 2 files changed, 1561 deletions(-) delete mode 100644 dist/HeapKit.common.js delete mode 100644 dist/HeapKit.iife.js diff --git a/dist/HeapKit.common.js b/dist/HeapKit.common.js deleted file mode 100644 index 5833def..0000000 --- a/dist/HeapKit.common.js +++ /dev/null @@ -1,779 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { value: true }); - -function Common() {} - -Common.prototype.exampleMethod = function () { - return 'I am an example'; -}; - -var common = Common; - -function CommerceHandler(common) { - this.common = common || {}; -} - -CommerceHandler.prototype.logCommerceEvent = function(event) { - /* - Sample ecommerce event schema: - { - CurrencyCode: 'USD', - DeviceId:'a80eea1c-57f5-4f84-815e-06fe971b6ef2', // MP generated - EventAttributes: { key1: 'value1', key2: 'value2' }, - EventType: 16, - EventCategory: 10, // (This is an add product to cart event, see below for additional ecommerce EventCategories) - EventName: "eCommerce - AddToCart", - MPID: "8278431810143183490", - ProductAction: { - Affiliation: 'aff1', - CouponCode: 'coupon', - ProductActionType: 7, - ProductList: [ - { - Attributes: { prodKey1: 'prodValue1', prodKey2: 'prodValue2' }, - Brand: 'Apple', - Category: 'phones', - CouponCode: 'coupon1', - Name: 'iPhone', - Price: '600', - Quantity: 2, - Sku: "SKU123", - TotalAmount: 1200, - Variant: '64GB' - } - ], - TransactionId: "tid1", - ShippingAmount: 10, - TaxAmount: 5, - TotalAmount: 1215, - }, - UserAttributes: { userKey1: 'userValue1', userKey2: 'userValue2' } - UserIdentities: [ - { - Identity: 'test@gmail.com', Type: 7 - } - ] - } - - If your SDK has specific ways to log different eCommerce events, see below for - mParticle's additional ecommerce EventCategory types: - - 10: ProductAddToCart, (as shown above) - 11: ProductRemoveFromCart, - 12: ProductCheckout, - 13: ProductCheckoutOption, - 14: ProductClick, - 15: ProductViewDetail, - 16: ProductPurchase, - 17: ProductRefund, - 18: PromotionView, - 19: PromotionClick, - 20: ProductAddToWishlist, - 21: ProductRemoveFromWishlist, - 22: ProductImpression - */ -}; - -var commerceHandler = CommerceHandler; - -/* -A non-ecommerce event has the following schema: - -{ - DeviceId: "a80eea1c-57f5-4f84-815e-06fe971b6ef2", - EventAttributes: {test: "Error", t: 'stack trace in string form'}, - EventName: "Error", - MPID: "123123123123", - UserAttributes: {userAttr1: 'value1', userAttr2: 'value2'}, - UserIdentities: [{Identity: 'email@gmail.com', Type: 7}] - User Identity Types can be found here: -} - -*/ - -function EventHandler(common) { - this.common = common || {}; -} -EventHandler.prototype.logEvent = function(event) {}; -EventHandler.prototype.logError = function(event) { - // The schema for a logError event is the same, but noteworthy differences are as follows: - // { - // EventAttributes: {m: 'name of error passed into MP', s: "Error", t: 'stack trace in string form if applicable'}, - // EventName: "Error" - // } -}; -EventHandler.prototype.logPageView = function(event) { - /* The schema for a logPagView event is the same, but noteworthy differences are as follows: - { - EventAttributes: {hostname: "www.google.com", title: 'Test Page'}, // These are event attributes only if no additional event attributes are explicitly provided to mParticle.logPageView(...) - } - */ -}; - -var eventHandler = EventHandler; - -/* -The 'mParticleUser' is an object with methods get user Identities and set/get user attributes -Partners can determine what userIds are available to use in their SDK -Call mParticleUser.getUserIdentities() to return an object of userIdentities --> { userIdentities: {customerid: '1234', email: 'email@gmail.com'} } -For more identity types, see https://docs.mparticle.com/developers/sdk/web/idsync/#supported-identity-types -Call mParticleUser.getMPID() to get mParticle ID -For any additional methods, see https://docs.mparticle.com/developers/sdk/web/core-apidocs/classes/mParticle.Identity.getCurrentUser().html -*/ - -/* -identityApiRequest has the schema: -{ - userIdentities: { - customerid: '123', - email: 'abc' - } -} -For more userIdentity types, see https://docs.mparticle.com/developers/sdk/web/idsync/#supported-identity-types -*/ - -function IdentityHandler(common) { - this.common = common || {}; -} -IdentityHandler.prototype.onUserIdentified = function(mParticleUser) {}; -IdentityHandler.prototype.onIdentifyComplete = function( - mParticleUser, - identityApiRequest -) {}; -IdentityHandler.prototype.onLoginComplete = function( - mParticleUser, - identityApiRequest -) {}; -IdentityHandler.prototype.onLogoutComplete = function( - mParticleUser, - identityApiRequest -) {}; -IdentityHandler.prototype.onModifyComplete = function( - mParticleUser, - identityApiRequest -) {}; - -/* In previous versions of the mParticle web SDK, setting user identities on - kits is only reachable via the onSetUserIdentity method below. We recommend - filling out `onSetUserIdentity` for maximum compatibility -*/ -IdentityHandler.prototype.onSetUserIdentity = function( - forwarderSettings, - id, - type -) {}; - -var identityHandler = IdentityHandler; - -var initialization = { - name: 'Heap', -/* ****** Fill out initForwarder to load your SDK ****** - Note that not all arguments may apply to your SDK initialization. - These are passed from mParticle, but leave them even if they are not being used. - forwarderSettings contain settings that your SDK requires in order to initialize - userAttributes example: {gender: 'male', age: 25} - userIdentities example: { 1: 'customerId', 2: 'facebookId', 7: 'emailid@email.com' } - additional identityTypes can be found at https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101 -*/ - initForwarder: function(forwarderSettings, testMode, userAttributes, userIdentities, processEvent, eventQueue, isInitialized, common, appVersion, appName, customFlags, clientId) { - /* `forwarderSettings` contains your SDK specific settings such as apiKey that your customer needs in order to initialize your SDK properly */ - if (!testMode) { - /* Load your Web SDK here using a variant of your snippet from your readme that your customers would generally put into their tags - Generally, our integrations create script tags and append them to the . Please follow the following format as a guide: - */ - - if (!window.heap) { - var heapScript = document.createElement('script'); - heapScript.type = 'text/javascript'; - heapScript.async = true; - heapScript.src = 'https://cdn.heapanalytics.com/js/heap-' + forwarderSettings.appId + '.js'; - (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(heapScript); - heapScript.onload = function() { - - if (window.heap && eventQueue.length > 0) { - // Process any events that may have been queued up while forwarder was being initialized. - for (var i = 0; i < eventQueue.length; i++) { - processEvent(eventQueue[i]); - } - // now that each queued event is processed, we empty the eventQueue - eventQueue = []; - } - window.heap.load(forwarderSettings.appId); - }; - } - - - - } - } -}; - -var initialization_1 = initialization; - -var sessionHandler = { - onSessionStart: function(event) { - - }, - onSessionEnd: function(event) { - - } -}; - -var sessionHandler_1 = sessionHandler; - -/* -The 'mParticleUser' is an object with methods on it to get user Identities and set/get user attributes -Partners can determine what userIds are available to use in their SDK -Call mParticleUser.getUserIdentities() to return an object of userIdentities --> { userIdentities: {customerid: '1234', email: 'email@gmail.com'} } -For more identity types, see http://docs.mparticle.com/developers/sdk/javascript/identity#allowed-identity-types -Call mParticleUser.getMPID() to get mParticle ID -For any additional methods, see http://docs.mparticle.com/developers/sdk/javascript/apidocs/classes/mParticle.Identity.getCurrentUser().html -*/ - -function UserAttributeHandler(common) { - this.common = common || {}; -} -UserAttributeHandler.prototype.onRemoveUserAttribute = function( - key, - mParticleUser -) {}; -UserAttributeHandler.prototype.onSetUserAttribute = function( - key, - value, - mParticleUser -) {}; -UserAttributeHandler.prototype.onConsentStateUpdated = function( - oldState, - newState, - mParticleUser -) {}; - -var userAttributeHandler = UserAttributeHandler; - -// =============== REACH OUT TO MPARTICLE IF YOU HAVE ANY QUESTIONS =============== -// -// Copyright 2018 mParticle, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - - - - - - - - - -var name = initialization_1.name, - moduleId = initialization_1.moduleId, - MessageType = { - SessionStart: 1, - SessionEnd: 2, - PageView: 3, - PageEvent: 4, - CrashReport: 5, - OptOut: 6, - Commerce: 16, - Media: 20, - }; - -var constructor = function() { - var self = this, - isInitialized = false, - forwarderSettings, - reportingService, - eventQueue = []; - - self.name = initialization_1.name; - self.moduleId = initialization_1.moduleId; - self.common = new common(); - - function initForwarder( - settings, - service, - testMode, - trackerId, - userAttributes, - userIdentities, - appVersion, - appName, - customFlags, - clientId - ) { - forwarderSettings = settings; - - if ( - typeof window !== 'undefined' && - window.mParticle.isTestEnvironment - ) { - reportingService = function() {}; - } else { - reportingService = service; - } - - try { - initialization_1.initForwarder( - settings, - testMode, - userAttributes, - userIdentities, - processEvent, - eventQueue, - isInitialized, - self.common, - appVersion, - appName, - customFlags, - clientId - ); - self.eventHandler = new eventHandler(self.common); - self.identityHandler = new identityHandler(self.common); - self.userAttributeHandler = new userAttributeHandler(self.common); - self.commerceHandler = new commerceHandler(self.common); - - isInitialized = true; - } catch (e) { - console.log('Failed to initialize ' + name + ' - ' + e); - } - } - - function processEvent(event) { - var reportEvent = false; - if (isInitialized) { - try { - if (event.EventDataType === MessageType.SessionStart) { - reportEvent = logSessionStart(event); - } else if (event.EventDataType === MessageType.SessionEnd) { - reportEvent = logSessionEnd(event); - } else if (event.EventDataType === MessageType.CrashReport) { - reportEvent = logError(event); - } else if (event.EventDataType === MessageType.PageView) { - reportEvent = logPageView(event); - } else if (event.EventDataType === MessageType.Commerce) { - reportEvent = logEcommerceEvent(event); - } else if (event.EventDataType === MessageType.PageEvent) { - reportEvent = logEvent(event); - } else if (event.EventDataType === MessageType.Media) { - // Kits should just treat Media Events as generic Events - reportEvent = logEvent(event); - } - if (reportEvent === true && reportingService) { - reportingService(self, event); - return 'Successfully sent to ' + name; - } else { - return ( - 'Error logging event or event type not supported on forwarder ' + - name - ); - } - } catch (e) { - return 'Failed to send to ' + name + ' ' + e; - } - } else { - eventQueue.push(event); - return ( - "Can't send to forwarder " + - name + - ', not initialized. Event added to queue.' - ); - } - } - - function logSessionStart(event) { - try { - sessionHandler_1.onSessionStart(event); - return true; - } catch (e) { - return { - error: 'Error starting session on forwarder ' + name + '; ' + e, - }; - } - } - - function logSessionEnd(event) { - try { - sessionHandler_1.onSessionEnd(event); - return true; - } catch (e) { - return { - error: 'Error ending session on forwarder ' + name + '; ' + e, - }; - } - } - - function logError(event) { - try { - self.eventHandler.logError(event); - return true; - } catch (e) { - return { - error: 'Error logging error on forwarder ' + name + '; ' + e, - }; - } - } - - function logPageView(event) { - try { - self.eventHandler.logPageView(event); - return true; - } catch (e) { - return { - error: - 'Error logging page view on forwarder ' + name + '; ' + e, - }; - } - } - - function logEvent(event) { - try { - self.eventHandler.logEvent(event); - return true; - } catch (e) { - return { - error: 'Error logging event on forwarder ' + name + '; ' + e, - }; - } - } - - function logEcommerceEvent(event) { - try { - self.commerceHandler.logCommerceEvent(event); - return true; - } catch (e) { - return { - error: - 'Error logging purchase event on forwarder ' + - name + - '; ' + - e, - }; - } - } - - function setUserAttribute(key, value) { - if (isInitialized) { - try { - self.userAttributeHandler.onSetUserAttribute( - key, - value, - forwarderSettings - ); - return 'Successfully set user attribute on forwarder ' + name; - } catch (e) { - return ( - 'Error setting user attribute on forwarder ' + - name + - '; ' + - e - ); - } - } else { - return ( - "Can't set user attribute on forwarder " + - name + - ', not initialized' - ); - } - } - - function removeUserAttribute(key) { - if (isInitialized) { - try { - self.userAttributeHandler.onRemoveUserAttribute( - key, - forwarderSettings - ); - return ( - 'Successfully removed user attribute on forwarder ' + name - ); - } catch (e) { - return ( - 'Error removing user attribute on forwarder ' + - name + - '; ' + - e - ); - } - } else { - return ( - "Can't remove user attribute on forwarder " + - name + - ', not initialized' - ); - } - } - - function setUserIdentity(id, type) { - if (isInitialized) { - try { - self.identityHandler.onSetUserIdentity( - forwarderSettings, - id, - type - ); - return 'Successfully set user Identity on forwarder ' + name; - } catch (e) { - return ( - 'Error removing user attribute on forwarder ' + - name + - '; ' + - e - ); - } - } else { - return ( - "Can't call setUserIdentity on forwarder " + - name + - ', not initialized' - ); - } - } - - function onUserIdentified(user) { - if (isInitialized) { - try { - self.identityHandler.onUserIdentified(user); - - return ( - 'Successfully called onUserIdentified on forwarder ' + name - ); - } catch (e) { - return { - error: - 'Error calling onUserIdentified on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't set new user identities on forwader " + - name + - ', not initialized' - ); - } - } - - function onIdentifyComplete(user, filteredIdentityRequest) { - if (isInitialized) { - try { - self.identityHandler.onIdentifyComplete( - user, - filteredIdentityRequest - ); - - return ( - 'Successfully called onIdentifyComplete on forwarder ' + - name - ); - } catch (e) { - return { - error: - 'Error calling onIdentifyComplete on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't call onIdentifyCompleted on forwader " + - name + - ', not initialized' - ); - } - } - - function onLoginComplete(user, filteredIdentityRequest) { - if (isInitialized) { - try { - self.identityHandler.onLoginComplete( - user, - filteredIdentityRequest - ); - - return ( - 'Successfully called onLoginComplete on forwarder ' + name - ); - } catch (e) { - return { - error: - 'Error calling onLoginComplete on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't call onLoginComplete on forwader " + - name + - ', not initialized' - ); - } - } - - function onLogoutComplete(user, filteredIdentityRequest) { - if (isInitialized) { - try { - self.identityHandler.onLogoutComplete( - user, - filteredIdentityRequest - ); - - return ( - 'Successfully called onLogoutComplete on forwarder ' + name - ); - } catch (e) { - return { - error: - 'Error calling onLogoutComplete on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't call onLogoutComplete on forwader " + - name + - ', not initialized' - ); - } - } - - function onModifyComplete(user, filteredIdentityRequest) { - if (isInitialized) { - try { - self.identityHandler.onModifyComplete( - user, - filteredIdentityRequest - ); - - return ( - 'Successfully called onModifyComplete on forwarder ' + name - ); - } catch (e) { - return { - error: - 'Error calling onModifyComplete on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't call onModifyComplete on forwader " + - name + - ', not initialized' - ); - } - } - - function setOptOut(isOptingOutBoolean) { - if (isInitialized) { - try { - self.initialization.setOptOut(isOptingOutBoolean); - - return 'Successfully called setOptOut on forwarder ' + name; - } catch (e) { - return { - error: - 'Error calling setOptOut on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't call setOptOut on forwader " + - name + - ', not initialized' - ); - } - } - - this.init = initForwarder; - this.process = processEvent; - this.setUserAttribute = setUserAttribute; - this.removeUserAttribute = removeUserAttribute; - this.onUserIdentified = onUserIdentified; - this.setUserIdentity = setUserIdentity; - this.onIdentifyComplete = onIdentifyComplete; - this.onLoginComplete = onLoginComplete; - this.onLogoutComplete = onLogoutComplete; - this.onModifyComplete = onModifyComplete; - this.setOptOut = setOptOut; -}; - -function getId() { - return moduleId; -} - -function isObject(val) { - return ( - val != null && typeof val === 'object' && Array.isArray(val) === false - ); -} - -function register(config) { - if (!config) { - console.log( - 'You must pass a config object to register the kit ' + name - ); - return; - } - - if (!isObject(config)) { - console.log( - "'config' must be an object. You passed in a " + typeof config - ); - return; - } - - if (isObject(config.kits)) { - config.kits[name] = { - constructor: constructor, - }; - } else { - config.kits = {}; - config.kits[name] = { - constructor: constructor, - }; - } - console.log( - 'Successfully registered ' + name + ' to your mParticle configuration' - ); -} - -if (typeof window !== 'undefined') { - if (window && window.mParticle && window.mParticle.addForwarder) { - window.mParticle.addForwarder({ - name: name, - constructor: constructor, - getId: getId, - }); - } -} - -var webKitWrapper = { - register: register, -}; -var webKitWrapper_1 = webKitWrapper.register; - -exports.default = webKitWrapper; -exports.register = webKitWrapper_1; diff --git a/dist/HeapKit.iife.js b/dist/HeapKit.iife.js deleted file mode 100644 index 937bc5b..0000000 --- a/dist/HeapKit.iife.js +++ /dev/null @@ -1,782 +0,0 @@ -var HeapKit = (function (exports) { - 'use strict'; - - function Common() {} - - Common.prototype.exampleMethod = function () { - return 'I am an example'; - }; - - var common = Common; - - function CommerceHandler(common) { - this.common = common || {}; - } - - CommerceHandler.prototype.logCommerceEvent = function(event) { - /* - Sample ecommerce event schema: - { - CurrencyCode: 'USD', - DeviceId:'a80eea1c-57f5-4f84-815e-06fe971b6ef2', // MP generated - EventAttributes: { key1: 'value1', key2: 'value2' }, - EventType: 16, - EventCategory: 10, // (This is an add product to cart event, see below for additional ecommerce EventCategories) - EventName: "eCommerce - AddToCart", - MPID: "8278431810143183490", - ProductAction: { - Affiliation: 'aff1', - CouponCode: 'coupon', - ProductActionType: 7, - ProductList: [ - { - Attributes: { prodKey1: 'prodValue1', prodKey2: 'prodValue2' }, - Brand: 'Apple', - Category: 'phones', - CouponCode: 'coupon1', - Name: 'iPhone', - Price: '600', - Quantity: 2, - Sku: "SKU123", - TotalAmount: 1200, - Variant: '64GB' - } - ], - TransactionId: "tid1", - ShippingAmount: 10, - TaxAmount: 5, - TotalAmount: 1215, - }, - UserAttributes: { userKey1: 'userValue1', userKey2: 'userValue2' } - UserIdentities: [ - { - Identity: 'test@gmail.com', Type: 7 - } - ] - } - - If your SDK has specific ways to log different eCommerce events, see below for - mParticle's additional ecommerce EventCategory types: - - 10: ProductAddToCart, (as shown above) - 11: ProductRemoveFromCart, - 12: ProductCheckout, - 13: ProductCheckoutOption, - 14: ProductClick, - 15: ProductViewDetail, - 16: ProductPurchase, - 17: ProductRefund, - 18: PromotionView, - 19: PromotionClick, - 20: ProductAddToWishlist, - 21: ProductRemoveFromWishlist, - 22: ProductImpression - */ - }; - - var commerceHandler = CommerceHandler; - - /* - A non-ecommerce event has the following schema: - - { - DeviceId: "a80eea1c-57f5-4f84-815e-06fe971b6ef2", - EventAttributes: {test: "Error", t: 'stack trace in string form'}, - EventName: "Error", - MPID: "123123123123", - UserAttributes: {userAttr1: 'value1', userAttr2: 'value2'}, - UserIdentities: [{Identity: 'email@gmail.com', Type: 7}] - User Identity Types can be found here: - } - - */ - - function EventHandler(common) { - this.common = common || {}; - } - EventHandler.prototype.logEvent = function(event) {}; - EventHandler.prototype.logError = function(event) { - // The schema for a logError event is the same, but noteworthy differences are as follows: - // { - // EventAttributes: {m: 'name of error passed into MP', s: "Error", t: 'stack trace in string form if applicable'}, - // EventName: "Error" - // } - }; - EventHandler.prototype.logPageView = function(event) { - /* The schema for a logPagView event is the same, but noteworthy differences are as follows: - { - EventAttributes: {hostname: "www.google.com", title: 'Test Page'}, // These are event attributes only if no additional event attributes are explicitly provided to mParticle.logPageView(...) - } - */ - }; - - var eventHandler = EventHandler; - - /* - The 'mParticleUser' is an object with methods get user Identities and set/get user attributes - Partners can determine what userIds are available to use in their SDK - Call mParticleUser.getUserIdentities() to return an object of userIdentities --> { userIdentities: {customerid: '1234', email: 'email@gmail.com'} } - For more identity types, see https://docs.mparticle.com/developers/sdk/web/idsync/#supported-identity-types - Call mParticleUser.getMPID() to get mParticle ID - For any additional methods, see https://docs.mparticle.com/developers/sdk/web/core-apidocs/classes/mParticle.Identity.getCurrentUser().html - */ - - /* - identityApiRequest has the schema: - { - userIdentities: { - customerid: '123', - email: 'abc' - } - } - For more userIdentity types, see https://docs.mparticle.com/developers/sdk/web/idsync/#supported-identity-types - */ - - function IdentityHandler(common) { - this.common = common || {}; - } - IdentityHandler.prototype.onUserIdentified = function(mParticleUser) {}; - IdentityHandler.prototype.onIdentifyComplete = function( - mParticleUser, - identityApiRequest - ) {}; - IdentityHandler.prototype.onLoginComplete = function( - mParticleUser, - identityApiRequest - ) {}; - IdentityHandler.prototype.onLogoutComplete = function( - mParticleUser, - identityApiRequest - ) {}; - IdentityHandler.prototype.onModifyComplete = function( - mParticleUser, - identityApiRequest - ) {}; - - /* In previous versions of the mParticle web SDK, setting user identities on - kits is only reachable via the onSetUserIdentity method below. We recommend - filling out `onSetUserIdentity` for maximum compatibility - */ - IdentityHandler.prototype.onSetUserIdentity = function( - forwarderSettings, - id, - type - ) {}; - - var identityHandler = IdentityHandler; - - var initialization = { - name: 'Heap', - /* ****** Fill out initForwarder to load your SDK ****** - Note that not all arguments may apply to your SDK initialization. - These are passed from mParticle, but leave them even if they are not being used. - forwarderSettings contain settings that your SDK requires in order to initialize - userAttributes example: {gender: 'male', age: 25} - userIdentities example: { 1: 'customerId', 2: 'facebookId', 7: 'emailid@email.com' } - additional identityTypes can be found at https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101 - */ - initForwarder: function(forwarderSettings, testMode, userAttributes, userIdentities, processEvent, eventQueue, isInitialized, common, appVersion, appName, customFlags, clientId) { - /* `forwarderSettings` contains your SDK specific settings such as apiKey that your customer needs in order to initialize your SDK properly */ - if (!testMode) { - /* Load your Web SDK here using a variant of your snippet from your readme that your customers would generally put into their tags - Generally, our integrations create script tags and append them to the . Please follow the following format as a guide: - */ - - if (!window.heap) { - var heapScript = document.createElement('script'); - heapScript.type = 'text/javascript'; - heapScript.async = true; - heapScript.src = 'https://cdn.heapanalytics.com/js/heap-' + forwarderSettings.appId + '.js'; - (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(heapScript); - heapScript.onload = function() { - - if (window.heap && eventQueue.length > 0) { - // Process any events that may have been queued up while forwarder was being initialized. - for (var i = 0; i < eventQueue.length; i++) { - processEvent(eventQueue[i]); - } - // now that each queued event is processed, we empty the eventQueue - eventQueue = []; - } - window.heap.load(forwarderSettings.appId); - }; - } - - - - } - } - }; - - var initialization_1 = initialization; - - var sessionHandler = { - onSessionStart: function(event) { - - }, - onSessionEnd: function(event) { - - } - }; - - var sessionHandler_1 = sessionHandler; - - /* - The 'mParticleUser' is an object with methods on it to get user Identities and set/get user attributes - Partners can determine what userIds are available to use in their SDK - Call mParticleUser.getUserIdentities() to return an object of userIdentities --> { userIdentities: {customerid: '1234', email: 'email@gmail.com'} } - For more identity types, see http://docs.mparticle.com/developers/sdk/javascript/identity#allowed-identity-types - Call mParticleUser.getMPID() to get mParticle ID - For any additional methods, see http://docs.mparticle.com/developers/sdk/javascript/apidocs/classes/mParticle.Identity.getCurrentUser().html - */ - - function UserAttributeHandler(common) { - this.common = common || {}; - } - UserAttributeHandler.prototype.onRemoveUserAttribute = function( - key, - mParticleUser - ) {}; - UserAttributeHandler.prototype.onSetUserAttribute = function( - key, - value, - mParticleUser - ) {}; - UserAttributeHandler.prototype.onConsentStateUpdated = function( - oldState, - newState, - mParticleUser - ) {}; - - var userAttributeHandler = UserAttributeHandler; - - // =============== REACH OUT TO MPARTICLE IF YOU HAVE ANY QUESTIONS =============== - // - // Copyright 2018 mParticle, Inc. - // - // Licensed under the Apache License, Version 2.0 (the "License"); - // you may not use this file except in compliance with the License. - // You may obtain a copy of the License at - // - // http://www.apache.org/licenses/LICENSE-2.0 - // - // Unless required by applicable law or agreed to in writing, software - // distributed under the License is distributed on an "AS IS" BASIS, - // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - // See the License for the specific language governing permissions and - // limitations under the License. - - - - - - - - - - var name = initialization_1.name, - moduleId = initialization_1.moduleId, - MessageType = { - SessionStart: 1, - SessionEnd: 2, - PageView: 3, - PageEvent: 4, - CrashReport: 5, - OptOut: 6, - Commerce: 16, - Media: 20, - }; - - var constructor = function() { - var self = this, - isInitialized = false, - forwarderSettings, - reportingService, - eventQueue = []; - - self.name = initialization_1.name; - self.moduleId = initialization_1.moduleId; - self.common = new common(); - - function initForwarder( - settings, - service, - testMode, - trackerId, - userAttributes, - userIdentities, - appVersion, - appName, - customFlags, - clientId - ) { - forwarderSettings = settings; - - if ( - typeof window !== 'undefined' && - window.mParticle.isTestEnvironment - ) { - reportingService = function() {}; - } else { - reportingService = service; - } - - try { - initialization_1.initForwarder( - settings, - testMode, - userAttributes, - userIdentities, - processEvent, - eventQueue, - isInitialized, - self.common, - appVersion, - appName, - customFlags, - clientId - ); - self.eventHandler = new eventHandler(self.common); - self.identityHandler = new identityHandler(self.common); - self.userAttributeHandler = new userAttributeHandler(self.common); - self.commerceHandler = new commerceHandler(self.common); - - isInitialized = true; - } catch (e) { - console.log('Failed to initialize ' + name + ' - ' + e); - } - } - - function processEvent(event) { - var reportEvent = false; - if (isInitialized) { - try { - if (event.EventDataType === MessageType.SessionStart) { - reportEvent = logSessionStart(event); - } else if (event.EventDataType === MessageType.SessionEnd) { - reportEvent = logSessionEnd(event); - } else if (event.EventDataType === MessageType.CrashReport) { - reportEvent = logError(event); - } else if (event.EventDataType === MessageType.PageView) { - reportEvent = logPageView(event); - } else if (event.EventDataType === MessageType.Commerce) { - reportEvent = logEcommerceEvent(event); - } else if (event.EventDataType === MessageType.PageEvent) { - reportEvent = logEvent(event); - } else if (event.EventDataType === MessageType.Media) { - // Kits should just treat Media Events as generic Events - reportEvent = logEvent(event); - } - if (reportEvent === true && reportingService) { - reportingService(self, event); - return 'Successfully sent to ' + name; - } else { - return ( - 'Error logging event or event type not supported on forwarder ' + - name - ); - } - } catch (e) { - return 'Failed to send to ' + name + ' ' + e; - } - } else { - eventQueue.push(event); - return ( - "Can't send to forwarder " + - name + - ', not initialized. Event added to queue.' - ); - } - } - - function logSessionStart(event) { - try { - sessionHandler_1.onSessionStart(event); - return true; - } catch (e) { - return { - error: 'Error starting session on forwarder ' + name + '; ' + e, - }; - } - } - - function logSessionEnd(event) { - try { - sessionHandler_1.onSessionEnd(event); - return true; - } catch (e) { - return { - error: 'Error ending session on forwarder ' + name + '; ' + e, - }; - } - } - - function logError(event) { - try { - self.eventHandler.logError(event); - return true; - } catch (e) { - return { - error: 'Error logging error on forwarder ' + name + '; ' + e, - }; - } - } - - function logPageView(event) { - try { - self.eventHandler.logPageView(event); - return true; - } catch (e) { - return { - error: - 'Error logging page view on forwarder ' + name + '; ' + e, - }; - } - } - - function logEvent(event) { - try { - self.eventHandler.logEvent(event); - return true; - } catch (e) { - return { - error: 'Error logging event on forwarder ' + name + '; ' + e, - }; - } - } - - function logEcommerceEvent(event) { - try { - self.commerceHandler.logCommerceEvent(event); - return true; - } catch (e) { - return { - error: - 'Error logging purchase event on forwarder ' + - name + - '; ' + - e, - }; - } - } - - function setUserAttribute(key, value) { - if (isInitialized) { - try { - self.userAttributeHandler.onSetUserAttribute( - key, - value, - forwarderSettings - ); - return 'Successfully set user attribute on forwarder ' + name; - } catch (e) { - return ( - 'Error setting user attribute on forwarder ' + - name + - '; ' + - e - ); - } - } else { - return ( - "Can't set user attribute on forwarder " + - name + - ', not initialized' - ); - } - } - - function removeUserAttribute(key) { - if (isInitialized) { - try { - self.userAttributeHandler.onRemoveUserAttribute( - key, - forwarderSettings - ); - return ( - 'Successfully removed user attribute on forwarder ' + name - ); - } catch (e) { - return ( - 'Error removing user attribute on forwarder ' + - name + - '; ' + - e - ); - } - } else { - return ( - "Can't remove user attribute on forwarder " + - name + - ', not initialized' - ); - } - } - - function setUserIdentity(id, type) { - if (isInitialized) { - try { - self.identityHandler.onSetUserIdentity( - forwarderSettings, - id, - type - ); - return 'Successfully set user Identity on forwarder ' + name; - } catch (e) { - return ( - 'Error removing user attribute on forwarder ' + - name + - '; ' + - e - ); - } - } else { - return ( - "Can't call setUserIdentity on forwarder " + - name + - ', not initialized' - ); - } - } - - function onUserIdentified(user) { - if (isInitialized) { - try { - self.identityHandler.onUserIdentified(user); - - return ( - 'Successfully called onUserIdentified on forwarder ' + name - ); - } catch (e) { - return { - error: - 'Error calling onUserIdentified on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't set new user identities on forwader " + - name + - ', not initialized' - ); - } - } - - function onIdentifyComplete(user, filteredIdentityRequest) { - if (isInitialized) { - try { - self.identityHandler.onIdentifyComplete( - user, - filteredIdentityRequest - ); - - return ( - 'Successfully called onIdentifyComplete on forwarder ' + - name - ); - } catch (e) { - return { - error: - 'Error calling onIdentifyComplete on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't call onIdentifyCompleted on forwader " + - name + - ', not initialized' - ); - } - } - - function onLoginComplete(user, filteredIdentityRequest) { - if (isInitialized) { - try { - self.identityHandler.onLoginComplete( - user, - filteredIdentityRequest - ); - - return ( - 'Successfully called onLoginComplete on forwarder ' + name - ); - } catch (e) { - return { - error: - 'Error calling onLoginComplete on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't call onLoginComplete on forwader " + - name + - ', not initialized' - ); - } - } - - function onLogoutComplete(user, filteredIdentityRequest) { - if (isInitialized) { - try { - self.identityHandler.onLogoutComplete( - user, - filteredIdentityRequest - ); - - return ( - 'Successfully called onLogoutComplete on forwarder ' + name - ); - } catch (e) { - return { - error: - 'Error calling onLogoutComplete on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't call onLogoutComplete on forwader " + - name + - ', not initialized' - ); - } - } - - function onModifyComplete(user, filteredIdentityRequest) { - if (isInitialized) { - try { - self.identityHandler.onModifyComplete( - user, - filteredIdentityRequest - ); - - return ( - 'Successfully called onModifyComplete on forwarder ' + name - ); - } catch (e) { - return { - error: - 'Error calling onModifyComplete on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't call onModifyComplete on forwader " + - name + - ', not initialized' - ); - } - } - - function setOptOut(isOptingOutBoolean) { - if (isInitialized) { - try { - self.initialization.setOptOut(isOptingOutBoolean); - - return 'Successfully called setOptOut on forwarder ' + name; - } catch (e) { - return { - error: - 'Error calling setOptOut on forwarder ' + - name + - '; ' + - e, - }; - } - } else { - return ( - "Can't call setOptOut on forwader " + - name + - ', not initialized' - ); - } - } - - this.init = initForwarder; - this.process = processEvent; - this.setUserAttribute = setUserAttribute; - this.removeUserAttribute = removeUserAttribute; - this.onUserIdentified = onUserIdentified; - this.setUserIdentity = setUserIdentity; - this.onIdentifyComplete = onIdentifyComplete; - this.onLoginComplete = onLoginComplete; - this.onLogoutComplete = onLogoutComplete; - this.onModifyComplete = onModifyComplete; - this.setOptOut = setOptOut; - }; - - function getId() { - return moduleId; - } - - function isObject(val) { - return ( - val != null && typeof val === 'object' && Array.isArray(val) === false - ); - } - - function register(config) { - if (!config) { - console.log( - 'You must pass a config object to register the kit ' + name - ); - return; - } - - if (!isObject(config)) { - console.log( - "'config' must be an object. You passed in a " + typeof config - ); - return; - } - - if (isObject(config.kits)) { - config.kits[name] = { - constructor: constructor, - }; - } else { - config.kits = {}; - config.kits[name] = { - constructor: constructor, - }; - } - console.log( - 'Successfully registered ' + name + ' to your mParticle configuration' - ); - } - - if (typeof window !== 'undefined') { - if (window && window.mParticle && window.mParticle.addForwarder) { - window.mParticle.addForwarder({ - name: name, - constructor: constructor, - getId: getId, - }); - } - } - - var webKitWrapper = { - register: register, - }; - var webKitWrapper_1 = webKitWrapper.register; - - exports.default = webKitWrapper; - exports.register = webKitWrapper_1; - - return exports; - -}({})); From e602a4a45d435aad8848b02fd9df1b67fdb768f8 Mon Sep 17 00:00:00 2001 From: SbDove <100377552+SbDove@users.noreply.github.com> Date: Mon, 24 Jun 2024 09:02:16 -0700 Subject: [PATCH 8/8] Fix failing unit tests --- src/user-attribute-handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user-attribute-handler.js b/src/user-attribute-handler.js index ac271df..d00a122 100644 --- a/src/user-attribute-handler.js +++ b/src/user-attribute-handler.js @@ -23,7 +23,7 @@ UserAttributeHandler.prototype.onSetUserAttribute = function( mParticleUser ) { if (!this.common.userAttributes) { - this.common.userAttributes = mParticleUser.getAllUserAttributes(); + this.common.userAttributes = {}; } this.common.userAttributes[key] = value;