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:

  1. Step 1. Integrate the AppMetrica React Native plugin
  2. Step 2. Initialize the AppMetrica library

Step 1. Integrate the AppMetrica React Native plugin

  1. Install the AppMetrica React Native plugin in your project using the npm package manager:

    npm install react-native-appmetrica --save
    Copied to clipboard
    If you use yarn

    Install the AppMetrica React Native plugin in your project using the yarn package manager:

    yarn add react-native-appmetrica
    Copied to clipboard
  2. For 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 clipboard
  3. For iOS projects:

    • If the project has the ${PROJECT_DIR}/ios/Podfile file, run the following console command:

      npx pod-install
      Copied to clipboard
    • If there is no such file ${PROJECT_DIR}/ios/Podfile, connect the library manually.

      Note. The framework must be located in the ${PROJECT_DIR}/ios/Frameworks folder
      .
  4. Rebuild your app:

    # Android:
    npx react-native run-android
    # iOS:
    npx react-native run-ios
    Copied to clipboard

Step 2. Initialize the AppMetrica library

  1. Import the library in the source code of your project:

    import AppMetrica from 'react-native-appmetrica';
    Copied to clipboard

    In this case, use AppMetrica in the source code for working with the library.

  2. Initialize the AppMetrica library using the activate() method:

    AppMetrica.activate({
      apiKey: 'Your API key',
      sessionTimeout: 120,
      firstActivationAsUpdate: true,
    });
    Copied to clipboard
    What 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.

  3. 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.