Skip to content

Commit 7756790

Browse files
authored
Merge pull request #11776 from google/enhancement/11722-ga-event-analytics-setup
[11722] Add GA event tracking for the Analytics setup screen.
2 parents 5fc5aa9 + 5087bf3 commit 7756790

File tree

4 files changed

+130
-21
lines changed

4 files changed

+130
-21
lines changed

assets/js/components/setup/ModuleSetup.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,25 @@ import { deleteItem } from '@/js/googlesitekit/api/cache';
4141
import { trackEvent } from '@/js/util';
4242
import { useFeature } from '@/js/hooks/useFeature';
4343
import useQueryArg from '@/js/hooks/useQueryArg';
44+
import useViewContext from '@/js/hooks/useViewContext';
4445
import HelpMenu from '@/js/components/help/HelpMenu';
4546
import { Cell, Grid, Row } from '@/js/material-components';
4647
import Header from '@/js/components/Header';
4748
import ModuleSetupFooter from './ModuleSetupFooter';
4849
import ExitSetup from '@/js/components/setup/ExitSetup';
4950
import ProgressIndicator from '@/js/components/ProgressIndicator';
50-
import useViewContext from '@/js/hooks/useViewContext';
5151

5252
export default function ModuleSetup( { moduleSlug } ) {
5353
const viewContext = useViewContext();
54-
5554
const { navigateTo } = useDispatch( CORE_LOCATION );
5655
const setupFlowRefreshEnabled = useFeature( 'setupFlowRefresh' );
5756
const [ showProgress ] = useQueryArg( 'showProgress' );
57+
58+
const isInitialSetupFlow =
59+
setupFlowRefreshEnabled &&
60+
moduleSlug === MODULE_SLUG_ANALYTICS_4 &&
61+
showProgress === 'true';
62+
5863
const module = useSelect( ( select ) =>
5964
select( CORE_MODULES ).getModule( moduleSlug )
6065
);
@@ -73,11 +78,18 @@ export default function ModuleSetup( { moduleSlug } ) {
7378
async ( redirectURL ) => {
7479
await deleteItem( 'module_setup' );
7580

76-
await trackEvent(
77-
'moduleSetup',
78-
'complete_module_setup',
79-
moduleSlug
80-
);
81+
if ( isInitialSetupFlow ) {
82+
await trackEvent(
83+
`${ viewContext }_setup`,
84+
'setup_flow_v3_complete_analytics_step'
85+
);
86+
} else {
87+
await trackEvent(
88+
'moduleSetup',
89+
'complete_module_setup',
90+
moduleSlug
91+
);
92+
}
8193

8294
if ( redirectURL ) {
8395
navigateTo( redirectURL );
@@ -95,7 +107,7 @@ export default function ModuleSetup( { moduleSlug } ) {
95107
);
96108
navigateTo( adminURL );
97109
},
98-
[ registry, navigateTo, moduleSlug ]
110+
[ registry, navigateTo, moduleSlug, viewContext, isInitialSetupFlow ]
99111
);
100112

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

111123
useMount( () => {
124+
if ( isInitialSetupFlow ) {
125+
trackEvent(
126+
`${ viewContext }_setup`,
127+
'setup_flow_v3_view_analytics_step'
128+
);
129+
130+
return;
131+
}
132+
112133
trackEvent( 'moduleSetup', 'view_module_setup', moduleSlug );
113134
} );
114135

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

119140
const { SetupComponent } = module;
120141

121-
const isInitialSetupFlow =
122-
setupFlowRefreshEnabled &&
123-
moduleSlug === MODULE_SLUG_ANALYTICS_4 &&
124-
showProgress === 'true';
125-
126142
return (
127143
<Fragment>
128144
<Header

assets/js/components/setup/ModuleSetup.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe( 'ModuleSetup', () => {
5757
} );
5858

5959
afterEach( () => {
60+
mockTrackEvent.mockClear();
6061
jest.resetAllMocks();
6162
} );
6263

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

203204
expect( mockTrackEvent ).toHaveBeenCalledTimes( 1 );
204205
} );
206+
207+
it( 'tracks only the initial setup analytics view event', () => {
208+
expect( mockTrackEvent ).toHaveBeenCalledWith(
209+
`${ VIEW_CONTEXT_MODULE_SETUP }_setup`,
210+
'setup_flow_v3_view_analytics_step'
211+
);
212+
213+
expect( mockTrackEvent ).toHaveBeenCalledTimes( 1 );
214+
} );
205215
} );
206216

207217
describe( 'not initial setup flow', () => {
@@ -257,6 +267,16 @@ describe( 'ModuleSetup', () => {
257267
it( 'should match the snapshot', () => {
258268
expect( container ).toMatchSnapshot();
259269
} );
270+
271+
it( 'tracks only the generic module setup view event', () => {
272+
expect( mockTrackEvent ).toHaveBeenCalledWith(
273+
'moduleSetup',
274+
'view_module_setup',
275+
MODULE_SLUG_ANALYTICS_4
276+
);
277+
278+
expect( mockTrackEvent ).toHaveBeenCalledTimes( 1 );
279+
} );
260280
} );
261281
} );
262282

assets/js/modules/analytics-4/components/common/AccountCreate/index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export default function AccountCreate( { className } ) {
155155
}, [ hasAccountCreateForm, siteName, siteURL, timezone, setValues ] );
156156

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

159160
const handleSubmit = useCallback( async () => {
160161
const scopes = [];
@@ -192,11 +193,19 @@ export default function AccountCreate( { className } ) {
192193
}
193194

194195
setValues( FORM_ACCOUNT_CREATE, { autoSubmit: false } );
195-
await trackEvent(
196-
`${ viewContext }_analytics`,
197-
'create_account',
198-
'proxy'
199-
);
196+
197+
if ( isInitialSetupFlow ) {
198+
await trackEvent(
199+
`${ viewContext }_setup`,
200+
'setup_flow_v3_create_analytics_account'
201+
);
202+
} else {
203+
await trackEvent(
204+
`${ viewContext }_analytics`,
205+
'create_account',
206+
'proxy'
207+
);
208+
}
200209

201210
const { error } = await createAccount( {
202211
showProgress: showProgress === 'true',
@@ -210,10 +219,11 @@ export default function AccountCreate( { className } ) {
210219
hasEditScope,
211220
hasGTMScope,
212221
setValues,
213-
viewContext,
222+
isInitialSetupFlow,
214223
createAccount,
215224
showProgress,
216225
setPermissionScopeError,
226+
viewContext,
217227
setConversionTrackingEnabled,
218228
saveConversionTrackingSettings,
219229
] );
@@ -242,8 +252,6 @@ export default function AccountCreate( { className } ) {
242252
return <ProgressBar />;
243253
}
244254

245-
const isInitialSetupFlow = !! showProgress && setupFlowRefreshEnabled;
246-
247255
return (
248256
<div className={ className }>
249257
<StoreErrorNotices

assets/js/modules/analytics-4/components/common/AccountCreate/index.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Internal dependencies
2121
*/
2222
import {
23+
cleanup,
2324
createTestRegistry,
2425
fireEvent,
2526
muteFetch,
@@ -42,9 +43,14 @@ import { MODULE_SLUG_ANALYTICS_4 } from '@/js/modules/analytics-4/constants';
4243
import { createCacheKey } from '@/js/googlesitekit/api';
4344
import { getKeys, setItem } from '@/js/googlesitekit/api/cache';
4445
import AccountCreate from '.';
46+
import * as tracking from '@/js/util/tracking';
47+
import { VIEW_CONTEXT_MODULE_SETUP } from '@/js/googlesitekit/constants';
4548
import { CORE_SITE } from '@/js/googlesitekit/datastore/site/constants';
4649
import { MODULE_SLUG_SEARCH_CONSOLE } from '@/js/modules/search-console/constants';
4750

51+
const mockTrackEvent = jest.spyOn( tracking, 'trackEvent' );
52+
mockTrackEvent.mockImplementation( () => Promise.resolve() );
53+
4854
const REGEX_REST_CONVERSION_TRACKING_SETTINGS = new RegExp(
4955
'^/google-site-kit/v1/core/site/data/conversion-tracking'
5056
);
@@ -312,5 +318,64 @@ describe( 'AccountCreate', () => {
312318
REGEX_REST_CONVERSION_TRACKING_SETTINGS
313319
);
314320
} );
321+
322+
describe( 'event tracking', () => {
323+
beforeEach( () => {
324+
mockTrackEvent.mockClear();
325+
cleanup();
326+
} );
327+
328+
it( 'should track the `setup_flow_v3_create_analytics_account` event when creating an account during the initial setup flow', async () => {
329+
global.location.href =
330+
'http://example.com/wp-admin/admin.php?page=googlesitekit-dashboard&slug=analytics-4&reAuth=true&showProgress=true';
331+
332+
( { getByRole, waitForRegistry, rerender } = render(
333+
<AccountCreate />,
334+
{
335+
registry,
336+
viewContext: VIEW_CONTEXT_MODULE_SETUP,
337+
features: [ 'setupFlowRefresh' ],
338+
}
339+
) );
340+
341+
fireEvent.click(
342+
getByRole( 'button', { name: 'Create Account' } )
343+
);
344+
345+
await waitForRegistry();
346+
347+
expect( mockTrackEvent ).toHaveBeenCalledWith(
348+
`${ VIEW_CONTEXT_MODULE_SETUP }_setup`,
349+
'setup_flow_v3_create_analytics_account'
350+
);
351+
352+
expect( mockTrackEvent ).toHaveBeenCalledTimes( 1 );
353+
} );
354+
355+
it( 'should track the `create_account` event when creating an account after the initial setup', async () => {
356+
global.location.href =
357+
'http://example.com/wp-admin/admin.php?page=googlesitekit-dashboard&slug=analytics-4&reAuth=true';
358+
( { getByRole, waitForRegistry, rerender } = render(
359+
<AccountCreate />,
360+
{
361+
registry,
362+
viewContext: VIEW_CONTEXT_MODULE_SETUP,
363+
}
364+
) );
365+
366+
fireEvent.click(
367+
getByRole( 'button', { name: 'Create Account' } )
368+
);
369+
370+
await waitForRegistry();
371+
372+
expect( mockTrackEvent ).toHaveBeenCalledWith(
373+
`${ VIEW_CONTEXT_MODULE_SETUP }_analytics`,
374+
'create_account',
375+
'proxy'
376+
);
377+
expect( mockTrackEvent ).toHaveBeenCalledTimes( 1 );
378+
} );
379+
} );
315380
} );
316381
} );

0 commit comments

Comments
 (0)