@@ -49,17 +49,20 @@ let scriptEl: HTMLScriptElement | null = null;
4949let endpoint : string | null = null ;
5050let entity : string | null = null ;
5151let referrer : string | null = null ;
52+ const noWindow = typeof window === "undefined" ;
5253
5354if ( typeof document !== "undefined" ) {
5455 scriptEl = document . querySelector ( `script[src^="${ import . meta. url } "]` ) ;
55- endpoint = scriptEl ?. getAttribute ( "data-api" ) || ( scriptEl && ` ${ new URL ( scriptEl . src ) . origin } /api/event` ) ;
56+ endpoint = scriptEl ?. getAttribute ( "data-api" ) || ( scriptEl && new URL ( scriptEl . src ) . origin + " /api/event" ) ;
5657 entity = scriptEl ?. getAttribute ( "data-entity" ) || null ;
5758 referrer = document . referrer ;
5859}
5960
60- const LOCALHOST_REGEX = / ^ l o c a l h o s t $ | ^ 1 2 7 ( \. [ 0 - 9 ] + ) { 0 , 2 } \. [ 0 - 9 ] + $ | ^ \[ : : 1 ? \] $ / ;
61- const log = ( message : string ) => console . info ( `[liwan]: ${ message } ` ) ;
62- const ignore = ( reason : string ) => log ( `Ignoring event: ${ reason } ` ) ;
61+ const log = ( message : string ) => console . info ( "[liwan]: " + message ) ;
62+ const ignore = ( reason : string ) => log ( "Ignoring event: " + reason ) ;
63+ const reject = ( message : string ) => {
64+ throw new Error ( "Failed to send event: " + message ) ;
65+ } ;
6366
6467/**
6568 * Sends an event to the Liwan API.
@@ -82,36 +85,33 @@ const ignore = (reason: string) => log(`Ignoring event: ${reason}`);
8285 * ```
8386 */
8487export async function event ( name = "pageview" , options ?: EventOptions ) : Promise < void > {
85- if ( typeof window === "undefined" && ! options ?. endpoint )
86- return Promise . reject ( new Error ( "endpoint is required in server-side environments" ) ) ;
88+ const endpoint_url = options ?. endpoint || endpoint ;
89+ if ( ! endpoint_url ) return reject ( "endpoint is required" ) ;
8790 if ( typeof localStorage !== "undefined" && localStorage . getItem ( "disable-liwan" ) ) return ignore ( "localStorage flag" ) ;
88- if ( LOCALHOST_REGEX . test ( location . hostname ) || location . protocol === "file:" ) return ignore ( "localhost" ) ;
89- if ( ! endpoint && ! options ?. endpoint ) return ignore ( "no endpoint" ) ;
90- const utm = new URLSearchParams ( location . search ) ;
91+ if ( / ^ l o c a l h o s t $ | ^ 1 2 7 (?: \. \d + ) { 0 , 2 } \. \d + $ | ^ \[ : : 1 ? \] $ / . test ( location . hostname ) || location . protocol === "file:" )
92+ return ignore ( "localhost" ) ;
9193
92- // biome-ignore lint/style/noNonNullAssertion: we know that endpoint is not null
93- return fetch ( ( options ?. endpoint || endpoint ) ! , {
94+ const response = await fetch ( endpoint_url , {
9495 method : "POST" ,
9596 headers : { "Content-Type" : "application/json" } ,
9697 body : JSON . stringify ( < Payload > {
97- entity_id : options ?. entity || entity ,
9898 name,
99+ entity_id : options ?. entity || entity ,
99100 referrer : options ?. referrer || referrer ,
100- url : options ?. url || ` ${ location . origin } ${ location . pathname } ` ,
101+ url : options ?. url || location . origin + location . pathname ,
101102 utm : {
102- campaign : utm . get ( "utm_campaign" ) ,
103- content : utm . get ( "utm_content" ) ,
104- medium : utm . get ( "utm_medium" ) ,
105- source : utm . get ( "utm_source" ) ,
106- term : utm . get ( "utm_term" ) ,
103+ ...[ "campaign" , "content" , "medium" , "source" , "term" ] . map ( ( v ) => [
104+ v ,
105+ new URLSearchParams ( location . search ) . get ( "utm_" + v ) ,
106+ ] ) ,
107107 } ,
108108 } ) ,
109- } ) . then ( ( response ) => {
110- if ( ! response . ok ) {
111- log ( `Failed to send event: ${ response . statusText } ` ) ;
112- return Promise . reject ( new Error ( `Failed to send event: ${ response . statusText } ` ) ) ;
113- }
114109 } ) ;
110+
111+ if ( ! response . ok ) {
112+ log ( "Failed to send event: " + response . statusText ) ;
113+ reject ( response . statusText ) ;
114+ }
115115}
116116
117117const trackPageviews = ( ) => {
@@ -150,6 +150,6 @@ const trackPageviews = () => {
150150 page ( ) ;
151151} ;
152152
153- if ( typeof window !== "undefined" && ! window . __liwan_loaded && scriptEl ) {
153+ if ( ! noWindow && ! window . __liwan_loaded && scriptEl ) {
154154 trackPageviews ( ) ;
155155}
0 commit comments