forked from pagopa/io-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixpanel.ts
77 lines (71 loc) · 2.28 KB
/
mixpanel.ts
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
import { Appearance } from "react-native";
import { MixpanelInstance } from "react-native-mixpanel";
import { mixpanelToken } from "./config";
import { isScreenReaderEnabled } from "./utils/accessibility";
import { getAppVersion } from "./utils/appVersion";
import { isAndroid, isIos } from "./utils/platform";
import { getDeviceId, getFontScale } from "./utils/device";
import { getBiometricsType } from "./utils/biometrics";
// eslint-disable-next-line
export let mixpanel: MixpanelInstance | undefined;
/**
* Initialize mixpanel at start
*/
export const initializeMixPanel = async () => {
if (mixpanel !== undefined) {
return;
}
const privateInstance = new MixpanelInstance(mixpanelToken);
await privateInstance.initialize();
mixpanel = privateInstance;
await setupMixpanel(mixpanel);
};
const setupMixpanel = async (mp: MixpanelInstance) => {
const screenReaderEnabled: boolean = await isScreenReaderEnabled();
await mp.optInTracking();
// on iOS it can be deactivate by invoking a SDK method
// on Android it can be done adding an extra config in AndroidManifest
// see https://help.mixpanel.com/hc/en-us/articles/115004494803-Disable-Geolocation-Collection
if (isIos) {
await mp.disableIpAddressGeolocalization();
}
const fontScale = await getFontScale();
const biometricTechnology = await getBiometricsType();
await mp.registerSuperProperties({
isScreenReaderEnabled: screenReaderEnabled,
fontScale,
appReadableVersion: getAppVersion(),
colorScheme: Appearance.getColorScheme(),
biometricTechnology
});
// Identify the user using the device uniqueId
await mp.identify(getDeviceId());
};
export const terminateMixpanel = async () => {
if (mixpanel) {
await mixpanel.flush();
await mixpanel.optOutTracking();
mixpanel = undefined;
}
return Promise.resolve();
};
export const setMixpanelPushNotificationToken = (token: string) => {
if (mixpanel) {
if (isIos) {
return mixpanel.addPushDeviceToken(token);
}
if (isAndroid) {
return mixpanel.setPushRegistrationId(token);
}
}
return Promise.resolve();
};
/**
* Track an event with properties
* @param event
* @param properties
*/
export const mixpanelTrack = (
event: string,
properties?: Record<string, unknown>
) => mixpanel?.track(event, properties);