-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathMixpanelPlugin.ts
More file actions
107 lines (96 loc) · 2.74 KB
/
Copy pathMixpanelPlugin.ts
File metadata and controls
107 lines (96 loc) · 2.74 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
97
98
99
100
101
102
103
104
105
106
107
import {
DestinationPlugin,
PluginType,
TrackEventType,
ScreenEventType,
SegmentAPISettings,
UpdateType,
IdentifyEventType,
GroupEventType,
JsonMap,
AliasEventType,
SegmentError,
ErrorType,
} from '@segment/analytics-react-native';
import type { SegmentMixpanelSettings } from './types';
import { Mixpanel } from 'mixpanel-react-native';
import identify from './methods/identify';
import screen from './methods/screen';
import group from './methods/group';
import alias from './methods/alias';
import track from './methods/track';
export const EU_SERVER = 'https://api-eu.mixpanel.com';
export const US_SERVER = 'https://api.mixpanel.com';
export class MixpanelPlugin extends DestinationPlugin {
type = PluginType.destination;
key = 'Mixpanel';
trackScreens = false;
serverURL = US_SERVER;
private mixpanel: Mixpanel | undefined;
private settings: SegmentMixpanelSettings | undefined;
private isInitialized = () =>
this.mixpanel !== undefined && this.settings !== undefined;
update(settings: SegmentAPISettings, _: UpdateType) {
const mixpanelSettings = settings.integrations[
this.key
] as SegmentMixpanelSettings;
if (mixpanelSettings === undefined || this.mixpanel !== undefined) {
return;
}
if (mixpanelSettings.token.length === 0) {
return;
}
if (mixpanelSettings.enableEuropeanEndpoint === true) {
this.serverURL = EU_SERVER;
}
this.mixpanel = new Mixpanel(mixpanelSettings.token, false);
this.mixpanel.init(undefined, undefined, this.serverURL).catch((error) => {
this.analytics?.reportInternalError(
new SegmentError(
ErrorType.PluginError,
'Error initializing Mixpanel',
error
)
);
});
this.settings = mixpanelSettings;
}
identify(event: IdentifyEventType) {
if (this.isInitialized()) {
identify(event, this.mixpanel!, this.settings!);
}
return event;
}
track(event: TrackEventType) {
const eventName = event.event;
const properties = event.properties as JsonMap;
if (this.isInitialized()) {
track(eventName, properties, this.settings!, this.mixpanel!);
}
return event;
}
screen(event: ScreenEventType) {
if (this.isInitialized()) {
screen(event, this.mixpanel!, this.settings!);
}
return event;
}
group(event: GroupEventType) {
if (this.isInitialized()) {
group(event, this.mixpanel!, this.settings!);
}
return event;
}
async alias(event: AliasEventType) {
if (this.mixpanel !== undefined && this.analytics !== undefined) {
await alias(event, this.mixpanel, this.analytics);
}
return event;
}
flush(): void {
this.mixpanel?.flush();
}
reset(): void {
this.mixpanel?.reset();
}
}