Skip to content

Commit 559a3bb

Browse files
fix: add logic to convert traits to strings (#763)
* fix: add logic to convert traits to strings * fix: refactor identify logic based on review * refactor: move identify event, fix tests, refactor ---------
1 parent a874459 commit 559a3bb

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

packages/plugins/plugin-firebase/src/FirebasePlugin.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,45 @@ import {
55
ScreenEventType,
66
TrackEventType,
77
} from '@segment/analytics-react-native';
8-
import identify from './methods/identify';
98
import screen from './methods/screen';
109
import track from './methods/track';
1110
import reset from './methods/reset';
11+
import firebaseAnalytics from '@react-native-firebase/analytics';
1212

1313
export class FirebasePlugin extends DestinationPlugin {
1414
type = PluginType.destination;
1515
key = 'Firebase';
1616

17-
identify(event: IdentifyEventType) {
18-
identify(event);
17+
async identify(event: IdentifyEventType) {
18+
if (event.userId !== undefined) {
19+
await firebaseAnalytics().setUserId(event.userId!);
20+
}
21+
if (event.traits) {
22+
let eventTraits = event.traits;
23+
let safeTraits: Record<string, string>;
24+
25+
const checkType = (value: unknown) => {
26+
return typeof value === 'object' && !Array.isArray(value);
27+
};
28+
safeTraits = Object.keys(eventTraits).reduce(
29+
(acc: Record<string, string>, trait) => {
30+
if (checkType(eventTraits[trait])) {
31+
this.analytics?.logger.warn(
32+
'We detected an object or array data type. Firebase does not accept nested traits.'
33+
);
34+
35+
return acc;
36+
}
37+
if (trait !== undefined) {
38+
acc[trait] = eventTraits[trait]!.toString();
39+
}
40+
return acc;
41+
},
42+
{}
43+
);
44+
45+
await firebaseAnalytics().setUserProperties(safeTraits);
46+
}
1947
return event;
2048
}
2149

packages/plugins/plugin-firebase/src/methods/___tests__/identify.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IdentifyEventType } from '@segment/analytics-react-native';
2-
import identify from '../identify';
2+
import { FirebasePlugin } from '../../FirebasePlugin';
33

44
const mockSetUserId = jest.fn();
55
const mockSetUserProperties = jest.fn();
@@ -15,6 +15,7 @@ describe('#identify', () => {
1515
});
1616

1717
it('forwards the identify event with ID only', async () => {
18+
const plugin = new FirebasePlugin();
1819
const event = {
1920
type: 'identify',
2021
userId: '123',
@@ -23,14 +24,15 @@ describe('#identify', () => {
2324
timestamp: '00000',
2425
} as IdentifyEventType;
2526

26-
await identify(event);
27+
await plugin.identify(event);
2728

2829
expect(mockSetUserId).toHaveBeenCalledTimes(1);
2930
expect(mockSetUserProperties).not.toHaveBeenCalled();
3031
expect(mockSetUserId).toHaveBeenCalledWith('123');
3132
});
3233

3334
it('forwards the identify event with ID and properties', async () => {
35+
const plugin = new FirebasePlugin();
3436
const event = {
3537
type: 'identify',
3638
userId: '123',
@@ -42,7 +44,7 @@ describe('#identify', () => {
4244
},
4345
} as IdentifyEventType;
4446

45-
await identify(event);
47+
await plugin.identify(event);
4648

4749
expect(mockSetUserId).toHaveBeenCalledTimes(1);
4850
expect(mockSetUserProperties).toHaveBeenCalledTimes(1);
@@ -51,6 +53,7 @@ describe('#identify', () => {
5153
});
5254

5355
it('forwards the identify event without ID', async () => {
56+
const plugin = new FirebasePlugin();
5457
const event = {
5558
type: 'identify',
5659
anonymousId: 'anon',
@@ -61,7 +64,7 @@ describe('#identify', () => {
6164
},
6265
} as IdentifyEventType;
6366

64-
await identify(event);
67+
await plugin.identify(event);
6568

6669
expect(mockSetUserId).not.toHaveBeenCalled();
6770
expect(mockSetUserProperties).toHaveBeenCalledTimes(1);

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

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

0 commit comments

Comments
 (0)