@@ -246,34 +246,20 @@ export async function insertCustomEventSpans(
246246 }
247247
248248 const now = Date . now ( ) ;
249- const spans : CustomEventSpan [ ] = [ ] ;
250-
251- for ( const event of events ) {
252- // Dedup by client+session+path+eventName+timestamp
253- const dedupKey = `custom_${ clientId } _${ event . sessionId } _${ event . path } _${ event . eventName } _${ event . timestamp } ` ;
254- const isDuplicate = await checkDuplicate ( dedupKey , "custom" ) ;
255- if ( isDuplicate ) {
256- continue ;
257- }
258-
259- const span : CustomEventSpan = {
260- client_id : clientId ,
261- session_id : validateSessionId ( event . sessionId ) ,
262- timestamp : typeof event . timestamp === "number" ? event . timestamp : now ,
263- path : sanitizeString ( event . path , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
264- event_name : sanitizeString ( event . eventName , VALIDATION_LIMITS . SHORT_STRING_MAX_LENGTH ) ,
265- properties : ( event . properties as Record < string , unknown > ) ?? { } ,
266- } ;
267-
268- spans . push ( span ) ;
269- }
249+ const spans : CustomEventSpan [ ] = events . map ( ( event ) => ( {
250+ client_id : clientId ,
251+ anonymous_id : sanitizeString ( event . anonymousId , VALIDATION_LIMITS . SHORT_STRING_MAX_LENGTH ) ,
252+ session_id : validateSessionId ( event . sessionId ) ,
253+ timestamp : typeof event . timestamp === "number" ? event . timestamp : now ,
254+ path : sanitizeString ( event . path , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
255+ event_name : sanitizeString ( event . eventName , VALIDATION_LIMITS . SHORT_STRING_MAX_LENGTH ) ,
256+ properties : ( event . properties as Record < string , unknown > ) ?? { } ,
257+ } ) ) ;
270258
271- if ( spans . length > 0 ) {
272- try {
273- await sendEventBatch ( "analytics-custom-event-spans" , spans ) ;
274- } catch ( error ) {
275- captureError ( error , { count : spans . length } ) ;
276- }
259+ try {
260+ await sendEventBatch ( "analytics-custom-event-spans" , spans ) ;
261+ } catch ( error ) {
262+ captureError ( error , { count : spans . length } ) ;
277263 }
278264}
279265
@@ -530,39 +516,24 @@ export async function insertErrorSpans(
530516 }
531517
532518 const now = Date . now ( ) ;
533- const spans : ErrorSpanRow [ ] = [ ] ;
534-
535- for ( const error of errors ) {
536- // Use message hash as dedup key
537- const dedupKey = `error_${ clientId } _${ error . message . slice ( 0 , 50 ) } _${ error . path } ` ;
538- const isDuplicate = await checkDuplicate ( dedupKey , "error" ) ;
539- if ( isDuplicate ) {
540- continue ;
541- }
542-
543- const errorSpan : ErrorSpanRow = {
544- client_id : clientId ,
545- anonymous_id : sanitizeString ( error . anonymousId , VALIDATION_LIMITS . SHORT_STRING_MAX_LENGTH ) ,
546- session_id : validateSessionId ( error . sessionId ) ,
547- timestamp : typeof error . timestamp === "number" ? error . timestamp : now ,
548- path : sanitizeString ( error . path , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
549- message : sanitizeString ( error . message , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
550- filename : sanitizeString ( error . filename , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
551- lineno : error . lineno ?? undefined ,
552- colno : error . colno ?? undefined ,
553- stack : sanitizeString ( error . stack , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
554- error_type : sanitizeString ( error . errorType , VALIDATION_LIMITS . SHORT_STRING_MAX_LENGTH ) || "Error" ,
555- } ;
556-
557- spans . push ( errorSpan ) ;
558- }
519+ const spans : ErrorSpanRow [ ] = errors . map ( ( error ) => ( {
520+ client_id : clientId ,
521+ anonymous_id : sanitizeString ( error . anonymousId , VALIDATION_LIMITS . SHORT_STRING_MAX_LENGTH ) ,
522+ session_id : validateSessionId ( error . sessionId ) ,
523+ timestamp : typeof error . timestamp === "number" ? error . timestamp : now ,
524+ path : sanitizeString ( error . path , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
525+ message : sanitizeString ( error . message , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
526+ filename : sanitizeString ( error . filename , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
527+ lineno : error . lineno ?? undefined ,
528+ colno : error . colno ?? undefined ,
529+ stack : sanitizeString ( error . stack , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
530+ error_type : sanitizeString ( error . errorType , VALIDATION_LIMITS . SHORT_STRING_MAX_LENGTH ) || "Error" ,
531+ } ) ) ;
559532
560- if ( spans . length > 0 ) {
561- try {
562- await sendEventBatch ( "analytics-error-spans" , spans ) ;
563- } catch ( error ) {
564- captureError ( error , { count : spans . length } ) ;
565- }
533+ try {
534+ await sendEventBatch ( "analytics-error-spans" , spans ) ;
535+ } catch ( error ) {
536+ captureError ( error , { count : spans . length } ) ;
566537 }
567538}
568539
@@ -602,34 +573,20 @@ export async function insertIndividualVitals(
602573 }
603574
604575 const now = Date . now ( ) ;
605- const spans : WebVitalsSpan [ ] = [ ] ;
606-
607- for ( const vital of vitals ) {
608- // Dedup by client+session+path+metric
609- const dedupKey = `vital_${ clientId } _${ vital . sessionId } _${ vital . path } _${ vital . metricName } ` ;
610- const isDuplicate = await checkDuplicate ( dedupKey , "web_vitals" ) ;
611- if ( isDuplicate ) {
612- continue ;
613- }
614-
615- const span : WebVitalsSpan = {
616- client_id : clientId ,
617- session_id : validateSessionId ( vital . sessionId ) ,
618- timestamp : typeof vital . timestamp === "number" ? vital . timestamp : now ,
619- path : sanitizeString ( vital . path , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
620- metric_name : vital . metricName ,
621- metric_value : vital . metricValue ,
622- } ;
623-
624- spans . push ( span ) ;
625- }
576+ const spans : WebVitalsSpan [ ] = vitals . map ( ( vital ) => ( {
577+ client_id : clientId ,
578+ anonymous_id : sanitizeString ( vital . anonymousId , VALIDATION_LIMITS . SHORT_STRING_MAX_LENGTH ) ,
579+ session_id : validateSessionId ( vital . sessionId ) ,
580+ timestamp : typeof vital . timestamp === "number" ? vital . timestamp : now ,
581+ path : sanitizeString ( vital . path , VALIDATION_LIMITS . STRING_MAX_LENGTH ) ,
582+ metric_name : vital . metricName ,
583+ metric_value : vital . metricValue ,
584+ } ) ) ;
626585
627- if ( spans . length > 0 ) {
628- try {
629- await sendEventBatch ( "analytics-vitals-spans" , spans ) ;
630- } catch ( error ) {
631- captureError ( error , { count : spans . length } ) ;
632- }
586+ try {
587+ await sendEventBatch ( "analytics-vitals-spans" , spans ) ;
588+ } catch ( error ) {
589+ captureError ( error , { count : spans . length } ) ;
633590 }
634591}
635592
0 commit comments