Skip to content

Commit

Permalink
Stepper: Provision for flows to override the default Tracks event pro…
Browse files Browse the repository at this point in the history
…ps (#93273)
  • Loading branch information
chriskmnds authored Aug 22, 2024
1 parent 228da8a commit acd9c42
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
9 changes: 9 additions & 0 deletions client/landing/stepper/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ export const HOW_TO_MIGRATE_OPTIONS = {
DO_IT_FOR_ME: 'difm',
DO_IT_MYSELF: 'myself',
};

/**
* All Tracks events related to Stepper.
* Prefixed with `STEPPER_TRACKS_EVENT_[scope]_[action]` to avoid conflicts with other Tracks events.
* Example: `STEPPER_TRACKS_EVENT_STEP_NAV_SUBMIT` -> scope = `STEP_NAV`, action = `SUBMIT`
*/
export const STEPPER_TRACKS_EVENT_STEP_NAV_SUBMIT = 'calypso_signup_actions_submit_step';

export const STEPPER_TRACKS_EVENTS = < const >[ STEPPER_TRACKS_EVENT_STEP_NAV_SUBMIT ];
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { OnboardSelect } from '@automattic/data-stores';
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { STEPPER_TRACKS_EVENT_STEP_NAV_SUBMIT } from 'calypso/landing/stepper/constants';
import { ONBOARD_STORE } from 'calypso/landing/stepper/stores';
import { recordSubmitStep } from '../../analytics/record-submit-step';
import type { Flow, Navigate, ProvidedDependencies, StepperStep } from '../../types';
Expand All @@ -24,21 +26,23 @@ export const useStepNavigationWithTracking = ( {
);
const intent =
useSelect( ( select ) => ( select( ONBOARD_STORE ) as OnboardSelect ).getIntent(), [] ) ?? '';
const tracksEventPropsFromFlow = flow.useTracksEventProps?.();

const shouldTrackSubmit = flow.use__Temporary__ShouldTrackEvent?.( 'submit' ) ?? false;

return {
...stepNavigation,
submit: ( providedDependencies: ProvidedDependencies = {}, ...params: string[] ) => {
shouldTrackSubmit &&
return useMemo(
() => ( {
...stepNavigation,
submit: ( providedDependencies: ProvidedDependencies = {}, ...params: string[] ) => {
recordSubmitStep(
providedDependencies,
intent,
flow.name,
currentStepRoute,
flow.variantSlug
flow.variantSlug,
tracksEventPropsFromFlow?.[ STEPPER_TRACKS_EVENT_STEP_NAV_SUBMIT ]
);
stepNavigation.submit?.( providedDependencies, ...params );
},
};
stepNavigation.submit?.( providedDependencies, ...params );
},
} ),
[ stepNavigation, intent, flow, currentStepRoute, tracksEventPropsFromFlow ]
);
};
11 changes: 10 additions & 1 deletion client/landing/stepper/declarative-flow/internals/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StepperInternal } from '@automattic/data-stores';
import React from 'react';
import { STEPPER_TRACKS_EVENTS } from '../../constants';

/**
* This is the return type of useStepNavigation hook
Expand Down Expand Up @@ -106,6 +107,14 @@ export type UseSideEffectHook< FlowSteps extends StepperStep[] > = (
navigate: Navigate< FlowSteps >
) => void;

/**
* Used for overriding props recorded by the default Tracks event loggers.
* Can pass any properties that should be recorded for the respective events.
*/
export type UseTracksEventPropsHook = () => {
[ key in ( typeof STEPPER_TRACKS_EVENTS )[ number ] ]?: Record< string, string | number | null >;
};

export type Flow = {
name: string;
/**
Expand Down Expand Up @@ -139,7 +148,7 @@ export type Flow = {
* A hook that is called in the flow's root at every render. You can use this hook to setup side-effects, call other hooks, etc..
*/
useSideEffect?: UseSideEffectHook< ReturnType< Flow[ 'useSteps' ] > >;

useTracksEventProps?: UseTracksEventPropsHook;
/**
* Temporary hook to allow gradual migration of flows to the globalised/default event tracking.
* IMPORTANT: This hook will be removed in the future.
Expand Down
1 change: 0 additions & 1 deletion client/landing/stepper/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** Step */

export type StepOptions = Record< string, unknown >;

export interface Step {
Expand Down

0 comments on commit acd9c42

Please sign in to comment.