Skip to content

Commit 355c3be

Browse files
authored
feat: Add support to forward subscriptionGroupIds (#57)
1 parent 89e2beb commit 355c3be

File tree

2 files changed

+155
-17
lines changed

2 files changed

+155
-17
lines changed

src/BrazeKit-dev.js

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var constructor = function () {
4646
reportingService,
4747
hasConsentMappings,
4848
parsedConsentMappings,
49+
parsedSubscriptionGroupMapping = {},
4950
mpCustomFlags;
5051

5152
self.name = name;
@@ -571,6 +572,21 @@ var constructor = function () {
571572
return reportEvent;
572573
}
573574

575+
function setSubscriptionGroups(key, value) {
576+
var subscriptionGroupId = parsedSubscriptionGroupMapping[key];
577+
578+
if (typeof value !== 'boolean') {
579+
kitLogger("Can't call setSubscriptionGroups on forwarder " +
580+
name +
581+
', setSubscriptionGroups must set this value to a boolean');
582+
return;
583+
}
584+
585+
var action = value ? 'addToSubscriptionGroup' : 'removeFromSubscriptionGroup';
586+
kitLogger('braze.getUser().' + action, subscriptionGroupId);
587+
braze.getUser()[action](subscriptionGroupId);
588+
}
589+
574590
function removeUserAttribute(key) {
575591
if (!(key in DefaultAttributeMethods)) {
576592
var sanitizedKey = getSanitizedValueForBraze(key);
@@ -588,25 +604,31 @@ var constructor = function () {
588604
}
589605

590606
function setUserAttribute(key, value) {
591-
if (!(key in DefaultAttributeMethods)) {
592-
var sanitizedKey = getSanitizedValueForBraze(key);
593-
var sanitizedValue = getSanitizedValueForBraze(value);
594-
if (value != null && sanitizedValue == null) {
595-
return 'Value did not pass validation for ' + key;
596-
}
607+
if (key in DefaultAttributeMethods) {
608+
return setDefaultAttribute(key, value);
609+
}
597610

598-
kitLogger(
599-
'braze.getUser().setCustomUserAttribute',
600-
sanitizedKey,
601-
sanitizedValue
602-
);
611+
if (parsedSubscriptionGroupMapping[key]) {
612+
setSubscriptionGroups(key, value);
613+
return;
614+
}
603615

604-
braze
605-
.getUser()
606-
.setCustomUserAttribute(sanitizedKey, sanitizedValue);
607-
} else {
608-
return setDefaultAttribute(key, value);
616+
var sanitizedKey = getSanitizedValueForBraze(key);
617+
var sanitizedValue = getSanitizedValueForBraze(value);
618+
619+
if (value != null && sanitizedValue == null) {
620+
return 'Value did not pass validation for ' + key;
609621
}
622+
623+
kitLogger(
624+
'braze.getUser().setCustomUserAttribute',
625+
sanitizedKey,
626+
sanitizedValue
627+
);
628+
629+
braze
630+
.getUser()
631+
.setCustomUserAttribute(sanitizedKey, sanitizedValue);
610632
}
611633

612634
function setUserIdentity(id, type) {
@@ -887,6 +909,10 @@ var constructor = function () {
887909
}
888910
}
889911

912+
if (forwarderSettings.subscriptionGroupMapping) {
913+
parsedSubscriptionGroupMapping = decodeSubscriptionGroupMappings(forwarderSettings.subscriptionGroupMapping);
914+
}
915+
890916
var cluster =
891917
forwarderSettings.cluster ||
892918
forwarderSettings.dataCenterLocation;
@@ -965,6 +991,18 @@ var constructor = function () {
965991
}
966992
}
967993

994+
function decodeSubscriptionGroupMappings(subscriptionGroupSetting) {
995+
var subscriptionGroupIds = {};
996+
var decodedSetting = subscriptionGroupSetting.replace(/"/g, '"');
997+
var parsedSetting = JSON.parse(decodedSetting);
998+
for (let subscriptionGroupMap of parsedSetting) {
999+
var key = subscriptionGroupMap.map;
1000+
var value = subscriptionGroupMap.value;
1001+
subscriptionGroupIds[key] = value;
1002+
}
1003+
return subscriptionGroupIds;
1004+
}
1005+
9681006
function getSanitizedStringForBraze(value) {
9691007
if (typeof value === 'string') {
9701008
if (value.substr(0, 1) === '$') {
@@ -1029,6 +1067,7 @@ var constructor = function () {
10291067
this.onUserIdentified = onUserIdentified;
10301068
this.removeUserAttribute = removeUserAttribute;
10311069
this.decodeClusterSetting = decodeClusterSetting;
1070+
this.decodeSubscriptionGroupMappings = decodeSubscriptionGroupMappings;
10321071

10331072
/* An example output of this logger if we pass in a purchase event for 1 iPhone
10341073
with a SKU of iphoneSku that cost $999 with a product attribute of

test/tests.js

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ describe('Braze Forwarder', function() {
8080
this.monthOfBirth = null;
8181
this.dayOfBirth = null;
8282
this.customAttributes = {};
83+
this.subscriptionGroup = {};
8384

8485
this.customAttributeSet = false;
8586

@@ -137,6 +138,14 @@ describe('Braze Forwarder', function() {
137138
self.customAttributeSet = true;
138139
self.customAttributes[key] = value;
139140
};
141+
142+
this.addToSubscriptionGroup = function(key) {
143+
self.subscriptionGroup[key] = true;
144+
}
145+
146+
this.removeFromSubscriptionGroup = function(key) {
147+
self.subscriptionGroup[key] = false;
148+
}
140149
},
141150
MockBraze = function() {
142151
var self = this;
@@ -175,6 +184,7 @@ describe('Braze Forwarder', function() {
175184
self.apiKey = apiKey;
176185
self.baseUrl = options.baseUrl || null;
177186
self.doNotLoadFontAwesome = options.doNotLoadFontAwesome;
187+
self.parsedSubscriptionGroupMapping = {};
178188
return true;
179189
};
180190

@@ -1107,6 +1117,95 @@ describe('Braze Forwarder', function() {
11071117
(window.braze.getUser().yearOfBirth === null).should.equal(true);
11081118
});
11091119

1120+
it('decodeSubscriptionGroupMappings should return parsed subscriptionGroupIds map when proper setting is given', function () {
1121+
// sample subscriptionGroupMapping from config
1122+
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"}]';
1123+
1124+
// get the decoded mapped subscriptionGroup
1125+
var parsedSubscriptionGroupMapping = mParticle.forwarder.decodeSubscriptionGroupMappings(subscriptionGroupMapping);
1126+
var expectedResult = {
1127+
'subscriptionGroupTest1': '00000000-0000-0000-0000-000000000000',
1128+
'subscriptionGroupTest2': '00000000-0000-0000-0000-000000000001',
1129+
};
1130+
1131+
parsedSubscriptionGroupMapping.should.deepEqual(expectedResult);
1132+
});
1133+
1134+
it('should set subscription group for mapped attributes when value is true with type boolean', function() {
1135+
// sample subscriptionGroupMapping from config
1136+
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"}]';
1137+
1138+
// initialize Braze kit with subscriptionGroupMappings
1139+
mParticle.forwarder.init(
1140+
{
1141+
apiKey: '123456',
1142+
subscriptionGroupMapping: subscriptionGroupMapping,
1143+
},
1144+
reportService.cb,
1145+
true,
1146+
null
1147+
);
1148+
1149+
// get the decoded mapped subscriptionGroup
1150+
var parsedSubscriptionGroupMapping = mParticle.forwarder.decodeSubscriptionGroupMappings(subscriptionGroupMapping);
1151+
1152+
// set attribute subscriptionGroupTest1 with boolean value true should call Braze's addToSubscriptionGroup since it's mapped
1153+
mParticle.forwarder.setUserAttribute('subscriptionGroupTest1', true);
1154+
var mappedSubscriptionGroupId = parsedSubscriptionGroupMapping['subscriptionGroupTest1'];
1155+
window.braze.getUser().subscriptionGroup[mappedSubscriptionGroupId].should.equal(true);
1156+
});
1157+
1158+
it('should set subscription group for mapped attributes when value is false with type boolean', function() {
1159+
// sample subscriptionGroupMapping from config
1160+
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"}]';
1161+
1162+
// initialize Braze kit with subscriptionGroupMappings
1163+
mParticle.forwarder.init(
1164+
{
1165+
apiKey: '123456',
1166+
subscriptionGroupMapping: subscriptionGroupMapping,
1167+
},
1168+
reportService.cb,
1169+
true,
1170+
null
1171+
);
1172+
1173+
// get the decoded mapped subscriptionGroup
1174+
var parsedSubscriptionGroupMapping = mParticle.forwarder.decodeSubscriptionGroupMappings(subscriptionGroupMapping);
1175+
1176+
// set attribute subscriptionGroupTest2 with boolean value false should call Braze's removeFromSubscriptionGroup since it's mapped
1177+
mParticle.forwarder.setUserAttribute('subscriptionGroupTest2', false);
1178+
var mappedSubscriptionGroupId = parsedSubscriptionGroupMapping['subscriptionGroupTest2'];
1179+
window.braze.getUser().subscriptionGroup[mappedSubscriptionGroupId].should.equal(false);
1180+
});
1181+
1182+
it('should not set subscription group for mapped attributes when value type is not boolean', function() {
1183+
// sample subscriptionGroupMapping from config
1184+
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"}]';
1185+
1186+
// initialize Braze kit with subscriptionGroupMappings
1187+
mParticle.forwarder.init(
1188+
{
1189+
apiKey: '123456',
1190+
subscriptionGroupMapping: subscriptionGroupMapping,
1191+
},
1192+
reportService.cb,
1193+
true,
1194+
null
1195+
);
1196+
1197+
// should log error if mapped attribute value is not type boolean
1198+
mParticle.forwarder.logger = {
1199+
verbose: function(msg) {
1200+
mParticle.forwarder.msg = msg;
1201+
},
1202+
};
1203+
1204+
mParticle.forwarder.setUserAttribute('subscriptionGroupTest1', 'testStringValue');
1205+
var expectedMessage = `mParticle - Braze Web Kit log:\nCan\'t call setSubscriptionGroups on forwarder Appboy, setSubscriptionGroups must set this value to a boolean:\n`;
1206+
mParticle.forwarder.msg.should.equal(expectedMessage)
1207+
});
1208+
11101209
it('should not set default values if a string is not passed as the attribute', function() {
11111210
mParticle.forwarder.setUserAttribute('first_name', 'John');
11121211
mParticle.forwarder.setUserAttribute('last_name', 'Doe');
@@ -1119,7 +1218,7 @@ describe('Braze Forwarder', function() {
11191218
it('should set a custom user attribute', function() {
11201219
mParticle.forwarder.setUserAttribute('test', 'result');
11211220
window.braze.getUser().should.have.property('customAttributeSet', true);
1122-
window.braze.getUser().customAttributes['test'].should.equal('result');;
1221+
window.braze.getUser().customAttributes['test'].should.equal('result');
11231222
});
11241223

11251224
it('should set a custom user attribute of diffferent types', function() {

0 commit comments

Comments
 (0)