@datadog/mobile-react-native-babel-plugin

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

A Babel plugin that enhances Datadog's React Native SDK by automatically enriching React components with contextual metadata.

原始使用文档
文档目录4 节
  1. Setup
  2. Configure Babel
  3. Configuration Options
  4. Troubleshooting

Babel Plugin for React Native

The @datadog/mobile-react-native-babel-plugin enhances the Datadog React Native SDK by automatically enriching React components with contextual metadata. This helps improve the accuracy of features such as RUM Action tracking and Session Replay.

Setup

Note: Make sure you've already integrated the Datadog React Native SDK.

To install with NPM, run:

npm install @datadog/mobile-react-native-babel-plugin

To install with Yarn, run:

yarn add @datadog/mobile-react-native-babel-plugin

Configure Babel

Add the plugin to your Babel configuration. Depending on your setup, you might be using a babel.config.js, .babelrc, or similar.

Example configuration:

module.exports = {
  presets: ['module:@react-native/babel-preset'],
  plugins: ['@datadog/mobile-react-native-babel-plugin'] // <-- Add here
};

Configuration Options

You can configure the plugin to adjust how it processes your code, giving you control over its behavior and allowing you to tailor it to your project’s needs.

Top-level options

OptionTypeDefaultDescription
actionNameAttributestring–The chosen attribute name to use for action names.
sessionReplayobject–Session Replay configuration.
componentsobject–Component tracking configuration.

sessionReplay options

OptionTypeDefaultDescription
svgTrackingbooleanfalseWhether to track SVG assets in the context of Session Replay.

components options

OptionTypeDefaultDescription
useContentbooleantrueWhether to use component content (for example: children, props) as the action name.
useNamePrefixbooleantrueWhether to prefix actions with the component name.
trackedarray–List of component-specific tracking configs.

components.tracked[] (per component)

Each entry in the tracked array is an object with the following shape:

OptionTypeDefaultDescription
namestring–The component name to track (e.g., Button).
useContentbooleaninherits from globalOverride useContent for this component.
useNamePrefixbooleaninherits from globalOverride useNamePrefix for this component.
contentPropstring–Property name to use for content instead of children (for example: "subTitle").
handlersarray–List of event/action pairs to track.

components.tracked[].handlers[]

FieldTypeDescription
eventstringThe event name to intercept (such as "onPress").
actionstringThe RUM action name to associate with this event. (Only "TAP" actions are currently supported)

Example configuration (using configuration options):

module.exports = {
  presets: ['module:@react-native/babel-preset'],
  plugins: [
    [
      '@datadog/mobile-react-native-babel-plugin',
      {
        actionNameAttribute: 'custom-prop-value',
        sessionReplay: {
          svgTracking: true
        },
        components: {
          useContent: true,
          useNamePrefix: true,
          tracked: [
            {
              name: 'CustomButton',
              contentProp: 'text'
              handlers: [{event: 'onPress', action: 'TAP'}],
            },
            {
              name: 'CustomTextInput',
              handlers: [{event: 'onFocus', action: 'TAP'}],
            },
            {
              useNamePrefix: false,
              useContent: false,
              name: 'Tab',
              handlers: [{event: 'onChange', action: 'TAP'}],
            },
          ],
        },
      },
    ],
  ],
};

Troubleshooting

Note: If you're on an older React Native version, and using Typescript in your project, you may need to install the preset @babel/preset-typescript.

To install with NPM, run:

npm install @babel/preset-typescript

To install with Yarn, run:

yarn add @babel/preset-typescript 

Then update your Babel configuration file like using the following example:

module.exports = {
  presets: [
    'module:@react-native/babel-preset',
    '@babel/preset-typescript' // <-- Add here
  ],
  plugins: ['@datadog/mobile-react-native-babel-plugin']
};

版本与发布记录

@datadog/mobile-react-native-babel-plugin 共发布 41 个版本,首次发布于 2025/08/01;过去 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 个月前

其他发布通道:v2 → 2.14.9。

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

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

A Babel plugin that enhances Datadog's React Native SDK by automatically enriching React components with contextual metadata.

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

在项目目录运行 npm install @datadog/mobile-react-native-babel-plugin,或使用 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-generate-sr-assets,可以用 npx datadog-generate-sr-assets 直接运行。

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

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

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

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

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

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

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

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

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

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