Skip to content

Commit

Permalink
Merge pull request #127 from dappforce/creator-staking-analytics
Browse files Browse the repository at this point in the history
Add analytics for creator staking page
  • Loading branch information
olehmell authored Dec 19, 2023
2 parents f5fac56 + 3968a1c commit cbc16b1
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/components/creatorsStaking/Banner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { NextEraStartDate } from '../utils/NextEraStartDate'
import { DashboardCard, TotalStakedBalance } from './utils'
import { useCalculateApr } from './calculateApr'
import { useMemo } from 'react'
import { useSendEvent } from '@/components/providers/AnalyticContext'

const skeletonClassName = 'h-[20px] mb-1'

Expand Down Expand Up @@ -76,6 +77,7 @@ const StatsCards = () => {

const Banner = () => {
useFetchGeneralEraInfo()
const sendEvent = useSendEvent()

return (
<div className='md:!mx-4 mx-0'>
Expand All @@ -100,6 +102,7 @@ const Banner = () => {
size='sm'
target='_blank'
href='https://docs.subsocial.network/docs/basics/creator-staking/'
onClick={() => sendEvent('cs_how_it_works_clicked')}
>
<span className='flex gap-2 items-center py-1'>
<AiOutlineQuestionCircle size={20} /> How does it work?
Expand Down
7 changes: 6 additions & 1 deletion src/components/creatorsStaking/Creators/AccountPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CreatorPreview } from '../utils/CreatorPreview'
import { useResponsiveSize } from 'src/components/responsive'
import { Tooltip } from 'antd'
import { MdArrowOutward } from 'react-icons/md'
import { useSendEvent } from '@/components/providers/AnalyticContext'

type AccountPreviewProps = {
space?: CreatorSpace
Expand All @@ -14,6 +15,7 @@ type AccountPreviewProps = {

const AccountPreview = ({ space, spaceId }: AccountPreviewProps) => {
const { isMobile } = useResponsiveSize()
const sendEvent = useSendEvent()

const { name, ownedByAccount, image, email, links, postsCount } = space || {}

Expand All @@ -25,7 +27,10 @@ const AccountPreview = ({ space, spaceId }: AccountPreviewProps) => {
<Tooltip title={'See their posts on Polkaverse'}>
<a
href={`https://polkaverse.com/${spaceId}`}
onClick={(e) => e.stopPropagation()}
onClick={(e) => {
e.stopPropagation()
sendEvent('cs_polkaverse_link_clicked', { value: spaceId })
}}
target='_blank'
rel='noreferrer'
className={clsx(
Expand Down
11 changes: 10 additions & 1 deletion src/components/creatorsStaking/Creators/CreatorCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { MdArrowOutward } from 'react-icons/md'
// import { useEraStakesById } from '@/rtk/features/creatorStaking/eraStake/eraStakeHooks'
import MoveStakeModal from './modals/MoveStakeModal'
import { useRouter } from 'next/router'
import { useSendEvent } from '@/components/providers/AnalyticContext'

type CreatorNameProps = {
name?: string
Expand Down Expand Up @@ -122,6 +123,7 @@ const CreatorCard = ({ spaceId }: CreatorCardProps) => {
const { setOpen, setSpaceId, setMetadata } = useChatContext()
const cardRef = useRef<HTMLDivElement>(null)
const router = useRouter()
const sendEvent = useSendEvent()

const { space, loading: spaceLoading } = creatorSpaceEntity || {}
// const { info: eraStakeInfo, loading: eraStakeLoading } = eraStake || {}
Expand Down Expand Up @@ -196,14 +198,19 @@ const CreatorCard = ({ spaceId }: CreatorCardProps) => {
body: about || '',
image: image || '',
})

sendEvent('cs_creator_chat_clicked', { value: spaceId })
}
}

const postsLink = (
<Tooltip title={'See their posts on Polkaverse'}>
<a
href={`https://polkaverse.com/${spaceId}`}
onClick={(e) => e.stopPropagation()}
onClick={(e) => {
e.stopPropagation()
sendEvent('cs_polkaverse_link_clicked', { value: spaceId })
}}
target='_blank'
rel='noreferrer'
className={clsx(
Expand Down Expand Up @@ -252,6 +259,7 @@ const CreatorCard = ({ spaceId }: CreatorCardProps) => {
)

setOpenAboutModal(true)
sendEvent('cs_about_modal_opened', { value: spaceId })
}}
>
<div className='w-full flex justify-between gap-2'>
Expand Down Expand Up @@ -333,6 +341,7 @@ const CreatorCard = ({ spaceId }: CreatorCardProps) => {
open={openStakeModal}
closeModal={() => setOpenStakeModal(false)}
spaceId={spaceId}
eventSource='creator-card'
modalVariant={modalVariant}
amount={amount}
setAmount={setAmount}
Expand Down
21 changes: 9 additions & 12 deletions src/components/creatorsStaking/Creators/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { toGenericAccountId } from 'src/rtk/app/util'
import { betaVersionAgreementStorageName } from './modals/StakeModal'
import store from 'store'
import DefaultAboutModal from './modals/DefaultAboutModal'
import { useSendEvent } from '@/components/providers/AnalyticContext'

// const DEFAULT_PAGE_SIZE = 9

Expand Down Expand Up @@ -52,12 +53,7 @@ const CreatorsCards = ({
)

const creatorsCards = ids.map((spaceId, i) => (
<CreatorCard
key={i}
spaceId={spaceId}
era={era}
{...modalProps}
/>
<CreatorCard key={i} spaceId={spaceId} era={era} {...modalProps} />
))

// const start = (page - 1) * DEFAULT_PAGE_SIZE
Expand Down Expand Up @@ -98,6 +94,7 @@ const CreatorsSectionInner = ({
useModalContext()
const creatorsList = useCreatorsList()
const isMulti = useIsMulti()
const sendEvent = useSendEvent()

const isCreator = useMemo(
() =>
Expand All @@ -122,11 +119,7 @@ const CreatorsSectionInner = ({
id: 'all-creators',
text: 'All Creators',
content: () => (
<CreatorsCards
spaceIds={spaceIds}
era={era}
sortBy={sortBy}
/>
<CreatorsCards spaceIds={spaceIds} era={era} sortBy={sortBy} />
),
},
{
Expand All @@ -151,6 +144,7 @@ const CreatorsSectionInner = ({
href='https://forms.gle/t3YfTtGnbVdwm7kY8'
target='_blank'
size={'sm'}
onClick={() => sendEvent('cs_apply_as_creator_clicked')}
>
Apply to join
</Button>
Expand All @@ -167,7 +161,10 @@ const CreatorsSectionInner = ({
tabsRightElement={
<SortByDropDown
sortBy={sortBy}
changeSortBy={changeSortBy}
changeSortBy={(sortBy) => {
sendEvent('cs_filter_changed', { type: sortBy })
changeSortBy(sortBy)
}}
panelSize='xs'
panelClassName='!w-32'
itemClassName='my-[2px]'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const AboutModal = ({
closeModal={() => setOpenStakeModal(false)}
spaceId={spaceId}
modalVariant={modalVariant}
eventSource='about-modal'
amount={amount}
setAmount={setAmount}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useBackerInfo } from '@/rtk/features/creatorStaking/backerInfo/backerIn
import { useMyAddress } from '@/components/providers/MyExtensionAccountsContext'
import { useModalContext } from '../../contexts/ModalContext'
import { useCreatorsList } from '@/rtk/features/creatorStaking/creatorsList/creatorsListHooks'
import { useSendEvent } from '@/components/providers/AnalyticContext'
import useRedirectToCreatorsPage from '../../hooks/useRedirectToCreatorsPage'

type DefaultAboutModalProps = {
Expand All @@ -15,6 +16,7 @@ const DefaultAboutModal = ({ defaultSpaceId }: DefaultAboutModalProps) => {
const { amount, setAmount } = useModalContext()
const [ openDefaultAboutModal, setOpenDefaultAboutModal ] = useState(false)
const creatorsList = useCreatorsList()
const sendEvent = useSendEvent()
const redirectToCreatorsPage = useRedirectToCreatorsPage()

const creatorsSpaceIds = creatorsList?.map(({ id }) => id)
Expand All @@ -33,6 +35,7 @@ const DefaultAboutModal = ({ defaultSpaceId }: DefaultAboutModalProps) => {
useEffect(() => {
if (defaultSpaceId && isCreator) {
setOpenDefaultAboutModal(true)
sendEvent('cs_about_modal_opened', { value: defaultSpaceId })
}

if (defaultSpaceId && !isCreator) {
Expand Down
3 changes: 3 additions & 0 deletions src/components/creatorsStaking/Creators/modals/StakeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type StakeModalProps = {
spaceId: string
modalVariant: StakingModalVariant
amount?: string
eventSource?: string
setAmount: (amount: string) => void
}

Expand All @@ -156,6 +157,7 @@ const StakingModal = ({
modalVariant,
setAmount,
amount,
eventSource
}: StakeModalProps) => {
const creatorSpaceEntity = useCreatorSpaceById(spaceId)
const [ inputError, setInputError ] = useState<string | undefined>(undefined)
Expand Down Expand Up @@ -235,6 +237,7 @@ const StakingModal = ({
decimal={decimal}
spaceId={spaceId}
label={modalButton}
eventSource={eventSource}
tokenSymbol={tokenSymbol}
closeModal={closeModal}
modalVariant={modalVariant}
Expand Down
34 changes: 34 additions & 0 deletions src/components/creatorsStaking/Creators/modals/TxButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import {
import { showParsedErrorMessage } from 'src/components/utils'
import { useModalContext } from '../../contexts/ModalContext'
import store from 'store'
import { useSendEvent } from '@/components/providers/AnalyticContext'
import getAmountRange from '../../utils/getAmountRangeForAnalytics'
import { useGetDecimalsAndSymbolByNetwork } from '@/components/utils/useGetDecimalsAndSymbolByNetwork'

export type CommonTxButtonProps = {
amount?: string
Expand All @@ -38,6 +41,8 @@ export type CommonTxButtonProps = {
modalVariant?: StakingModalVariant
inputError?: string
disabled?: boolean
eventSource?: string
onClick?: () => void
}

type StakingTxButtonProps = CommonTxButtonProps & {
Expand All @@ -52,6 +57,7 @@ function StakingTxButton ({
disabled,
tx,
closeModal,
onClick,
modalVariant,
inputError,
}: StakingTxButtonProps) {
Expand Down Expand Up @@ -110,6 +116,7 @@ function StakingTxButton ({
tx={tx}
disabled={disableButton}
component={Component}
onClick={onClick}
params={buildParams}
onFailed={showParsedErrorMessage}
onSuccess={onSuccess}
Expand All @@ -119,6 +126,8 @@ function StakingTxButton ({

export function StakeOrIncreaseTxButton (props: CommonTxButtonProps) {
const myAddress = useMyAddress()
const sendEvent = useSendEvent()
const { decimal } = useGetDecimalsAndSymbolByNetwork('subsocial')

const balancesByCurrency = useBalancesByNetwork({
address: myAddress,
Expand All @@ -133,6 +142,12 @@ export function StakeOrIncreaseTxButton (props: CommonTxButtonProps) {
return (
<StakingTxButton
{...props}
onClick={() =>
sendEvent('cs_stake_increase', {
amountRange: getAmountRange(decimal, props.amount),
eventSource: props.eventSource,
})
}
disabled={availableBalance.isZero() || props.disabled}
tx='creatorStaking.stake'
/>
Expand All @@ -143,6 +158,8 @@ export function UnstakeTxButton (props: CommonTxButtonProps) {
const myAddress = useMyAddress()
const backerInfo = useBackerInfo(props.spaceId, myAddress)
const { info } = backerInfo || {}
const sendEvent = useSendEvent()
const { decimal } = useGetDecimalsAndSymbolByNetwork('subsocial')

const { totalStaked } = info || {}

Expand All @@ -151,6 +168,12 @@ export function UnstakeTxButton (props: CommonTxButtonProps) {
{...props}
disabled={!totalStaked || new BN(totalStaked).isZero() || props.disabled}
tx='creatorStaking.unstake'
onClick={() => {
sendEvent('cs_stake_increase', {
amountRange: getAmountRange(decimal, props.amount),
eventSource: props.eventSource,
})
}}
/>
)
}
Expand All @@ -163,6 +186,7 @@ type MoveStakeTxButtonProps = {
inputError?: string
disabled?: boolean
closeModal: () => void
eventSource?: string
}

export function MoveStakeTxButton ({
Expand All @@ -173,10 +197,12 @@ export function MoveStakeTxButton ({
inputError,
disabled,
closeModal,
eventSource,
}: MoveStakeTxButtonProps) {
const myAddress = useMyAddress()
const dispatch = useAppDispatch()
const eraInfo = useGeneralEraInfo()
const sendEvent = useSendEvent()

const onSuccess = () => {
if (!spaceIdTo) return
Expand Down Expand Up @@ -225,6 +251,14 @@ export function MoveStakeTxButton ({
disabled={disableButton}
component={Component}
params={buildParams}
onClick={() =>
sendEvent('cs_stake_increase', {
from: spaceIdFrom,
to: spaceIdTo,
amountRange: getAmountRange(decimal, amount),
eventSource: eventSource,
})
}
onFailed={showParsedErrorMessage}
onSuccess={onSuccess}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { fetchBackerInfo } from 'src/rtk/features/creatorStaking/backerInfo/back
import { fetchGeneralEraInfo, useGeneralEraInfo } from 'src/rtk/features/creatorStaking/generalEraInfo/generalEraInfoHooks'
import { fetchEraStakes } from 'src/rtk/features/creatorStaking/eraStake/eraStakeHooks'
import { fetchBackerLedger } from 'src/rtk/features/creatorStaking/backerLedger/backerLedgerHooks'
import { useSendEvent } from '@/components/providers/AnalyticContext'

type ClaimRewardsTxButtonProps = {
rewardsSpaceIds: string[]
Expand All @@ -29,6 +30,7 @@ const ClaimRewardsTxButton = ({
const myAddress = useMyAddress()
const eraInfo = useGeneralEraInfo()
const backerRewards = useBackerRewards(myAddress)
const sendEvent = useSendEvent()

const { loading } = backerRewards || {}

Expand Down Expand Up @@ -97,6 +99,7 @@ const ClaimRewardsTxButton = ({
accountId={myAddress}
tx={'utility.batch'}
disabled={disableButton}
onClick={() => sendEvent('cs_claim', { restake })}
component={Component}
params={buildParams}
onFailed={showParsedErrorMessage}
Expand Down
3 changes: 3 additions & 0 deletions src/components/creatorsStaking/MyStaking/WithdrawTxButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useGeneralEraInfo } from 'src/rtk/features/creatorStaking/generalEraInf
import { useMyAddress } from 'src/components/providers/MyExtensionAccountsContext'
import BN from 'bignumber.js'
import { showParsedErrorMessage } from 'src/components/utils'
import { useSendEvent } from '@/components/providers/AnalyticContext'

type WithdrawTxButtonProps = {
switchToFirstTab: () => void
Expand All @@ -22,6 +23,7 @@ const WithdrawTxButton = ({ switchToFirstTab }: WithdrawTxButtonProps) => {
const backerLedger = useBackerLedger(myAddress)
const eraInfo = useGeneralEraInfo()
const dispatch = useAppDispatch()
const sendEvent = useSendEvent()

const { ledger, loading } = backerLedger || {}
const { currentEra } = eraInfo?.info || {}
Expand Down Expand Up @@ -73,6 +75,7 @@ const WithdrawTxButton = ({ switchToFirstTab }: WithdrawTxButtonProps) => {
tx={'creatorStaking.withdrawUnstaked'}
disabled={!isSomeAvailable}
component={Component}
onClick={() => sendEvent('cs_withdraw')}
onFailed={showParsedErrorMessage}
onSuccess={onSuccess}
/>
Expand Down

0 comments on commit cbc16b1

Please sign in to comment.