Skip to content

Commit

Permalink
[Multichain] fix: Add analytics events [SW-165] (#4228)
Browse files Browse the repository at this point in the history
* fix: Adjust chainId inbetween events when creating multichain safe

* fix: Add missing events
  • Loading branch information
usame-algan authored Sep 24, 2024
1 parent 916318f commit 8e6b2e8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const NetworkMultiSelector = ({
))
}
renderOption={(props, chain, { selected }) => (
<li {...props}>
<li key={chain.chainId} {...props}>
<Checkbox size="small" checked={selected} />
<ChainIndicator chainId={chain.chainId} inline />
</li>
Expand Down
39 changes: 21 additions & 18 deletions src/components/common/NetworkSelector/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ChainIndicator from '@/components/common/ChainIndicator'
import Track from '@/components/common/Track'
import { useDarkMode } from '@/hooks/useDarkMode'
import { useAppSelector } from '@/store'
import { selectChains } from '@/store/chainsSlice'
Expand Down Expand Up @@ -26,7 +27,7 @@ import { useRouter } from 'next/router'
import css from './styles.module.css'
import { useChainId } from '@/hooks/useChainId'
import { type ReactElement, useCallback, useMemo, useState } from 'react'
import { trackEvent, OVERVIEW_EVENTS } from '@/services/analytics'
import { trackEvent, OVERVIEW_EVENTS, OVERVIEW_LABELS } from '@/services/analytics'

import { useAllSafesGrouped } from '@/components/welcome/MyAccounts/useAllSafesGrouped'
import useSafeAddress from '@/hooks/useSafeAddress'
Expand Down Expand Up @@ -79,23 +80,25 @@ const UndeployedNetworkMenuItem = ({
const isDisabled = !chain.available

return (
<MenuItem
value={chain.chainId}
sx={{ '&:hover': { backgroundColor: 'inherit' } }}
onClick={() => onSelect(chain)}
disabled={isDisabled}
>
<Box className={css.item}>
<ChainIndicator responsive={isSelected} chainId={chain.chainId} inline />
{isDisabled ? (
<Typography variant="caption" component="span" className={css.comingSoon}>
Not available
</Typography>
) : (
<PlusIcon className={css.plusIcon} />
)}
</Box>
</MenuItem>
<Track {...OVERVIEW_EVENTS.ADD_NEW_NETWORK} label={OVERVIEW_LABELS.top_bar}>
<MenuItem
value={chain.chainId}
sx={{ '&:hover': { backgroundColor: 'inherit' } }}
onClick={() => onSelect(chain)}
disabled={isDisabled}
>
<Box className={css.item}>
<ChainIndicator responsive={isSelected} chainId={chain.chainId} inline />
{isDisabled ? (
<Typography variant="caption" component="span" className={css.comingSoon}>
Not available
</Typography>
) : (
<PlusIcon className={css.plusIcon} />
)}
</Box>
</MenuItem>
</Track>
)
}

Expand Down
6 changes: 5 additions & 1 deletion src/components/new-safe/create/steps/ReviewStep/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { useLeastRemainingRelays } from '@/hooks/useRemainingRelays'
import useWalletCanPay from '@/hooks/useWalletCanPay'
import useWallet from '@/hooks/wallets/useWallet'
import { CREATE_SAFE_CATEGORY, CREATE_SAFE_EVENTS, OVERVIEW_EVENTS, trackEvent } from '@/services/analytics'
import { gtmSetSafeAddress } from '@/services/analytics/gtm'
import { gtmSetChainId, gtmSetSafeAddress } from '@/services/analytics/gtm'
import { asError } from '@/services/exceptions/utils'
import { useAppDispatch, useAppSelector } from '@/store'
import { FEATURES, hasFeature } from '@/utils/chains'
Expand Down Expand Up @@ -215,6 +215,8 @@ const ReviewStep = ({ data, onSubmit, onBack, setStep }: StepRenderProps<NewSafe
createSafe(network, replayedSafeWithNonce, safeAddress)
}

gtmSetChainId(chain.chainId)

if (isCounterfactualEnabled && payMethod === PayMethod.PayLater) {
router?.push({
pathname: AppRoutes.home,
Expand All @@ -237,6 +239,8 @@ const ReviewStep = ({ data, onSubmit, onBack, setStep }: StepRenderProps<NewSafe
const createSafe = async (chain: ChainInfo, props: ReplayedSafeProps, safeAddress: string) => {
if (!wallet) return

gtmSetChainId(chain.chainId)

try {
if (isCounterfactualEnabled && payMethod === PayMethod.PayLater) {
gtmSetSafeAddress(safeAddress)
Expand Down
10 changes: 7 additions & 3 deletions src/components/welcome/MyAccounts/AddNetworkButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Track from '@/components/common/Track'
import { CreateSafeOnNewChain } from '@/features/multichain/components/CreateSafeOnNewChain'
import { OVERVIEW_EVENTS, OVERVIEW_LABELS } from '@/services/analytics'
import { Button } from '@mui/material'
import { useState } from 'react'
import PlusIcon from '@/public/images/common/plus.svg'
Expand All @@ -16,9 +18,11 @@ export const AddNetworkButton = ({

return (
<>
<Button variant="text" fullWidth onClick={() => setOpen(true)}>
<PlusIcon /> Add another network
</Button>
<Track {...OVERVIEW_EVENTS.ADD_NEW_NETWORK} label={OVERVIEW_LABELS.sidebar}>
<Button variant="text" fullWidth onClick={() => setOpen(true)}>
<PlusIcon /> Add another network
</Button>
</Track>

{open && (
<CreateSafeOnNewChain
Expand Down
32 changes: 14 additions & 18 deletions src/features/multichain/components/CreateSafeOnNewChain/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import ModalDialog from '@/components/common/ModalDialog'
import NameInput from '@/components/common/NameInput'
import NetworkInput from '@/components/common/NetworkInput'
import ErrorMessage from '@/components/tx/ErrorMessage'
import {
Box,
Button,
CircularProgress,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
Divider,
Stack,
Typography,
} from '@mui/material'
import { OVERVIEW_EVENTS, trackEvent } from '@/services/analytics'
import { Box, Button, CircularProgress, DialogActions, DialogContent, Divider, Stack, Typography } from '@mui/material'
import { FormProvider, useForm } from 'react-hook-form'
import { useSafeCreationData } from '../../hooks/useSafeCreationData'
import { replayCounterfactualSafeDeployment } from '@/features/counterfactual/utils'
Expand Down Expand Up @@ -60,7 +51,7 @@ const ReplaySafeDialog = ({
mode: 'all',
defaultValues: {
name: currentName,
chainId: chain?.chainId,
chainId: chain?.chainId || '',
},
})
const { handleSubmit, formState } = formMethods
Expand All @@ -74,6 +65,11 @@ const ReplaySafeDialog = ({
// Load some data
const [safeCreationData, safeCreationDataError, safeCreationDataLoading] = safeCreationResult

const onCancel = () => {
trackEvent({ ...OVERVIEW_EVENTS.CANCEL_ADD_NEW_NETWORK })
onClose()
}

const onFormSubmit = handleSubmit(async (data) => {
setIsSubmitting(true)

Expand All @@ -97,6 +93,8 @@ const ReplaySafeDialog = ({
return
}

trackEvent({ ...OVERVIEW_EVENTS.SUBMIT_ADD_NEW_NETWORK, label: selectedChain.chainName })

// 2. Replay Safe creation and add it to the counterfactual Safes
replayCounterfactualSafeDeployment(selectedChain.chainId, safeAddress, safeCreationData, data.name, dispatch)

Expand Down Expand Up @@ -126,10 +124,8 @@ const ReplaySafeDialog = ({
!chain && safeCreationData && replayableChains && replayableChains.filter((chain) => chain.available).length === 0

return (
<Dialog open={open} onClose={onClose} onClick={(e) => e.stopPropagation()}>
<ModalDialog open={open} onClose={onClose} dialogTitle="Add another network" hideChainIndicator>
<form onSubmit={onFormSubmit} id="recreate-safe">
<DialogTitle fontWeight={700}>Add another network</DialogTitle>
<Divider />
<DialogContent>
<FormProvider {...formMethods}>
<Stack spacing={2}>
Expand Down Expand Up @@ -187,7 +183,7 @@ const ReplaySafeDialog = ({
</Box>
) : (
<>
<Button variant="outlined" onClick={onClose}>
<Button variant="outlined" onClick={onCancel}>
Cancel
</Button>
<Button type="submit" variant="contained" disabled={submitDisabled}>
Expand All @@ -197,7 +193,7 @@ const ReplaySafeDialog = ({
)}
</DialogActions>
</form>
</Dialog>
</ModalDialog>
)
}

Expand Down
8 changes: 8 additions & 0 deletions src/services/analytics/events/overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export const OVERVIEW_EVENTS = {
action: 'Add new network',
category: OVERVIEW_CATEGORY,
},
SUBMIT_ADD_NEW_NETWORK: {
action: 'Submit add new network',
category: OVERVIEW_CATEGORY,
},
CANCEL_ADD_NEW_NETWORK: {
action: 'Cancel add new network',
category: OVERVIEW_CATEGORY,
},
DELETED_FROM_WATCHLIST: {
action: 'Deleted from watchlist',
category: OVERVIEW_CATEGORY,
Expand Down

0 comments on commit 8e6b2e8

Please sign in to comment.