-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update/tracking fix inserter menu no results issue (#41168)
* tracking: track Inserter Menu search using Slot * tracking: remove picker-search handler * tracking: register track inserter menu plugin * tracking: track no-results with a temp hacky fix * tracking: add a 7.8.1 fallback * tracking: fix checking plugin version bug * tracking: debug plugin version * tracking: update deendencies in Inserter Menu * Update apps/wpcom-block-editor/src/wpcom/features/tracking/wpcom-navigation-menu-search-handler.js Co-Authored-By: Dave Smith <[email protected]> * tracking: fix typo Co-authored-by: Dave Smith <[email protected]>
- Loading branch information
Showing
4 changed files
with
140 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,25 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
|
||
import { registerPlugin } from '@wordpress/plugins'; | ||
|
||
/* eslint-enable import/no-extraneous-dependencies */ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './features/deprecate-coblocks-buttons'; | ||
import './features/fix-block-invalidation-errors'; | ||
import './features/reorder-block-categories'; | ||
import './features/tracking'; | ||
|
||
import InserterMenuTrackingEvent from './features/tracking/wpcom-navigation-menu-search-handler'; | ||
|
||
registerPlugin( 'track-inserter-menu-events', { | ||
render() { | ||
return <InserterMenuTrackingEvent />; | ||
}, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
.../wpcom-block-editor/src/wpcom/features/tracking/wpcom-block-picker-search-term-handler.js
This file was deleted.
Oops, something went wrong.
121 changes: 121 additions & 0 deletions
121
apps/wpcom-block-editor/src/wpcom/features/tracking/wpcom-navigation-menu-search-handler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { debounce } from 'lodash'; | ||
import debugFactory from 'debug'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useEffect } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { __experimentalInserterMenuExtension as InserterMenuExtension } from '@wordpress/block-editor'; | ||
|
||
/* eslint-enable import/no-extraneous-dependencies */ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import tracksRecordEvent from './track-record-event'; | ||
|
||
// let's remove this line once the core version updates. | ||
const debug = debugFactory( 'wpcom-block-editor:tracking:inserter-menu' ); | ||
|
||
const InserterMenuTrackingEvent = function() { | ||
const [ searchTerm, setSearchTerm ] = useState( '' ); | ||
const { selectedBlock } = useSelect( select => ( { | ||
selectedBlock: select( 'core/block-editor' ).getSelectedBlock(), | ||
} ) ); | ||
|
||
const pluginVersion = window.wpcomGutenberg ? window.wpcomGutenberg.pluginVersion : null; | ||
|
||
const debouncedSetFilterValue = debounce( ( search_term, has_items ) => { | ||
setSearchTerm( search_term ); | ||
|
||
if ( search_term.length < 3 ) { | ||
return; | ||
} | ||
|
||
const eventProperties = { | ||
search_term, | ||
context: 'inserter_menu', | ||
selected_block: selectedBlock ? selectedBlock.name : null, | ||
}; | ||
|
||
tracksRecordEvent( 'wpcom_block_picker_search_term', eventProperties ); | ||
|
||
/* | ||
* Let's delegate registering no-results search event | ||
* to the temporary solution below if the core version | ||
* is equal to 7.8.1. | ||
*/ | ||
if ( pluginVersion && pluginVersion === '7.8.1' ) { | ||
return null; | ||
} | ||
|
||
// let's remove this line once the core version updates. | ||
debug( '%o: tracking with Slot parameter', pluginVersion ); | ||
|
||
if ( has_items ) { | ||
return; | ||
} | ||
|
||
tracksRecordEvent( 'wpcom_block_picker_no_results', eventProperties ); | ||
}, 500 ); | ||
|
||
/* | ||
* Hacky temporal solution to detect no-results search result. | ||
* Unfortunately, __experimentalInserterMenuExtension has a bug | ||
* in the 7.8.1 core version. | ||
* The `hasItems` property is always true so it isn't possible | ||
* to rely on this value. | ||
* | ||
* @TODO: The following is a temporary solution and it should be removed | ||
* and replaced by the usage of the `hasItems` property, | ||
* once a new version of core is available in dotcom. | ||
*/ | ||
useEffect( () => { | ||
// Skip whether isn't the 7.8.1 version. | ||
if ( pluginVersion && pluginVersion !== '7.8.1' ) { | ||
return; | ||
} | ||
// let's remove this line once the core version updates. | ||
debug( '%o: tracking inspecting DOM tree', pluginVersion ); | ||
|
||
if ( ! searchTerm || searchTerm.length < 3 ) { | ||
return; | ||
} | ||
|
||
const eventProperties = { | ||
search_term: searchTerm, | ||
context: 'inserter_menu', | ||
selected_block: selectedBlock ? selectedBlock.name : null, | ||
}; | ||
|
||
const hasResultsEl = document.querySelectorAll( '.block-editor-inserter__results' ); | ||
const hasNoResultsEl = | ||
document.querySelectorAll( '.block-editor-inserter__no-results' ).length !== 0; | ||
|
||
if ( | ||
hasNoResultsEl || | ||
( hasResultsEl && hasResultsEl[ 0 ] && hasResultsEl[ 0 ].children.length === 0 ) | ||
) { | ||
tracksRecordEvent( 'wpcom_block_picker_no_results', eventProperties ); | ||
} | ||
}, [ searchTerm, pluginVersion, selectedBlock ] ); | ||
|
||
return ( | ||
<InserterMenuExtension> | ||
{ ( { filterValue, hasItems } ) => { | ||
if ( searchTerm !== filterValue ) { | ||
debouncedSetFilterValue( filterValue, hasItems ); | ||
} | ||
|
||
return null; | ||
} } | ||
</InserterMenuExtension> | ||
); | ||
}; | ||
|
||
export default InserterMenuTrackingEvent; |