# @capacitor-community/firebase-analytics 中文文档

规范地址：https://npmcn.com/package/%40capacitor-community/firebase-analytics/

版本：8\.1\.0
npm 元数据更新时间：2026-10-23T21:47:46.691Z
中文译文更新时间：2026-11-09T02:35:25.535Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40capacitor-community/firebase-analytics
原文对照：https://npmcn.com/package/%40capacitor-community/firebase-analytics/?lang=original
项目主页：https://github.com/capacitor-community/firebase-analytics
源码仓库：https://github.com/capacitor-community/firebase-analytics

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：116 kB，23 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：@capacitor/core \>=8\.0\.0
- 首次发布：2020-06-23T17:27:56.204Z
- 最新版本发布：2026-08-27T13:33:53.347Z
- 近 30 天下载量：94616（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：32（近 12 个月 3 个）

## 中文简介

火堆分析的本地插件 .

## 安装

```sh
npm install @capacitor-community/firebase-analytics
```

也可以使用：`pnpm add @capacitor-community/firebase-analytics`、`yarn add @capacitor-community/firebase-analytics`、`bun add @capacitor-community/firebase-analytics`

## README

  
![](https://user-images.githubusercontent.com/236501/85893648-1c92e880-b7a8-11ea-926d-95355b8175c7.png)

### 火场分析

**`@capacitor-community/firebase-analytics`**

本地电容社区插件 [火场分析](https://firebase.google.com/docs/analytics)。 。 。 .

![](https://img.shields.io/maintenance/yes/2025?style=flat-square) [![](https://img.shields.io/github/actions/workflow/status/capacitor-community/firebase-analytics/test.yml?Test%20and%20Build%20Plugin?style=flat-square)](https://github.com/capacitor-community/firebase-analytics/actions?query=workflow%3A%22Plugin+Unit+Tests%22) [![](https://img.shields.io/npm/l/@capacitor-community/firebase-analytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-community/firebase-analytics)  
[![](https://img.shields.io/npm/dw/@capacitor-community/firebase-analytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-community/firebase-analytics) [![](https://img.shields.io/npm/v/@capacitor-community/firebase-analytics?style=flat-square)](https://www.npmjs.com/package/@capacitor-community/firebase-analytics) [![](https://img.shields.io/badge/all%20contributors-14-orange?style=flat-square)](https://npmcn.com/package/%40capacitor-community/firebase-analytics/#readme-contributors-)

## QQ 更新到QQv8. 0.0QQ时突破更改

✏️✏️ 这个插件现在只支持 Capacitor 8 。 对于7、6、5和4号电容器,请使用以下所列版本

## 插件版本

| 电容版本 | 插件版本 |
| :-- | :-- |
| 8.x 电话 | 8.x 电话 |
| 7.x 电话 | 7.x 电话 |
| 6.x (单位:千美元) | 6.x (单位:千美元) |
| 5.x 电话 | 第5.0.1条 |
| 4.x (中文(简体) ) | 1.0.1 (中文(简体) ) |

## 安装

使用 npm :

```bash
npm install @capacitor-community/firebase-analytics@latest
```

使用线条 :

```bash
yarn add @capacitor-community/firebase-analytics@latest
```

同步本地文件 :

```bash
# Update the native plugins and dependencies referenced in package.json
npx cap sync
```

> **说明:** 在iOS上,CocoaPods和Swift包管理器都得到了支持. `npx cap sync` 配置您的应用程序使用的任意一个 .

> **说明:** 您可能还需要运行 **文件 > 与 Gradle 文件同步工程** 以便Android Studio识别导入.

## 配置

此插件不需要配置 .

## 实例

[点击这里](https://github.com/jcesarmobile/capacitor-firebase-analytics-example) 对于如何执行此插件的示例.

您也可以克隆仓库 :

```bash
git clone https://github.com/jcesarmobile/capacitor-firebase-analytics-example
```

## 支持的方法

| 名称 | 机器人 | iOS系统 | 网页 |
| :-- | :-- | :-- | :-- |
| 设置用户 | ✅ | ✅ | ✅ |
| 产品 | ✅ | ✅ | ✅ |
| 获取 AppInstanceId | ✅ | ✅ | ❌ |
| 设置ScreenName | ✅ | ✅ | ❌ |
| 重设 | ✅ | ✅ | ❌ |
| 日志事件 | ✅ | ✅ | ✅ |
| 集成已启用 | ✅ | ✅ | ✅ |
| 时长 | ✅ | ✅ | ✅ |
| 启用 | ✅ | ✅ | ✅ |
| 禁用 | ✅ | ✅ | ✅ |

## 使用量

```typescript
import { FirebaseAnalytics } from "@capacitor-community/firebase-analytics";

/**
 * Platform: Web
 * Configure and initialize the firebase app.
 * @param options - firebase web app configuration options
 * */
FirebaseAnalytics.initializeFirebase({
  apiKey: "...",
  authDomain: "...",
  databaseURL: "...",
  projectId: "...",
  storageBucket: "...",
  messagingSenderId: "...",
  appId: "...",
  measurementId: "...",
});

/**
 * Platform: Web/Android/iOS
 * Sets the user ID property.
 * @param userId - unique identifier of a user
 * @returns void
 * https://firebase.google.com/docs/analytics/userid
 */
FirebaseAnalytics.setUserId({
  userId: "john_doe_123",
});

/**
 * Platform: Web/Android/iOS
 * Sets a user property to a given value.
 * @param options - property name and value to set
 * @returns void
 * https://firebase.google.com/docs/analytics/user-properties
 */
FirebaseAnalytics.setUserProperty({
  name: "favorite_food",
  value: "pizza",
});

/**
 * Platform: Android/iOS
 * Retrieves the app instance id from the service.
 * @param none
 * @returns instanceId - individual instance id value
 * https://firebase.google.com/docs/analytics/user-properties
 */
FirebaseAnalytics.getAppInstanceId();

/**
 * Platform: Android/iOS
 * Sets the current screen name, which specifies the current visual context in your app.
 * @param screenName - name of the current screen to track
 *        nameOverride - name of the screen class to override
 * @returns instanceId - individual instance id value
 * https://firebase.google.com/docs/analytics/screenviews
 */
FirebaseAnalytics.setScreenName({
  screenName: "login",
  nameOverride: "LoginScreen",
});

/**
 * Platform: Web/Android/iOS
 * Clears all analytics data for this app from the device and resets the app instance id.
 * @param none
 * @returns void
 */
FirebaseAnalytics.reset();

/**
 * Platform: Web/Android/iOS
 * Logs an app event.
 * @param name - name of the event to log
 *        params - key/value pairs of properties (25 maximum per event)
 * @returns void
 */
FirebaseAnalytics.logEvent({
  name: "select_content",
  params: {
    content_type: "image",
    content_id: "P12453",
    items: [{ name: "Kittens" }],
  },
});

/**
 * Platform: Web/Android/iOS
 * Sets whether analytics collection is enabled for this app on this device.
 * @param name - enabled - boolean true/false
 * @returns void
 */
FirebaseAnalytics.setCollectionEnabled({
  enabled: false,
});

/**
 * Platform: Web/Android/iOS
 * Deprecated - use setCollectionEnabled() instead
 * Enable analytics collection for this app on this device.
 * @param none
 * @returns void
 */
FirebaseAnalytics.enable();

/**
 * Platform: Web/Android/iOS
 * Deprecated - use setCollectionEnabled() instead
 * Disable analytics collection for this app on this device.
 * @param none
 * @returns void
 */
FirebaseAnalytics.disable();

/**
 * Platform: Web/Android/iOS
 * Sets the duration of inactivity that terminates the current session.
 * @param duration - duration in seconds (default - 18000)
 * @returns void
 */
FirebaseAnalytics.setSessionTimeoutDuration({
  duration: 10000,
});
```

## 设置

导航您在 Firebase 上的应用程序的工程设置页面 .

### iOS系统

下载 `GoogleService-Info.plist` 文档。 在 Xcode 中右键单击名为“ App” 的黄色文件夹,并选择 `Add files to "App"`。 。 。 .

> 提示:如果拖放文件到此位置, Xcode 可能找不到它 .

### 机器人

下载 `google-services.json` 存档并复制到 `android/app/` 您的电容器项目目录 .

#### 变量

此插件将使用以下工程变量( 在您的应用程序中定义) `variables.gradle` 文件名 :

-   `$firebaseAnalyticsVersion` 版本 `com.google.firebase:firebase-analytics` (默认 : `21.2.2`(中文(简体) )

## iOS 设置

-   `ionic start my-cap-app --capacitor`
-   `cd my-cap-app`
-   `npm install --save @capacitor-community/firebase-analytics`
-   `mkdir www && touch www/index.html`
-   `sudo gem install cocoapods` (只有一次,而且只针对以可可Pods为基础的应用)
-   `npx cap add ios`
-   `npx cap sync ios` (每次你跑动) `npm install`(中文(简体) )
-   `npx cap open ios`
-   在 xcode( 一般标签) 签名您的应用程序
-   添加 `GoogleService-Info.plist` 到 xcode 中的应用程序文件夹

### 启用调试视图

1.  在 Xcode 中,选择产品 > 方案 > 编辑方案
2.  从左菜单中选择运行
3.  选择参数标签
4.  在发射时通过的论点部分,添加 `-FIRAnalyticsDebugEnabled`

> 提示:每次您更改本地代码时, 您可能需要清理缓存( Product > Clean building文件夹) , 然后再次运行应用程序 .

## 机器人设置

-   `ionic start my-cap-app --capacitor`
-   `cd my-cap-app`
-   `npm install --save @capacitor-community/firebase-analytics`
-   `mkdir www && touch www/index.html`
-   `npx cap add android`
-   `npx cap sync android` (每次你跑动) `npm install`(中文(简体) )
-   `npx cap open android`
-   添加 `google-services.json` 给您的 `android/app` 文件夹

现在你该走了 尝试使用 `ionic cap run android --livereload --address=0.0.0.0`。 。 。 .

> 提示:每次您更改本地代码, 您可能需要清理缓存( Build > Clean Project QQ Build > Rebuilt Project) , 然后再次运行应用程序 .

## 更新

对于已有的项目, 您可以使用此单项命令升级所有与电容器相关的软件包( 包括此插件)

`npx npm-upgrade '*capacitor*' && npm install`

## 移徙

如果你以前使用过 `capacitor-analytics` npm 软件包

1.  更新国家防范机制一揽子计划:
    
    ```bash
    npm uninstall --save capacitor-analytics
    npm install --save-prod @capacitor-community/firebase-analytics@latest
    ```
    
2.  更新Android的插件初始化 _主要活动. java_
    
    删除旧插件导入 :
    
    ```diff
    -import io.stewan.capacitor.analytics.AnalyticsPlugin;
    ```
    
    更新 `init()` 调用以删除旧插件导入 。 你也许可以把全部都删除 `init()` 如果里面没有别的东西就打电话.
    
    ```diff
    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
    - add(AnalyticsPlugin.class);
    }});
    ```
    
3.  公共 API 更改 :
    
    -   `instance()` 已更名为 `getAppInstanceId()`
    -   `setScreen()` 已更名为 `setScreenName()`
    -   `setUserID()` 已更名为 `setUserId()`
    -   `setUserProp()` 已更名为 `setUserProperty()`
    -   `enable()` 已经贬值,有利于 `setCollectionEnabled()`
    -   `disable()` 已经贬值,有利于 `setCollectionEnabled()`

## 进一步信息

-   [机器人](https://firebase.google.com/docs/android/setup)
-   [iOS系统](https://firebase.google.com/docs/analytics/get-started?platform=ios)
-   [网页](https://firebase.google.com/docs/analytics/get-started?platform=web)

## 贡献者

感谢这些伟大的人[emoji 密钥](https://allcontributors.org/docs/en/emoji-key):

<table><tbody><tr><td align="center"><a href="https://github.com/brownoxford" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/755209?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>克里斯·阿伯内特</b></sub></a><br><a href="https://npmcn.com/package/%40capacitor-community/firebase-analytics/#readme-maintenance-brownoxford" title="Maintenance" rel="nofollow noopener noreferrer">🚧</a></td><td align="center"><a href="http://priyankpatel.io/" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/5585797?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>普里扬克帕特尔</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/commits?author=priyankpat" title="Code" rel="nofollow noopener noreferrer">💻</a></td><td align="center"><a href="http://github.com/stewones" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/19799027?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>炖肉</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/commits?author=stewwan" title="Code" rel="nofollow noopener noreferrer">💻</a></td><td align="center"><a href="https://www.codewithkarma.com/" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/6672354?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>卡尔姆吉特·辛格</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/commits?author=karm435" title="Tests" rel="nofollow noopener noreferrer">⚠️</a> <a href="https://github.com/capacitor-community/firebase-analytics/issues?q=author%3Akarm435" title="Bug reports" rel="nofollow noopener noreferrer">🐛</a></td><td align="center"><a href="https://github.com/mRoca" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/4746261?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>米歇尔·罗卡</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/issues?q=author%3AmRoca" title="Bug reports" rel="nofollow noopener noreferrer">🐛</a></td><td align="center"><a href="http://www.ultramixer.com/" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/1584274?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>马提亚斯</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/pulls?q=is%3Apr+reviewed-by%3Aslajar" title="Reviewed Pull Requests" rel="nofollow noopener noreferrer">👀</a></td><td align="center"><a href="http://www.ultramixer.com/" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/2484805?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>托比语Name</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/commits?author=tobium" title="Code" rel="nofollow noopener noreferrer">💻</a></td></tr><tr><td align="center"><a href="https://github.com/salohcin714" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/41271531?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>尼古拉斯·诺里斯</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/issues?q=author%3Asalohcin714" title="Bug reports" rel="nofollow noopener noreferrer">🐛</a></td><td align="center"><a href="https://github.com/gabrielscarvalho" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/1574205?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>加布里埃尔斯卡瓦略</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/issues?q=author%3Agabrielscarvalho" title="Bug reports" rel="nofollow noopener noreferrer">🐛</a> <a href="https://github.com/capacitor-community/firebase-analytics/commits?author=gabrielscarvalho" title="Code" rel="nofollow noopener noreferrer">💻</a></td><td align="center"><a href="https://github.com/ptmkenny" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/1451472?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>普克尼语name</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/issues?q=author%3Aptmkenny" title="Bug reports" rel="nofollow noopener noreferrer">🐛</a> <a href="https://github.com/capacitor-community/firebase-analytics/commits?author=ptmkenny" title="Code" rel="nofollow noopener noreferrer">💻</a> <a href="https://github.com/capacitor-community/firebase-analytics/commits?author=ptmkenny" title="Documentation" rel="nofollow noopener noreferrer">📖</a></td><td align="center"><a href="https://github.com/vkyeswa" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/5016129?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>维基百科</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/issues?q=author%3Avkyeswa" title="Bug reports" rel="nofollow noopener noreferrer">🐛</a></td><td align="center"><a href="https://github.com/losciur" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/62714342?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>卢克西</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/commits?author=losciur" title="Tests" rel="nofollow noopener noreferrer">⚠️</a> <a href="https://github.com/capacitor-community/firebase-analytics/issues?q=author%3Alosciur" title="Bug reports" rel="nofollow noopener noreferrer">🐛</a></td><td align="center"><a href="https://github.com/wyattades" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/992076?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>阴道</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/commits?author=wyattades" title="Code" rel="nofollow noopener noreferrer">💻</a></td><td align="center"><a href="https://github.com/quqkuk" rel="nofollow noopener noreferrer"><img alt="" src="https://avatars.githubusercontent.com/u/40238888?v=4?s=100" width="100px;" loading="lazy" decoding="async" referrerpolicy="no-referrer"><br><sub><b>卢克西</b></sub></a><br><a href="https://github.com/capacitor-community/firebase-analytics/commits?author=quqkuk" title="Code" rel="nofollow noopener noreferrer">💻</a></td></tr></tbody></table>

本项目遵循 [全部贡献者](https://github.com/all-contributors/all-contributors) 规格。 任何捐款都欢迎!

## 常见问题

### @capacitor-community/firebase-analytics 是什么？

火堆分析的本地插件 \.

### 如何安装 @capacitor-community/firebase-analytics？

在项目目录运行 npm install @capacitor-community/firebase-analytics，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@capacitor/core（\>=8\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @capacitor-community/firebase-analytics 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @capacitor-community/firebase-analytics 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @capacitor-community/firebase-analytics 可以商用吗？

@capacitor-community/firebase-analytics 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @capacitor-community/firebase-analytics 的下载量有多少？

根据 npm 官方下载统计，@capacitor-community/firebase-analytics 在 2026/10/09 – 2026/11/07 这 30 天内被下载 94,616 次，日均约 3,154 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @capacitor-community/firebase-analytics 还在维护吗？

最新版本 8\.1\.0 发布于 2026/08/27（2 个月前）；至今共发布 32 个版本，其中过去 12 个月发布了 3 个；首次发布于 2020/06/24。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

