Skip to content

Commit abd371d

Browse files
authored
Merge pull request #175 from segmentio/gps/braze-at
[DEST-877] [DEST-876] Braze: Remove nested objects/arrays, Add undefined trait checks, use serverWorkerLocation from settings if available
2 parents a396d6a + f5bc7ff commit abd371d

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
lines changed

integrations/appboy/HISTORY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
1.11.0 / 2019-08-08
2+
==================
3+
4+
* Excludes nested non-null objects from customer user attributes in identify method
5+
* Excludes non-null objects from custom event properties in track method
6+
* Conditionally set user-related traits (ID, name, address, etc.)
7+
* Use serviceWorkerLocation from settings if it is available.
8+
19
1.9.0 / 2019-06-19
210
==================
311

integrations/appboy/lib/index.js

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ Appboy.prototype.initializeV2 = function(customEndpoint) {
203203
openNewsFeedCardsInNewTab: options.openNewsFeedCardsInNewTab,
204204
requireExplicitInAppMessageDismissal:
205205
options.requireExplicitInAppMessageDismissal,
206+
serviceWorkerLocation: options.serviceWorkerLocation,
206207
sessionTimeoutInSeconds: Number(options.sessionTimeoutInSeconds) || 30
207208
};
208209

@@ -255,13 +256,31 @@ Appboy.prototype.identify = function(identify) {
255256
var phone = identify.phone();
256257
var traits = clone(identify.traits());
257258

258-
window.appboy.changeUser(userId);
259-
window.appboy.getUser().setAvatarImageUrl(avatar);
260-
window.appboy.getUser().setEmail(email);
261-
window.appboy.getUser().setFirstName(firstName);
262-
window.appboy.getUser().setGender(getGender(gender));
263-
window.appboy.getUser().setLastName(lastName);
264-
window.appboy.getUser().setPhoneNumber(phone);
259+
if (userId) {
260+
window.appboy.changeUser(userId);
261+
}
262+
if (avatar) {
263+
window.appboy.getUser().setAvatarImageUrl(avatar);
264+
}
265+
if (email) {
266+
window.appboy.getUser().setEmail(email);
267+
}
268+
if (firstName) {
269+
window.appboy.getUser().setFirstName(firstName);
270+
}
271+
if (gender) {
272+
window.appboy.getUser().setGender(getGender(gender));
273+
}
274+
if (lastName) {
275+
window.appboy.getUser().setLastName(lastName);
276+
}
277+
if (phone) {
278+
window.appboy.getUser().setPhoneNumber(phone);
279+
}
280+
if (address) {
281+
window.appboy.getUser().setCountry(address.country);
282+
window.appboy.getUser().setHomeCity(address.city);
283+
}
265284
if (address) {
266285
window.appboy.getUser().setCountry(address.country);
267286
window.appboy.getUser().setHomeCity(address.city);
@@ -307,6 +326,14 @@ Appboy.prototype.identify = function(identify) {
307326
delete traits[key];
308327
}, reserved);
309328

329+
// Remove nested hash objects as Braze only supports nested array objects in identify calls
330+
// https://segment.com/docs/destinations/braze/#identify
331+
each(function(value, key) {
332+
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
333+
delete traits[key];
334+
}
335+
}, traits);
336+
310337
each(function(value, key) {
311338
window.appboy.getUser().setCustomUserAttribute(key, value);
312339
}, traits);
@@ -325,7 +352,9 @@ Appboy.prototype.group = function(group) {
325352
var userId = group.userId();
326353
var groupIdKey = 'ab_segment_group_' + group.groupId();
327354

328-
window.appboy.changeUser(userId);
355+
if (userId) {
356+
window.appboy.changeUser(userId);
357+
}
329358
window.appboy.getUser().setCustomUserAttribute(groupIdKey, true);
330359
};
331360

@@ -356,7 +385,17 @@ Appboy.prototype.track = function(track) {
356385
delete properties[key];
357386
}, reserved);
358387

359-
window.appboy.changeUser(userId);
388+
// Remove nested objects as Braze doesn't support objects in tracking calls
389+
// https://segment.com/docs/destinations/braze/#track
390+
each(function(value, key) {
391+
if (value != null && typeof value === 'object') {
392+
delete properties[key];
393+
}
394+
}, properties);
395+
396+
if (userId) {
397+
window.appboy.changeUser(userId);
398+
}
360399
window.appboy.logCustomEvent(eventName, properties);
361400
};
362401

@@ -379,7 +418,9 @@ Appboy.prototype.page = function(page) {
379418
var eventName = pageEvent.event();
380419
var properties = page.properties();
381420

382-
window.appboy.changeUser(userId);
421+
if (userId) {
422+
window.appboy.changeUser(userId);
423+
}
383424
window.appboy.logCustomEvent(eventName, properties);
384425
};
385426

@@ -400,7 +441,9 @@ Appboy.prototype.orderCompleted = function(track) {
400441
var currencyCode = track.currency();
401442
var purchaseProperties = track.properties();
402443

403-
window.appboy.changeUser(userId);
444+
if (userId) {
445+
window.appboy.changeUser(userId);
446+
}
404447

405448
// remove reduntant properties
406449
del(purchaseProperties, 'products');

integrations/appboy/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-appboy",
33
"description": "The Appboy analytics.js integration.",
4-
"version": "1.10.0",
4+
"version": "1.11.0",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",

integrations/appboy/test/index.test.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ describe('Appboy', function() {
229229
openInAppMessagesInNewTab: false,
230230
openNewsFeedCardsInNewTab: false,
231231
sessionTimeoutInSeconds: 30,
232+
serviceWorkerLocation: undefined,
232233
requireExplicitInAppMessageDismissal: false,
233234
enableHtmlInAppMessages: false
234235
};
@@ -335,12 +336,13 @@ describe('Appboy', function() {
335336
);
336337
});
337338

338-
it('should handle custom traits of all types', function() {
339+
it('should handle custom traits of valid types and exclude nested objects', function() {
339340
analytics.identify('userId', {
340341
song: "Who's That Chick?",
341342
artists: ['David Guetta', 'Rihanna'],
342343
number: 16,
343-
date: 'Tue Apr 25 2017 14:22:48 GMT-0700 (PDT)'
344+
date: 'Tue Apr 25 2017 14:22:48 GMT-0700 (PDT)',
345+
details: { nested: 'object' }
344346
});
345347
analytics.called(window.appboy.changeUser, 'userId');
346348
analytics.called(
@@ -411,15 +413,19 @@ describe('Appboy', function() {
411413
analytics.track('event with properties', {
412414
nickname: 'noonz',
413415
spiritAnimal: 'rihanna',
414-
best_friend: 'han'
416+
best_friend: 'han',
417+
number_of_friends: 12,
418+
idols: ['beyonce', 'madonna'],
419+
favoriteThings: { whiskers: 'on-kittins' }
415420
});
416421
analytics.called(
417422
window.appboy.logCustomEvent,
418423
'event with properties',
419424
{
420425
nickname: 'noonz',
421426
spiritAnimal: 'rihanna',
422-
best_friend: 'han'
427+
best_friend: 'han',
428+
number_of_friends: 12
423429
}
424430
);
425431
});

0 commit comments

Comments
 (0)