import { TrackEventType , generateMapTransform , } from '@segment/analytics-react-native' ; import { AppEventsLogger } from 'react-native-fbsdk-next' ; import { mapEventProps , transformMap } from './parameterMapping' ; const MAX_CHARACTERS_EVENT_NAME = 40 ; const mappedPropNames = generateMapTransform ( mapEventProps , transformMap ) ; const sanitizeName = ( name ) => { let sanitizedName = name . replace ( '.' , '_' ) ; return sanitizedName . substring ( 0 , MAX_CHARACTERS_EVENT_NAME ) ; } ; const sanitizeEvent = ( event ) : { [ key ] : string | number ; } => { let products = event . properties . products ?? [ ] ; const productCount = ( event . properties . fb_num_items || products . length ) ?? 0 ; let params = { } ; let logTime = event . timestamp ?? undefined ; Object . keys ( event . properties ) . forEach ( ( property ) => { if ( Object . values ( mapEventProps ) . some ( ( fbProp ) => fbProp === property ) ) { params [ property ] = event . properties [ property ] ; } } ) ; return { ... params , fb_num_items : productCount , _logTime : logTime , _appVersion : event . context . app . version , } ; } ; export default ( event ) => { const safeEvent = mappedPropNames ( event as Record < string , any > ) ; let convertedName = safeEvent . event as string ; let safeName = sanitizeName ( convertedName ) ; let safeProps = sanitizeEvent ( safeEvent ) ; const currency = ( safeProps . fb_currency as string | undefined ) ?? 'USD' ; if ( safeProps . _valueToSum !== undefined ) { let purchasePrice = safeProps . _valueToSum as number ; AppEventsLogger . logPurchase ( purchasePrice , currency , safeProps ) ; } else { AppEventsLogger . logEvent ( safeName , safeProps ) ; } } ;