@@ -145,22 +145,41 @@ export class Databuddy {
145145 return { success : true } ;
146146 }
147147
148+ /**
149+ * Convert BatchEventInput to CustomEventSpan format expected by /events endpoint
150+ */
151+ private toCustomEventSpan ( event : BatchEventInput ) {
152+ const timestamp = event . timestamp
153+ ? Math . floor ( event . timestamp )
154+ : Date . now ( ) ;
155+
156+ return {
157+ timestamp,
158+ path : "" ,
159+ eventName : event . name ,
160+ anonymousId : event . anonymousId ?? null ,
161+ sessionId : event . sessionId ?? null ,
162+ properties : event . properties ?? null ,
163+ } ;
164+ }
165+
148166 private async send ( event : BatchEventInput ) : Promise < EventResponse > {
149167 try {
150- const url = `${ this . apiUrl } /?client_id=${ encodeURIComponent ( this . clientId ) } ` ;
168+ const url = `${ this . apiUrl } /events?client_id=${ encodeURIComponent ( this . clientId ) } ` ;
169+ const customEventSpan = this . toCustomEventSpan ( event ) ;
151170
152171 this . logger . info ( "📤 SENDING SINGLE EVENT:" , {
153- name : event . name ,
154- properties : JSON . stringify ( event . properties , null , 2 ) ,
155- propertiesCount : Object . keys ( event . properties || { } ) . length ,
172+ eventName : customEventSpan . eventName ,
173+ properties : JSON . stringify ( customEventSpan . properties , null , 2 ) ,
174+ propertiesCount : Object . keys ( customEventSpan . properties || { } ) . length ,
156175 } ) ;
157176
158177 const response = await fetch ( url , {
159178 method : "POST" ,
160179 headers : {
161180 "Content-Type" : "application/json" ,
162181 } ,
163- body : JSON . stringify ( event ) ,
182+ body : JSON . stringify ( [ customEventSpan ] ) ,
164183 } ) ;
165184
166185 if ( ! response . ok ) {
@@ -329,18 +348,21 @@ export class Databuddy {
329348 }
330349
331350 try {
332- const url = `${ this . apiUrl } /batch?client_id=${ encodeURIComponent ( this . clientId ) } ` ;
351+ const url = `${ this . apiUrl } /events?client_id=${ encodeURIComponent ( this . clientId ) } ` ;
352+ const customEventSpans = processedEvents . map ( ( event ) =>
353+ this . toCustomEventSpan ( event )
354+ ) ;
333355
334356 this . logger . info ( "📦 SENDING BATCH EVENTS:" , {
335- count : processedEvents . length ,
336- firstEventName : processedEvents [ 0 ] ?. name ,
357+ count : customEventSpans . length ,
358+ firstEventName : customEventSpans [ 0 ] ?. eventName ,
337359 firstEventProperties : JSON . stringify (
338- processedEvents [ 0 ] ?. properties ,
360+ customEventSpans [ 0 ] ?. properties ,
339361 null ,
340362 2
341363 ) ,
342364 firstEventPropertiesCount : Object . keys (
343- processedEvents [ 0 ] ?. properties || { }
365+ customEventSpans [ 0 ] ?. properties || { }
344366 ) . length ,
345367 } ) ;
346368
@@ -349,7 +371,7 @@ export class Databuddy {
349371 headers : {
350372 "Content-Type" : "application/json" ,
351373 } ,
352- body : JSON . stringify ( processedEvents ) ,
374+ body : JSON . stringify ( customEventSpans ) ,
353375 } ) ;
354376
355377 if ( ! response . ok ) {
0 commit comments