2019-06-24 23:49:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import debug from 'debug';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module variables
|
|
|
|
*/
|
|
|
|
const tracksDebug = debug( 'wc-admin:tracks' );
|
2018-09-17 16:42:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Record an event to Tracks
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {string} eventName The name of the event to record, don't include the wcadmin_ prefix
|
2018-09-17 16:42:04 +00:00
|
|
|
* @param {Object} eventProperties event properties to include in the event
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function recordEvent( eventName, eventProperties ) {
|
2020-05-18 19:35:50 +00:00
|
|
|
tracksDebug( 'recordevent %s %o', 'wcadmin_' + eventName, eventProperties, {
|
|
|
|
_tqk: window._tkq,
|
|
|
|
shouldRecord:
|
|
|
|
process.env.NODE_ENV !== 'development' &&
|
|
|
|
!! window._tkq &&
|
|
|
|
!! window.wcTracks &&
|
|
|
|
!! window.wcTracks.isEnabled,
|
|
|
|
} );
|
2019-06-24 23:49:52 +00:00
|
|
|
|
2019-03-04 04:08:13 +00:00
|
|
|
if (
|
|
|
|
! window.wcTracks ||
|
2020-02-14 02:23:21 +00:00
|
|
|
typeof window.wcTracks.recordEvent !== 'function' ||
|
|
|
|
process.env.NODE_ENV === 'development'
|
2019-03-04 04:08:13 +00:00
|
|
|
) {
|
2018-09-17 16:42:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:08:13 +00:00
|
|
|
window.wcTracks.recordEvent( eventName, eventProperties );
|
2018-09-17 16:42:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 13:32:02 +00:00
|
|
|
const tracksQueue = {
|
2020-02-14 02:23:21 +00:00
|
|
|
localStorageKey() {
|
2019-11-15 13:32:02 +00:00
|
|
|
return 'tracksQueue';
|
|
|
|
},
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
clear() {
|
2019-11-15 13:32:02 +00:00
|
|
|
if ( ! window.localStorage ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.localStorage.removeItem( tracksQueue.localStorageKey() );
|
|
|
|
},
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
get() {
|
2019-11-15 13:32:02 +00:00
|
|
|
if ( ! window.localStorage ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
let items = window.localStorage.getItem(
|
|
|
|
tracksQueue.localStorageKey()
|
|
|
|
);
|
2019-11-15 13:32:02 +00:00
|
|
|
|
|
|
|
items = items ? JSON.parse( items ) : [];
|
|
|
|
items = Array.isArray( items ) ? items : [];
|
|
|
|
|
|
|
|
return items;
|
|
|
|
},
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
add( ...args ) {
|
2019-11-15 13:32:02 +00:00
|
|
|
if ( ! window.localStorage ) {
|
|
|
|
// If unable to queue, run it now.
|
|
|
|
tracksDebug( 'Unable to queue, running now', { args } );
|
|
|
|
recordEvent.apply( null, args || undefined );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let items = tracksQueue.get();
|
|
|
|
const newItem = { args };
|
|
|
|
|
|
|
|
items.push( newItem );
|
|
|
|
items = items.slice( -100 ); // Upper limit.
|
|
|
|
|
|
|
|
tracksDebug( 'Adding new item to queue.', newItem );
|
2020-02-14 02:23:21 +00:00
|
|
|
window.localStorage.setItem(
|
|
|
|
tracksQueue.localStorageKey(),
|
|
|
|
JSON.stringify( items )
|
|
|
|
);
|
2019-11-15 13:32:02 +00:00
|
|
|
},
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
process() {
|
2019-11-15 13:32:02 +00:00
|
|
|
if ( ! window.localStorage ) {
|
|
|
|
return; // Not possible.
|
|
|
|
}
|
|
|
|
|
|
|
|
const items = tracksQueue.get();
|
|
|
|
tracksQueue.clear();
|
|
|
|
|
|
|
|
tracksDebug( 'Processing items in queue.', items );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
items.forEach( ( item ) => {
|
|
|
|
if ( typeof item === 'object' ) {
|
2019-11-15 13:32:02 +00:00
|
|
|
tracksDebug( 'Processing item in queue.', item );
|
|
|
|
recordEvent.apply( null, item.args || undefined );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue a tracks event.
|
|
|
|
*
|
|
|
|
* This allows you to delay tracks events that would otherwise cause a race condition.
|
|
|
|
* For example, when we trigger `wcadmin_tasklist_appearance_continue_setup` we're simultaneously moving the user to a new page via
|
|
|
|
* `window.location`. This is an example of a race condition that should be avoided by enqueueing the event,
|
|
|
|
* and therefore running it on the next pageview.
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {string} eventName The name of the event to record, don't include the wcadmin_ prefix
|
2019-11-15 13:32:02 +00:00
|
|
|
* @param {Object} eventProperties event properties to include in the event
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function queueRecordEvent( eventName, eventProperties ) {
|
|
|
|
tracksQueue.add( eventName, eventProperties );
|
|
|
|
}
|
|
|
|
|
2018-09-17 16:42:04 +00:00
|
|
|
/**
|
|
|
|
* Record a page view to Tracks
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {string} path the page/path to record a page view for
|
2019-11-15 13:32:02 +00:00
|
|
|
* @param {Object} extraProperties extra event properties to include in the event
|
2018-09-17 16:42:04 +00:00
|
|
|
*/
|
|
|
|
|
2019-11-15 13:32:02 +00:00
|
|
|
export function recordPageView( path, extraProperties ) {
|
2018-09-17 16:42:04 +00:00
|
|
|
if ( ! path ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-15 13:32:02 +00:00
|
|
|
|
|
|
|
recordEvent( 'page_view', { path, ...extraProperties } );
|
|
|
|
|
|
|
|
// Process queue.
|
|
|
|
tracksQueue.process();
|
2018-09-17 16:42:04 +00:00
|
|
|
}
|