type EventTypes = 'click' | 'copy-to-clipboard' | 'edit-preset' ; see const transport = ( ( ) => { if ( typeof navigator . sendBeacon === 'function' ) { return 'beacon' ; } return 'image' ; } ) ( ) ; see export interface CustomDimensions { href ? : string ; } export interface Event extends CustomDimensions { action ? : EventTypes ; category ? : string ; label ? : string ; value ? : number ; } export const getCategoryForPage = ( ) => { const { pathname } = window . location ; if ( pathname === '/' ) { return 'editor' ; } return pathname . split ( '/' ) [ 1 ] ; } ; export const track = ( event ) => { const { action = 'click' , category = getCategoryForPage ( ) , label , value , ... rest } = event ; const params = { event_category : category , transport_type : transport , ... ( label && { event_label : label } ) , ... ( typeof value !== 'undefined' && Number . isInteger ( value ) && { value } ) , ... rest , } ; if ( process . env . NODE_ENV === 'production' ) { window . gtag ( 'event' , action , params ) ; } else { console . groupCollapsed ( action ) ; console . table ( params ) ; console . groupEnd ( ) ; } } ; export const trackClickExternalLink = ( e ) => { const element = e . target as HTMLAnchorElement ; if ( element && element . tagName === 'A' ) { track ( { label : element . innerText , href : element . href , } ) ; } else if ( process . env . NODE_ENV !== 'production' ) { console . warn ( 'trackClickExternalLink() called on non-anchor element.' ) ; } } ;