Skip to content

Commit

Permalink
Add gla_table_sort track event to AppTableCard,
Browse files Browse the repository at this point in the history
as requested in #96.
  • Loading branch information
tomalec committed Jan 11, 2021
1 parent 024f58e commit 3ea2fcb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
37 changes: 29 additions & 8 deletions js/src/components/app-table-card/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,43 @@ import { TableCard } from '@woocommerce/components';
* Internal dependencies
*/
import recordColumnToggleEvent from './recordColumnToggleEvent';
import { recordTableSortEvent } from '../../utils/recordEvent';
import './index.scss';

const AppTableCard = ( props ) => {
const { trackEventReportId, onColumnsChange = () => {}, ...rest } = props;
const { trackEventReportId, ...rest } = props;
/**
* Returns a function that records a track event before executing an original handler.
*
* @param {String} handlerName The name of the event handler.
* @param {Function} recordEvent The function to record the event.
*
* @returns {decorateHandlerWithTrackEvent~decoratedHandler} Decorated handler.
*/
const decorateHandlerWithTrackEvent = ( handlerName, recordEvent ) => {
/**
* Records track event with specified `trackEventReportId` and any args given,
* then calls original handler if any.
*/
return function decoratedHandler( ...args ) {
if ( trackEventReportId ) {
recordEvent( trackEventReportId, ...args );
}

const handleColumnsChange = ( shown, toggled ) => {
if ( trackEventReportId ) {
recordColumnToggleEvent( trackEventReportId, shown, toggled );
// Call the original handler if given.
const handler = props.handlerName;
if( handler ){
handler( ...args );
}
}

onColumnsChange( shown, toggled );
};
}

return (
<div className="app-table-card">
<TableCard onColumnsChange={ handleColumnsChange } { ...rest } />
<TableCard
onColumnsChange={ decorateHandlerWithTrackEvent( 'onColumnsChange', recordColumnToggleEvent) }
onSort={ decorateHandlerWithTrackEvent( 'onSort', recordTableSortEvent) }
{ ...rest } />
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion js/src/reports/compare-programs-table-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const CompareProgramsTableCard = () => {

return (
<AppTableCard
trackEventReportId="all-programs"
trackEventReportId="compare-programs"
className="gla-all-programs-table-card"
title={
<>
Expand Down
4 changes: 4 additions & 0 deletions js/src/utils/recordEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const recordTableHeaderToggleEvent = ( report, column, status ) => {
} );
};

export const recordTableSortEvent = ( report, column, direction ) => {
recordEvent( 'gla_table_sort', { report, column, direction } );
};

export const recordSetupMCEvent = ( target, trigger = 'click' ) => {
recordEvent( 'gla_setup_mc', {
target,
Expand Down

0 comments on commit 3ea2fcb

Please sign in to comment.