2023-10-24 10:05:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
|
|
|
|
interface MarketplaceViewProps {
|
2023-10-24 12:54:31 +00:00
|
|
|
view?: string;
|
2023-10-24 10:05:25 +00:00
|
|
|
search_term?: string;
|
|
|
|
product_type?: string;
|
|
|
|
category?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record a marketplace view event.
|
|
|
|
* This is a new event that is easier to understand and implement consistently
|
|
|
|
*/
|
|
|
|
function recordMarketplaceView( props: MarketplaceViewProps ) {
|
2023-10-24 12:54:31 +00:00
|
|
|
// The category prop changes to a blank string on first viewing all products after a search.
|
2023-10-25 08:44:57 +00:00
|
|
|
// This is undesirable and causes a duplicate event that will artificially inflate event counts.
|
2023-10-24 12:54:31 +00:00
|
|
|
if ( props.category === '' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-25 08:44:57 +00:00
|
|
|
const view = props.view || 'discover';
|
2023-10-24 12:54:31 +00:00
|
|
|
const search_term = props.search_term || null;
|
|
|
|
const product_type = props.product_type || null;
|
|
|
|
const category = props.category || null;
|
|
|
|
|
|
|
|
const eventProps = {
|
|
|
|
...( view && { view } ),
|
|
|
|
...( search_term && { search_term } ),
|
|
|
|
...( product_type && { product_type } ),
|
|
|
|
...( category && { category } ),
|
|
|
|
};
|
|
|
|
|
2024-05-15 12:43:06 +00:00
|
|
|
// User sees the default extensions, themes or business services view
|
|
|
|
if (
|
|
|
|
view &&
|
|
|
|
[ 'extensions', 'themes', 'business-services' ].includes( view ) &&
|
|
|
|
! category
|
|
|
|
) {
|
2023-10-25 08:44:57 +00:00
|
|
|
eventProps.category = '_all';
|
|
|
|
}
|
|
|
|
|
|
|
|
// User clicks the `View All` button on search results
|
|
|
|
if ( view && view === 'search' && product_type && ! category ) {
|
2023-10-24 12:54:31 +00:00
|
|
|
eventProps.category = '_all';
|
|
|
|
}
|
2023-10-24 10:05:25 +00:00
|
|
|
|
|
|
|
recordEvent( 'marketplace_view', eventProps );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure we still have legacy events in place
|
|
|
|
* the "view" prop maps to a "section" prop in the event for compatibility with old funnels.
|
|
|
|
*
|
|
|
|
* @param props The props object containing view, search_term, section, and category.
|
|
|
|
*/
|
2023-10-24 12:54:31 +00:00
|
|
|
function recordLegacyTabView( props: MarketplaceViewProps ) {
|
2023-10-25 08:44:57 +00:00
|
|
|
// product_type will artificially inflate legacy event counts.
|
|
|
|
if ( props.product_type ) {
|
2023-10-24 12:54:31 +00:00
|
|
|
return;
|
2023-10-24 10:05:25 +00:00
|
|
|
}
|
2023-10-24 12:54:31 +00:00
|
|
|
let oldEventName = 'extensions_view';
|
2023-10-25 08:44:57 +00:00
|
|
|
const view = props.view || '_featured';
|
2023-10-24 12:54:31 +00:00
|
|
|
const search_term = props.search_term || null;
|
|
|
|
const category = props.category || null;
|
2023-10-24 10:05:25 +00:00
|
|
|
|
2023-10-24 12:54:31 +00:00
|
|
|
const oldEventProps = {
|
2023-10-25 08:44:57 +00:00
|
|
|
// legacy event refers to "section" instead of "view"
|
|
|
|
...( view && { section: view } ),
|
2023-10-24 12:54:31 +00:00
|
|
|
...( search_term && { search_term } ),
|
|
|
|
version: '2',
|
|
|
|
};
|
2023-10-24 10:05:25 +00:00
|
|
|
|
2023-10-25 08:44:57 +00:00
|
|
|
switch ( view ) {
|
2023-10-24 12:54:31 +00:00
|
|
|
case 'extensions':
|
|
|
|
oldEventProps.section = category || '_all';
|
|
|
|
break;
|
|
|
|
case 'themes':
|
|
|
|
oldEventProps.section = 'themes';
|
|
|
|
break;
|
|
|
|
case 'search':
|
|
|
|
oldEventName = 'extensions_view_search';
|
2023-10-25 08:44:57 +00:00
|
|
|
oldEventProps.section = view;
|
2023-10-24 12:54:31 +00:00
|
|
|
oldEventProps.search_term = search_term || '';
|
|
|
|
break;
|
|
|
|
case 'my-subscriptions':
|
|
|
|
oldEventName = 'subscriptions_view';
|
|
|
|
oldEventProps.section = 'helper';
|
|
|
|
break;
|
2023-10-24 10:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
recordEvent( oldEventName, oldEventProps );
|
|
|
|
}
|
|
|
|
|
|
|
|
export { recordMarketplaceView, recordLegacyTabView };
|