dt-obs-react-native
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDynatrace React Native Plugin Integration Skill
Dynatrace React Native插件集成指南
Prerequisites
前提条件
- Node.js 16.0+ and npm available on
PATH - An existing React Native project (v0.68+) or Expo project (v45+) with a at the project root
package.json - and
android/platform folders present (Expo managed workflow requiresios/first)npx expo prebuild - A Dynatrace environment with access to Experience Vitals (to obtain and
applicationId, or to downloadbeaconUrl)dynatrace.config.js - Console access to Experience Vitals → Mobile to configure app settings (Data Privacy, Enablement and Cost Control)
- macOS with CocoaPods for iOS builds
Work through the steps below in order, interacting with the user at each decision point. Read actual project files before suggesting changes — do not assume the current state.
- 系统PATH中需包含Node.js 16.0+及npm
- 已存在的React Native项目(v0.68+)或Expo项目(v45+),项目根目录下有文件
package.json - 存在和
android/平台文件夹(Expo托管工作流需先执行ios/)npx expo prebuild - 拥有可访问Experience Vitals的Dynatrace环境(用于获取和
applicationId,或下载beaconUrl)dynatrace.config.js - 拥有Experience Vitals → Mobile的控制台访问权限,用于配置应用设置(数据隐私、功能启用与成本控制)
- 用于iOS构建的macOS系统且已安装CocoaPods
请按以下步骤依次操作,在每个决策点与用户交互。在建议修改前先读取实际项目文件——请勿假设当前状态。
Step 1 — Check for existing dynatrace.config.js
dynatrace.config.js步骤1 — 检查是否存在dynatrace.config.js
dynatrace.config.jsBefore asking the user anything, check whether exists at the project root.
dynatrace.config.js- File exists: Read it, show the and
applicationId, and confirm they match the target environment. If correct, run thebeaconUrlcheck below and skip to Step 3 (config is already present — Step 2 is not needed).userOptIn - File does not exist: Proceed to Step 2.
userOptInInspect the file for (Android block) and (iOS block).
userOptInDTXUserOptIn- Present on both platforms: Trust the value as-is. If , flag Step 9. If
true, skip Step 9.false - Absent from either platform: Ask the user: "Do you have User Opt-In mode enabled or disabled?" (If unsure: Experience Vitals → Mobile → [Your App] → Settings → Data Privacy.) Add the missing value to the relevant platform(s), then flag Step 9 if the final value is .
true
在询问用户任何问题前,先检查项目根目录下是否存在文件。
dynatrace.config.js- 文件已存在: 读取文件,展示和
applicationId,确认它们与目标环境匹配。若正确,执行下方的beaconUrl检查并跳至步骤3(配置已存在,无需执行步骤2)。userOptIn - 文件不存在: 继续执行步骤2。
userOptIn检查文件中是否存在Android区块的和iOS区块的。
userOptInDTXUserOptIn- 两个平台均已配置: 保留现有值。若值为,标记步骤9;若为
true,跳过步骤9。false - 任一平台未配置: 询问用户:"您是否启用了用户选择加入模式?"(若不确定:前往Experience Vitals → Mobile → [您的应用] → 设置 → 数据隐私。)为对应平台添加缺失的值,若最终值为则标记步骤9。
true
Step 2 — Obtain dynatrace.config.js
(only if Step 1 found no file)
dynatrace.config.js步骤2 — 获取dynatrace.config.js
(仅当步骤1未找到文件时执行)
dynatrace.config.jsAsk the user which approach they prefer:
Option A — Download from console (recommended):
- Open their Dynatrace environment
- Navigate to: Experience Vitals → New Frontend → Mobile
- Enter app name and choose React Native as the platform
- On the Select capability and settings screen, configure monitoring features (crash reporting, user action monitoring, etc.)
- Download and place it at the project root (same level as
dynatrace.config.js)package.json
Once the file is in place, apply the check from Step 1 and flag Step 9 if needed.
userOptInOption B — Provide credentials manually:
Read for the full template and conditional blocks. Collect all required values from the user before creating any files, then apply the check and flag Step 9 if needed.
references/config-js.mduserOptIn询问用户偏好的获取方式:
选项A — 从控制台下载(推荐):
- 打开Dynatrace环境
- 导航至:Experience Vitals → New Frontend → Mobile
- 输入应用名称并选择React Native作为平台
- 在选择功能与设置页面,配置监控功能(崩溃报告、用户行为监控等)
- 下载并放置在项目根目录(与
dynatrace.config.js同级)package.json
文件放置完成后,执行步骤1中的检查,若需要则标记步骤9。
userOptIn选项B — 手动提供凭据:
读取获取完整模板和条件区块。在创建文件前向用户收集所有必填值,然后执行检查,若需要则标记步骤9。
references/config-js.mduserOptInStep 3 — Install the plugin
步骤3 — 安装插件
Read first.
package.json- Not present: Run:
bash
npm install @dynatrace/react-native-plugin - Already under : No change needed.
dependencies - Under : Remove it and run
devDependencies. The plugin is required at runtime including in release builds.npm install @dynatrace/react-native-plugin
Note: alphabetically sorts the object in . Existing entries may appear reordered after this step — this is expected npm behavior, not an error.
npm installdependenciespackage.json先读取文件。
package.json- 插件未安装: 执行命令:
bash
npm install @dynatrace/react-native-plugin - 已在中: 无需修改。
dependencies - 在中: 将其移除并执行
devDependencies。该插件为运行时依赖,包括发布构建场景也需使用。npm install @dynatrace/react-native-plugin
注意:会按字母顺序对中的对象排序。执行此步骤后现有条目可能会重新排序——这是npm的正常行为,并非错误。
npm installpackage.jsondependenciesStep 4 — Install iOS pods (macOS only)
步骤4 — 安装iOS pods(仅macOS系统)
If on macOS and targeting iOS, run:
bash
cd ios && pod install && cd ..Confirm success before continuing. CocoaPods must be installed ( if missing).
gem install cocoapods若使用macOS系统且目标平台为iOS,执行命令:
bash
cd ios && pod install && cd ..确认执行成功后再继续。必须安装CocoaPods(若缺失可执行)。
gem install cocoapodsStep 5 — Register Babel plugin in babel.config.js
babel.config.js步骤5 — 在babel.config.js
中注册Babel插件
babel.config.jsRead . Append to the array, just before if that plugin is present (reanimated must always be last):
babel.config.jsBabelPluginDynatracepluginsreact-native-reanimated/pluginjs
module.exports = {
presets: ['module:@react-native/babel-preset'],
plugins: [
// ... existing plugins ...
'@dynatrace/react-native-plugin/instrumentation/BabelPluginDynatrace',
// react-native-reanimated/plugin goes here if present — must stay last
],
};- If the plugins array already contains : skip this step.
'@dynatrace/react-native-plugin/instrumentation/BabelPluginDynatrace' - If is set in
useLegacyJscodeshift: true: the Babel plugin is already applied internally — skip this step.dynatrace.config.js - If the project already configures with
metro.config.js: that is the legacy Metro transformer approach — auto-instrumentation is already handled; skip this step and Step 6.babelTransformerPath: '@dynatrace/react-native-plugin/lib/dynatrace-transformer'
Common mistakes to flag and correct:
- Placing the plugin in instead of
presetsplugins - Placing after
BabelPluginDynatrace— reanimated must always be the absolute last pluginreact-native-reanimated/plugin
读取文件。将添加至数组,且需在之前(若该插件存在,reanimated必须始终位于最后):
babel.config.jsBabelPluginDynatracepluginsreact-native-reanimated/pluginjs
module.exports = {
presets: ['module:@react-native/babel-preset'],
plugins: [
// ... 现有插件 ...
'@dynatrace/react-native-plugin/instrumentation/BabelPluginDynatrace',
// react-native-reanimated/plugin 若存在需放在此处——必须保持最后
],
};- 若数组已包含
plugins:跳过此步骤。'@dynatrace/react-native-plugin/instrumentation/BabelPluginDynatrace' - 若中设置了
dynatrace.config.js:Babel插件已在内部应用——跳过此步骤。useLegacyJscodeshift: true - 若项目已在中配置
metro.config.js:这是旧版Metro转换器方案——自动插桩已处理完成;跳过此步骤和步骤6。babelTransformerPath: '@dynatrace/react-native-plugin/lib/dynatrace-transformer'
需标记并修正的常见错误:
- 将插件放在而非
presets中plugins - 将放在
BabelPluginDynatrace之后——reanimated必须绝对位于最后react-native-reanimated/plugin
Step 6 — Register JSX runtime in babel.config.js
babel.config.js步骤6 — 在babel.config.js
中注册JSX运行时
babel.config.jsRead and determine which preset the project uses — this controls how the JSX runtime is registered.
babel.config.js读取并确定项目使用的预设——这将决定JSX运行时的注册方式。
babel.config.jsExpo (babel-preset-expo
)
babel-preset-expoExpo(babel-preset-expo
)
babel-preset-expoIf appears in , set on the preset itself. Do not add a separate plugin — Expo's preset already owns the JSX transform, and stacking a second JSX plugin over it breaks the instrumentation.
babel-preset-expopresetsjsxImportSource@babel/plugin-transform-react-jsxjs
module.exports = function (api) {
api.cache(true);
return {
presets: [
['babel-preset-expo', {
jsxRuntime: 'automatic',
jsxImportSource: '@dynatrace/react-native-plugin',
}],
],
plugins: [
'@dynatrace/react-native-plugin/instrumentation/BabelPluginDynatrace',
'react-native-reanimated/plugin', // stays last
],
};
};- If already has
babel-preset-expo: no change needed.jsxImportSource: '@dynatrace/react-native-plugin' - If a different is already set: replace it with
jsxImportSource.'@dynatrace/react-native-plugin' - Remove any existing plugin entry — it must not coexist with the preset-level
@babel/plugin-transform-react-jsxonjsxImportSource.babel-preset-expo
若中包含,在预设本身上设置。请勿添加单独的插件——Expo的预设已接管JSX转换,叠加第二个JSX插件会破坏插桩效果。
presetsbabel-preset-expojsxImportSource@babel/plugin-transform-react-jsxjs
module.exports = function (api) {
api.cache(true);
return {
presets: [
['babel-preset-expo', {
jsxRuntime: 'automatic',
jsxImportSource: '@dynatrace/react-native-plugin',
}],
],
plugins: [
'@dynatrace/react-native-plugin/instrumentation/BabelPluginDynatrace',
'react-native-reanimated/plugin', // 保持最后
],
};
};- 若已设置
babel-preset-expo:无需修改。jsxImportSource: '@dynatrace/react-native-plugin' - 若已设置其他:替换为
jsxImportSource。'@dynatrace/react-native-plugin' - 删除所有现有插件条目——它不能与
@babel/plugin-transform-react-jsx的预设级babel-preset-expo共存。jsxImportSource
Bare React Native (@react-native/babel-preset
or metro-react-native-babel-preset
)
@react-native/babel-presetmetro-react-native-babel-preset原生React Native(@react-native/babel-preset
或metro-react-native-babel-preset
)
@react-native/babel-presetmetro-react-native-babel-presetFor metro 0.72.0+ (React Native 0.71+), add with the Dynatrace before in the plugins array:
@babel/plugin-transform-react-jsximportSourceBabelPluginDynatracejs
module.exports = {
presets: [
['module:@react-native/babel-preset', { unstable_transformProfile: 'hermes-stable' }],
],
plugins: [
['@babel/plugin-transform-react-jsx', {
runtime: 'automatic',
importSource: '@dynatrace/react-native-plugin',
}],
'@dynatrace/react-native-plugin/instrumentation/BabelPluginDynatrace',
'react-native-reanimated/plugin', // stays last
],
};The required plugin order is:
- (JSX runtime — first)
@babel/plugin-transform-react-jsx - (auto-instrumentation — before reanimated)
BabelPluginDynatrace - (must be absolutely last)
react-native-reanimated/plugin
- If the project already has with
@babel/plugin-transform-react-jsx: no change needed.importSource: '@dynatrace/react-native-plugin' - If a different is set: replace it with
importSource.'@dynatrace/react-native-plugin'
After any Babel change, reset Metro cache on next build:
bash
npx react-native start --reset-cache对于metro 0.72.0+(React Native 0.71+),在数组中添加带有Dynatrace 的,且需在之前:
pluginsimportSource@babel/plugin-transform-react-jsxBabelPluginDynatracejs
module.exports = {
presets: [
['module:@react-native/babel-preset', { unstable_transformProfile: 'hermes-stable' }],
],
plugins: [
['@babel/plugin-transform-react-jsx', {
runtime: 'automatic',
importSource: '@dynatrace/react-native-plugin',
}],
'@dynatrace/react-native-plugin/instrumentation/BabelPluginDynatrace',
'react-native-reanimated/plugin', // 必须绝对位于最后
],
};插件的必要顺序:
- (JSX运行时——第一个)
@babel/plugin-transform-react-jsx - (自动插桩——在reanimated之前)
BabelPluginDynatrace - (必须绝对位于最后)
react-native-reanimated/plugin
- 若项目已配置且
@babel/plugin-transform-react-jsx:无需修改。importSource: '@dynatrace/react-native-plugin' - 若已设置其他:替换为
importSource。'@dynatrace/react-native-plugin'
完成任何Babel修改后,下次构建时重置Metro缓存:
bash
npx react-native start --reset-cacheStep 7 — Run npx instrumentDynatrace
npx instrumentDynatrace步骤7 — 执行npx instrumentDynatrace
npx instrumentDynatracebash
npx instrumentDynatraceThis reads and automatically configures Android and iOS . Must be re-run whenever changes.
dynatrace.config.jsbuild.gradleInfo.plistdynatrace.config.jsCommon mistakes to flag and correct:
- Skipping this step after changing
dynatrace.config.js - Using — same effect but deprecated for RN 0.70+
react-native instrument-dynatrace - Manual edits to or
build.gradle— not needed, the script handles itInfo.plist
If and folders exist but automatic plist discovery fails, pass explicit paths:
android/ios/bash
npx instrumentDynatrace plist=ios/YourApp/Info.plistExpo only: If and folders do not yet exist, run first, then re-run .
android/ios/npx expo prebuildnpx instrumentDynatracebash
npx instrumentDynatrace该命令会读取并自动配置Android的和iOS的。每当更改时必须重新执行此命令。
dynatrace.config.jsbuild.gradleInfo.plistdynatrace.config.js需标记并修正的常见错误:
- 修改后跳过此步骤
dynatrace.config.js - 使用——效果相同但在RN 0.70+中已废弃
react-native instrument-dynatrace - 手动编辑或
build.gradle——无需操作,脚本会自动处理Info.plist
若和文件夹存在但自动plist发现失败,可传入明确路径:
android/ios/bash
npx instrumentDynatrace plist=ios/YourApp/Info.plist仅Expo项目: 若和文件夹尚未存在,先执行,再重新执行。
android/ios/npx expo prebuildnpx instrumentDynatraceStep 7a — Manual SDK startup (only if autoStart: false
)
autoStart: false步骤7a — 手动启动SDK(仅当autoStart: false
时)
autoStart: falseSkip this step if is or absent in — the SDK starts automatically.
autoStarttruedynatrace.config.jsCheck for in the block. If present, the SDK will not start on its own and no data will be collected until is called explicitly.
dynatrace.config.jsautoStart: falsereactDynatrace.start()Add the startup call at the top level of the app entry file (for example or ). Any logic that depends on the SDK being ready goes after the :
App.tsxindex.jsawaitts
import { Dynatrace, ConfigurationBuilder } from '@dynatrace/react-native-plugin';
await Dynatrace.start(
new ConfigurationBuilder('<BEACON_URL>', '<APPLICATION_ID>').buildConfiguration()
);
// SDK is initialized — place any SDK-dependent logic hereReplace and with the exact same values from — they must match or the SDK will report to the wrong environment.
<BEACON_URL><APPLICATION_ID>dynatrace.config.jsImportant: Even with , the and must still be present in (used by to configure the native files). The values passed to at runtime take effect — values in the config file are ignored when manual startup is used.
autoStart: falsebeaconUrlapplicationIddynatrace.config.jsnpx instrumentDynatraceConfigurationBuilderTradeoff: Manual startup causes the SDK to miss the native application start event and any interactions that happen before is called. Use (the default) unless runtime credential injection is a hard requirement.
start()autoStart: true若中为或未设置,跳过此步骤——SDK会自动启动。
dynatrace.config.jsautoStarttrue检查的区块中是否存在。若存在,SDK不会自行启动,直到显式调用才会开始收集数据。
dynatrace.config.jsreactautoStart: falseDynatrace.start()在应用入口文件(如或)的顶层添加启动调用。所有依赖SDK就绪的逻辑需放在之后:
App.tsxindex.jsawaitts
import { Dynatrace, ConfigurationBuilder } from '@dynatrace/react-native-plugin';
await Dynatrace.start(
new ConfigurationBuilder('<BEACON_URL>', '<APPLICATION_ID>').buildConfiguration()
);
// SDK已初始化——此处放置依赖SDK的逻辑将和替换为中的完全相同的值——必须匹配,否则SDK会向错误环境上报数据。
<BEACON_URL><APPLICATION_ID>dynatrace.config.js重要提示: 即使设置了,中仍需存在和(供用于配置原生文件)。运行时传入的值会生效——使用手动启动时配置文件中的值会被忽略。
autoStart: falsedynatrace.config.jsbeaconUrlapplicationIdnpx instrumentDynatraceConfigurationBuilder权衡: 手动启动会导致SDK错过原生应用启动事件以及调用前的所有交互。除非必须在运行时注入凭据,否则请使用默认的。
start()autoStart: trueStep 8 — Enable navigation tracking
步骤8 — 启用导航追踪
Check for the block and check for .
dynatrace.config.jsreact.navigationpackage.json@react-navigation/nativeIf is present and is not set to , add it:
@react-navigation/nativenavigation.enabledtruejs
module.exports = {
react: {
navigation: {
enabled: true, // requires @react-navigation/native v5–v7
},
// ...
},
// ...
};Then re-run .
npx instrumentDynatraceWhat this does: When enabled, the plugin hooks into React Navigation's and detects route changes automatically. Each navigation event is reported to Dynatrace as a view change, with the current route represented as a URL-style path (e.g., , ). This populates the screen timeline in Dynatrace user sessions and associates all events with the currently active screen. This setting is enabled by default in the plugin's own config template.
NavigationContainer/Home/Home/DetailsIf is not present: inform the user that automatic view tracking requires . They can use for manual view tracking instead.
@react-navigation/native@react-navigation/nativeDynatrace.startView("ScreenName")检查中的区块,以及中是否存在。
dynatrace.config.jsreact.navigationpackage.json@react-navigation/native若已存在且未设置为,添加该配置:
@react-navigation/nativenavigation.enabledtruejs
module.exports = {
react: {
navigation: {
enabled: true, // 需要@react-navigation/native v5–v7
},
// ...
},
// ...
};然后重新执行。
npx instrumentDynatrace功能说明: 启用后,插件会挂钩React Navigation的并自动检测路由变化。每个导航事件会作为视图变化上报至Dynatrace,当前路由会以URL风格路径表示(如、)。这会在Dynatrace用户会话中填充屏幕时间线,并将所有事件与当前活跃屏幕关联。该设置在插件自身的配置模板中默认启用。
NavigationContainer/Home/Home/Details若未安装:告知用户自动视图追踪需要,他们可使用进行手动视图追踪。
@react-navigation/native@react-navigation/nativeDynatrace.startView("ScreenName")Step 9 — Privacy options call (only if userOptIn: true
)
userOptIn: true步骤9 — 隐私选项调用(仅当userOptIn: true
时)
userOptIn: trueSkip this step if was not set to during Steps 1 or 2.
userOptIntrueRead for the full guidance on , , and placement options. Ask the user the questions defined there, then apply the call to the relevant file.
references/user-opt-in.mdDataCollectionLevelcrashReportingOptedIn若步骤1或步骤2中未设置为,跳过此步骤。
userOptIntrue读取获取关于、和放置选项的完整指南。询问该文档中定义的问题,然后在相关文件中添加调用代码。
references/user-opt-in.mdDataCollectionLevelcrashReportingOptedInStep 10 — Post-setup summary
步骤10 — 安装后总结
Confirm to the user what is active:
Enabled by default (when is or absent):
userOptInfalse- ✅ Crash reporting
- ✅ User action tracking (Touchables, Buttons, Pressable, Switch)
- ✅ Network monitoring
- ✅ Error handler
- ✅ Auto-start
When, all data collection — including crash reporting — is gated on theuserOptIn: truecall.applyUserPrivacyOptions(...)
Configured during this setup:
- Privacy mode: [userOptIn: true — consent call added / opt-out (SDK default)]
- Navigation tracking: [react.navigation.enabled: true / manual via Dynatrace.startView()]
向用户确认已启用的功能:
默认启用(当为或未设置时):
userOptInfalse- ✅ 崩溃报告
- ✅ 用户行为追踪(Touchables、Buttons、Pressable、Switch)
- ✅ 网络监控
- ✅ 错误处理
- ✅ 自动启动
当时,所有数据收集——包括崩溃报告——均受userOptIn: true调用控制。applyUserPrivacyOptions(...)
本次安装中配置的项:
- 隐私模式:[userOptIn: true — 已添加同意调用 / 选择退出(SDK默认)]
- 导航追踪:[react.navigation.enabled: true / 通过Dynatrace.startView()手动追踪]
Step 11 — Verification
步骤11 — 验证
Read and show the user the verification checklist. If no data appears after 5 minutes, work through the troubleshooting steps in that file.
references/verification.md读取并向用户展示验证清单。若5分钟后仍无数据显示,执行该文件中的故障排除步骤。
references/verification.mdReference Files
参考文件
- — Full
references/config-js.mdtemplate with Grail and userOptIn conditional blocksdynatrace.config.js - —
references/user-opt-in.mdguidance,applyUserPrivacyOptionsoptions, placement optionsDataCollectionLevel - — Post-setup verification checklist and troubleshooting
references/verification.md
- — 完整的
references/config-js.md模板,包含Grail和userOptIn条件区块dynatrace.config.js - —
references/user-opt-in.md指南、applyUserPrivacyOptions选项、放置选项DataCollectionLevel - — 安装后验证清单及故障排除
references/verification.md
External References
外部参考
- @dynatrace/react-native-plugin on npm — package changelog, API docs, and latest version
- Dynatrace React Native Installation Docs — official setup guide
- npm上的@dynatrace/react-native-plugin — 包更新日志、API文档及最新版本
- Dynatrace React Native安装文档 — 官方安装指南