aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/redux/analytics_middleware.ts
blob: 3dc5fe924e307947c5823d0c79f112d813a92b5c (plain) (blame)
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
import { Web3Wrapper } from '@0x/web3-wrapper';
import * as _ from 'lodash';
import { Middleware } from 'redux';

import { ETH_DECIMALS } from '../constants';
import { Account, AccountState, StandardSlidingPanelContent } from '../types';
import { analytics } from '../util/analytics';

import { Action, ActionTypes } from './actions';

import { State } from './reducer';

export const analyticsMiddleware: Middleware = store => next => middlewareAction => {
    const prevState = store.getState() as State;
    const prevAccount = prevState.providerState.account;

    const nextAction = next(middlewareAction) as Action;

    const curState = store.getState() as State;
    const curAccount = curState.providerState.account;

    switch (nextAction.type) {
        case ActionTypes.SET_ACCOUNT_STATE_READY:
            if (curAccount.state === AccountState.Ready) {
                const didJustTurnReady = prevAccount.state !== AccountState.Ready;
                const didJustUpdateAddress =
                    prevAccount.state === AccountState.Ready && prevAccount.address !== curAccount.address;
                const ethAddress = curAccount.address;
                if (didJustTurnReady) {
                    analytics.trackAccountReady(ethAddress);
                    analytics.addUserProperties({ lastKnownEthAddress: ethAddress });
                } else if (didJustUpdateAddress) {
                    analytics.trackAccountAddressChanged(ethAddress);
                    analytics.addUserProperties({ lastKnownEthAddress: ethAddress });
                }
            }
            break;
        case ActionTypes.SET_ACCOUNT_STATE_LOCKED:
            if (prevAccount.state !== AccountState.Locked && curAccount.state === AccountState.Locked) {
                // if we are moving from account not locked to account locked, track `Account - Locked`
                analytics.trackAccountLocked();
            }
            break;
        case ActionTypes.UPDATE_ACCOUNT_ETH_BALANCE:
            if (
                curAccount.state === AccountState.Ready &&
                curAccount.ethBalanceInWei &&
                !_.isEqual(curAccount, prevAccount)
            ) {
                const ethBalanceInUnitAmount = Web3Wrapper.toUnitAmount(
                    curAccount.ethBalanceInWei,
                    ETH_DECIMALS,
                ).toString();
                analytics.addUserProperties({ ethBalanceInUnitAmount });
            }
            break;
        case ActionTypes.UPDATE_SELECTED_ASSET:
            const selectedAsset = curState.selectedAsset;
            if (selectedAsset) {
                const assetName = selectedAsset.metaData.name;
                const assetData = selectedAsset.assetData;
                analytics.trackTokenSelectorChose({
                    assetName,
                    assetData,
                });
                analytics.addEventProperties({
                    selectedAssetName: assetName,
                    selectedAssetData: assetData,
                });
            }
            break;
        case ActionTypes.SET_AVAILABLE_ASSETS:
            const availableAssets = curState.availableAssets;
            if (availableAssets) {
                analytics.addEventProperties({
                    numberAvailableAssets: availableAssets.length,
                });
            }
            break;
        case ActionTypes.OPEN_STANDARD_SLIDING_PANEL:
            const openSlidingContent = curState.standardSlidingPanelSettings.content;
            if (openSlidingContent === StandardSlidingPanelContent.InstallWallet) {
                analytics.trackInstallWalletModalOpened();
            }
            break;
        case ActionTypes.CLOSE_STANDARD_SLIDING_PANEL:
            const closeSlidingContent = curState.standardSlidingPanelSettings.content;
            if (closeSlidingContent === StandardSlidingPanelContent.InstallWallet) {
                analytics.trackInstallWalletModalClosed();
            }
            break;
    }

    return nextAction;
};