const connectionStr = process . env . NEXT_PUBLIC_UMAMI_URL ; let connection ; if ( typeof window !== "undefined" && connectionStr ) { const url = new URL ( connectionStr , new URL ( window . location . href ) ) ; connection = { collectUrl : new URL ( "/api/collect" , url ) . href , websiteId : url . pathname . substring ( 1 ) , } ; } /** Collects an arbitrary Umami metric. */ export function collect ( type , data ) { if ( ! process . browser ) { return ; } const request = { type , payload : { website : connection ?. websiteId , hostname : window . location . hostname , screen : `${ window . screen . width }x${ window . screen . height }` , language : navigator . language , cache : sessionStorage . getItem ( "umami.cache" ) || undefined , ... data , } , } ; if ( process . env . NODE_ENV === "development" ) { console . log ( "umami" , type , request ) ; } if ( ! connection ) { return ; } ( async ( ) => { try { const response = await fetch ( connection . collectUrl , { method : "POST" , headers : { "Content-Type" : "application/json" } , body : JSON . stringify ( request ) , } ) ; sessionStorage . setItem ( "umami.cache" , await response . text ( ) ) ; } catch { } } ) ( ) ; } let currentPath ; /** Collects a page view metric. */ export function trackView ( path , referrer = document . referrer ) { collect ( "pageview" , { url : ( currentPath = path ) , referrer , } ) ; } /** Collects an event metric for the current page. */ export function trackEvent ( type , value ) { if ( ! currentPath ) { return ; } collect ( "event" , { url : currentPath , event_type : type . replace ( " " , "-" ) . toLowerCase ( ) , event_value : value , } ) ; }