-
Notifications
You must be signed in to change notification settings - Fork 328
[11722] Add GA event tracking for the Analytics setup screen. #11776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
f8b25f2
6b6aa0c
de48db5
c18d236
5daa659
00d55fe
54cd414
0c0865f
66a9748
8867812
8d9b9ee
5087bf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ 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'; | ||
|
|
@@ -49,6 +50,7 @@ import ExitSetup from '@/js/components/setup/ExitSetup'; | |
| import ProgressIndicator from '@/js/components/ProgressIndicator'; | ||
|
|
||
| export default function ModuleSetup( { moduleSlug } ) { | ||
| const viewContext = useViewContext(); | ||
| const { navigateTo } = useDispatch( CORE_LOCATION ); | ||
| const setupFlowRefreshEnabled = useFeature( 'setupFlowRefresh' ); | ||
| const [ showProgress ] = useQueryArg( 'showProgress' ); | ||
|
|
@@ -71,11 +73,13 @@ export default function ModuleSetup( { moduleSlug } ) { | |
| async ( redirectURL ) => { | ||
| await deleteItem( 'module_setup' ); | ||
|
|
||
| await trackEvent( | ||
| 'moduleSetup', | ||
| 'complete_module_setup', | ||
| moduleSlug | ||
| ); | ||
| const completionEventArgs = isInitialSetupFlow | ||
| ? [ | ||
| `${ viewContext }_setup`, | ||
| 'setup_flow_v3_complete_analytics_step', | ||
| ] | ||
| : [ 'moduleSetup', 'complete_module_setup', moduleSlug ]; | ||
| await trackEvent( ...completionEventArgs ); | ||
|
|
||
| if ( redirectURL ) { | ||
| navigateTo( redirectURL ); | ||
|
|
@@ -106,7 +110,21 @@ export default function ModuleSetup( { moduleSlug } ) { | |
| await trackEvent( 'moduleSetup', 'cancel_module_setup', moduleSlug ); | ||
| }, [ moduleSlug ] ); | ||
|
|
||
| const isInitialSetupFlow = | ||
|
||
| setupFlowRefreshEnabled && | ||
| moduleSlug === MODULE_SLUG_ANALYTICS_4 && | ||
| showProgress === 'true'; | ||
|
|
||
| useMount( () => { | ||
| if ( isInitialSetupFlow ) { | ||
| trackEvent( | ||
| `${ viewContext }_setup`, | ||
| 'setup_flow_v3_view_analytics_step' | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| trackEvent( 'moduleSetup', 'view_module_setup', moduleSlug ); | ||
| } ); | ||
|
|
||
|
|
@@ -116,11 +134,6 @@ export default function ModuleSetup( { moduleSlug } ) { | |
|
|
||
| const { SetupComponent } = module; | ||
|
|
||
| const isInitialSetupFlow = | ||
| setupFlowRefreshEnabled && | ||
| moduleSlug === MODULE_SLUG_ANALYTICS_4 && | ||
| showProgress === 'true'; | ||
|
|
||
| return ( | ||
| <Fragment> | ||
| <Header | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,13 +36,18 @@ import { VIEW_CONTEXT_MODULE_SETUP } from '@/js/googlesitekit/constants'; | |
| import { mockLocation } from '../../../../tests/js/mock-browser-utils'; | ||
| import { CORE_USER } from '@/js/googlesitekit/datastore/user/constants'; | ||
| import { CORE_MODULES } from '@/js/googlesitekit/modules/datastore/constants'; | ||
| import * as tracking from '@/js/util/tracking'; | ||
|
|
||
| const mockTrackEvent = jest.spyOn( tracking, 'trackEvent' ); | ||
| mockTrackEvent.mockImplementation( () => Promise.resolve() ); | ||
|
|
||
| describe( 'ModuleSetup', () => { | ||
| mockLocation(); | ||
|
|
||
| let registry; | ||
|
|
||
| beforeEach( () => { | ||
| mockTrackEvent.mockClear(); | ||
|
||
| registry = createTestRegistry(); | ||
| registry.dispatch( CORE_USER ).receiveGetDismissedItems( [] ); | ||
| registry.dispatch( CORE_USER ).receiveGetDismissedPrompts( {} ); | ||
|
|
@@ -175,6 +180,20 @@ describe( 'ModuleSetup', () => { | |
| it( 'should match the snapshot', () => { | ||
| expect( container ).toMatchSnapshot(); | ||
| } ); | ||
|
|
||
| it( 'tracks only the initial setup analytics view event', () => { | ||
| expect( mockTrackEvent ).toHaveBeenCalledWith( | ||
| `${ VIEW_CONTEXT_MODULE_SETUP }_setup`, | ||
| 'setup_flow_v3_view_analytics_step' | ||
| ); | ||
| const genericViewCall = mockTrackEvent.mock.calls.find( | ||
| ( call ) => | ||
| call[ 0 ] === 'moduleSetup' && | ||
| call[ 1 ] === 'view_module_setup' && | ||
| call[ 2 ] === MODULE_SLUG_ANALYTICS_4 | ||
| ); | ||
| expect( genericViewCall ).toBeUndefined(); | ||
techanvil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'not initial setup flow', () => { | ||
|
|
@@ -230,6 +249,20 @@ 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 | ||
| ); | ||
| const initialAnalyticsCall = mockTrackEvent.mock.calls.find( | ||
| ( call ) => | ||
| call[ 0 ] === `${ VIEW_CONTEXT_MODULE_SETUP }_setup` && | ||
| call[ 1 ] === 'setup_flow_v3_view_analytics_step' | ||
| ); | ||
| expect( initialAnalyticsCall ).toBeUndefined(); | ||
techanvil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } ); | ||
| } ); | ||
| } ); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = []; | ||
|
|
@@ -192,11 +193,14 @@ export default function AccountCreate( { className } ) { | |
| } | ||
|
|
||
| setValues( FORM_ACCOUNT_CREATE, { autoSubmit: false } ); | ||
| await trackEvent( | ||
| `${ viewContext }_analytics`, | ||
| 'create_account', | ||
| 'proxy' | ||
| ); | ||
|
|
||
| const createAccountEventArgs = isInitialSetupFlow | ||
|
||
| ? [ | ||
| `${ viewContext }_setup`, | ||
| 'setup_flow_v3_create_analytics_account', | ||
| ] | ||
| : [ `${ viewContext }_analytics`, 'create_account', 'proxy' ]; | ||
| await trackEvent( ...createAccountEventArgs ); | ||
|
|
||
| const { error } = await createAccount( { | ||
| showProgress: showProgress === 'true', | ||
|
|
@@ -210,10 +214,11 @@ export default function AccountCreate( { className } ) { | |
| hasEditScope, | ||
| hasGTMScope, | ||
| setValues, | ||
| viewContext, | ||
| isInitialSetupFlow, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason why you added
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| ] ); | ||
|
|
@@ -242,8 +247,6 @@ export default function AccountCreate( { className } ) { | |
| return <ProgressBar />; | ||
| } | ||
|
|
||
| const isInitialSetupFlow = !! showProgress && setupFlowRefreshEnabled; | ||
|
|
||
| return ( | ||
| <div className={ className }> | ||
| <StoreErrorNotices | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| * Internal dependencies | ||
| */ | ||
| import { | ||
| cleanup, | ||
| createTestRegistry, | ||
| fireEvent, | ||
| muteFetch, | ||
|
|
@@ -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' | ||
| ); | ||
|
|
@@ -312,5 +318,77 @@ describe( 'AccountCreate', () => { | |
| REGEX_REST_CONVERSION_TRACKING_SETTINGS | ||
| ); | ||
| } ); | ||
|
|
||
| describe( 'event tracking', () => { | ||
| beforeEach( () => { | ||
techanvil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mockTrackEvent.mockClear(); | ||
| cleanup(); | ||
| } ); | ||
|
|
||
| it( 'should track initial setup flow create account event when showProgress=true', 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' | ||
| ); | ||
|
|
||
| const genericCreateAccountCall = mockTrackEvent.mock.calls.find( | ||
| ( call ) => | ||
| call[ 0 ]?.endsWith( '_analytics' ) && | ||
| call[ 1 ] === 'create_account' | ||
| ); | ||
| expect( genericCreateAccountCall ).toBeUndefined(); | ||
techanvil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } ); | ||
|
|
||
| it( 'should track generic create account event when showProgress is not true', 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(); | ||
|
|
||
| const genericCreateAccountCall = mockTrackEvent.mock.calls.find( | ||
| ( call ) => | ||
| call[ 0 ]?.endsWith( '_analytics' ) && | ||
| call[ 1 ] === 'create_account' && | ||
| call[ 2 ] === 'proxy' | ||
| ); | ||
| expect( genericCreateAccountCall ).toBeDefined(); | ||
|
|
||
| const initialSetupCall = mockTrackEvent.mock.calls.find( | ||
| ( call ) => | ||
| call[ 0 ] === `${ VIEW_CONTEXT_MODULE_SETUP }_setup` && | ||
| call[ 1 ] === 'setup_flow_v3_create_analytics_account' | ||
| ); | ||
| expect( initialSetupCall ).toBeUndefined(); | ||
techanvil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } ); | ||
| } ); | ||
| } ); | ||
| } ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use a simple if/else here? It would be more readable in my opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion @abdelmalekkkkk.
I think we should stick to ternary operator here because of following reasons:
if...elsestatement we may have to either calltrackEventtwice or need to uselet completionEventArgswhich is susceptible to change which is a source of potential side-effect bugs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ankitrox, thanks for explaining your rationale here, however, I would agree with @abdelmalekkkkk that it would be more readable to use an
if/else. It won't result in callingtrackEvent()twice, simply writing it twice, which seems fine when considering the improved legibility: