Skip to content

Commit 2777bea

Browse files
authored
fix: add warnings when the date format does not parse (#748)
1 parent d49b4ad commit 2777bea

File tree

3 files changed

+110
-82
lines changed

3 files changed

+110
-82
lines changed

packages/plugins/plugin-braze/src/BrazePlugin.tsx

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import {
55
TrackEventType,
66
UserInfoState,
77
} from '@segment/analytics-react-native';
8-
import identify from './methods/identify';
9-
import track from './methods/track';
8+
import ReactAppboy, {
9+
GenderTypes,
10+
MonthsAsNumber,
11+
} from 'react-native-appboy-sdk';
1012
import flush from './methods/flush';
13+
import track from './methods/track';
1114

1215
export class BrazePlugin extends DestinationPlugin {
1316
type = PluginType.destination;
@@ -24,7 +27,82 @@ export class BrazePlugin extends DestinationPlugin {
2427
) {
2528
return;
2629
} else {
27-
identify(event);
30+
if (event.userId) {
31+
ReactAppboy.changeUser(event.userId);
32+
}
33+
34+
if (event.traits?.birthday !== undefined) {
35+
const birthday = new Date(event.traits.birthday);
36+
if (
37+
birthday !== undefined &&
38+
birthday !== null &&
39+
!isNaN(birthday.getTime())
40+
) {
41+
const data = new Date(event.traits.birthday);
42+
ReactAppboy.setDateOfBirth(
43+
data.getFullYear(),
44+
// getMonth is zero indexed
45+
(data.getMonth() + 1) as MonthsAsNumber,
46+
data.getDate()
47+
);
48+
} else {
49+
this.analytics?.logger.warn(
50+
`Birthday found "${event.traits?.birthday}" could not be parsed as a Date. Try converting to ISO format.`
51+
);
52+
}
53+
}
54+
55+
if (event.traits?.email !== undefined) {
56+
ReactAppboy.setEmail(event.traits.email);
57+
}
58+
59+
if (event.traits?.firstName !== undefined) {
60+
ReactAppboy.setFirstName(event.traits.firstName);
61+
}
62+
63+
if (event.traits?.lastName !== undefined) {
64+
ReactAppboy.setLastName(event.traits.lastName);
65+
}
66+
67+
if (event.traits?.gender !== undefined) {
68+
const validGenders = ['m', 'f', 'n', 'o', 'p', 'u'];
69+
const isValidGender = validGenders.indexOf(event.traits.gender) > -1;
70+
if (isValidGender) {
71+
ReactAppboy.setGender(
72+
event.traits.gender as GenderTypes[keyof GenderTypes]
73+
);
74+
}
75+
}
76+
77+
if (event.traits?.phone !== undefined) {
78+
ReactAppboy.setPhoneNumber(event.traits.phone);
79+
}
80+
81+
if (event.traits?.address !== undefined) {
82+
if (event.traits.address.city !== undefined) {
83+
ReactAppboy.setHomeCity(event.traits.address.city);
84+
}
85+
if (event.traits?.address.country !== undefined) {
86+
ReactAppboy.setCountry(event.traits.address.country);
87+
}
88+
}
89+
90+
const appBoyTraits = [
91+
'birthday',
92+
'email',
93+
'firstName',
94+
'lastName',
95+
'gender',
96+
'phone',
97+
'address',
98+
];
99+
100+
Object.entries(event.traits ?? {}).forEach(([key, value]) => {
101+
if (appBoyTraits.indexOf(key) < 0) {
102+
ReactAppboy.setCustomUserAttribute(key, value as any);
103+
}
104+
});
105+
28106
this.lastSeenTraits = {
29107
anonymousId: event.anonymousId ?? '',
30108
userId: event.userId,

packages/plugins/plugin-braze/src/methods/__tests__/identify.test.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import identify from '../identify';
21
import {
32
changeUser,
43
setFirstName,
@@ -12,13 +11,15 @@ import {
1211
setCustomUserAttribute,
1312
} from '../__mocks__/react-native-appboy-sdk';
1413
import type { IdentifyEventType } from '@segment/analytics-react-native';
14+
import { BrazePlugin } from '../../BrazePlugin';
1515

1616
describe('#identify', () => {
1717
beforeEach(() => {
1818
jest.clearAllMocks();
1919
});
2020

2121
it('calls correct methods #1', () => {
22+
const plugin = new BrazePlugin();
2223
const payload = {
2324
type: 'identify',
2425
traits: {
@@ -29,7 +30,7 @@ describe('#identify', () => {
2930
userId: 'user',
3031
};
3132

32-
identify(payload as IdentifyEventType);
33+
plugin.identify(payload as IdentifyEventType);
3334

3435
expect(changeUser).toHaveBeenCalledWith('user');
3536
expect(setFirstName).toHaveBeenCalledWith('John');
@@ -38,6 +39,7 @@ describe('#identify', () => {
3839
});
3940

4041
it('calls correct methods #2', () => {
42+
const plugin = new BrazePlugin();
4143
const payload = {
4244
type: 'identify',
4345
traits: {
@@ -49,14 +51,35 @@ describe('#identify', () => {
4951
},
5052
};
5153

52-
identify(payload as IdentifyEventType);
54+
plugin.identify(payload as IdentifyEventType);
5355

5456
expect(setDateOfBirth).toHaveBeenCalledWith(2020, 2, 29);
5557
expect(setLastName).toHaveBeenCalledWith('Smith');
5658
expect(setHomeCity).toHaveBeenCalledWith('Denver');
5759
});
5860

61+
it('handles invalid dates gracefully', () => {
62+
const plugin = new BrazePlugin();
63+
const payload = {
64+
type: 'identify',
65+
traits: {
66+
lastName: 'Smith',
67+
birthday: 'not a valid date',
68+
address: {
69+
city: 'Denver',
70+
},
71+
},
72+
};
73+
74+
plugin.identify(payload as IdentifyEventType);
75+
76+
expect(setDateOfBirth).not.toHaveBeenCalled();
77+
expect(setLastName).toHaveBeenCalledWith('Smith');
78+
expect(setHomeCity).toHaveBeenCalledWith('Denver');
79+
});
80+
5981
it('calls correct methods #3', () => {
82+
const plugin = new BrazePlugin();
6083
const payload = {
6184
type: 'identify',
6285
traits: {
@@ -68,22 +91,23 @@ describe('#identify', () => {
6891
},
6992
};
7093

71-
identify(payload as IdentifyEventType);
94+
plugin.identify(payload as IdentifyEventType);
7295

7396
expect(setEmail).toHaveBeenCalledWith('[email protected]');
7497
expect(setGender).toHaveBeenCalledWith('o');
7598
expect(setCountry).toHaveBeenCalledWith('US');
7699
});
77100

78101
it('only calls setGender with defined values', () => {
102+
const plugin = new BrazePlugin();
79103
const payload = {
80104
type: 'identify',
81105
traits: {
82106
gender: 'male',
83107
},
84108
};
85109

86-
identify(payload as IdentifyEventType);
110+
plugin.identify(payload as IdentifyEventType);
87111

88112
expect(setGender).not.toHaveBeenCalled();
89113
});

packages/plugins/plugin-braze/src/methods/identify.ts

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)