@@ -48,14 +48,15 @@ let entity: string | null = null;
4848let referrer : string | null = null ;
4949
5050if ( typeof document !== "undefined" ) {
51- scriptEl = document . currentScript as HTMLScriptElement ;
51+ scriptEl = document . querySelector ( `script[src^=" ${ import . meta . url } "]` ) ;
5252 endpoint = scriptEl ?. getAttribute ( "data-api" ) || ( scriptEl && `${ new URL ( scriptEl . src ) . origin } /api/event` ) ;
5353 entity = scriptEl ?. getAttribute ( "data-entity" ) || null ;
5454 referrer = document . referrer ;
5555}
5656
5757const LOCALHOST_REGEX = / ^ l o c a l h o s t $ | ^ 1 2 7 ( \. [ 0 - 9 ] + ) { 0 , 2 } \. [ 0 - 9 ] + $ | ^ \[ : : 1 ? \] $ / ;
58- const ignoreEvent = ( reason : string ) => console . info ( `[liwan]: ignoring event: ${ reason } ` ) ;
58+ const log = ( message : string ) => console . info ( `[liwan]: ${ message } ` ) ;
59+ const ignore = ( reason : string ) => log ( `Ignoring event: ${ reason } ` ) ;
5960
6061/**
6162 * Sends an event to the Liwan API.
@@ -80,10 +81,9 @@ const ignoreEvent = (reason: string) => console.info(`[liwan]: ignoring event: $
8081export async function event ( name = "pageview" , options ?: EventOptions ) {
8182 if ( typeof window === "undefined" && ! options ?. endpoint )
8283 return Promise . reject ( new Error ( "endpoint is required in server-side environments" ) ) ;
83- if ( typeof localStorage !== "undefined" && localStorage . getItem ( "disable-liwan" ) )
84- return ignoreEvent ( "localStorage flag" ) ;
85- if ( LOCALHOST_REGEX . test ( location . hostname ) || location . protocol === "file:" ) return ignoreEvent ( "localhost" ) ;
86- if ( ! endpoint && ! options ?. endpoint ) return ignoreEvent ( "no endpoint" ) ;
84+ if ( typeof localStorage !== "undefined" && localStorage . getItem ( "disable-liwan" ) ) return ignore ( "localStorage flag" ) ;
85+ if ( LOCALHOST_REGEX . test ( location . hostname ) || location . protocol === "file:" ) return ignore ( "localhost" ) ;
86+ if ( ! endpoint && ! options ?. endpoint ) return ignore ( "no endpoint" ) ;
8787
8888 // biome-ignore lint/style/noNonNullAssertion: we know that endpoint is not null
8989 return fetch ( ( options ?. endpoint || endpoint ) ! , {
@@ -96,7 +96,7 @@ export async function event(name = "pageview", options?: EventOptions) {
9696 url : options ?. url || `${ location . origin } ${ location . pathname } ` ,
9797 } ) ,
9898 } ) . then ( ( response ) => {
99- if ( ! response . ok ) console . error ( "[liwan]: failed to send event: " , response ) ;
99+ if ( ! response . ok ) log ( `Failed to send event: ${ response . statusText } ` ) ;
100100 return { status : response . status } ;
101101 } ) ;
102102}
0 commit comments