File size: 1,120 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import tracksRecordEvent from './track-record-event';

// Curiously, the numeric ids of the tabs increment every time you close and
// reopen the inserter, so we use this regex to ignore that and grab the tab
// slug
const tabPanelRegex = /tab-panel-\d*-(\w*)/;
const gutenbergTabPanelName = ( tabPanel ) =>
	( tabPanel.id.match( tabPanelRegex ) || [ null, null ] )[ 1 ];

/**
 * Return the event definition object to track `wpcom_block_picker_tab_panel_selected`.
 * @returns {import('./types').DelegateEventHandler} event object definition.
 */
export default () => ( {
	id: 'wpcom-inserter-tab-panel-selected',
	selector: `.block-editor-inserter__tabs .components-tab-panel__tabs button:not(.is-active)`,
	type: 'click',
	// Using capture event listener to make sure the tab is not set to active
	// before this event listener runs. This way we can prevent the listener
	// from triggering when the tab is already active.
	capture: true,
	handler: ( event ) => {
		const tabName = gutenbergTabPanelName( event.target );
		tracksRecordEvent( 'wpcom_block_picker_tab_panel_selected', {
			tab: tabName,
		} );
	},
} );