Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
React Native is a JavaScript framework for developing hybrid mobile applications that can run natively on both Android and iOS.
If you have a hybrid application that is built on React Native, you can use Datadog to monitor the same application from both the native Android or iOS side and the React Native side.
RUM Events from both of these sources are reported as coming from the same application and the same source in Datadog RUM.
For errors, resources, and interactions tracking, the SDKs can work in the following ways:
Through auto-instrumentation - Some React classes and methods are modified to automate this. Auto-instrumentation for JavaScript errors, resources, and interactions can only be started from JavaScript code.
Through manual instrumentation - For example, if you want to report something that you consider an error, but which is not going to crash the app.
You can share the same instance of the core SDK between native and React Native without having to initialize the SDK on both sides separately. This allows you to initialize the native SDK on either the native side or on the React Native side (by calling DdSdkReactNative.initialize) and have it initialized for both sides, with events appearing in the same RUM session. React Native uses the default core instance. This means that you can use manual instrumentation on both sides, but auto-instrumentation is only activated for the side that the SDK was initialized.
You can report Datadog RUM events or logs only after the initialization. If you have not initialized the SDK yet, events and logs are not sent.
You cannot change the source attribute of a RUM session - all your RUM events appear under the same source.
On Android, add the Datadog Android SDKs to your dependencies in your android/app/build.gradle file:
// The version is set by @datadog/mobile-react-nativeimplementation"com.datadoghq:dd-sdk-android-rum"implementation"com.datadoghq:dd-sdk-android-logs"implementation"com.datadoghq:dd-sdk-android-trace"implementation"com.datadoghq:dd-sdk-android-webview"
On iOS, add the Datadog iOS SDKs to your dependencies in your ios/Podfile to use in Objective C files:
# Make sure the version matches the one from node_modules/@datadog/mobile-react-native/DatadogSDKReactNative.podspecpod'DatadogSDKObjc','~> 2.5.0'
If you use a navigation library for your React Native app like react-navigation, using the nativeViewTracking configuration option creates many duplicate views.
If this is the case, track your native RUM Views manually. See documentation for iOS and for Android.
If you write any native code that relies on the Datadog SDK, make sure you execute that code after initializing the SDK on the React Native side. When you initialize the SDK on the React Native side, it is also initialized on the native side.
Add the Datadog Android SDK to your dependencies in your android/app/build.gradle file:
// The version is set by @datadog/mobile-react-native
implementation"com.datadoghq:dd-sdk-android-rum"implementation"com.datadoghq:dd-sdk-android-logs"implementation"com.datadoghq:dd-sdk-android-trace"implementation"com.datadoghq:dd-sdk-android-webview"
Initialize the SDK on the native side. See the official Android documentation for instructions.
Initialize the SDK on the native side. See the official iOS documentation for instructions.
Use a ComponentPredicate to filter out native views that are created by your navigation libraries:
// Adapt the Fragment type to your View tracking strategy
classRNComponentPredicate:ComponentPredicate<Fragment>{overridefunaccept(component:Fragment):Boolean{// Identify and drop react native screen views
if(component.javaClass.name.startsWith("com.swmansion.rnscreens")){returnfalse}if(component.javaClass.name.startsWith("com.facebook.react")){returnfalse}returntrue}overridefungetViewName(component:Fragment):String?{returnnull}}// Use it in your RUM configuration
rumConfiguration.useViewTrackingStrategy(FragmentViewTrackingStrategy(true,RNComponentPredicate()))
Then, use @datadog/mobile-react-navigation to track your views.
If you have enabled ProGuard obfuscation, add rules to prevent obfuscation of the target packages in release builds.
Use a UIKitRUMViewsPredicate to filter out native views that are created by your navigation libraries:
classRNHybridPredicate:UIKitRUMViewsPredicate{vardefaultPredicate=DefaultUIKitRUMViewsPredicate()funcrumView(forviewController:UIViewController)->RUMView?{letcanonicalClassName=NSStringFromClass(type(of:viewController))// Dropping RN Viewsif(canonicalClassName.starts(with:"RN")){returnnil}returndefaultPredicate.rumView(for:viewController)}}// Use it in your RUM configurationletrumConfiguration=RUM.Configuration(applicationID:applicationId,uiKitViewsPredicate:RNHybridPredicate(),)
Wrap your React Native app with the DatadogProvider component to automatically register React Native RUM errors, interactions, and resources:
constconfiguration={trackResources:true,trackErrors:true,trackInteractions:true};constRNApp=props=>{useEffect(()=>{/**
* In here we can put fake values. The only goal of this call
* is to empty the buffer of RUM events.
*/DatadogProvider.initialize({clientToken:'fake_value',env:'fake_value',applicationId:'fake_value'});},[]);constnavigationRef=useRef(null);return(<DatadogProviderconfiguration={configuration}>{/* Content of your app goes here */}</DatadogProvider>);};AppRegistry.registerComponent('RNApp',()=>RNApp);
To remove duplicated interactions on Android, filter out the React Native interactions on the native side with an EventMapper:
classRNActionEventMapper:EventMapper<ActionEvent>{overridefunmap(event:ActionEvent):ActionEvent?{vartargetClassName=(event.context?.additionalProperties?.get("action.target.classname")as?String)if(targetClassName?.startsWith("com.facebook.react")==true){returnnull}returnevent}}// Use it in your RUM configuration
rumConfiguration.setActionEventMapper(RNActionEventMapper())
If you have enabled ProGuard obfuscation, add rules to prevent obfuscation of the target packages in release builds.
If you specified a resourceEventMapper or actionEventMapper in your React Native configuration, resources and actions won’t be dropped if you return null in the mapper.
To keep this functionality, add the following snippets in your native configuration for your platform:
valconfig=RumConfiguration.Builder(applicationId=appId).setResourceEventMapper(object: EventMapper<ResourceEvent>{overridefunmap(event:ResourceEvent):ResourceEvent?{if(event.context?.additionalProperties?.containsKey("_dd.resource.drop_resource")==true){returnnull}// You can add your custom event mapper logic here
returnevent}}).setActionEventMapper(object: EventMapper<ActionEvent>{overridefunmap(event:ActionEvent):ActionEvent?{if(event.context?.additionalProperties?.containsKey("_dd.action.drop_action")==true){returnnull}// You can add your custom event mapper logic here
returnevent}})
RUM.Configuration(applicationID:applicationId,resourceEventMapper:{resourceEventinifresourceEvent.context?.contextInfo["_dd.resource.drop_resource"]!=nil{returnnil}// You can add your custom event mapper logic herereturnresourceEvent},actionEventMapper:{actionEventinifactionEvent.context?.contextInfo["_dd.resource.drop_action"]!=nil{returnnil}// You can add your custom event mapper logic herereturnresourceEvent})