Skip to content
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

Add tracking when opening or searching in the Command Palette #41838

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { addQueryArgs } from '@wordpress/url';
* Internal dependencies
*/
import { registerCommandWithTracking } from '../command-palette/register-command-with-tracking';
import { useEditedPostType } from '../command-palette/use-edited-post-type';

const registerWooCommerceAnalyticsCommand = ( { label, path } ) => {
const registerWooCommerceAnalyticsCommand = ( { label, path, origin } ) => {
registerCommandWithTracking( {
name: `woocommerce${ path }`,
label: sprintf(
Expand All @@ -26,10 +27,13 @@ const registerWooCommerceAnalyticsCommand = ( { label, path } ) => {
path,
} );
},
origin,
} );
};

const WooCommerceAnalyticsCommands = () => {
const { editedPostType } = useEditedPostType();
const origin = editedPostType ? editedPostType + '-editor' : null;
if (
window.hasOwnProperty( 'wcCommandPaletteAnalytics' ) &&
window.wcCommandPaletteAnalytics.hasOwnProperty( 'reports' ) &&
Expand All @@ -41,6 +45,7 @@ const WooCommerceAnalyticsCommands = () => {
registerWooCommerceAnalyticsCommand( {
label: analyticsReport.title,
path: analyticsReport.path,
origin,
} );
} );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
import { registerPlugin } from '@wordpress/plugins';
import { __, sprintf } from '@wordpress/i18n';
import { box, plus, settings } from '@wordpress/icons';
import { useMemo } from '@wordpress/element';
import { useEffect, useMemo, useRef } from '@wordpress/element';
import { dispatch, useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { addQueryArgs } from '@wordpress/url';
import { queueRecordEvent } from '@woocommerce/tracks';
import { recordEvent, queueRecordEvent } from '@woocommerce/tracks';
import { store as commandsStore } from '@wordpress/commands';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
*/
import { registerCommandWithTracking } from './register-command-with-tracking';
import { useEditedPostType } from './use-edited-post-type';

const registerWooCommerceSettingsCommand = ( { label, tab } ) => {
const registerWooCommerceSettingsCommand = ( { label, tab, origin } ) => {
registerCommandWithTracking( {
name: `woocommerce/settings-${ tab }`,
label: sprintf(
Expand All @@ -32,12 +33,32 @@ const registerWooCommerceSettingsCommand = ( { label, tab } ) => {
tab,
} );
},
origin,
} );
};

// Code adapted from the equivalent in Gutenberg:
// https://github.com/WordPress/gutenberg/blob/8863b49b7e686f555e8b8adf70cc588c4feebfbf/packages/core-commands/src/site-editor-navigation-commands.js#L36C7-L36C44
function useProductCommandLoader( { search } ) {
const { editedPostType } = useEditedPostType();
const origin = editedPostType ? editedPostType + '-editor' : null;
// Track searched values. We add a 300 ms delay to avoid tracking while typing.
const trackingSearchTimeout = useRef( null );
useEffect( () => {
if ( search !== '' ) {
clearTimeout( trackingSearchTimeout.current );
trackingSearchTimeout.current = setTimeout( () => {
recordEvent( 'woocommerce_command_palette_search', {
value: search,
origin,
} );
}, 300 );
}
return () => {
clearTimeout( trackingSearchTimeout.current );
};
}, [ search, origin ] );

const postType = 'product';
const { records, isLoading } = useSelect(
( select ) => {
Expand Down Expand Up @@ -74,6 +95,7 @@ function useProductCommandLoader( { search } ) {
callback: ( { close } ) => {
queueRecordEvent( 'woocommerce_command_palette_submit', {
name: 'woocommerce/product',
origin,
} );

const args = {
Expand All @@ -86,7 +108,7 @@ function useProductCommandLoader( { search } ) {
},
};
} );
}, [ records ] );
}, [ records, origin ] );

return {
commands,
Expand All @@ -95,6 +117,25 @@ function useProductCommandLoader( { search } ) {
}

const WooCommerceCommands = () => {
const { editedPostType } = useEditedPostType();
const origin = editedPostType ? editedPostType + '-editor' : null;
const { isCommandPaletteOpen } = useSelect( ( select ) => {
const { isOpen } = select( commandsStore );
return {
isCommandPaletteOpen: isOpen(),
};
}, [] );
const wasCommandPaletteOpen = useRef( false );

useEffect( () => {
if ( isCommandPaletteOpen && ! wasCommandPaletteOpen.current ) {
recordEvent( 'woocommerce_command_palette_open', {
origin,
} );
}
wasCommandPaletteOpen.current = isCommandPaletteOpen;
}, [ isCommandPaletteOpen, origin ] );

registerCommandWithTracking( {
name: 'woocommerce/add-new-product',
label: __( 'Add new product', 'woocommerce' ),
Expand All @@ -104,6 +145,7 @@ const WooCommerceCommands = () => {
post_type: 'product',
} );
},
origin,
} );
registerCommandWithTracking( {
name: 'woocommerce/add-new-order',
Expand All @@ -115,6 +157,7 @@ const WooCommerceCommands = () => {
action: 'new',
} );
},
origin,
} );
registerCommandWithTracking( {
name: 'woocommerce/view-products',
Expand All @@ -125,6 +168,7 @@ const WooCommerceCommands = () => {
post_type: 'product',
} );
},
origin,
} );
registerCommandWithTracking( {
name: 'woocommerce/view-orders',
Expand All @@ -135,6 +179,7 @@ const WooCommerceCommands = () => {
page: 'wc-orders',
} );
},
origin,
} );
dispatch( commandsStore ).registerCommandLoader( {
name: 'woocommerce/product',
Expand All @@ -152,6 +197,7 @@ const WooCommerceCommands = () => {
registerWooCommerceSettingsCommand( {
label: settingsCommand.label,
tab: settingsCommand.key,
origin,
} );
} );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const registerCommandWithTracking = ( {
label,
icon,
callback,
origin,
} ) => {
dispatch( commandsStore ).registerCommand( {
name,
Expand All @@ -19,6 +20,7 @@ export const registerCommandWithTracking = ( {
callback: ( ...args ) => {
queueRecordEvent( 'woocommerce_command_palette_submit', {
name,
origin,
} );

callback( ...args );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';

export const useEditedPostType = () => {
// This will return 'post' or 'page' when in the post/page editor.
const { currentPostType } = useSelect( ( select ) => {
const store = select( 'core/editor' );
if ( ! store ) {
return {
currentPostType: null,
};
}
const { getCurrentPostType } = store;
return {
currentPostType: getCurrentPostType(),
};
} );
// This will return 'wp_template' or 'wp_template_part' when in the Site Editor.
const { editedPostType } = useSelect( ( select ) => {
const store = select( 'core/edit-site' );
if ( ! store ) {
return {
editedPostType: null,
};
}
const { getEditedPostType } = store;
return {
editedPostType: getEditedPostType(),
};
} );

return { editedPostType: editedPostType || currentPostType };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: update
Comment: Add tracking when opening or searching in the Command Palette