-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.ts
64 lines (54 loc) · 1.94 KB
/
track.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
import memoizeOne from "memoize-one";
import { AmplitudeClient } from "lib/amplitude";
import { storage } from "lib/ext/storage";
import { Setting, AnalyticsState } from "core/common/settings";
export enum TEvent {
SetupWallet = "WALLET_SETUPED",
SetupNexWallet = "NEX_WALLET_SETUPED",
ProfileCreation = "PROFILE_CREATED",
ProfileUpdate = "PROFILE_UPDATED",
NetworkChange = "NETWORK_CHANGED",
NetworkCreation = "NETWORK_CREATED",
NetworkEdit = "NETWORK_EDITED",
Contact = "CONTACT_CREATED",
DappConnect = "DAPP_CONNECT_OPENED",
DappSigning = "DAPP_SIGNING_OPENED",
DappTransaction = "DAPP_TRANSACTION_OPENED",
SwapNavigated = "SWAP_NAVIGATED",
FaucetNavigated = "FAUCET_NAVIGATED",
SwapStarted = "SWAP_STARTED",
SwapFinished = "SWAP_FINISHED",
BuyNavigated = "BUY_NAVIGATED",
BuyStarted = "BUY_STARTED",
BuyFinished = "BUY_FINISHED",
TokenTransferCreated = "TOKEN_TRANSFER_CREATED",
NftTransferCreated = "NFT_TRANSFER_CREATED",
ReceiveModalOpened = "RECEIVE_MODAL_OPENED",
SidebarNavigated = "SIDEBAR_NAVIGATED",
Web3SharedModeToggled = "WEB3_SHARED_MODE_TOGGLED",
Web3ManualConnected = "WEB3_MANUAL_CONNECTED",
}
export const trackEvent = async (
...args: Parameters<AmplitudeClient["track"]>
): Promise<any> => {
const amplitued = await getAmplitude();
return amplitued?.track(...args).promise.then((result) => {
console.info("Tracked:", ...args);
return result;
});
};
export const isTrackingEnabled = () => getAmplitude().then(Boolean);
const getAmplitude = memoizeOne(async () => {
const apiKey = process.env.NEX_WALLET_ANALYTICS_API_KEY;
const state = await storage.fetchForce<AnalyticsState>(Setting.Analytics);
if (!apiKey || !state?.enabled) return null;
try {
const client = new AmplitudeClient();
await client.init(apiKey, state.userId);
return client;
} catch (err) {
console.error(err);
return null;
}
});
storage.subscribe(Setting.Analytics, () => getAmplitude.clear());