Skip to content

fix: add warnings when the date format does not parse #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2023
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
2 changes: 1 addition & 1 deletion packages/core/src/info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const libraryInfo = {
name: '@segment/analytics-react-native',
version: '2.10.0',
version: '2.10.1',
};
84 changes: 81 additions & 3 deletions packages/plugins/plugin-braze/src/BrazePlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import {
TrackEventType,
UserInfoState,
} from '@segment/analytics-react-native';
import identify from './methods/identify';
import track from './methods/track';
import ReactAppboy, {
GenderTypes,
MonthsAsNumber,
} from 'react-native-appboy-sdk';
import flush from './methods/flush';
import track from './methods/track';

export class BrazePlugin extends DestinationPlugin {
type = PluginType.destination;
Expand All @@ -24,7 +27,82 @@ export class BrazePlugin extends DestinationPlugin {
) {
return;
} else {
identify(event);
if (event.userId) {
ReactAppboy.changeUser(event.userId);
}

if (event.traits?.birthday !== undefined) {
const birthday = new Date(event.traits.birthday);
if (
birthday !== undefined &&
birthday !== null &&
!isNaN(birthday.getTime())
) {
const data = new Date(event.traits.birthday);
ReactAppboy.setDateOfBirth(
data.getFullYear(),
// getMonth is zero indexed
(data.getMonth() + 1) as MonthsAsNumber,
data.getDate()
);
} else {
this.analytics?.logger.warn(
`Birthday found "${event.traits?.birthday}" could not be parsed as a Date. Try converting to ISO format.`
);
}
}

if (event.traits?.email !== undefined) {
ReactAppboy.setEmail(event.traits.email);
}

if (event.traits?.firstName !== undefined) {
ReactAppboy.setFirstName(event.traits.firstName);
}

if (event.traits?.lastName !== undefined) {
ReactAppboy.setLastName(event.traits.lastName);
}

if (event.traits?.gender !== undefined) {
const validGenders = ['m', 'f', 'n', 'o', 'p', 'u'];
const isValidGender = validGenders.indexOf(event.traits.gender) > -1;
if (isValidGender) {
ReactAppboy.setGender(
event.traits.gender as GenderTypes[keyof GenderTypes]
);
}
}

if (event.traits?.phone !== undefined) {
ReactAppboy.setPhoneNumber(event.traits.phone);
}

if (event.traits?.address !== undefined) {
if (event.traits.address.city !== undefined) {
ReactAppboy.setHomeCity(event.traits.address.city);
}
if (event.traits?.address.country !== undefined) {
ReactAppboy.setCountry(event.traits.address.country);
}
}

const appBoyTraits = [
'birthday',
'email',
'firstName',
'lastName',
'gender',
'phone',
'address',
];

Object.entries(event.traits ?? {}).forEach(([key, value]) => {
if (appBoyTraits.indexOf(key) < 0) {
ReactAppboy.setCustomUserAttribute(key, value as any);
}
});

this.lastSeenTraits = {
anonymousId: event.anonymousId ?? '',
userId: event.userId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import identify from '../identify';
import {
changeUser,
setFirstName,
Expand All @@ -12,13 +11,15 @@ import {
setCustomUserAttribute,
} from '../__mocks__/react-native-appboy-sdk';
import type { IdentifyEventType } from '@segment/analytics-react-native';
import { BrazePlugin } from '../../BrazePlugin';

describe('#identify', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('calls correct methods #1', () => {
const plugin = new BrazePlugin();
const payload = {
type: 'identify',
traits: {
Expand All @@ -29,7 +30,7 @@ describe('#identify', () => {
userId: 'user',
};

identify(payload as IdentifyEventType);
plugin.identify(payload as IdentifyEventType);

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

it('calls correct methods #2', () => {
const plugin = new BrazePlugin();
const payload = {
type: 'identify',
traits: {
Expand All @@ -49,14 +51,35 @@ describe('#identify', () => {
},
};

identify(payload as IdentifyEventType);
plugin.identify(payload as IdentifyEventType);

expect(setDateOfBirth).toHaveBeenCalledWith(2020, 2, 29);
expect(setLastName).toHaveBeenCalledWith('Smith');
expect(setHomeCity).toHaveBeenCalledWith('Denver');
});

it('handles invalid dates gracefully', () => {
const plugin = new BrazePlugin();
const payload = {
type: 'identify',
traits: {
lastName: 'Smith',
birthday: 'not a valid date',
address: {
city: 'Denver',
},
},
};

plugin.identify(payload as IdentifyEventType);

expect(setDateOfBirth).not.toHaveBeenCalled();
expect(setLastName).toHaveBeenCalledWith('Smith');
expect(setHomeCity).toHaveBeenCalledWith('Denver');
});

it('calls correct methods #3', () => {
const plugin = new BrazePlugin();
const payload = {
type: 'identify',
traits: {
Expand All @@ -68,22 +91,23 @@ describe('#identify', () => {
},
};

identify(payload as IdentifyEventType);
plugin.identify(payload as IdentifyEventType);

expect(setEmail).toHaveBeenCalledWith('[email protected]');
expect(setGender).toHaveBeenCalledWith('o');
expect(setCountry).toHaveBeenCalledWith('US');
});

it('only calls setGender with defined values', () => {
const plugin = new BrazePlugin();
const payload = {
type: 'identify',
traits: {
gender: 'male',
},
};

identify(payload as IdentifyEventType);
plugin.identify(payload as IdentifyEventType);

expect(setGender).not.toHaveBeenCalled();
});
Expand Down
74 changes: 0 additions & 74 deletions packages/plugins/plugin-braze/src/methods/identify.ts

This file was deleted.