@capacitor-community/firebase-analytics

中文文档
8.1.0发布于 2026/08/27MITTypeScriptESM + CJS零依赖近 30 天下载 9.5万

A native plugin for firebase analytics.

原始使用文档
文档目录18 节
  1. Firebase Analytics
  2. ❗❗ Breaking changes when updating to ✏️v8.0.0✏️❗❗
  3. Plugin versions
  4. Installation
  5. Configuration
  6. Examples
  7. Supported methods
  8. Usage
  9. Setup
  10. iOS
  11. Android
  12. iOS setup
  13. Enable debug view
  14. Android setup
  15. Updating
  16. Migration
  17. Further info
  18. Contributors ✨


Firebase Analytics

@capacitor-community/firebase-analytics

Capacitor community plugin for native Firebase Analytics.


❗❗ Breaking changes when updating to ✏️v8.0.0✏️❗❗

✏️✏️ This plugin now supports Capacitor 8 only. For Capacitor 7, 6, 5 and 4 please use version listed below ✏️✏️

Plugin versions

Capacitor versionPlugin version
8.x8.x
7.x7.x
6.x6.x
5.x5.0.1
4.x1.0.1

Installation

Using npm:

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

Using yarn:

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

Sync native files:

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

Note: On iOS, both CocoaPods and Swift Package Manager are supported. npx cap sync configures whichever one your app uses.

Note: You may also need to run File > Sync Project with Gradle Files in order for Android Studio to recognize the import.

Configuration

No configuration is required for this plugin.

Examples

Click here for an example on how to implement this plugin.

You can also clone the repository:

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

Supported methods

NameAndroidiOSWeb
setUserId✅✅✅
setUserProperty✅✅✅
getAppInstanceId✅✅❌
setScreenName✅✅❌
reset✅✅❌
logEvent✅✅✅
setCollectionEnabled✅✅✅
setSessionTimeoutDuration✅✅✅
enable✅✅✅
disable✅✅✅

Usage

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,
});

Setup

Navigate to the project settings page for your app on Firebase.

iOS

Download the GoogleService-Info.plist file. In Xcode right-click on the yellow folder named "App" and select the Add files to "App".

Tip: if you drag and drop your file to this location, Xcode may not be able to find it.

Android

Download the google-services.json file and copy it to android/app/ directory of your capacitor project.

Variables

This plugin will use the following project variables (defined in your app’s variables.gradle file):

  • $firebaseAnalyticsVersion version of com.google.firebase:firebase-analytics (default: 21.2.2)

iOS setup

  • 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 (only once, and only for CocoaPods-based apps)
  • npx cap add ios
  • npx cap sync ios (every time you run npm install)
  • npx cap open ios
  • sign your app at xcode (general tab)
  • add GoogleService-Info.plist to the app folder in xcode

Enable debug view

  1. In Xcode, select Product > Scheme > Edit scheme
  2. Select Run from the left menu
  3. Select the Arguments tab
  4. In the Arguments Passed On Launch section, add -FIRAnalyticsDebugEnabled

Tip: every time you change a native code you may need to clean up the cache (Product > Clean build folder) and then run the app again.

Android setup

  • 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 (every time you run npm install)
  • npx cap open android
  • add google-services.json to your android/app folder

Now you should be set to go. Try to run your client using ionic cap run android --livereload --address=0.0.0.0.

Tip: every time you change a native code you may need to clean up the cache (Build > Clean Project | Build > Rebuild Project) and then run the app again.

Updating

For existing projects you can upgrade all capacitor related packages (including this plugin) with this single command

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

Migration

If you were previously using the capacitor-analytics package from npm

  1. Update NPM package:

    npm uninstall --save capacitor-analytics
    npm install --save-prod @capacitor-community/firebase-analytics@latest
    
  2. Update the plugin initialization in Android's MainActivity.java

    Remove the old plugin import:

    -import io.stewan.capacitor.analytics.AnalyticsPlugin;
    

    Update the init() call to remove the old plugin import. You may be able to remove the entire init() call if there is nothing else in there.

    // 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. Public API changes:

    • instance() has been renamed to getAppInstanceId()
    • setScreen() has been renamed to setScreenName()
    • setUserID() has been renamed to setUserId()
    • setUserProp() has been renamed to setUserProperty()
    • enable() has been deprecated in favor of setCollectionEnabled()
    • disable() has been deprecated in favor of setCollectionEnabled()

Further info

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Chris Abernethy

🚧

Priyank Patel

💻

stewwan

💻

Karmjit Singh

⚠️ 🐛

Michel Roca

🐛

Matthias

👀

Tobi

💻

Nicholas Norris

🐛

gabrielscarvalho

🐛 💻

ptmkenny

🐛 💻 📖

vkyeswa

🐛

losciur

⚠️ 🐛

wyattades

💻

losciur

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

版本与发布记录

@capacitor-community/firebase-analytics 共发布 32 个版本,首次发布于 2020/06/24;过去 12 个月发布了 3 个版本。

版本发布日期距今
8.1.0latest2026/08/272 个月前
8.0.0-dev.96b25f48.1786515572dev2026/08/122 个月前
8.0.02026/03/207 个月前
7.0.12025/06/191.4 年前
7.0.02025/03/041.7 年前
6.0.02024/05/032 年前

其他发布通道:alpha → 1.0.1-alpha.0,capacitor2-lts → 0.3.3,next → 2.0.0-1,dev → 8.0.0-dev.96b25f48.1786515572。

关于 @capacitor-community/firebase-analytics 的常见问题

@capacitor-community/firebase-analytics 是什么?

A native plugin for 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 和提交记录判断。