2019-06-24 23:49:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import debug from 'debug';
|
|
|
|
|
2022-09-01 16:34:42 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { isDevelopmentMode } from './utils';
|
|
|
|
|
2019-06-24 23:49:52 +00:00
|
|
|
/**
|
|
|
|
* Module variables
|
|
|
|
*/
|
|
|
|
const tracksDebug = debug( 'wc-admin:tracks' );
|
2018-09-17 16:42:04 +00:00
|
|
|
|
2022-05-10 07:11:10 +00:00
|
|
|
export type ExtraProperties = {
|
|
|
|
[ key: string ]: unknown;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isRecordEventArgs = (
|
|
|
|
args: unknown[]
|
|
|
|
): args is [ string, ExtraProperties | undefined ] => {
|
|
|
|
return args.length === 2 && typeof args[ 0 ] === 'string';
|
|
|
|
};
|
|
|
|
|
2018-09-17 16:42:04 +00:00
|
|
|
/**
|
|
|
|
* Record an event to Tracks
|
|
|
|
*
|
2022-03-18 11:45:14 +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
|
|
|
|
*/
|
2022-05-10 07:11:10 +00:00
|
|
|
export function recordEvent(
|
|
|
|
eventName: string,
|
|
|
|
eventProperties?: ExtraProperties
|
|
|
|
) {
|
2020-05-18 19:35:50 +00:00
|
|
|
tracksDebug( 'recordevent %s %o', 'wcadmin_' + eventName, eventProperties, {
|
|
|
|
_tqk: window._tkq,
|
|
|
|
shouldRecord:
|
2022-09-01 16:34:42 +00:00
|
|
|
! isDevelopmentMode &&
|
2020-05-18 19:35:50 +00:00
|
|
|
!! 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 ||
|
2022-09-01 16:34:42 +00:00
|
|
|
typeof window.wcTracks.recordEvent !== 'function'
|
2019-03-04 04:08:13 +00:00
|
|
|
) {
|
2018-09-17 16:42:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:34:42 +00:00
|
|
|
if ( isDevelopmentMode ) {
|
|
|
|
window.wcTracks.validateEvent( eventName, eventProperties );
|
|
|
|
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 [];
|
|
|
|
}
|
|
|
|
|
2022-05-10 07:11:10 +00:00
|
|
|
const items = window.localStorage.getItem(
|
2020-02-14 02:23:21 +00:00
|
|
|
tracksQueue.localStorageKey()
|
|
|
|
);
|
2022-05-10 07:11:10 +00:00
|
|
|
const parsedJSONItems = items ? JSON.parse( items ) : [];
|
|
|
|
const arrayItems = Array.isArray( parsedJSONItems )
|
|
|
|
? parsedJSONItems
|
|
|
|
: [];
|
2019-11-15 13:32:02 +00:00
|
|
|
|
2022-05-10 07:11:10 +00:00
|
|
|
return arrayItems;
|
2019-11-15 13:32:02 +00:00
|
|
|
},
|
|
|
|
|
2022-05-10 07:11:10 +00:00
|
|
|
add( ...args: unknown[] ) {
|
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 } );
|
2022-05-10 07:11:10 +00:00
|
|
|
if ( isRecordEventArgs( args ) ) {
|
|
|
|
recordEvent( ...args );
|
|
|
|
} else {
|
|
|
|
tracksDebug( 'Invalid args', args );
|
|
|
|
}
|
2019-11-15 13:32:02 +00:00
|
|
|
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 );
|
2022-05-10 07:11:10 +00:00
|
|
|
const args = item.args;
|
|
|
|
if ( isRecordEventArgs( args ) ) {
|
|
|
|
recordEvent( ...args );
|
|
|
|
} else {
|
|
|
|
tracksDebug( 'Invalid item args', item.args );
|
|
|
|
}
|
2019-11-15 13:32:02 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2022-03-18 11:45:14 +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
|
|
|
|
*/
|
|
|
|
|
2022-05-10 07:11:10 +00:00
|
|
|
export function queueRecordEvent(
|
|
|
|
eventName: string,
|
|
|
|
eventProperties?: ExtraProperties
|
|
|
|
) {
|
2019-11-15 13:32:02 +00:00
|
|
|
tracksQueue.add( eventName, eventProperties );
|
|
|
|
}
|
|
|
|
|
2018-09-17 16:42:04 +00:00
|
|
|
/**
|
|
|
|
* Record a page view to Tracks
|
|
|
|
*
|
2022-03-18 11:45:14 +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
|
|
|
*/
|
|
|
|
|
2022-05-10 07:11:10 +00:00
|
|
|
export function recordPageView(
|
|
|
|
path: string,
|
|
|
|
extraProperties?: 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
|
|
|
}
|