diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-21 05:26:00 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-21 05:26:00 +0800 |
commit | eca63b1a5815c61901453837f9c2b737fa82eb20 (patch) | |
tree | 7763d95f1922f6c16848a8b506384e6e9992260b /packages/instant/src/redux/analytics_middleware.ts | |
parent | 2bda6dd719d748a4bfa5ff8e23e97924e0258af1 (diff) | |
parent | ba41fc9275bebe17577f87d72f54b7e7dc420acc (diff) | |
download | dexon-sol-tools-eca63b1a5815c61901453837f9c2b737fa82eb20.tar dexon-sol-tools-eca63b1a5815c61901453837f9c2b737fa82eb20.tar.gz dexon-sol-tools-eca63b1a5815c61901453837f9c2b737fa82eb20.tar.bz2 dexon-sol-tools-eca63b1a5815c61901453837f9c2b737fa82eb20.tar.lz dexon-sol-tools-eca63b1a5815c61901453837f9c2b737fa82eb20.tar.xz dexon-sol-tools-eca63b1a5815c61901453837f9c2b737fa82eb20.tar.zst dexon-sol-tools-eca63b1a5815c61901453837f9c2b737fa82eb20.zip |
Merge branch 'development' into feature/header-tweaks
Diffstat (limited to 'packages/instant/src/redux/analytics_middleware.ts')
-rw-r--r-- | packages/instant/src/redux/analytics_middleware.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/packages/instant/src/redux/analytics_middleware.ts b/packages/instant/src/redux/analytics_middleware.ts new file mode 100644 index 000000000..f971dbd33 --- /dev/null +++ b/packages/instant/src/redux/analytics_middleware.ts @@ -0,0 +1,59 @@ +import { Web3Wrapper } from '@0x/web3-wrapper'; +import * as _ from 'lodash'; +import { Middleware } from 'redux'; + +import { ETH_DECIMALS } from '../constants'; +import { Account, AccountState } from '../types'; +import { analytics } from '../util/analytics'; + +import { Action, ActionTypes } from './actions'; + +import { State } from './reducer'; + +const shouldTriggerWalletReady = (prevAccount: Account, curAccount: Account): boolean => { + const didJustTurnReady = curAccount.state === AccountState.Ready && prevAccount.state !== AccountState.Ready; + if (didJustTurnReady) { + return true; + } + + if (curAccount.state === AccountState.Ready && prevAccount.state === AccountState.Ready) { + // Account was ready, and is now ready again, but address has changed + return curAccount.address !== prevAccount.address; + } + + return false; +}; + +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 && shouldTriggerWalletReady(prevAccount, curAccount)) { + const ethAddress = curAccount.address; + analytics.addUserProperties({ ethAddress }); + analytics.trackWalletReady(); + } + 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 }); + } + } + + return nextAction; +}; |