Skip to content

Commit

Permalink
feat: amplitude logs is_reconnect (#4214)
Browse files Browse the repository at this point in the history
* modified redux state to track wallet connections to properly log reconnects

* linted and removed console.log

* fixes for lynn's comments + documenting
  • Loading branch information
cartcrom committed Aug 2, 2022
1 parent 134879e commit f918b34
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/components/AccountDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Context, useCallback, useContext } from 'react'
import { ExternalLink as LinkIcon } from 'react-feather'
import { useAppDispatch } from 'state/hooks'
import { updateSelectedWallet } from 'state/user/reducer'
import { removeConnectedWallet } from 'state/wallets/reducer'
import { DefaultTheme } from 'styled-components/macro'
import styled, { ThemeContext } from 'styled-components/macro'
import { isMobile } from 'utils/userAgent'
Expand Down Expand Up @@ -245,13 +246,15 @@ export default function AccountDetails({
<WalletAction
style={{ fontSize: '.825rem', fontWeight: 400, marginRight: '8px' }}
onClick={() => {
const walletType = getConnectionName(getConnection(connector).type, getIsMetaMask())
if (connector.deactivate) {
connector.deactivate()
} else {
connector.resetState()
}

dispatch(updateSelectedWallet({ wallet: undefined }))
dispatch(removeConnectedWallet({ account, walletType }))
openOptions()
}}
>
Expand Down
23 changes: 19 additions & 4 deletions src/components/WalletModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ArrowLeft } from 'react-feather'
import { updateConnectionError } from 'state/connection/reducer'
import { useAppDispatch, useAppSelector } from 'state/hooks'
import { updateSelectedWallet } from 'state/user/reducer'
import { useConnectedWallets } from 'state/wallets/hooks'
import styled from 'styled-components/macro'
import { isMobile } from 'utils/userAgent'

Expand Down Expand Up @@ -113,13 +114,18 @@ const WALLET_VIEWS = {
PENDING: 'pending',
}

const sendAnalyticsEventAndUserInfo = (account: string, walletType: string, chainId: number | undefined) => {
const sendAnalyticsEventAndUserInfo = (
account: string,
walletType: string,
chainId: number | undefined,
isReconnect: boolean
) => {
const currentDate = new Date().toISOString()
sendAnalyticsEvent(EventName.WALLET_CONNECT_TXN_COMPLETED, {
result: WALLET_CONNECTION_RESULT.SUCCEEDED,
wallet_address: account,
wallet_type: walletType,
// TODO(lynnshaoyu): Send correct is_reconnect value after modifying user state.
is_reconnect: isReconnect,
})
user.set(CUSTOM_USER_PROPERTIES.WALLET_ADDRESS, account)
user.set(CUSTOM_USER_PROPERTIES.WALLET_TYPE, walletType)
Expand All @@ -140,6 +146,7 @@ export default function WalletModal({
}) {
const dispatch = useAppDispatch()
const { connector, account, chainId } = useWeb3React()
const [connectedWallets, updateConnectedWallets] = useConnectedWallets()

const [walletView, setWalletView] = useState(WALLET_VIEWS.ACCOUNT)
const [lastActiveWalletAddress, setLastActiveWalletAddress] = useState<string | undefined>(account)
Expand Down Expand Up @@ -173,10 +180,18 @@ export default function WalletModal({
useEffect(() => {
if (account && account !== lastActiveWalletAddress) {
const walletType = getConnectionName(getConnection(connector).type, getIsMetaMask())
sendAnalyticsEventAndUserInfo(account, walletType, chainId)

if (
connectedWallets.filter((wallet) => wallet.account === account && wallet.walletType === walletType).length > 0
) {
sendAnalyticsEventAndUserInfo(account, walletType, chainId, true)
} else {
sendAnalyticsEventAndUserInfo(account, walletType, chainId, false)
updateConnectedWallets({ account, walletType })
}
}
setLastActiveWalletAddress(account)
}, [lastActiveWalletAddress, account, connector, chainId])
}, [connectedWallets, updateConnectedWallets, lastActiveWalletAddress, account, connector, chainId])

const tryActivation = useCallback(
async (connector: Connector) => {
Expand Down
2 changes: 2 additions & 0 deletions src/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { routingApi } from './routing/slice'
import swap from './swap/reducer'
import transactions from './transactions/reducer'
import user from './user/reducer'
import wallets from './wallets/reducer'

const PERSISTED_KEYS: string[] = ['user', 'transactions', 'lists']

Expand All @@ -26,6 +27,7 @@ const store = configureStore({
user,
connection,
transactions,
wallets,
swap,
mint,
mintV3,
Expand Down
17 changes: 17 additions & 0 deletions src/state/wallets/hooks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useCallback } from 'react'
import { useAppDispatch, useAppSelector } from 'state/hooks'

import { addConnectedWallet } from './reducer'
import { Wallet } from './types'

export function useConnectedWallets(): [Wallet[], (wallet: Wallet) => void] {
const dispatch = useAppDispatch()
const connectedWallets = useAppSelector((state) => state.wallets.connectedWallets)
const addWallet = useCallback(
(wallet: Wallet) => {
dispatch(addConnectedWallet(wallet))
},
[dispatch]
)
return [connectedWallets, addWallet]
}
30 changes: 30 additions & 0 deletions src/state/wallets/reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createSlice } from '@reduxjs/toolkit'
import { shallowEqual } from 'react-redux'

import { Wallet } from './types'

/* Used to track wallets that have been connected by the user in current session, and remove them when deliberately disconnected.
Used to compute is_reconnect event property for analytics */
export interface WalletState {
connectedWallets: Wallet[]
}

export const initialState: WalletState = {
connectedWallets: [],
}

const walletsSlice = createSlice({
name: 'wallets',
initialState,
reducers: {
addConnectedWallet(state, { payload }) {
state.connectedWallets = state.connectedWallets.concat(payload)
},
removeConnectedWallet(state, { payload }) {
state.connectedWallets = state.connectedWallets.filter((wallet) => !shallowEqual(wallet, payload))
},
},
})

export const { addConnectedWallet, removeConnectedWallet } = walletsSlice.actions
export default walletsSlice.reducer
4 changes: 4 additions & 0 deletions src/state/wallets/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Wallet {
walletType: string
account: string
}

1 comment on commit f918b34

@vercel
Copy link

@vercel vercel bot commented on f918b34 Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

interface – ./

interface-uniswap.vercel.app
interface-git-main-uniswap.vercel.app

Please sign in to comment.