|
|
import { debounce } from '@wordpress/compose'; |
|
|
import { use, select } from '@wordpress/data'; |
|
|
import { applyFilters } from '@wordpress/hooks'; |
|
|
import { __ } from '@wordpress/i18n'; |
|
|
import { registerPlugin } from '@wordpress/plugins'; |
|
|
import debugFactory from 'debug'; |
|
|
import { find, isEqual, cloneDeep } from 'lodash'; |
|
|
import delegateEventTracking, { |
|
|
registerSubscriber as registerDelegateEventSubscriber, |
|
|
} from './tracking/delegate-event-tracking'; |
|
|
import tracksRecordEvent from './tracking/track-record-event'; |
|
|
import { |
|
|
buildGlobalStylesContentEvents, |
|
|
getFlattenedBlockNames, |
|
|
getBlockEventContextProperties, |
|
|
findSavingSource, |
|
|
} from './utils'; |
|
|
|
|
|
const INSERTERS = { |
|
|
HEADER_INSERTER: 'header-inserter', |
|
|
SLASH_INSERTER: 'slash-inserter', |
|
|
QUICK_INSERTER: 'quick-inserter', |
|
|
BLOCK_SWITCHER: 'block-switcher', |
|
|
PAYMENTS_INTRO_BLOCK: 'payments-intro-block', |
|
|
PATTERNS_EXPLORER: 'patterns-explorer', |
|
|
PATTERN_SELECTION_MODAL: 'pattern-selection-modal', |
|
|
}; |
|
|
|
|
|
const SELECTORS = { |
|
|
|
|
|
|
|
|
|
|
|
PATTERNS_EXPLORER: '.block-editor-block-patterns-explorer', |
|
|
PATTERNS_EXPLORER_SELECTED_CATEGORY: |
|
|
'.block-editor-block-patterns-explorer__sidebar__categories-list__item.is-pressed', |
|
|
PATTERNS_EXPLORER_SEARCH_INPUT: '.block-editor-block-patterns-explorer__search input', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PATTERN_INSERTER_SELECTED_CATEGORY: '.block-editor-inserter__patterns-selected-category', |
|
|
PATTERN_INSERTER_SEARCH_INPUT: '.block-editor-inserter__search input', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PATTERN_SELECTION_MODAL: '.block-library-query-pattern__selection-modal', |
|
|
PATTERN_SELECTION_MODAL_SEARCH_INPUT: '.block-library-query-pattern__selection-search input', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QUICK_INSERTER: '.block-editor-inserter__quick-inserter', |
|
|
QUICK_INSERTER_SEARCH_INPUT: '.block-editor-inserter__quick-inserter input', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LEGACY_BLOCK_INSERTER: '.block-editor-inserter__block-list', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
COMMAND_PALETTE_ROOT: '.commands-command-menu__container div[cmdk-root]', |
|
|
COMMAND_PALETTE_INPUT: '.commands-command-menu__container input[cmdk-input]', |
|
|
COMMAND_PALETTE_LIST: '.commands-command-menu__container div[cmdk-list]', |
|
|
}; |
|
|
|
|
|
|
|
|
const debug = debugFactory( 'wpcom-block-editor:tracking' ); |
|
|
|
|
|
const noop = () => {}; |
|
|
|
|
|
let ignoreNextReplaceBlocksAction = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function globalEventPropsHandler( block ) { |
|
|
if ( ! block?.name ) { |
|
|
return {}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { getActiveBlockVariation } = select( 'core/blocks' ); |
|
|
if ( getActiveBlockVariation ) { |
|
|
return { |
|
|
variation_slug: getActiveBlockVariation( block.name, block.attributes )?.name, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
if ( block.name === 'core/embed' && block?.attributes?.providerNameSlug ) { |
|
|
return { variation_slug: block.attributes.providerNameSlug }; |
|
|
} |
|
|
|
|
|
|
|
|
if ( block.name === 'core/social-link' && block?.attributes?.service ) { |
|
|
return { variation_slug: block.attributes.service }; |
|
|
} |
|
|
|
|
|
return {}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getTypeForBlockId = ( blockId ) => { |
|
|
const block = select( 'core/block-editor' ).getBlock( blockId ); |
|
|
return block ? block.name : null; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getBlockInserterUsed = ( originalBlockIds = [] ) => { |
|
|
const clientIds = Array.isArray( originalBlockIds ) ? originalBlockIds : [ originalBlockIds ]; |
|
|
|
|
|
if ( document.querySelector( SELECTORS.PATTERNS_EXPLORER ) ) { |
|
|
return INSERTERS.PATTERNS_EXPLORER; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
select( 'core/edit-post' )?.isInserterOpened() || |
|
|
select( 'core/edit-site' )?.isInserterOpened() || |
|
|
select( 'core/edit-widgets' )?.isInserterOpened() || |
|
|
document |
|
|
.querySelector( '.customize-widgets-layout__inserter-panel' ) |
|
|
?.contains( document.activeElement ) |
|
|
) { |
|
|
return INSERTERS.HEADER_INSERTER; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( clientIds.length && document.querySelector( '.block-editor-block-switcher__container' ) ) { |
|
|
return INSERTERS.BLOCK_SWITCHER; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
clientIds.length === 1 && |
|
|
select( 'core/block-editor' ).getBlockName( clientIds[ 0 ] ) === 'core/paragraph' && |
|
|
select( 'core/block-editor' ).getBlockAttributes( clientIds[ 0 ] ).content.startsWith( '/' ) |
|
|
) { |
|
|
return INSERTERS.SLASH_INSERTER; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
document.querySelector( SELECTORS.QUICK_INSERTER ) || |
|
|
|
|
|
document.querySelector( SELECTORS.LEGACY_BLOCK_INSERTER ) |
|
|
) { |
|
|
return INSERTERS.QUICK_INSERTER; |
|
|
} |
|
|
|
|
|
|
|
|
if ( |
|
|
clientIds.length === 1 && |
|
|
select( 'core/block-editor' ).getBlockName( clientIds[ 0 ] ) === 'jetpack/payments-intro' |
|
|
) { |
|
|
return INSERTERS.PAYMENTS_INTRO_BLOCK; |
|
|
} |
|
|
|
|
|
if ( document.querySelector( SELECTORS.PATTERN_SELECTION_MODAL ) ) { |
|
|
return INSERTERS.PATTERN_SELECTION_MODAL; |
|
|
} |
|
|
|
|
|
return undefined; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getBlockInserterSearchTerm = ( inserter ) => { |
|
|
let searchInput; |
|
|
|
|
|
switch ( inserter ) { |
|
|
case INSERTERS.PATTERNS_EXPLORER: |
|
|
searchInput = document.querySelector( SELECTORS.PATTERNS_EXPLORER_SEARCH_INPUT ); |
|
|
break; |
|
|
case INSERTERS.HEADER_INSERTER: |
|
|
searchInput = document.querySelector( SELECTORS.PATTERN_INSERTER_SEARCH_INPUT ); |
|
|
break; |
|
|
case INSERTERS.QUICK_INSERTER: |
|
|
searchInput = document.querySelector( SELECTORS.QUICK_INSERTER_SEARCH_INPUT ); |
|
|
break; |
|
|
case INSERTERS.PATTERN_SELECTION_MODAL: |
|
|
searchInput = document.querySelector( SELECTORS.PATTERN_SELECTION_MODAL_SEARCH_INPUT ); |
|
|
break; |
|
|
} |
|
|
|
|
|
return searchInput ? searchInput.value : ''; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ensureBlockObject = ( block ) => { |
|
|
if ( typeof block === 'object' ) { |
|
|
return block; |
|
|
} |
|
|
return select( 'core/block-editor' ).getBlock( block ) || {}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function trackBlocksHandler( blocks, eventName, propertiesHandler = noop, parentBlock ) { |
|
|
const castBlocks = Array.isArray( blocks ) ? blocks : [ blocks ]; |
|
|
if ( ! castBlocks || ! castBlocks.length ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
castBlocks.forEach( ( block ) => { |
|
|
|
|
|
block = ensureBlockObject( block ); |
|
|
|
|
|
const eventProperties = { |
|
|
...globalEventPropsHandler( block ), |
|
|
...propertiesHandler( block, parentBlock ), |
|
|
inner_block: !! parentBlock, |
|
|
}; |
|
|
|
|
|
if ( parentBlock ) { |
|
|
eventProperties.parent_block_name = parentBlock.name; |
|
|
} |
|
|
|
|
|
tracksRecordEvent( eventName, eventProperties ); |
|
|
|
|
|
if ( block.innerBlocks && block.innerBlocks.length ) { |
|
|
trackBlocksHandler( block.innerBlocks, eventName, propertiesHandler, block ); |
|
|
} |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getBlocksTracker = ( eventName ) => ( blockIds, fromRootClientId, toRootClientId ) => { |
|
|
const blockIdArray = Array.isArray( blockIds ) ? blockIds : [ blockIds ]; |
|
|
|
|
|
const fromContext = getBlockEventContextProperties( fromRootClientId ); |
|
|
const toContext = getBlockEventContextProperties( toRootClientId ); |
|
|
|
|
|
if ( toRootClientId === undefined || isEqual( fromContext, toContext ) ) { |
|
|
|
|
|
blockIdArray.forEach( ( blockId ) => { |
|
|
tracksRecordEvent( eventName, { |
|
|
block_name: getTypeForBlockId( blockId ), |
|
|
...fromContext, |
|
|
} ); |
|
|
} ); |
|
|
} else { |
|
|
const fromProps = {}; |
|
|
const toProps = {}; |
|
|
for ( const key of Object.keys( fromContext ) ) { |
|
|
fromProps[ `from_${ key }` ] = fromContext[ key ]; |
|
|
} |
|
|
for ( const key of Object.keys( toContext ) ) { |
|
|
toProps[ `from_${ key }` ] = toContext[ key ]; |
|
|
} |
|
|
|
|
|
blockIdArray.forEach( ( blockId ) => { |
|
|
tracksRecordEvent( eventName, { |
|
|
block_name: getTypeForBlockId( blockId ), |
|
|
...fromProps, |
|
|
...toProps, |
|
|
} ); |
|
|
} ); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const maybeTrackPatternInsertion = ( actionData, additionalData ) => { |
|
|
const { rootClientId, blocks_replaced, insert_method, search_term } = additionalData; |
|
|
const context = getBlockEventContextProperties( rootClientId ); |
|
|
const { __experimentalBlockPatternCategories: patternCategories } = |
|
|
select( 'core/block-editor' ).getSettings(); |
|
|
const patterns = select( 'core/block-editor' ).__experimentalGetAllowedPatterns(); |
|
|
|
|
|
const meta = find( actionData, ( item ) => item?.patternName ); |
|
|
let patternName = meta?.patternName; |
|
|
|
|
|
|
|
|
if ( ! patternName ) { |
|
|
const actionDataToCheck = Object.values( actionData ).filter( |
|
|
( data ) => typeof data === 'string' |
|
|
); |
|
|
const foundPattern = patterns.find( ( pattern ) => actionDataToCheck.includes( pattern.name ) ); |
|
|
if ( foundPattern ) { |
|
|
patternName = foundPattern.name; |
|
|
} |
|
|
} |
|
|
|
|
|
if ( patternName ) { |
|
|
const categoryElement = |
|
|
document.querySelector( SELECTORS.PATTERNS_EXPLORER_SELECTED_CATEGORY ) || |
|
|
document.querySelector( SELECTORS.PATTERN_INSERTER_SELECTED_CATEGORY ); |
|
|
|
|
|
const patternCategory = patternCategories.find( |
|
|
( { label } ) => label === categoryElement?.ariaLabel |
|
|
); |
|
|
|
|
|
tracksRecordEvent( 'wpcom_pattern_inserted', { |
|
|
pattern_name: patternName, |
|
|
pattern_category: patternCategory?.name, |
|
|
blocks_replaced, |
|
|
insert_method, |
|
|
search_term, |
|
|
is_user_created: patternName?.startsWith( 'core/block/' ), |
|
|
...context, |
|
|
} ); |
|
|
|
|
|
return { |
|
|
name: patternName, |
|
|
categoryName: patternCategory?.name, |
|
|
}; |
|
|
} |
|
|
|
|
|
return null; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackBlockDragDrop = ( clientIds, fromRootClientId, toRootClientId ) => { |
|
|
const isDragging = select( 'core/block-editor' ).isDraggingBlocks(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( ! isDragging ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
getBlocksTracker( 'wpcom_block_moved_via_dragging' )( |
|
|
clientIds, |
|
|
fromRootClientId, |
|
|
toRootClientId |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackBlockInsertion = ( blocks, ...args ) => { |
|
|
const [ , rootClientId ] = args; |
|
|
const insert_method = getBlockInserterUsed(); |
|
|
const search_term = getBlockInserterSearchTerm( insert_method ); |
|
|
const insertedPattern = maybeTrackPatternInsertion( args, { |
|
|
rootClientId, |
|
|
blocks_replaced: false, |
|
|
insert_method, |
|
|
search_term, |
|
|
} ); |
|
|
const context = getBlockEventContextProperties( rootClientId ); |
|
|
|
|
|
trackBlocksHandler( blocks, 'wpcom_block_inserted', ( { name } ) => ( { |
|
|
block_name: name, |
|
|
blocks_replaced: false, |
|
|
pattern_name: insertedPattern?.name, |
|
|
pattern_category: insertedPattern?.categoryName, |
|
|
insert_method, |
|
|
search_term, |
|
|
...context, |
|
|
} ) ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackBlockRemoval = ( blocks ) => { |
|
|
const rootClientId = select( 'core/block-editor' ).getBlockRootClientId( |
|
|
Array.isArray( blocks ) ? blocks[ 0 ] : blocks |
|
|
); |
|
|
const context = getBlockEventContextProperties( rootClientId ); |
|
|
trackBlocksHandler( blocks, 'wpcom_block_deleted', ( { name } ) => ( { |
|
|
block_name: name, |
|
|
...context, |
|
|
} ) ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackBlockReplacement = ( originalBlockIds, blocks, ...args ) => { |
|
|
if ( ignoreNextReplaceBlocksAction ) { |
|
|
ignoreNextReplaceBlocksAction = false; |
|
|
return; |
|
|
} |
|
|
|
|
|
const rootClientId = select( 'core/block-editor' ).getBlockRootClientId( |
|
|
Array.isArray( originalBlockIds ) ? originalBlockIds[ 0 ] : originalBlockIds |
|
|
); |
|
|
const insert_method = getBlockInserterUsed( originalBlockIds ); |
|
|
const search_term = getBlockInserterSearchTerm( insert_method ); |
|
|
const insertedPattern = maybeTrackPatternInsertion( args, { |
|
|
rootClientId, |
|
|
blocks_replaced: true, |
|
|
insert_method, |
|
|
search_term, |
|
|
} ); |
|
|
const context = getBlockEventContextProperties( rootClientId ); |
|
|
|
|
|
trackBlocksHandler( blocks, 'wpcom_block_picker_block_inserted', ( { name } ) => ( { |
|
|
block_name: name, |
|
|
blocks_replaced: true, |
|
|
pattern_name: insertedPattern?.name, |
|
|
pattern_category: insertedPattern?.categoryName, |
|
|
insert_method, |
|
|
search_term, |
|
|
...context, |
|
|
} ) ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackInnerBlocksReplacement = ( rootClientId, blocks ) => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parentBlock = select( 'core/block-editor' ).getBlocksByClientId( rootClientId )?.[ 0 ]; |
|
|
if ( parentBlock ) { |
|
|
const { name } = parentBlock; |
|
|
if ( |
|
|
|
|
|
name === 'core/template-part' || |
|
|
|
|
|
name === 'core/block' || |
|
|
|
|
|
name === 'core/post-content' || |
|
|
|
|
|
name === 'core/page-list' || |
|
|
|
|
|
name === 'core/navigation' |
|
|
) { |
|
|
return; |
|
|
} |
|
|
} |
|
|
const context = getBlockEventContextProperties( rootClientId ); |
|
|
|
|
|
trackBlocksHandler( blocks, 'wpcom_block_inserted', ( { name } ) => ( { |
|
|
block_name: name, |
|
|
blocks_replaced: true, |
|
|
|
|
|
|
|
|
from_template_selector: |
|
|
applyFilters( 'isInsertingPagePattern', false ) || |
|
|
applyFilters( 'isInsertingPageTemplate', false ), |
|
|
...context, |
|
|
} ) ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackGlobalStyles = ( eventName ) => ( options ) => { |
|
|
tracksRecordEvent( eventName, { |
|
|
...options, |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackErrorNotices = ( content, options ) => |
|
|
tracksRecordEvent( 'wpcom_gutenberg_error_notice', { |
|
|
notice_text: content, |
|
|
notice_options: JSON.stringify( options ), |
|
|
} ); |
|
|
|
|
|
const trackEnableComplementaryArea = ( scope, id ) => { |
|
|
const activeArea = select( 'core/interface' ).getActiveComplementaryArea( scope ); |
|
|
|
|
|
|
|
|
if ( activeArea !== 'edit-site/global-styles' && id === 'edit-site/global-styles' ) { |
|
|
tracksRecordEvent( 'wpcom_block_editor_global_styles_panel_toggle', { |
|
|
open: true, |
|
|
} ); |
|
|
} else if ( activeArea === 'edit-site/global-styles' && id !== 'edit-site/global-styles' ) { |
|
|
tracksRecordEvent( 'wpcom_block_editor_global_styles_panel_toggle', { |
|
|
open: false, |
|
|
} ); |
|
|
} |
|
|
}; |
|
|
|
|
|
const trackDisableComplementaryArea = ( scope ) => { |
|
|
const activeArea = select( 'core/interface' ).getActiveComplementaryArea( scope ); |
|
|
if ( activeArea === 'edit-site/global-styles' && scope === 'core/edit-site' ) { |
|
|
tracksRecordEvent( 'wpcom_block_editor_global_styles_panel_toggle', { |
|
|
open: false, |
|
|
} ); |
|
|
} |
|
|
}; |
|
|
|
|
|
const trackSaveEntityRecord = ( kind, name, record ) => { |
|
|
if ( kind === 'postType' && name === 'wp_template_part' ) { |
|
|
const variationSlug = record.area !== 'uncategorized' ? record.area : undefined; |
|
|
if ( document.querySelector( '.edit-site-create-template-part-modal' ) ) { |
|
|
ignoreNextReplaceBlocksAction = true; |
|
|
const convertedParentBlocks = select( 'core/block-editor' ).getBlocksByClientId( |
|
|
select( 'core/block-editor' ).getSelectedBlockClientIds() |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tracksRecordEvent( 'wpcom_block_editor_convert_to_template_part', { |
|
|
variation_slug: variationSlug, |
|
|
} ); |
|
|
tracksRecordEvent( 'wpcom_block_editor_convert_to_template_part', { |
|
|
variation_slug: variationSlug, |
|
|
block_names: getFlattenedBlockNames( convertedParentBlocks ).join( ',' ), |
|
|
} ); |
|
|
} else { |
|
|
tracksRecordEvent( 'wpcom_block_editor_create_template_part', { |
|
|
variation_slug: variationSlug, |
|
|
content: record.content ? record.content : undefined, |
|
|
} ); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackListViewToggle = ( isOpen ) => { |
|
|
tracksRecordEvent( 'wpcom_block_editor_list_view_toggle', { |
|
|
is_open: isOpen, |
|
|
} ); |
|
|
}; |
|
|
|
|
|
const trackSiteEditorBrowsingSidebarOpen = () => { |
|
|
|
|
|
|
|
|
|
|
|
const isOpen = select( 'core/edit-site' ).isNavigationOpened(); |
|
|
if ( isOpen ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
tracksRecordEvent( 'wpcom_block_editor_nav_sidebar_open' ); |
|
|
}; |
|
|
|
|
|
const trackSiteEditorCreateTemplate = ( { slug } ) => { |
|
|
tracksRecordEvent( 'wpcom_block_editor_nav_sidebar_item_add', { |
|
|
item_type: 'template', |
|
|
item_slug: slug, |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let isSiteEditorFirstSidebarItemEditCalled = false; |
|
|
const trackSiteEditorChangeTemplate = ( id, slug ) => { |
|
|
if ( ! isSiteEditorFirstSidebarItemEditCalled ) { |
|
|
isSiteEditorFirstSidebarItemEditCalled = true; |
|
|
return; |
|
|
} |
|
|
|
|
|
tracksRecordEvent( 'wpcom_block_editor_nav_sidebar_item_edit', { |
|
|
item_type: 'template', |
|
|
item_id: id, |
|
|
item_slug: slug, |
|
|
} ); |
|
|
}; |
|
|
|
|
|
const trackSiteEditorChangeTemplatePart = ( id ) => { |
|
|
if ( ! isSiteEditorFirstSidebarItemEditCalled ) { |
|
|
isSiteEditorFirstSidebarItemEditCalled = true; |
|
|
return; |
|
|
} |
|
|
|
|
|
tracksRecordEvent( 'wpcom_block_editor_nav_sidebar_item_edit', { |
|
|
item_type: 'template_part', |
|
|
item_id: id, |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let lastTrackSiteEditorChangeContentCall = 0; |
|
|
const trackSiteEditorChangeContent = ( { type, slug } ) => { |
|
|
if ( Date.now() - lastTrackSiteEditorChangeContentCall < 50 ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( ! isSiteEditorFirstSidebarItemEditCalled ) { |
|
|
isSiteEditorFirstSidebarItemEditCalled = true; |
|
|
return; |
|
|
} |
|
|
|
|
|
const activeMenu = select( 'core/edit-site' ).getNavigationPanelActiveMenu(); |
|
|
if ( ! type && activeMenu === 'content-categories' ) { |
|
|
type = 'taxonomy_category'; |
|
|
} |
|
|
|
|
|
tracksRecordEvent( 'wpcom_block_editor_nav_sidebar_item_edit', { |
|
|
item_type: type, |
|
|
item_slug: slug, |
|
|
} ); |
|
|
|
|
|
lastTrackSiteEditorChangeContentCall = Date.now(); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackEditEntityRecord = ( kind, type, id, updates ) => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
! isSiteEditorFirstSidebarItemEditCalled && |
|
|
kind === 'postType' && |
|
|
( type === 'wp_template' || type === 'wp_template_part' ) |
|
|
) { |
|
|
isSiteEditorFirstSidebarItemEditCalled = true; |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( kind === 'root' && type === 'globalStyles' ) { |
|
|
const editedEntity = select( 'core' ).getEditedEntityRecord( kind, type, id ); |
|
|
const entityContent = { |
|
|
settings: cloneDeep( editedEntity.settings ), |
|
|
styles: cloneDeep( editedEntity.styles ), |
|
|
}; |
|
|
const updatedContent = { |
|
|
settings: cloneDeep( updates.settings ), |
|
|
styles: cloneDeep( updates.styles ), |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
if ( ! isEqual( updatedContent, entityContent ) ) { |
|
|
buildGlobalStylesContentEvents( |
|
|
updatedContent, |
|
|
entityContent, |
|
|
'wpcom_block_editor_global_styles_update' |
|
|
); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackSaveEditedEntityRecord = ( kind, type, id ) => { |
|
|
const savedEntity = select( 'core' ).getEntityRecord( kind, type, id ); |
|
|
const editedEntity = select( 'core' ).getEditedEntityRecord( kind, type, id ); |
|
|
|
|
|
|
|
|
const templatePartArea = type === 'wp_template_part' ? savedEntity?.area : undefined; |
|
|
|
|
|
const newTemplatePartArea = |
|
|
type === 'wp_template_part' && savedEntity?.area !== editedEntity?.area |
|
|
? editedEntity.area |
|
|
: undefined; |
|
|
|
|
|
tracksRecordEvent( 'wpcom_block_editor_edited_entity_saved', { |
|
|
entity_kind: kind, |
|
|
entity_type: type, |
|
|
entity_id: id, |
|
|
saving_source: findSavingSource(), |
|
|
template_part_area: templatePartArea, |
|
|
new_template_part_area: newTemplatePartArea, |
|
|
} ); |
|
|
|
|
|
if ( kind === 'root' && type === 'globalStyles' ) { |
|
|
const entityContent = { |
|
|
settings: cloneDeep( savedEntity.settings ), |
|
|
styles: cloneDeep( savedEntity.styles ), |
|
|
}; |
|
|
const updatedContent = { |
|
|
settings: cloneDeep( editedEntity.settings ), |
|
|
styles: cloneDeep( editedEntity.styles ), |
|
|
}; |
|
|
|
|
|
buildGlobalStylesContentEvents( |
|
|
updatedContent, |
|
|
entityContent, |
|
|
'wpcom_block_editor_global_styles_save' |
|
|
); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackSaveSpecifiedEntityEdits = ( kind, type, id, itemsToSave ) => { |
|
|
const source = findSavingSource(); |
|
|
|
|
|
itemsToSave.forEach( ( item ) => |
|
|
tracksRecordEvent( 'wpcom_block_editor_edited_entity_saved', { |
|
|
entity_kind: kind, |
|
|
entity_type: type, |
|
|
entity_id: id, |
|
|
saving_source: source, |
|
|
item_saved: item, |
|
|
} ) |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackInstallBlockType = ( block ) => { |
|
|
tracksRecordEvent( 'wpcom_block_directory_install', { |
|
|
block_slug: block.id, |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackCommandPaletteSearch = debounce( ( event ) => { |
|
|
tracksRecordEvent( 'wpcom_editor_command_palette_search', { |
|
|
keyword: event.target.value, |
|
|
} ); |
|
|
}, 500 ); |
|
|
|
|
|
const trackCommandPaletteSelected = ( event ) => { |
|
|
let selectedCommandElement; |
|
|
if ( event.type === 'keydown' && event.code === 'Enter' ) { |
|
|
selectedCommandElement = event.currentTarget.querySelector( |
|
|
'div[cmdk-item][aria-selected="true"]' |
|
|
); |
|
|
} else if ( event.type === 'click' ) { |
|
|
selectedCommandElement = event.target.closest( 'div[cmdk-item][aria-selected="true"]' ); |
|
|
} |
|
|
|
|
|
if ( selectedCommandElement ) { |
|
|
tracksRecordEvent( 'wpcom_editor_command_palette_selected', { |
|
|
value: selectedCommandElement.dataset.value, |
|
|
} ); |
|
|
} |
|
|
}; |
|
|
|
|
|
const trackCommandPaletteOpen = () => { |
|
|
tracksRecordEvent( 'wpcom_editor_command_palette_open' ); |
|
|
|
|
|
window.setTimeout( () => { |
|
|
const commandPaletteInputElement = document.querySelector( SELECTORS.COMMAND_PALETTE_INPUT ); |
|
|
if ( commandPaletteInputElement ) { |
|
|
commandPaletteInputElement.addEventListener( 'input', trackCommandPaletteSearch ); |
|
|
} |
|
|
|
|
|
const commandPaletteListElement = document.querySelector( SELECTORS.COMMAND_PALETTE_LIST ); |
|
|
if ( commandPaletteListElement ) { |
|
|
commandPaletteListElement.addEventListener( 'click', trackCommandPaletteSelected ); |
|
|
} |
|
|
|
|
|
const commandPaletteRootElement = document.querySelector( SELECTORS.COMMAND_PALETTE_ROOT ); |
|
|
if ( commandPaletteRootElement ) { |
|
|
commandPaletteRootElement.addEventListener( 'keydown', trackCommandPaletteSelected ); |
|
|
} |
|
|
} ); |
|
|
}; |
|
|
|
|
|
const trackCommandPaletteClose = () => { |
|
|
tracksRecordEvent( 'wpcom_editor_command_palette_close' ); |
|
|
|
|
|
const commandPaletteInputElement = document.querySelector( SELECTORS.COMMAND_PALETTE_INPUT ); |
|
|
if ( commandPaletteInputElement ) { |
|
|
commandPaletteInputElement.removeEventListener( 'input', trackCommandPaletteSearch ); |
|
|
} |
|
|
|
|
|
const commandPaletteListElement = document.querySelector( SELECTORS.COMMAND_PALETTE_LIST ); |
|
|
if ( commandPaletteListElement ) { |
|
|
commandPaletteListElement.removeEventListener( 'click', trackCommandPaletteSelected ); |
|
|
} |
|
|
|
|
|
const commandPaletteRootElement = document.querySelector( SELECTORS.COMMAND_PALETTE_ROOT ); |
|
|
if ( commandPaletteRootElement ) { |
|
|
commandPaletteRootElement.removeEventListener( 'keydown', trackCommandPaletteSelected ); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const REDUX_TRACKING = { |
|
|
'jetpack/global-styles': { |
|
|
resetLocalChanges: 'wpcom_global_styles_reset', |
|
|
updateOptions: trackGlobalStyles( 'wpcom_global_styles_update' ), |
|
|
publishOptions: trackGlobalStyles( 'wpcom_global_styles_publish' ), |
|
|
}, |
|
|
|
|
|
'core/editor': { |
|
|
undo: 'wpcom_block_editor_undo_performed', |
|
|
redo: 'wpcom_block_editor_redo_performed', |
|
|
}, |
|
|
|
|
|
core: { |
|
|
undo: 'wpcom_block_editor_undo_performed', |
|
|
redo: 'wpcom_block_editor_redo_performed', |
|
|
saveEntityRecord: trackSaveEntityRecord, |
|
|
editEntityRecord: trackEditEntityRecord, |
|
|
saveEditedEntityRecord: trackSaveEditedEntityRecord, |
|
|
__experimentalSaveSpecifiedEntityEdits: trackSaveSpecifiedEntityEdits, |
|
|
}, |
|
|
'core/block-directory': { |
|
|
installBlockType: trackInstallBlockType, |
|
|
}, |
|
|
'core/block-editor': { |
|
|
moveBlocksUp: getBlocksTracker( 'wpcom_block_moved_up' ), |
|
|
moveBlocksDown: getBlocksTracker( 'wpcom_block_moved_down' ), |
|
|
removeBlocks: trackBlockRemoval, |
|
|
removeBlock: trackBlockRemoval, |
|
|
moveBlocksToPosition: trackBlockDragDrop, |
|
|
insertBlock: trackBlockInsertion, |
|
|
insertBlocks: trackBlockInsertion, |
|
|
replaceBlock: trackBlockReplacement, |
|
|
replaceBlocks: trackBlockReplacement, |
|
|
replaceInnerBlocks: trackInnerBlocksReplacement, |
|
|
}, |
|
|
'core/notices': { |
|
|
createErrorNotice: trackErrorNotices, |
|
|
}, |
|
|
'core/edit-site': { |
|
|
setIsListViewOpened: trackListViewToggle, |
|
|
openNavigationPanelToMenu: trackSiteEditorBrowsingSidebarOpen, |
|
|
addTemplate: trackSiteEditorCreateTemplate, |
|
|
setTemplate: trackSiteEditorChangeTemplate, |
|
|
setTemplatePart: trackSiteEditorChangeTemplatePart, |
|
|
setPage: trackSiteEditorChangeContent, |
|
|
}, |
|
|
'core/edit-post': { |
|
|
setIsListViewOpened: trackListViewToggle, |
|
|
}, |
|
|
'core/interface': { |
|
|
enableComplementaryArea: trackEnableComplementaryArea, |
|
|
disableComplementaryArea: trackDisableComplementaryArea, |
|
|
}, |
|
|
'core/commands': { |
|
|
open: trackCommandPaletteOpen, |
|
|
close: trackCommandPaletteClose, |
|
|
}, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const EVENT_TYPES = [ 'keyup', 'click' ]; |
|
|
|
|
|
|
|
|
|
|
|
const rewrittenActions = {}; |
|
|
const originalActions = {}; |
|
|
|
|
|
if ( |
|
|
undefined === window || |
|
|
undefined === window._currentSiteId || |
|
|
undefined === window._currentSiteType |
|
|
) { |
|
|
debug( 'Skip: No data available.' ); |
|
|
} else { |
|
|
debug( 'registering tracking handlers.' ); |
|
|
|
|
|
use( ( registry ) => ( { |
|
|
dispatch: ( namespace ) => { |
|
|
const namespaceName = typeof namespace === 'object' ? namespace.name : namespace; |
|
|
const actions = registry.dispatch( namespaceName ); |
|
|
const trackers = REDUX_TRACKING[ namespaceName ]; |
|
|
|
|
|
|
|
|
if ( ! rewrittenActions[ namespaceName ] ) { |
|
|
rewrittenActions[ namespaceName ] = {}; |
|
|
} |
|
|
if ( ! originalActions[ namespaceName ] ) { |
|
|
originalActions[ namespaceName ] = {}; |
|
|
} |
|
|
|
|
|
if ( trackers ) { |
|
|
Object.keys( trackers ).forEach( ( actionName ) => { |
|
|
const originalAction = actions[ actionName ]; |
|
|
const tracker = trackers[ actionName ]; |
|
|
|
|
|
if ( ! originalActions[ namespaceName ][ actionName ] ) { |
|
|
|
|
|
originalActions[ namespaceName ][ actionName ] = originalAction; |
|
|
rewrittenActions[ namespaceName ][ actionName ] = ( ...args ) => { |
|
|
debug( 'action "%s" called with %o arguments', actionName, [ ...args ] ); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
if ( typeof tracker === 'string' ) { |
|
|
|
|
|
tracksRecordEvent( tracker ); |
|
|
} else if ( typeof tracker === 'function' ) { |
|
|
|
|
|
tracker( ...args ); |
|
|
} |
|
|
} catch ( err ) { |
|
|
|
|
|
console.error( err ); |
|
|
} |
|
|
return originalAction( ...args ); |
|
|
}; |
|
|
} |
|
|
|
|
|
actions[ actionName ] = rewrittenActions[ namespaceName ][ actionName ]; |
|
|
} ); |
|
|
} |
|
|
return actions; |
|
|
}, |
|
|
} ) ); |
|
|
|
|
|
const delegateNonCaptureListener = ( event ) => { |
|
|
delegateEventTracking( false, event ); |
|
|
}; |
|
|
|
|
|
const delegateCaptureListener = ( event ) => { |
|
|
delegateEventTracking( true, event ); |
|
|
}; |
|
|
|
|
|
|
|
|
registerPlugin( 'wpcom-block-editor-tracking', { |
|
|
render: () => { |
|
|
EVENT_TYPES.forEach( ( eventType ) => { |
|
|
document.addEventListener( eventType, delegateNonCaptureListener ); |
|
|
document.addEventListener( eventType, delegateCaptureListener, true ); |
|
|
} ); |
|
|
return null; |
|
|
}, |
|
|
} ); |
|
|
|
|
|
registerDelegateEventSubscriber( |
|
|
'wpcom-block-editor-template-part-detach-blocks', |
|
|
'before', |
|
|
( mapping, event, target ) => { |
|
|
const item = target.querySelector( '.components-menu-item__item' ); |
|
|
if ( item?.innerText === __( 'Detach blocks from template part' ) ) { |
|
|
ignoreNextReplaceBlocksAction = true; |
|
|
} |
|
|
} |
|
|
); |
|
|
} |
|
|
|