diff --git a/src/BrazeKit-dev.js b/src/BrazeKit-dev.js index 6bed429..fd97f77 100644 --- a/src/BrazeKit-dev.js +++ b/src/BrazeKit-dev.js @@ -46,6 +46,7 @@ var constructor = function () { reportingService, hasConsentMappings, parsedConsentMappings, + parsedSubscriptionGroupMapping = {}, mpCustomFlags; self.name = name; @@ -571,6 +572,21 @@ var constructor = function () { return reportEvent; } + function setSubscriptionGroups(key, value) { + var subscriptionGroupId = parsedSubscriptionGroupMapping[key]; + + if (typeof value !== 'boolean') { + kitLogger("Can't call setSubscriptionGroups on forwarder " + + name + + ', setSubscriptionGroups must set this value to a boolean'); + return; + } + + var action = value ? 'addToSubscriptionGroup' : 'removeFromSubscriptionGroup'; + kitLogger('braze.getUser().' + action, subscriptionGroupId); + braze.getUser()[action](subscriptionGroupId); + } + function removeUserAttribute(key) { if (!(key in DefaultAttributeMethods)) { var sanitizedKey = getSanitizedValueForBraze(key); @@ -588,25 +604,31 @@ var constructor = function () { } function setUserAttribute(key, value) { - if (!(key in DefaultAttributeMethods)) { - var sanitizedKey = getSanitizedValueForBraze(key); - var sanitizedValue = getSanitizedValueForBraze(value); - if (value != null && sanitizedValue == null) { - return 'Value did not pass validation for ' + key; - } + if (key in DefaultAttributeMethods) { + return setDefaultAttribute(key, value); + } - kitLogger( - 'braze.getUser().setCustomUserAttribute', - sanitizedKey, - sanitizedValue - ); + if (parsedSubscriptionGroupMapping[key]) { + setSubscriptionGroups(key, value); + return; + } - braze - .getUser() - .setCustomUserAttribute(sanitizedKey, sanitizedValue); - } else { - return setDefaultAttribute(key, value); + var sanitizedKey = getSanitizedValueForBraze(key); + var sanitizedValue = getSanitizedValueForBraze(value); + + if (value != null && sanitizedValue == null) { + return 'Value did not pass validation for ' + key; } + + kitLogger( + 'braze.getUser().setCustomUserAttribute', + sanitizedKey, + sanitizedValue + ); + + braze + .getUser() + .setCustomUserAttribute(sanitizedKey, sanitizedValue); } function setUserIdentity(id, type) { @@ -887,6 +909,10 @@ var constructor = function () { } } + if (forwarderSettings.subscriptionGroupMapping) { + parsedSubscriptionGroupMapping = decodeSubscriptionGroupMappings(forwarderSettings.subscriptionGroupMapping); + } + var cluster = forwarderSettings.cluster || forwarderSettings.dataCenterLocation; @@ -965,6 +991,18 @@ var constructor = function () { } } + function decodeSubscriptionGroupMappings(subscriptionGroupSetting) { + var subscriptionGroupIds = {}; + var decodedSetting = subscriptionGroupSetting.replace(/"/g, '"'); + var parsedSetting = JSON.parse(decodedSetting); + for (let subscriptionGroupMap of parsedSetting) { + var key = subscriptionGroupMap.map; + var value = subscriptionGroupMap.value; + subscriptionGroupIds[key] = value; + } + return subscriptionGroupIds; + } + function getSanitizedStringForBraze(value) { if (typeof value === 'string') { if (value.substr(0, 1) === '$') { @@ -1029,6 +1067,7 @@ var constructor = function () { this.onUserIdentified = onUserIdentified; this.removeUserAttribute = removeUserAttribute; this.decodeClusterSetting = decodeClusterSetting; + this.decodeSubscriptionGroupMappings = decodeSubscriptionGroupMappings; /* An example output of this logger if we pass in a purchase event for 1 iPhone with a SKU of iphoneSku that cost $999 with a product attribute of diff --git a/test/tests.js b/test/tests.js index 05480a3..861d1c1 100644 --- a/test/tests.js +++ b/test/tests.js @@ -80,6 +80,7 @@ describe('Braze Forwarder', function() { this.monthOfBirth = null; this.dayOfBirth = null; this.customAttributes = {}; + this.subscriptionGroup = {}; this.customAttributeSet = false; @@ -137,6 +138,14 @@ describe('Braze Forwarder', function() { self.customAttributeSet = true; self.customAttributes[key] = value; }; + + this.addToSubscriptionGroup = function(key) { + self.subscriptionGroup[key] = true; + } + + this.removeFromSubscriptionGroup = function(key) { + self.subscriptionGroup[key] = false; + } }, MockBraze = function() { var self = this; @@ -175,6 +184,7 @@ describe('Braze Forwarder', function() { self.apiKey = apiKey; self.baseUrl = options.baseUrl || null; self.doNotLoadFontAwesome = options.doNotLoadFontAwesome; + self.parsedSubscriptionGroupMapping = {}; return true; }; @@ -1107,6 +1117,95 @@ describe('Braze Forwarder', function() { (window.braze.getUser().yearOfBirth === null).should.equal(true); }); + it('decodeSubscriptionGroupMappings should return parsed subscriptionGroupIds map when proper setting is given', function () { + // sample subscriptionGroupMapping from config + var subscriptionGroupMapping = '[{"jsmap":null,"map":"subscriptionGroupTest1","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000000"},{"jsmap":null,"map":"subscriptionGroupTest2","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000001"}]'; + + // get the decoded mapped subscriptionGroup + var parsedSubscriptionGroupMapping = mParticle.forwarder.decodeSubscriptionGroupMappings(subscriptionGroupMapping); + var expectedResult = { + 'subscriptionGroupTest1': '00000000-0000-0000-0000-000000000000', + 'subscriptionGroupTest2': '00000000-0000-0000-0000-000000000001', + }; + + parsedSubscriptionGroupMapping.should.deepEqual(expectedResult); + }); + + it('should set subscription group for mapped attributes when value is true with type boolean', function() { + // sample subscriptionGroupMapping from config + var subscriptionGroupMapping = '[{"jsmap":null,"map":"subscriptionGroupTest1","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000000"},{"jsmap":null,"map":"subscriptionGroupTest2","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000001"}]'; + + // initialize Braze kit with subscriptionGroupMappings + mParticle.forwarder.init( + { + apiKey: '123456', + subscriptionGroupMapping: subscriptionGroupMapping, + }, + reportService.cb, + true, + null + ); + + // get the decoded mapped subscriptionGroup + var parsedSubscriptionGroupMapping = mParticle.forwarder.decodeSubscriptionGroupMappings(subscriptionGroupMapping); + + // set attribute subscriptionGroupTest1 with boolean value true should call Braze's addToSubscriptionGroup since it's mapped + mParticle.forwarder.setUserAttribute('subscriptionGroupTest1', true); + var mappedSubscriptionGroupId = parsedSubscriptionGroupMapping['subscriptionGroupTest1']; + window.braze.getUser().subscriptionGroup[mappedSubscriptionGroupId].should.equal(true); + }); + + it('should set subscription group for mapped attributes when value is false with type boolean', function() { + // sample subscriptionGroupMapping from config + var subscriptionGroupMapping = '[{"jsmap":null,"map":"subscriptionGroupTest1","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000000"},{"jsmap":null,"map":"subscriptionGroupTest2","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000001"}]'; + + // initialize Braze kit with subscriptionGroupMappings + mParticle.forwarder.init( + { + apiKey: '123456', + subscriptionGroupMapping: subscriptionGroupMapping, + }, + reportService.cb, + true, + null + ); + + // get the decoded mapped subscriptionGroup + var parsedSubscriptionGroupMapping = mParticle.forwarder.decodeSubscriptionGroupMappings(subscriptionGroupMapping); + + // set attribute subscriptionGroupTest2 with boolean value false should call Braze's removeFromSubscriptionGroup since it's mapped + mParticle.forwarder.setUserAttribute('subscriptionGroupTest2', false); + var mappedSubscriptionGroupId = parsedSubscriptionGroupMapping['subscriptionGroupTest2']; + window.braze.getUser().subscriptionGroup[mappedSubscriptionGroupId].should.equal(false); + }); + + it('should not set subscription group for mapped attributes when value type is not boolean', function() { + // sample subscriptionGroupMapping from config + var subscriptionGroupMapping = '[{"jsmap":null,"map":"subscriptionGroupTest1","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000000"},{"jsmap":null,"map":"subscriptionGroupTest2","maptype":"UserAttributeClass.Name","value":"00000000-0000-0000-0000-000000000001"}]'; + + // initialize Braze kit with subscriptionGroupMappings + mParticle.forwarder.init( + { + apiKey: '123456', + subscriptionGroupMapping: subscriptionGroupMapping, + }, + reportService.cb, + true, + null + ); + + // should log error if mapped attribute value is not type boolean + mParticle.forwarder.logger = { + verbose: function(msg) { + mParticle.forwarder.msg = msg; + }, + }; + + mParticle.forwarder.setUserAttribute('subscriptionGroupTest1', 'testStringValue'); + var expectedMessage = `mParticle - Braze Web Kit log:\nCan\'t call setSubscriptionGroups on forwarder Appboy, setSubscriptionGroups must set this value to a boolean:\n`; + mParticle.forwarder.msg.should.equal(expectedMessage) + }); + it('should not set default values if a string is not passed as the attribute', function() { mParticle.forwarder.setUserAttribute('first_name', 'John'); mParticle.forwarder.setUserAttribute('last_name', 'Doe'); @@ -1119,7 +1218,7 @@ describe('Braze Forwarder', function() { it('should set a custom user attribute', function() { mParticle.forwarder.setUserAttribute('test', 'result'); window.braze.getUser().should.have.property('customAttributeSet', true); - window.braze.getUser().customAttributes['test'].should.equal('result');; + window.braze.getUser().customAttributes['test'].should.equal('result'); }); it('should set a custom user attribute of diffferent types', function() {