This package has been deprecated

Author message:

This package is deprecated, use the @datadog/mobile-react-native package instead

dd-sdk-reactnative
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-beta3 • Public • Published

React-Native Monitoring

This feature is in open beta. Contact Support to ask questions or to provide feedback on this feature.

Datadog Real User Monitoring (RUM) enables you to visualize and analyze the real-time performance and user journeys of your application’s individual users.

Setup

To install with NPM, run:

npm install dd-sdk-reactnative

To install with Yarn, run:

yarn add dd-sdk-reactnative

Minimum React Native version: SDK supports React Native version 0.63.4 or higher. Compatibility with older versions is not guaranteed out of the box.

Specify application details in UI

  1. In the Datadog app, select UX Monitoring > RUM Applications > New Application.
  2. Choose react-native as your Application Type.
  3. Provide a new application name to generate a unique Datadog application ID and client token.

image

To ensure the safety of your data, you must use a client token. You cannot use only Datadog API keys to configure the dd-sdk-reactnative library, because they would be exposed client-side. For more information about setting up a client token, see the Client Token documentation.

Initialize the library with application context

import { DdSdkReactNative, DdSdkReactNativeConfiguration } from 'dd-sdk-reactnative';


const config = new DdSdkReactNativeConfiguration(
    "<CLIENT_TOKEN>", 
    "<ENVIRONMENT_NAME>", 
    "<RUM_APPLICATION_ID>",
    true, // track User interactions (e.g.: Tap on buttons. You can use 'accessibilityLabel' element property to give tap action the name, otherwise element type will be reported)
    true, // track XHR Resources
    true // track Errors
)
// Optional: Select your Datadog website (one of "US", "EU" or "GOV")
config.site = "US"
// Optional: enable or disable native crash reports
config.nativeCrashReportEnabled = true
// Optional: sample RUM sessions (here, 80% of session will be sent to Datadog. Default = 100%)
config.sampleRate = 80

await DdSdkReactNative.initialize(config)

// Once SDK is initialized you need to setup view tracking to be able to see data in the RUM Dashboard.

Track view navigation

Note: Automatic View tracking relies on the React Navigation package (minimum supported version is react-navigation/native@5.6.0). If you use another package to handle navigation in your application, use the manual instrumentation method described below.

To track changes in navigation as RUM Views, set the onready callback of your NavigationContainer component:

import * as React from 'react';
import { DdRumReactNavigationTracking } from 'dd-sdk-reactnative';

function App() {
  const navigationRef = React.useRef(null);
  return (
    <View>
      <NavigationContainer ref={navigationRef} onReady={() => {
        DdRumReactNavigationTracking.startTrackingViews(navigationRef.current)
      }}>
        // …
      </NavigationContainer>
    </View>
  );
}

Note: Only one NavigationContainer can be tracked at the time. If you need to track another container, stop tracking previous one first.

Track custom attributes

You can attach user information to all RUM events to get more detailed information from your RUM sessions.

User information

For user-specific information, use the following code wherever you want in your app (after the SDK has been initialized). The id, name, and email attributes are built into Datadog, and you can add other attributes that makes sense for your app.

DdSdkReactNative.setUser({
    id: "1337", 
    name: "John Smith", 
    email: "john@example.com", 
    type: "premium"
})

Global attributes

You can also keep global attributes to track information about a specific session, such as A/B testing configuration, ad campaign origin, or cart status.

DdSdkReactNative.setAttributes({
    profile_mode: "wall",
    chat_enabled: true,
    campaign_origin: "example_ad_network"
})

Manual instrumentation

If automatic instrumentation doesn't suit your needs, you can manually create RUM Events and Logs:

import { DdSdkReactNative, DdSdkReactNativeConfiguration, DdLogs, DdRum } from 'dd-sdk-reactnative';

// Initialize the SDK
const config = new DdSdkReactNativeConfiguration(
    "<CLIENT_TOKEN>",
    "<ENVIRONMENT_NAME>",
    "<RUM_APPLICATION_ID>",
    true, // track User interactions (e.g.: Tap on buttons)
    true, // track XHR Resources
    true // track Errors
)
DdSdkReactNative.initialize(config);

// Send logs (use the debug, info, warn or error methods)
DdLogs.debug("Lorem ipsum dolor sit amet…", 0, {});
DdLogs.info("Lorem ipsum dolor sit amet…", 0, {});
DdLogs.warn("Lorem ipsum dolor sit amet…", 0, {});
DdLogs.error("Lorem ipsum dolor sit amet…", 0, {});

// Track RUM Views manually
DdRum.startView('<view-key>', 'View Url', Date.now(), {});
//…
DdRum.stopView('<view-key>', Date.now(), { 'custom': 42 });

// Track RUM Actions manually
DdRum.addAction('TAP', 'button name', Date.now(), {});
// or in case of continuous action
DdRum.startAction('TAP', 'button name', Date.now(), {});
// to stop action above
DdRum.stopAction(Date.now(), {});

// Add custom timings
DdRum.addTiming('<timing-name>');

// Track RUM Errors manually
DdRum.addError('<message>', 'source', '<stacktrace>', Date.now(), {});

// Track RUM Resource manually
DdRum.startResource('<res-key>', 'GET', 'http://www.example.com/api/v1/test', Date.now(), {} );
//…
DdRum.stopResource('<res-key>', 200, 'xhr', Date.now(), {});

// Send spans manually
const spanId = await DdTrace.startSpan("foo", Date.now(), { 'custom': 42 });
//...
DdTrace.finishSpan(spanId, Date.now(), { 'custom': 21 });

Resource timings

Resource tracking is able to provide the following timings:

  • First Byte - The time between the scheduled request and the first byte of the response. This includes time for the request preparation on the native level, network latency, and the time it took the server to prepare the response.
  • Download - The time it took to receive a response.

License

Apache License, v2.0

Further Reading

{{< partial name="whats-next/whats-next.html" >}}

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Published

Version History

  • Version
    Downloads (Last 7 Days)
    • Published

Package Sidebar

Install

npm i dd-sdk-reactnative

Weekly Downloads

38

Version

1.0.0-beta3

License

Apache-2.0

Unpacked Size

8.12 MB

Total Files

424

Last publish

Collaborators

  • datadog