Installation and initialization
AppMetrica React Native is a plugin for the React Native platform. It includes the AppMetrica SDK support for Android and iOS.
The minimum supported version of React Native is 0.59.
This section describes the steps to enable and initialize AppMetrica React Native:
Step 1. Integrate the AppMetrica React Native plugin
Install the AppMetrica React Native plugin in your project using the
npm
package manager:npm install react-native-appmetrica --save
Copied to clipboardIf you use yarnInstall the AppMetrica React Native plugin in your project using the
yarn
package manager:yarn add react-native-appmetrica
Copied to clipboardFor React Native version 0.59 and below, run the following console command to link AppMetrica to your project:
react-native link react-native-appmetrica
Copied to clipboardFor iOS projects:
If the project has the ${PROJECT_DIR}/ios/Podfile file, run the following console command:
npx pod-install
Copied to clipboardIf there is no such file ${PROJECT_DIR}/ios/Podfile, connect the library manually.
Note. The framework should be located in the ${PROJECT_DIR}/ios/Frameworks folder.—
Rebuild your app:
# Android: npx react-native run-android # iOS: npx react-native run-ios
Copied to clipboard
Step 2. Initialize the AppMetrica library
Import the library in the source code of your project:
import AppMetrica from 'react-native-appmetrica';
Copied to clipboardIn this case, use
AppMetrica
in the source code for working with the library.Initialize the AppMetrica library using the
activate()
method:AppMetrica.activate({ apiKey: 'Your API key', sessionTimeout: 120, firstActivationAsUpdate: false });
Copied to clipboardWhat is the API key?The API key is a unique application identifier that is issued in the AppMetrica web interface during app registration.
Make sure you have entered it correctly.
Send an event to test the library:
// Sends a custom event message and additional parameters (optional). AppMetrica.reportEvent('My event'); AppMetrica.reportEvent('My event', { foo: 'bar' }); // Send a custom error event. AppMetrica.reportError('My error')
Copied to clipboard
Example of a project with the integrated AppMetrica SDK on GitHub.