---
metadata:
  - name: generator
    content: Diplodoc Platform v5.63.4
alternate:
  - https://yandex.com/support/appmetrica-io/en/sdk/react-native/analytics/quick-start.md
  - https://yandex.com/support/appmetrica-io/ru/sdk/react-native/analytics/quick-start.md
  - https://yandex.com/support/appmetrica-io/zh/sdk/react-native/analytics/quick-start.md
  - href: https://yandex.com/support/appmetrica-io/zh/sdk/react-native/analytics/quick-start.md
    type: text/markdown
    title: Markdown version
  - href: https://yandex.com/support/appmetrica-io/zh/llms.txt?revision=r21687218
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/support/appmetrica-io/zh/llms.txt?revision=r21687218

# 安装和初始化

AppMetrica React Native 是一个用于 [React Native](https://reactnative.dev/) 平台的插件。 它包含 AppMetrica 对 Android 和 iOS 的 SDK 支持。

React Native 的最低支持版本是 0.59。

插件会访问原生的 Android/iOS 库，因此要与 Expo 一起使用它，需要遵循与原生组件集成的 [库的说明](https://docs.expo.dev/workflow/using-libraries/#determining-third-party-library-compatibility)。

此部分介绍如何启用并初始化 AppMetrica React Native：

## 步骤 1. 集成 AppMetrica React Native 插件 {#integration}

{% list tabs %}

- React Native 命令行界面

    1. 在项目中安装 AppMetrica React Native 插件：

        {% list tabs %}

        - yarn

            ```bash translate=no
            yarn add @appmetrica/react-native-analytics
            ```

        - npm

            ```bash translate=no
            npm install @appmetrica/react-native-analytics
            ```

        {% endlist %}

    2. 对于 React Native 版本 0.59 及以下，请运行以下控制台命令，将 AppMetrica 链接到您的项目：

        ```bash translate=no
        react-native link @appmetrica/react-native-analytics
        ```

    3. 如果你正在开发 iOS 项目，请在控制台执行此命令：

        ```bash translate=no
        npx pod-install
        ```

    4. 重建您的应用：

        ```bash translate=no
        # Android:
        npx react-native run-android
        # iOS:
        npx react-native run-ios
        ```

- Expo

    1. 插件使用原生的 Android 和 iOS 库。 要使用本地代码，请创建开发版本。 [了解更多](https://docs.expo.dev/workflow/using-libraries/#determining-third-party-library-compatibility).

        ```bash translate=no
        npx expo install expo-dev-client
        ```

    2. 在项目中安装 AppMetrica React Native 插件：

        ```bash translate=no
        npx expo install @appmetrica/react-native-analytics
        ```

    3. 重建您的应用：

        ```bash translate=no
        # Android:
        npx expo run:android
        # iOS:
        npx expo run:ios
        ```

{% endlist %}

## 步骤 2. 初始化 AppMetrica 库 {#initialization}

1. 在项目的源代码中导入库：

    ```javascript translate=no
    import AppMetrica from '@appmetrica/react-native-analytics';
    ```

    在这种情况下，在源代码中使用`AppMetrica`来工作于库。

2. 使用 AppMetrica 库的 `activate()` 方法进行初始化：

    ```javascript translate=no
    AppMetrica.activate({
      apiKey: 'Your API key',
      sessionTimeout: 120,
      logs: true
    });
    ```

    {% cut "什么是 API 密钥？" %}

    <!-- source: zh/sdk/_includes/api-key.md -->
    API 密钥是在AppMetrica网页界面进行[应用注册](https://appmetrica.yandex.com/application/new)时分配的唯一应用 ID。 你可以在 **设置** → **主页** 中找到它。
    <!-- endsource: zh/sdk/_includes/api-key.md -->

    {% endcut %}

3. 发送事件以测试库：

    ```javascript translate=no
    // 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')
    ```

示例项目：已集成AppMetrica SDK 的 [GitHub](https://github.com/yandexmobile/react-native-appmetrica/tree/master/example) 仓库。

## 了解更多 {#learn-more}

- [发送电商事件](https://yandex.com/support/appmetrica-io/zh/sdk/react-native/analytics/react-native-operations.md#send-ecommerce)
- [发送收入数据](https://yandex.com/support/appmetrica-io/zh/sdk/react-native/analytics/react-native-operations.md#send-revenue)
- [发送广告收入数据](https://yandex.com/support/appmetrica-io/zh/sdk/react-native/analytics/react-native-operations.md#send-adrevenue)
- [集成示例](https://github.com/yandexmobile/react-native-appmetrica/tree/master/example)
- [如何启用发送用户位置](https://yandex.com/support/appmetrica-io/zh/troubleshooting/troubleshooting.md#region)

<!-- source: zh/_includes/feedback-button-3.md -->
如果您没有找到所需答案，可以通过反馈表单提交您的问题。 请尽可能详细地描述问题。 如果可能，请附上截图。

<a href="../troubleshooting/feedback-new">
  <span class="button">联系支持</span>
</a>

<a href="../troubleshooting/feedback-docs">
  <span class="button">为文档提出改进建议</span>
</a>
<!-- endsource: zh/_includes/feedback-button-3.md -->
