Get started with the React Native SDK

このページはまだあなたの言語に翻訳されていません。 現在取り組んでいますので、後でもう一度確認してください。

For React Native developers10 minBeginner

This guide installs the React Native SDK (@amplitude/analytics-react-native), initializes it, and sends one custom event to Amplitude. Each step lists the problems that commonly break that step, so you can fix each problem where it happens. For the full option list, go to Configuration options. For changes between versions, go to the React Native SDK release notes.

The React Native SDK is the analytics-only package. If you also need Session Replay, Feature Experiment, or Guides and Surveys, install the Unified SDK for React Native (@amplitude/unified-react-native) instead. It includes this SDK, so don't install both as direct dependencies.

Prerequisites

  • An Amplitude project and its API key. Find the key in Settings > Projects > your project > General.
  • The data center your organization uses: US (default) or EU.
  • A React Native app for iOS, Android, or the web (react-native-web). React Native doesn't keep backward compatibility between releases, so check the React Native CLI compatibility list before you upgrade.
  • React Native 0.68 or later, Gradle 7.5.1 or later, and Android Gradle Plugin 7.2.1 or later. These are the minimums SDK 1.4.0 and later build with. Amplitude support covers the latest React Native version only. For older SDK versions, go to Platform support.
  • iOS 12.0 or later, and Android API level 21 or later. These are the minimums the SDK's native modules declare. Your React Native version can require higher ones.

Install the SDK

The SDK stores identity and unsent events in @react-native-async-storage/async-storage, so install that package too. To use a different storage backend, go to Store data somewhere other than AsyncStorage.

bash
npm install @amplitude/analytics-react-native @react-native-async-storage/async-storage
cd ios && pod install

Then import the package:

ts
import * as amplitude from "@amplitude/analytics-react-native";

The SDK has native modules for iOS and Android. If you ship over-the-air (OTA) updates, for example with Expo EAS Update, publish a new native build when you upgrade across a release that changed native code. Go to Platform support for those releases.

Fix installation problems

Metro can't resolve an optional Session Replay package

The React Native SDK uses @amplitude/plugin-session-replay-react-native and @amplitude/session-replay-react-native as optional peers for dead-click detection. Analytics works without either package. Dead clicks require a compatible Session Replay package installed and recording.

If Metro reports Unable to resolve module for either package when it isn't installed, set transformer.allowOptionalDependencies to true in your app's metro.config.js. This lets Metro bundle the SDK's guarded require calls when an optional package is absent. Setting optionalDependencies or peerDependenciesMeta in package.json alone doesn't configure Metro.

For React Native CLI apps, merge the option into your existing configuration. Preserve your other configuration and transformer options.

js
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");

const config = {
  // Keep your existing configuration here.
  transformer: {
    // Keep your existing transformer options here.
    allowOptionalDependencies: true,
  },
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);

For Expo apps, add the option to the config returned by expo/metro-config before exporting it:

js
const { getDefaultConfig } = require("expo/metro-config");

const config = getDefaultConfig(__dirname);

// Keep your existing configuration changes.
config.transformer = {
  ...config.transformer,
  allowOptionalDependencies: true,
};

module.exports = config;

Stop Metro, then restart it with a cleared cache. Use the command for your app's framework:

bash
# React Native CLI
npx react-native start --reset-cache

# Expo
npx expo start --clear

You don't need to install both Replay packages to resolve this error. To capture dead clicks, follow the React Native Session Replay setup instructions. For details about optional module resolution, refer to Metro's allowOptionalDependencies option.

Initialize the SDK

Call init() once, as early as possible, for example at the top of your app's entry file. The SDK queues calls you make before init() finishes and runs them in order afterward.

ts
amplitude.init("AMPLITUDE_API_KEY");

Pass a user ID as the second argument when you already know it. Pass options as the third argument. To pass options without a user ID, set the second argument to undefined:

ts
amplitude.init("AMPLITUDE_API_KEY", "user-12345", {
  autocapture: true,
});

amplitude.init("AMPLITUDE_API_KEY", undefined, {
  autocapture: true,
});

If your project uses the EU data center, set serverZone:

ts
amplitude.init("AMPLITUDE_API_KEY", undefined, {
  serverZone: "EU",
});

Autocapture is off by default. autocapture: true turns on sessions, application lifecycles, screen views, element interactions, frustration interactions, and network tracking. For each feature, go to Autocapture.

Fix initialization problems

Send a first event

Track one custom event and wait for the upload result. track() returns an object whose promise resolves after the SDK finishes the upload attempt.

ts
const result = await amplitude.track("Sign Up Clicked", {
  plan: "starter",
}).promise;

console.log(result.code, result.message);
// 200 "Event tracked successfully"

The SDK uploads queued events about one second after the first one enters the queue, in requests of up to 30 events, so expect about one second of delay.

Fix upload problems

Confirm the event in Amplitude

  1. In Amplitude, open User Lookup and search for the user ID you passed, or for the device ID from amplitude.getDeviceId().
  2. Open the Activity tab and turn on Live event updates.
  3. Confirm that Sign Up Clicked appears with the plan property.

If the event doesn't appear within a minute, follow Verify events. That page walks through each stage between track() and Amplitude's servers.

Next steps

これは役に立ちましたか?

For AI agents: a documentation index is available at /docs/llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.

このページでは