-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stepper: Add a flag to skip the
calypso_signup_start
on sign up flo…
…ws (#91597) * Add hook to manage the signup_start events * Use hook to manage the sign up event
- Loading branch information
1 parent
84cb354
commit 6f03668
Showing
3 changed files
with
188 additions
and
27 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
client/landing/stepper/declarative-flow/internals/hooks/use-sign-up-start-tracking/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { SENSEI_FLOW } from '@automattic/onboarding'; | ||
import { useEffect, useMemo } from 'react'; | ||
import { useQuery } from 'calypso/landing/stepper/hooks/use-query'; | ||
import { recordSignupStart } from 'calypso/lib/analytics/signup'; | ||
import { type Flow } from '../../types'; | ||
|
||
/** | ||
* Hook to track the start of a signup flow. | ||
*/ | ||
interface Props { | ||
flow: Flow; | ||
currentStepRoute: string; | ||
} | ||
|
||
export const useSignUpStartTracking = ( { flow, currentStepRoute }: Props ) => { | ||
const steps = flow.useSteps(); | ||
const queryParams = useQuery(); | ||
const ref = queryParams.get( 'ref' ) || ''; | ||
const signedUp = queryParams.has( 'signed_up' ); | ||
|
||
// TODO: Check if we can remove the sensei flow reference from here. | ||
const firstStepSlug = ( flow.name === SENSEI_FLOW ? steps[ 1 ] : steps[ 0 ] ).slug; | ||
const isFirstStep = firstStepSlug === currentStepRoute; | ||
const extraProps = useMemo( () => flow.useSignupStartEventProps?.() || {}, [ flow ] ); | ||
const flowName = flow.name; | ||
const shouldTrack = flow.isSignupFlow && isFirstStep && ! signedUp; | ||
|
||
useEffect( () => { | ||
if ( ! shouldTrack ) { | ||
return; | ||
} | ||
|
||
recordSignupStart( flowName, ref, extraProps ); | ||
}, [ extraProps, flowName, ref, shouldTrack ] ); | ||
}; |
147 changes: 147 additions & 0 deletions
147
...anding/stepper/declarative-flow/internals/hooks/use-sign-up-start-tracking/test/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { recordTracksEvent } from '@automattic/calypso-analytics'; | ||
import { SENSEI_FLOW } from '@automattic/onboarding'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
import React from 'react'; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import { renderHookWithProvider } from 'calypso/test-helpers/testing-library'; | ||
import { useSignUpStartTracking } from '../'; | ||
import type { Flow, StepperStep } from '../../../types'; | ||
|
||
const steps = [ { slug: 'step-1' }, { slug: 'step-2' } ] as StepperStep[]; | ||
|
||
const regularFlow: Flow = { | ||
name: 'regular-flow', | ||
useSteps: () => steps, | ||
useStepNavigation: () => ( { | ||
submit: () => {}, | ||
} ), | ||
isSignupFlow: false, | ||
}; | ||
|
||
const signUpFlow: Flow = { | ||
...regularFlow, | ||
name: 'sign-up-flow', | ||
isSignupFlow: true, | ||
}; | ||
|
||
const senseiFlow: Flow = { | ||
...regularFlow, | ||
name: SENSEI_FLOW, | ||
// The original sensei flow is missing the isSignupFlow flag as true, it will be addressed by wp-calypso/pull/91593 | ||
isSignupFlow: true, | ||
}; | ||
|
||
jest.mock( '@automattic/calypso-analytics' ); | ||
|
||
const render = ( { flow, currentStepRoute, queryParams = {} } ) => { | ||
return renderHookWithProvider( | ||
() => | ||
useSignUpStartTracking( { | ||
flow, | ||
currentStepRoute, | ||
} ), | ||
{ | ||
wrapper: ( { children } ) => ( | ||
<MemoryRouter initialEntries={ [ addQueryArgs( `/setup/${ flow.name }`, queryParams ) ] }> | ||
{ children } | ||
</MemoryRouter> | ||
), | ||
} | ||
); | ||
}; | ||
|
||
describe( 'useSignUpTracking', () => { | ||
beforeEach( () => { | ||
jest.clearAllMocks(); | ||
} ); | ||
|
||
it( 'does not track event when the flow is not a isSignupFlow', () => { | ||
render( { flow: regularFlow, currentStepRoute: 'step-1' } ); | ||
|
||
expect( recordTracksEvent ).not.toHaveBeenCalled(); | ||
} ); | ||
|
||
describe( 'sign-up-flow', () => { | ||
it( 'tracks the event current step is first step', () => { | ||
render( { | ||
flow: signUpFlow, | ||
currentStepRoute: 'step-1', | ||
queryParams: { ref: 'another-flow-or-cta' }, | ||
} ); | ||
|
||
expect( recordTracksEvent ).toHaveBeenCalledWith( 'calypso_signup_start', { | ||
flow: 'sign-up-flow', | ||
ref: 'another-flow-or-cta', | ||
} ); | ||
} ); | ||
|
||
it( 'skips the tracking when the signed up flag is set', () => { | ||
render( { | ||
flow: signUpFlow, | ||
currentStepRoute: 'step-1', | ||
queryParams: { signed_up: 1 }, | ||
} ); | ||
|
||
expect( recordTracksEvent ).not.toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'tracks the event with extra props from useSighupStartEventProps', () => { | ||
render( { | ||
flow: { | ||
...signUpFlow, | ||
useSignupStartEventProps: () => ( { extra: 'props' } ), | ||
} satisfies Flow, | ||
currentStepRoute: 'step-1', | ||
queryParams: { ref: 'another-flow-or-cta' }, | ||
} ); | ||
|
||
expect( recordTracksEvent ).toHaveBeenCalledWith( 'calypso_signup_start', { | ||
flow: 'sign-up-flow', | ||
ref: 'another-flow-or-cta', | ||
extra: 'props', | ||
} ); | ||
} ); | ||
|
||
it( 'does not track events current step is NOT the first step', () => { | ||
render( { flow: signUpFlow, currentStepRoute: 'step-2' } ); | ||
|
||
expect( recordTracksEvent ).not.toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'does not track events current step is the first step AND the user is returning from the sign in flow', () => { | ||
render( { flow: signUpFlow, currentStepRoute: 'step-1', queryParams: { signed_up: true } } ); | ||
|
||
expect( recordTracksEvent ).not.toHaveBeenCalled(); | ||
} ); | ||
|
||
// Check if sensei is a sign-up flow; | ||
it( "tracks when the user is on the sensei's flow second step", () => { | ||
render( { flow: senseiFlow, currentStepRoute: 'step-2' } ); | ||
|
||
expect( recordTracksEvent ).toHaveBeenCalledWith( 'calypso_signup_start', { | ||
flow: SENSEI_FLOW, | ||
ref: '', | ||
} ); | ||
} ); | ||
|
||
it( 'does not trigger the event on rerender', () => { | ||
const { rerender } = render( { | ||
flow: { ...signUpFlow, useSignupStartEventProps: () => ( { extra: 'props' } ) }, | ||
currentStepRoute: 'step-1', | ||
queryParams: { ref: 'another-flow-or-cta' }, | ||
} ); | ||
|
||
rerender(); | ||
|
||
expect( recordTracksEvent ).toHaveBeenNthCalledWith( 1, 'calypso_signup_start', { | ||
flow: 'sign-up-flow', | ||
ref: 'another-flow-or-cta', | ||
extra: 'props', | ||
} ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters