react-code-dataset / wp-calypso /client /landing /stepper /declarative-flow /internals /hooks /use-sign-up-start-tracking /index.tsx
| import { useEffect } from 'react'; | |
| import { STEPPER_TRACKS_EVENT_SIGNUP_START } from 'calypso/landing/stepper/constants'; | |
| import recordSignupStart from 'calypso/landing/stepper/declarative-flow/internals/analytics/record-signup-start'; | |
| import useSnakeCasedKeys from 'calypso/landing/stepper/utils/use-snake-cased-keys'; | |
| import { adTrackSignupStart } from 'calypso/lib/analytics/ad-tracking'; | |
| import { gaRecordEvent } from 'calypso/lib/analytics/ga'; | |
| import { setSignupStartTime } from 'calypso/signup/storageUtils'; | |
| import { FlowV2, type Flow } from '../../types'; | |
| /** | |
| * Hook to track the start of a signup flow. | |
| */ | |
| interface Props { | |
| flow: Flow | FlowV2< any >; | |
| } | |
| export const useSignUpStartTracking = ( { flow }: Props ) => { | |
| const flowName = flow.name; | |
| const flowVariant = flow.variantSlug; | |
| const isSignupFlow = flow.isSignupFlow; | |
| const customPropsConfig = flow.useTracksEventProps?.(); | |
| const isLoading = customPropsConfig?.isLoading; | |
| const signupStartEventProps = useSnakeCasedKeys( { | |
| input: customPropsConfig?.eventsProperties[ STEPPER_TRACKS_EVENT_SIGNUP_START ], | |
| } ); | |
| /** | |
| * Timers and other analytics | |
| * | |
| * Important: Ideally, this hook should only run once per signup (`isSignupFlow`) session. | |
| * Avoid introducing more dependencies. | |
| */ | |
| useEffect( () => { | |
| if ( ! isSignupFlow ) { | |
| return; | |
| } | |
| setSignupStartTime(); | |
| // Google Analytics | |
| gaRecordEvent( 'Signup', 'calypso_signup_start' ); | |
| // Marketing | |
| adTrackSignupStart( flowName ); | |
| }, [ isSignupFlow, flowName ] ); | |
| useEffect( () => { | |
| if ( ! isSignupFlow || isLoading ) { | |
| return; | |
| } | |
| // Read the ref here to avoid re-rendering the hook when it changes. | |
| const ref = new URLSearchParams( window.location.search ).get( 'ref' ) || ''; | |
| recordSignupStart( { | |
| flow: flowName, | |
| ref, | |
| optionalProps: { | |
| ...signupStartEventProps, | |
| ...( flowVariant && { flow_variant: flowVariant } ), | |
| }, | |
| } ); | |
| }, [ isSignupFlow, flowName, signupStartEventProps, isLoading, flowVariant ] ); | |
| }; | |