-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathFirebasePlugin.tsx
More file actions
96 lines (89 loc) · 2.28 KB
/
FirebasePlugin.tsx
File metadata and controls
96 lines (89 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {
DestinationPlugin,
IdentifyEventType,
PluginType,
ScreenEventType,
SegmentError,
ErrorType,
TrackEventType,
} from '@segment/analytics-react-native';
import screen from './methods/screen';
import track from './methods/track';
import reset from './methods/reset';
import { firebaseAnalytics } from './firebaseAnalytics';
export class FirebasePlugin extends DestinationPlugin {
type = PluginType.destination;
key = 'Firebase';
async identify(event: IdentifyEventType) {
if (event.userId !== undefined) {
await firebaseAnalytics.setUserId(event.userId);
}
if (event.traits) {
const eventTraits = event.traits;
const checkType = (value: unknown) => {
return typeof value === 'object' && !Array.isArray(value);
};
const safeTraits = Object.keys(eventTraits).reduce(
(acc: Record<string, string>, trait) => {
if (checkType(eventTraits[trait])) {
this.analytics?.logger.warn(
'We detected an object or array data type. Firebase does not accept nested traits.'
);
return acc;
}
if (trait !== undefined) {
acc[trait] =
typeof eventTraits[trait] === 'undefined'
? ''
: eventTraits[trait]!.toString();
}
return acc;
},
{}
);
await firebaseAnalytics.setUserProperties(safeTraits);
}
return event;
}
async track(event: TrackEventType) {
try {
await track(event);
} catch (error) {
this.analytics?.reportInternalError(
new SegmentError(
ErrorType.PluginError,
'Error on Firebase Track',
error
)
);
}
return event;
}
async screen(event: ScreenEventType) {
try {
await screen(event);
} catch (error) {
this.analytics?.reportInternalError(
new SegmentError(
ErrorType.PluginError,
'Error on Firebase Track',
error
)
);
}
return event;
}
async reset() {
try {
await reset();
} catch (error) {
this.analytics?.reportInternalError(
new SegmentError(
ErrorType.PluginError,
'Error on Firebase Track',
error
)
);
}
}
}