import { withQuery } from 'ufo' ; import { UID , send } from '../utils' ; const googleAnalyticsURL = 'https://www.google-analytics.com/collect' ; export interface Options { debug : boolean ; eventCategory : string ; id : string ; } export function sendToAnalytics ( { fullPath , href } , metric , options ) { const opts = { ec : options . eventCategory , ea : metric . name as string , el : metric . id as string , ev : parseInt ( metric . delta ) + '' , dl : fullPath as string , dh : href as string , ni : 'true' } ; if ( metric . name === 'TTFB' ) { opts . ev = parseInt ( metric . delta - metric . entries [ 0 ] . requestStart ) ; } const url = withQuery ( googleAnalyticsURL , { v : '1' , t : 'event' , tid : options . id , cid : UID , ... opts , z : Date . now ( ) + '' } ) ; send ( url ) ; }