import { DestinationPlugin , PluginType , TrackEventType , ScreenEventType , SegmentAPISettings , UpdateType , IntegrationSettings , } from '@segment/analytics-react-native' ; import track from './methods/track' ; import screen from './methods/screen' ; import type { SegmentClient } from '@segment/analytics-react-native/src/analytics' ; import { Settings } from 'react-native-fbsdk-next' ; interface FBPluginSettings extends Record < string , any > { trackScreenEvent : boolean ; } const isFBPluginSettings = ( settings ) : settings is FBPluginSettings => { return ( typeof settings === 'object' && 'trackScreenEvent' in Object . keys ( settings ) ) ; } ; export class FacebookAppEventsPlugin extends DestinationPlugin { type = PluginType . destination ; key = 'Facebook App Events' ; trackScreens = false ; async configure ( analytics ) { this . analytics = analytics ; let adTrackingEnabled = this . analytics ?. adTrackingEnabled . get ( ) ; this . analytics . adTrackingEnabled . onChange ( ( value ) => { Settings . setAdvertiserTrackingEnabled ( value ) ; } ) ; await Settings . initializeSDK ( ) ; if ( adTrackingEnabled ) { try { await Settings . setAdvertiserTrackingEnabled ( true ) ; } catch ( e ) { console . log ( 'Add Tracking Enabled Error' , e ) ; } } Settings . setDataProcessingOptions ( [ ] , 0 , 0 ) ; } update ( settings , _ ) { const fbSettings = settings . integrations [ this . key ] ; if ( isFBPluginSettings ( fbSettings ) ) { this . trackScreens = fbSettings . trackScreenEvent ; } } track ( event ) { track ( event ) ; return event ; } screen ( event ) { if ( this . trackScreens === true ) { screen ( event ) ; } return event ; } }