Skip to content
Merged
42 changes: 29 additions & 13 deletions assets/js/components/setup/ModuleSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,25 @@ import { deleteItem } from '@/js/googlesitekit/api/cache';
import { trackEvent } from '@/js/util';
import { useFeature } from '@/js/hooks/useFeature';
import useQueryArg from '@/js/hooks/useQueryArg';
import useViewContext from '@/js/hooks/useViewContext';
import HelpMenu from '@/js/components/help/HelpMenu';
import { Cell, Grid, Row } from '@/js/material-components';
import Header from '@/js/components/Header';
import ModuleSetupFooter from './ModuleSetupFooter';
import ExitSetup from '@/js/components/setup/ExitSetup';
import ProgressIndicator from '@/js/components/ProgressIndicator';
import useViewContext from '@/js/hooks/useViewContext';

export default function ModuleSetup( { moduleSlug } ) {
const viewContext = useViewContext();

const { navigateTo } = useDispatch( CORE_LOCATION );
const setupFlowRefreshEnabled = useFeature( 'setupFlowRefresh' );
const [ showProgress ] = useQueryArg( 'showProgress' );

const isInitialSetupFlow =
setupFlowRefreshEnabled &&
moduleSlug === MODULE_SLUG_ANALYTICS_4 &&
showProgress === 'true';

const module = useSelect( ( select ) =>
select( CORE_MODULES ).getModule( moduleSlug )
);
Expand All @@ -73,11 +78,18 @@ export default function ModuleSetup( { moduleSlug } ) {
async ( redirectURL ) => {
await deleteItem( 'module_setup' );

await trackEvent(
'moduleSetup',
'complete_module_setup',
moduleSlug
);
if ( isInitialSetupFlow ) {
await trackEvent(
`${ viewContext }_setup`,
'setup_flow_v3_complete_analytics_step'
);
} else {
await trackEvent(
'moduleSetup',
'complete_module_setup',
moduleSlug
);
}

if ( redirectURL ) {
navigateTo( redirectURL );
Expand All @@ -95,7 +107,7 @@ export default function ModuleSetup( { moduleSlug } ) {
);
navigateTo( adminURL );
},
[ registry, navigateTo, moduleSlug ]
[ registry, navigateTo, moduleSlug, viewContext, isInitialSetupFlow ]
);

const onCompleteSetup = module?.onCompleteSetup;
Expand All @@ -109,6 +121,15 @@ export default function ModuleSetup( { moduleSlug } ) {
}, [ moduleSlug ] );

useMount( () => {
if ( isInitialSetupFlow ) {
trackEvent(
`${ viewContext }_setup`,
'setup_flow_v3_view_analytics_step'
);

return;
}

trackEvent( 'moduleSetup', 'view_module_setup', moduleSlug );
} );

Expand All @@ -118,11 +139,6 @@ export default function ModuleSetup( { moduleSlug } ) {

const { SetupComponent } = module;

const isInitialSetupFlow =
setupFlowRefreshEnabled &&
moduleSlug === MODULE_SLUG_ANALYTICS_4 &&
showProgress === 'true';

return (
<Fragment>
<Header
Expand Down
20 changes: 20 additions & 0 deletions assets/js/components/setup/ModuleSetup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe( 'ModuleSetup', () => {
} );

afterEach( () => {
mockTrackEvent.mockClear();
jest.resetAllMocks();
} );

Expand Down Expand Up @@ -202,6 +203,15 @@ describe( 'ModuleSetup', () => {

expect( mockTrackEvent ).toHaveBeenCalledTimes( 1 );
} );

it( 'tracks only the initial setup analytics view event', () => {
expect( mockTrackEvent ).toHaveBeenCalledWith(
`${ VIEW_CONTEXT_MODULE_SETUP }_setup`,
'setup_flow_v3_view_analytics_step'
);

expect( mockTrackEvent ).toHaveBeenCalledTimes( 1 );
} );
} );

describe( 'not initial setup flow', () => {
Expand Down Expand Up @@ -257,6 +267,16 @@ describe( 'ModuleSetup', () => {
it( 'should match the snapshot', () => {
expect( container ).toMatchSnapshot();
} );

it( 'tracks only the generic module setup view event', () => {
expect( mockTrackEvent ).toHaveBeenCalledWith(
'moduleSetup',
'view_module_setup',
MODULE_SLUG_ANALYTICS_4
);

expect( mockTrackEvent ).toHaveBeenCalledTimes( 1 );
} );
} );
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export default function AccountCreate( { className } ) {
}, [ hasAccountCreateForm, siteName, siteURL, timezone, setValues ] );

const showProgress = getQueryArg( location.href, 'showProgress' );
const isInitialSetupFlow = !! showProgress && setupFlowRefreshEnabled;

const handleSubmit = useCallback( async () => {
const scopes = [];
Expand Down Expand Up @@ -192,11 +193,19 @@ export default function AccountCreate( { className } ) {
}

setValues( FORM_ACCOUNT_CREATE, { autoSubmit: false } );
await trackEvent(
`${ viewContext }_analytics`,
'create_account',
'proxy'
);

if ( isInitialSetupFlow ) {
await trackEvent(
`${ viewContext }_setup`,
'setup_flow_v3_create_analytics_account'
);
} else {
await trackEvent(
`${ viewContext }_analytics`,
'create_account',
'proxy'
);
}

const { error } = await createAccount( {
showProgress: showProgress === 'true',
Expand All @@ -210,10 +219,11 @@ export default function AccountCreate( { className } ) {
hasEditScope,
hasGTMScope,
setValues,
viewContext,
isInitialSetupFlow,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you added isInitialSetupFlow and viewContext to the dependency array here but not in ModuleSetup.js? Doesn't seem like a big deal, but I'm wondering if we can keep things consistent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good catch @abdelmalekkkkk , thanks for spotting it.

I've added it now.

createAccount,
showProgress,
setPermissionScopeError,
viewContext,
setConversionTrackingEnabled,
saveConversionTrackingSettings,
] );
Expand Down Expand Up @@ -242,8 +252,6 @@ export default function AccountCreate( { className } ) {
return <ProgressBar />;
}

const isInitialSetupFlow = !! showProgress && setupFlowRefreshEnabled;

return (
<div className={ className }>
<StoreErrorNotices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Internal dependencies
*/
import {
cleanup,
createTestRegistry,
fireEvent,
muteFetch,
Expand All @@ -42,9 +43,14 @@ import { MODULE_SLUG_ANALYTICS_4 } from '@/js/modules/analytics-4/constants';
import { createCacheKey } from '@/js/googlesitekit/api';
import { getKeys, setItem } from '@/js/googlesitekit/api/cache';
import AccountCreate from '.';
import * as tracking from '@/js/util/tracking';
import { VIEW_CONTEXT_MODULE_SETUP } from '@/js/googlesitekit/constants';
import { CORE_SITE } from '@/js/googlesitekit/datastore/site/constants';
import { MODULE_SLUG_SEARCH_CONSOLE } from '@/js/modules/search-console/constants';

const mockTrackEvent = jest.spyOn( tracking, 'trackEvent' );
mockTrackEvent.mockImplementation( () => Promise.resolve() );

const REGEX_REST_CONVERSION_TRACKING_SETTINGS = new RegExp(
'^/google-site-kit/v1/core/site/data/conversion-tracking'
);
Expand Down Expand Up @@ -312,5 +318,64 @@ describe( 'AccountCreate', () => {
REGEX_REST_CONVERSION_TRACKING_SETTINGS
);
} );

describe( 'event tracking', () => {
beforeEach( () => {
mockTrackEvent.mockClear();
cleanup();
} );

it( 'should track the `setup_flow_v3_create_analytics_account` event when creating an account during the initial setup flow', async () => {
global.location.href =
'http://example.com/wp-admin/admin.php?page=googlesitekit-dashboard&slug=analytics-4&reAuth=true&showProgress=true';

( { getByRole, waitForRegistry, rerender } = render(
<AccountCreate />,
{
registry,
viewContext: VIEW_CONTEXT_MODULE_SETUP,
features: [ 'setupFlowRefresh' ],
}
) );

fireEvent.click(
getByRole( 'button', { name: 'Create Account' } )
);

await waitForRegistry();

expect( mockTrackEvent ).toHaveBeenCalledWith(
`${ VIEW_CONTEXT_MODULE_SETUP }_setup`,
'setup_flow_v3_create_analytics_account'
);

expect( mockTrackEvent ).toHaveBeenCalledTimes( 1 );
} );

it( 'should track the `create_account` event when creating an account after the initial setup', async () => {
global.location.href =
'http://example.com/wp-admin/admin.php?page=googlesitekit-dashboard&slug=analytics-4&reAuth=true';
( { getByRole, waitForRegistry, rerender } = render(
<AccountCreate />,
{
registry,
viewContext: VIEW_CONTEXT_MODULE_SETUP,
}
) );

fireEvent.click(
getByRole( 'button', { name: 'Create Account' } )
);

await waitForRegistry();

expect( mockTrackEvent ).toHaveBeenCalledWith(
`${ VIEW_CONTEXT_MODULE_SETUP }_analytics`,
'create_account',
'proxy'
);
expect( mockTrackEvent ).toHaveBeenCalledTimes( 1 );
} );
} );
} );
} );
Loading