Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
refactor: move tracking fn to utils, prevent tracking system bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitK-deriv committed Dec 28, 2023
1 parent ec99933 commit ed623a0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 51 deletions.
5 changes: 4 additions & 1 deletion src/pages/signup-affiliates/components/_signup-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import styled from 'styled-components'
import affiliate_validation from '../validations/_affilaite_validation'
import AffiliateInput from '../utils/_affiliate-input'
import { SignUpFormProps } from '../_types'
import { trackEvent } from '../utils/_utils'
import { localize, Localize } from 'components/localization'
import { Container } from 'components/containers'
import { Button } from 'components/form'
Expand Down Expand Up @@ -88,6 +89,7 @@ export const SignUpWrapper = styled(Container)`
padding: 0;
}
`
const email = localize('_t_Email_t_')
let language = getLanguage()
language = language !== 'en' ? '/' + language : ''

Expand All @@ -97,7 +99,6 @@ const AffiliateSignupForm = ({
setShowWizard,
}: SignUpFormProps) => {
const [email_error_msg, setEmailErrorMsg] = useState('')
const email = localize('_t_Email_t_')

const handleStateChange = useCallback(
({ e, field }: { e?: React.ChangeEvent<HTMLInputElement>; field: string }) => {
Expand Down Expand Up @@ -172,6 +173,8 @@ const AffiliateSignupForm = ({
secondary
onClick={() => {
window.scrollTo(0, 0)
console.log(affiliate_account.email)
trackEvent({ action: 'open_wizard', email: affiliate_account.email })
setShowWizard(true)
}}
disabled={!(affiliate_account.email && !email_error_msg)}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/signup-affiliates/components/_signup-status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from 'styled-components'
import affiliate_validation from '../validations/_affilaite_validation'
import AffiliateInput from '../utils/_affiliate-input'
import { SignUpStatusProps } from '../_types'
import trackEvent from '../utils/_tracking'
import { trackEvent } from '../utils/_utils'
import Image from 'features/components/atoms/image'
import { localize, Localize } from 'components/localization'
import { useIsRtl } from 'components/hooks/use-isrtl'
Expand Down Expand Up @@ -140,7 +140,6 @@ const AffiliateSignupStatus = ({
}, [])

const handleTryAgain = useCallback(() => {
trackEvent({ action: 'try_submit' })
setSignupStatus('loading')
onSubmit()
}, [signup_status])
Expand Down
6 changes: 0 additions & 6 deletions src/pages/signup-affiliates/components/_wizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
AccountType,
} from '../_lazy-loading'
import { AffiliateAccountTypes, WizardProps } from '../_types'
import trackEvent from '../utils/_tracking'
import WizardComponent from './wizard-component'
import { useResidenceList } from 'features/hooks/use-residence-list'

Expand All @@ -26,11 +25,6 @@ const Wizard = ({

const is_individual = affiliate_account?.account_type == 1

useEffect(() => {
show_wizard && trackEvent({ action: 'open_wizard', email: affiliate_account.email })
return () => trackEvent({ action: 'close_wizard' })
}, [show_wizard])

useEffect(() => {
if (is_individual) {
setAffiliateAccount((prev) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from 'styled-components'
import Button from 'components/form/button'
import { Localize } from 'components/localization'
import { WizardComponentTypes } from 'pages/signup-affiliates/_types'
import trackEvent from 'pages/signup-affiliates/utils/_tracking'
import { trackEvent } from 'pages/signup-affiliates/utils/_utils'
import device from 'themes/device'

const StyledFooter = styled.div`
Expand Down Expand Up @@ -58,23 +58,21 @@ const WizardFooter = ({
}: WizardComponentTypes) => {
const buttonHandler = React.useCallback(
(button_type: ButtonType): void => {
const prev_step = step - 1
if (button_type === ButtonType.Previous) {
trackEvent({
action: 'step_back',
step_num: prev_step,
step_codename: getCodeName(prev_step),
step_num: step,
step_codename: getCodeName(step),
})
step > 1 && setStep(prev_step)
step > 1 && setStep(step)
}
if (button_type === ButtonType.Next) {
const next_step = step + 1
trackEvent({
action: 'step_passed',
step_num: next_step,
step_codename: getCodeName(next_step),
step_num: step,
step_codename: getCodeName(step),
})
step < max_step && setStep(next_step)
step < max_step && setStep(step + 1)
}
},
[max_step, setStep, step],
Expand Down
25 changes: 12 additions & 13 deletions src/pages/signup-affiliates/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect, useState } from 'react'
import styled from 'styled-components'
import trackEvent from './utils/_tracking'
import { Submit } from './utils/_utils'
import { Submit, trackEvent } from './utils/_utils'
import { AffiliateAccountTypes, SignUpStatusTypes } from './_types'
import { AffiliateSignupForm, AffiliateSignupStatus, Wizard } from './_lazy-loading'
import { isBrowser } from 'common/utility'
Expand Down Expand Up @@ -93,6 +92,7 @@ const AffiliateSignup = () => {
const handleBeforeUnload = (event) => {
event.preventDefault()
trackEvent({ action: 'close' })
trackEvent({ action: 'close_wizard' })
}
window.addEventListener('beforeunload', handleBeforeUnload)
return () => window.removeEventListener('beforeunload', handleBeforeUnload)
Expand All @@ -113,8 +113,15 @@ const AffiliateSignup = () => {

useEffect(() => {
const partner_signup_error_message = affiliate_api_error?.error.message

if (partner_signup_error_message == 'Username not available') {
if (affiliate_api_data) {
trackEvent({
action: 'success_popup_opened',
user_choice:
JSON.stringify(affiliate_api_error?.echo_req) ||
'success, but without echo_req',
})
setSignupStatus('success')
} else if (partner_signup_error_message == 'Username not available') {
trackEvent({ action: 'partners_signup_error', partner_signup_error_message })
setSignupStatus(partner_signup_error_message)
} else if (
Expand All @@ -126,15 +133,7 @@ const AffiliateSignup = () => {
setSignupStatus('Your website is not a valid entry')
} else if (partner_signup_error_message)
trackEvent({ action: 'other_error', partner_signup_error_message })

if (affiliate_api_data) {
trackEvent({
action: 'success_popup_opened',
user_choice: JSON.stringify(affiliate_api_error?.echo_req) || 'default or null',
})
setSignupStatus('success')
}
}, [affiliate_api_data, affiliate_api_error, affiliateSend])
}, [affiliate_api_data, affiliate_api_error])

useEffect(() => {
setAffiliateAccount({
Expand Down
19 changes: 0 additions & 19 deletions src/pages/signup-affiliates/utils/_tracking.ts

This file was deleted.

21 changes: 20 additions & 1 deletion src/pages/signup-affiliates/utils/_utils.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Analytics } from '@deriv/analytics'
import { Analytics, TEvents } from '@deriv/analytics'
import { SubmitTypes } from '../_types'

export const customSlugify = (text: string): string => {
Expand Down Expand Up @@ -69,3 +69,22 @@ export const Submit = ({
}),
})
}

export const trackEvent = ({
action,
email,
partner_signup_error_message,
user_choice,
step_num,
step_codename,
}: TEvents['ce_partner_account_signup_form']) => {
Analytics?.trackEvent('ce_partner_account_signup_form', {
action,
form_name: 'ce_partner_account_signup_form',
...(email && { email }),
...(user_choice && { user_choice }),
...(step_num && { step_num }),
...(step_codename && { step_codename }),
...(partner_signup_error_message && { partner_signup_error_message }),
})
}

0 comments on commit ed623a0

Please sign in to comment.