@datadog/mobile-react-native

中文文档
3.11.0发布于 2026/10/20Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 261.5万

A client-side React Native module to interact with Datadog

原始使用文档
文档目录7 节
  1. Setup
  2. Specify application details in UI
  3. Initialize the library with application context
  4. Track view navigation
  5. Data Storage
  6. Android
  7. iOS

React-Native Monitoring

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 @datadog/mobile-react-native

To install with Yarn, run:

yarn add @datadog/mobile-react-native

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.

Versions 1.0.0-rc5 and higher require you to have compileSdkVersion = 31 in the Android application setup, which implies that you should use Build Tools version 31, Android Gradle Plugin version 7, and Gradle version 7 or higher. To modify the versions, change the values in the buildscript.ext block of your application's top-level build.gradle file. Datadog recommends using React Native version 0.67 or higher.

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 @datadog/mobile-react-native 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 {
    DatadogProvider,
    DatadogProviderConfiguration
} from '@datadog/mobile-react-native';

const datadogConfiguration = new DatadogProviderConfiguration(
    '<CLIENT_TOKEN>',
    '<ENVIRONMENT_NAME>',
    TrackingConsent.GRANTED,
    {
        rumConfiguration: {
            applicationId: '<RUM_APPLICATION_ID>',
            trackInteractions: 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)
            trackResources: true, // track XHR Resources
            trackFetchResources: true, // Optional: also track requests made with the global Expo Fetch implementation
            trackFrustrations: true, // track Frustrations
            trackErrors: true, // track errors
            nativeCrashReportEnabled: true, // Optional: enable or disable native crash reports
            sessionSampleRate: 80, // Optional: sample RUM sessions (here, 80% of session will be sent to Datadog. Default = 100%)
            resourceTraceSampleRate: 80, // Optional: sample tracing integrations for network calls between your app and your backend (here, 80% of calls to your instrumented backend will be linked from the RUM view to the APM view. Default = 20%)
            // You need to specify the hosts of your backends to enable tracing with these backends
            firstPartyHosts: ['example.com'], // matches 'example.com' and subdomains like 'api.example.com'
        },
        logsConfiguration: {
            logEventMapper: logEvent => {
                logEvent.message = `[CUSTOM] ${logEvent.message}`;
                return logEvent;
            }
        },
        traceConfiguration: {}
    }
);
// Optional: Select your Datadog website (one of "US1", "US3", "US5", "EU1", "AP1", "AP2", or "US1_FED"). Default is "US1".
datadogConfiguration.site = 'US1';
// Optional: set the reported service name (by default, it'll use the package name / bundleIdentifier of your Android / iOS app respectively)
datadogConfiguration.service = 'com.example.reactnative';
// Optional: let the SDK print internal logs (above or equal to the provided level. Default = undefined (meaning no logs))
datadogConfiguration.verbosity = SdkVerbosity.WARN;

export default function App() {
    return (
        <DatadogProvider configuration={datadogConfiguration}>
            <Navigation />
        </DatadogProvider>
    );
}

trackFetchResources tracks calls made through the Expo-installed global fetch. Direct imports retained from expo/fetch are not intercepted. XHR tracking remains enabled for clients such as axios.

GraphQL error extraction (via DatadogLink({ trackErrors: true })) for requests made through expo/fetch requires Expo SDK 56 or later, as older versions of expo/fetch do not implement Response.clone(). On earlier versions, GraphQL metadata is still reported, but response errors are not.

Track view navigation

Because React Native offers a wide range of libraries to create screen navigation, by default only manual View tracking is supported. You can manually start and stop a View using the following startView() and stopView methods.

import {
    DdSdkReactNative,
    DdSdkReactNativeConfiguration,
    DdLogs,
    DdRum
} from '@datadog/mobile-react-native';

// Start a view with a unique view identifier, a custom view url, and an object to attach additional attributes to the view
DdRum.startView('ViewKey', 'ViewName', Date.now(), {
    'custom.foo': 'something'
});
// Stops a previously started view with the same unique view identifier, and an object to attach additional attributes to the view
DdRum.stopView('ViewKey', Date.now(), { 'custom.bar': 42 });

Data Storage

Android

Before data is uploaded to Datadog, it is stored in cleartext in your application's cache directory. This cache folder is protected by Android's Application Sandbox, meaning that on most devices this data can't be read by other applications. However, if the mobile device is rooted, or someone tempers with the linux kernel, the stored data might become readable.

iOS

Before data is uploaded to Datadog, it is stored in cleartext in the cache directory (Library/Caches) of your application sandbox, which can't be read by any other app installed on the device.

版本与发布记录

@datadog/mobile-react-native 共发布 118 个版本,首次发布于 2021/07/26;过去 12 个月发布了 32 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.11.0latest2026/10/2021 天前
3.10.02026/10/091 个月前
3.9.02026/10/011 个月前
3.8.02026/09/221 个月前
3.7.02026/09/032 个月前
3.6.02026/08/172 个月前

其他发布通道:next → 2.0.0-rc.0,v2 → 2.14.9。

关于 @datadog/mobile-react-native 的常见问题

@datadog/mobile-react-native 是什么?

A client-side React Native module to interact with Datadog

如何安装 @datadog/mobile-react-native?

在项目目录运行 npm install @datadog/mobile-react-native,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.13.1)、react-native(>=0.63.4 <1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@datadog/mobile-react-native 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@datadog/mobile-react-native 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@datadog/mobile-react-native 可以商用吗?

@datadog/mobile-react-native 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@datadog/mobile-react-native 的下载量有多少?

根据 npm 官方下载统计,@datadog/mobile-react-native 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,614,571 次,日均约 87,152 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@datadog/mobile-react-native 还在维护吗?

最新版本 3.11.0 发布于 2026/10/20(21 天前);至今共发布 118 个版本,其中过去 12 个月发布了 32 个;首次发布于 2021/07/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。