Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 55 additions & 16 deletions src/BrazeKit-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var constructor = function () {
reportingService,
hasConsentMappings,
parsedConsentMappings,
parsedSubscriptionGroupMapping = {},
mpCustomFlags;

self.name = name;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -887,6 +909,10 @@ var constructor = function () {
}
}

if (forwarderSettings.subscriptionGroupMapping) {
parsedSubscriptionGroupMapping = decodeSubscriptionGroupMappings(forwarderSettings.subscriptionGroupMapping);
}

var cluster =
forwarderSettings.cluster ||
forwarderSettings.dataCenterLocation;
Expand Down Expand Up @@ -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;
Comment on lines +996 to +1003
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmustafa-tse I realize, we should actually put a try/catch block here just to be extra safe.

}

function getSanitizedStringForBraze(value) {
if (typeof value === 'string') {
if (value.substr(0, 1) === '$') {
Expand Down Expand Up @@ -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
Expand Down
101 changes: 100 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('Braze Forwarder', function() {
this.monthOfBirth = null;
this.dayOfBirth = null;
this.customAttributes = {};
this.subscriptionGroup = {};

this.customAttributeSet = false;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -175,6 +184,7 @@ describe('Braze Forwarder', function() {
self.apiKey = apiKey;
self.baseUrl = options.baseUrl || null;
self.doNotLoadFontAwesome = options.doNotLoadFontAwesome;
self.parsedSubscriptionGroupMapping = {};
return true;
};

Expand Down Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a separate test. You should have one test for the "positive" case, a separate one for the "negative" case, and then additional tests for specific edge cases.

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');
Expand All @@ -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() {
Expand Down