forked from pagopa/io-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixpanel.ts
65 lines (58 loc) · 1.86 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
import { Mixpanel } from "mixpanel-react-native";
import { mixpanelToken, mixpanelUrl } from "./config";
import { getDeviceId } from "./utils/device";
import { GlobalState } from "./store/reducers/types";
import { updateMixpanelSuperProperties } from "./mixpanelConfig/superProperties";
import { updateMixpanelProfileProperties } from "./mixpanelConfig/profileProperties";
// eslint-disable-next-line functional/no-let
export let mixpanel: Mixpanel | undefined;
/**
* Initialize mixpanel at start
*/
export const initializeMixPanel = async (state: GlobalState) => {
if (mixpanel !== undefined) {
return;
}
const trackAutomaticEvents = true;
const privateInstance = new Mixpanel(mixpanelToken, trackAutomaticEvents);
await privateInstance.init(undefined, undefined, mixpanelUrl);
mixpanel = privateInstance;
// On app first open
// On profile page, when user opt-in
await setupMixpanel(mixpanel, state);
};
const setupMixpanel = async (mp: Mixpanel, state: GlobalState) => {
mp.optInTracking();
mp.setUseIpAddressForGeolocation(false);
await updateMixpanelSuperProperties(state);
await updateMixpanelProfileProperties(state);
};
export const identifyMixpanel = async () => {
// Identify the user using the device uniqueId
await mixpanel?.identify(getDeviceId());
};
export const resetMixpanel = () => {
// Reset mixpanel auto generated uniqueId
mixpanel?.reset();
};
export const terminateMixpanel = () => {
if (mixpanel) {
const mp = mixpanel;
mp.flush();
// Wait for the flush to complete
// (mainly) to let profile properties to update.
setTimeout(() => {
mp.optOutTracking();
}, 1000);
mixpanel = undefined;
}
};
/**
* Track an event with properties
* @param event
* @param properties
*/
export const mixpanelTrack = (
event: string,
properties?: Record<string, unknown>
) => mixpanel?.track(event, properties);