@capacitor-firebase/analytics

中文文档
8.6.0发布于 2026/10/31Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 39.9万

Capacitor plugin for Firebase Analytics on Android, iOS, and Web.

原始使用文档
文档目录45 节
  1. Use Cases
  2. Compatibility
  3. Guides
  4. Installation
  5. Android
  6. iOS
  7. Configuration
  8. Demo
  9. Usage
  10. Log events
  11. Track screens
  12. Identify users
  13. Manage data collection
  14. Configure the session timeout
  15. Initiate on-device conversion measurement
  16. API
  17. getAppInstanceId()
  18. setConsent(...)
  19. setUserId(...)
  20. setUserProperty(...)
  21. setCurrentScreen(...)
  22. logEvent(...)
  23. setSessionTimeoutDuration(...)
  24. setEnabled(...)
  25. isEnabled()
  26. resetAnalyticsData()
  27. logTransaction(...)
  28. initiateOnDeviceConversionMeasurementWithEmailAddress(...)
  29. initiateOnDeviceConversionMeasurementWithPhoneNumber(...)
  30. initiateOnDeviceConversionMeasurementWithHashedEmailAddress…
  31. initiateOnDeviceConversionMeasurementWithHashedPhoneNumber(…
  32. Interfaces
  33. Enums
  34. Test your implementation
  35. FAQ
  36. How can I verify that my events are being logged?
  37. How can I disable analytics data collection?
  38. How do I set the user's consent mode?
  39. Why is the app instance ID not defined?
  40. How do I disable Advertising ID or IDFA collection?
  41. Can I use this plugin with Ionic, React, Vue or Angular?
  42. Related Plugins
  43. Newsletter
  44. Changelog
  45. License

Capacitor Firebase Analytics Plugin

Unofficial Capacitor plugin for Firebase Analytics.[^1]

Use Cases

The Firebase Analytics plugin is typically used to understand how users interact with your app, for example:

  • Event tracking: Log app events such as sign-ups with custom parameters using logEvent(...).
  • Screen tracking: Record which screens users visit using setCurrentScreen(...).
  • Audience segmentation: Assign user IDs and custom user properties to segment your users.
  • Consent management: Set the user's consent mode and enable or disable data collection to comply with privacy requirements.
  • Conversion measurement: Initiate on-device conversion measurement with an email address or phone number on iOS.

Compatibility

Plugin VersionCapacitor VersionStatus
8.x.x>=8.x.xActive support
7.x.x7.x.xDeprecated
6.x.x6.x.xDeprecated
5.x.x5.x.xDeprecated
1.x.x4.x.xDeprecated

Guides

Installation

You can use our AI-Assisted Setup to install the plugin. Add the Capawesome Skills to your AI tool using the following command:

npx skills add capawesome-team/skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capacitor-firebase/analytics` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

npm install @capacitor-firebase/analytics firebase
npx cap sync

Add Firebase to your project if you haven't already (Android / iOS / Web).

Android

Disable Analytics data collection

See Disable Analytics data collection if you want to disable Analytics data collection.

Disable Advertising ID collection

See Disable Advertising ID collection if you want to disable Advertising ID collection.

Variables

If needed, you can define the following project variable in your app’s variables.gradle file to change the default version of the dependency:

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

This can be useful if you encounter dependency conflicts with other plugins in your project.

iOS

Swift Package Manager

Add the following to your capacitor.config.json (or capacitor.config.ts) to avoid a SwiftPM package identity collision:

{
  "experimental": {
    "ios": {
      "spm": {
        "packageOptions": {
          "@capacitor-firebase/analytics": {
            "symlink": true
          }
        }
      }
    }
  }
}

Attention: SPM packageOptions support requires Capacitor CLI 8.4.0+.

If you are using CocoaPods for your iOS project, you need to add the CapacitorFirebaseAnalytics/Analytics pod to your Podfile (usually ios/App/Podfile):

target 'App' do
capacitor_pods
# Add your Pods here
+  pod 'CapacitorFirebaseAnalytics/Analytics', :path => '../../node_modules/@capacitor-firebase/analytics'
end

Attention: Do not add the pod in the section def capacitor_pods, but under the comment # Add your Pods here (example).

Disable Analytics data collection

See Disable Analytics data collection if you want to disable Analytics data collection.

Disable IDFA collection

If you are using CocoaPods for your iOS project and you want to disable IDFA collection, you can use the CapacitorFirebaseAnalytics/AnalyticsWithoutAdIdSupport pod instead of the CapacitorFirebaseAnalytics/Analytics pod:

target 'App' do
capacitor_pods
# Add your Pods here
-  pod 'CapacitorFirebaseAnalytics/Analytics', :path => '../../node_modules/@capacitor-firebase/analytics'
+  pod 'CapacitorFirebaseAnalytics/AnalyticsWithoutAdIdSupport', :path => '../../node_modules/@capacitor-firebase/analytics'
end

If you are using Swift Package Manager for your iOS project and you want to disable IDFA collection, you can enable the AnalyticsWithoutAdIdSupport trait in your capacitor.config.json (or capacitor.config.ts):

{
  "experimental": {
    "ios": {
      "spm": {
        "swiftToolsVersion": "6.1",
        "packageTraits": {
          "@capacitor-firebase/analytics": ["AnalyticsWithoutAdIdSupport"]
        }
      }
    }
  }
}

Note: SPM trait support requires Capacitor CLI 8.3.0+ and Xcode 16.3+ (Swift 6.1+).

Configuration

No configuration required for this plugin.

Demo

A working example can be found here: robingenz/capacitor-firebase-plugin-demo

Usage

The following examples show how to log events, track screens, identify users, manage data collection, configure the session timeout, and initiate on-device conversion measurement.

Log events

Log an app event with a name and optional parameters:

import { FirebaseAnalytics } from '@capacitor-firebase/analytics';

const logEvent = async () => {
  await FirebaseAnalytics.logEvent({
    name: 'sign_up',
    params: { method: 'password' },
  });
};

Track screens

Set the current screen name to record which screens users visit. The screenClassOverride option is only available on Android and iOS:

import { FirebaseAnalytics } from '@capacitor-firebase/analytics';

const setCurrentScreen = async () => {
  await FirebaseAnalytics.setCurrentScreen({
    screenName: 'Login',
    screenClassOverride: 'LoginPage',
  });
};

Identify users

Set the user ID property and custom user properties to segment your users:

import { FirebaseAnalytics } from '@capacitor-firebase/analytics';

const setUserId = async () => {
  await FirebaseAnalytics.setUserId({
    userId: '123',
  });
};

const setUserProperty = async () => {
  await FirebaseAnalytics.setUserProperty({
    key: 'language',
    value: 'en',
  });
};

Manage data collection

Enable or disable automatic data collection (the value does not apply until the next run of the app), check whether it is enabled (only available on Web), or clear all analytics data from the device (only available on Android and iOS):

import { FirebaseAnalytics } from '@capacitor-firebase/analytics';

const setEnabled = async () => {
  await FirebaseAnalytics.setEnabled({
    enabled: true,
  });
};

const isEnabled = async () => {
  const { enabled } = await FirebaseAnalytics.isEnabled();
  return enabled;
};

const resetAnalyticsData = async () => {
  await FirebaseAnalytics.resetAnalyticsData();
};

Configure the session timeout

Set the duration of inactivity that terminates the current session. Only available on Android and iOS:

import { FirebaseAnalytics } from '@capacitor-firebase/analytics';

const setSessionTimeoutDuration = async () => {
  await FirebaseAnalytics.setSessionTimeoutDuration({
    duration: '120',
  });
};

Initiate on-device conversion measurement

Initiate on-device conversion measurement with a plain or hashed email address or phone number. Only available on iOS:

import { FirebaseAnalytics } from '@capacitor-firebase/analytics';

const initiateOnDeviceConversionMeasurementWithEmailAddress = async () => {
  await FirebaseAnalytics.initiateOnDeviceConversionMeasurementWithEmailAddress({
    emailAddress: '[email protected]',
  });
};

const initiateOnDeviceConversionMeasurementWithPhoneNumber = async () => {
  await FirebaseAnalytics.initiateOnDeviceConversionMeasurementWithPhoneNumber({
    phoneNumber: '+49123456789',
  });
};

const initiateOnDeviceConversionMeasurementWithHashedEmailAddress = async () => {
  await FirebaseAnalytics.initiateOnDeviceConversionMeasurementWithHashedEmailAddress({
    emailAddressToHash: '[email protected]',
  });
};

const initiateOnDeviceConversionMeasurementWithHashedPhoneNumber = async () => {
  await FirebaseAnalytics.initiateOnDeviceConversionMeasurementWithHashedPhoneNumber({
    phoneNumberToHash: '+49123456789',
  });
};

API

getAppInstanceId()

getAppInstanceId() => Promise<GetAppInstanceIdResult>

Retrieves the app instance id.

Only available for Android and iOS.

Returns: Promise<GetAppInstanceIdResult>

Since: 1.4.0


setConsent(...)

setConsent(options: SetConsentOptions) => Promise<void>

Sets the user's consent mode.

ParamType
optionsSetConsentOptions

Since: 6.0.0


setUserId(...)

setUserId(options: SetUserIdOptions) => Promise<void>

Sets the user ID property.

ParamType
optionsSetUserIdOptions

Since: 0.1.0


setUserProperty(...)

setUserProperty(options: SetUserPropertyOptions) => Promise<void>

Sets a custom user property to a given value.

ParamType
optionsSetUserPropertyOptions

Since: 0.1.0


setCurrentScreen(...)

setCurrentScreen(options: SetCurrentScreenOptions) => Promise<void>

Sets the current screen name.

ParamType
optionsSetCurrentScreenOptions

Since: 0.1.0


logEvent(...)

logEvent(options: LogEventOptions) => Promise<void>

Logs an app event.

ParamType
optionsLogEventOptions

Since: 0.1.0


setSessionTimeoutDuration(...)

setSessionTimeoutDuration(options: SetSessionTimeoutDurationOptions) => Promise<void>

Sets the duration of inactivity that terminates the current session.

Only available for Android and iOS.

ParamType
optionsSetSessionTimeoutDurationOptions

Since: 0.1.0


setEnabled(...)

setEnabled(options: SetEnabledOptions) => Promise<void>

Enables/disables automatic data collection. The value does not apply until the next run of the app.

ParamType
optionsSetEnabledOptions

Since: 0.1.0


isEnabled()

isEnabled() => Promise<IsEnabledResult>

Returns whether or not automatic data collection is enabled.

Only available for Web.

Returns: Promise<IsEnabledResult>

Since: 0.1.0


resetAnalyticsData()

resetAnalyticsData() => Promise<void>

Clears all analytics data for this app from the device. Resets the app instance id.

Only available for Android and iOS.

Since: 0.1.0


logTransaction(...)

logTransaction(options: LogTransactionOptions) => Promise<void>

Logs a StoreKit 2 transaction.

Only available for iOS (15.0+).

ParamType
optionsLogTransactionOptions

Since: 8.2.0


initiateOnDeviceConversionMeasurementWithEmailAddress(...)

initiateOnDeviceConversionMeasurementWithEmailAddress(options: InitiateOnDeviceConversionMeasurementWithEmailAddressOptions) => Promise<void>

Initiates on-device conversion measurement with an email address.

Only available for iOS.

ParamType
optionsInitiateOnDeviceConversionMeasurementWithEmailAddressOptions

Since: 7.2.0


initiateOnDeviceConversionMeasurementWithPhoneNumber(...)

initiateOnDeviceConversionMeasurementWithPhoneNumber(options: InitiateOnDeviceConversionMeasurementWithPhoneNumberOptions) => Promise<void>

Initiates on-device conversion measurement with a phone number.

Only available for iOS.

ParamType
optionsInitiateOnDeviceConversionMeasurementWithPhoneNumberOptions

Since: 7.2.0


initiateOnDeviceConversionMeasurementWithHashedEmailAddress(...)

initiateOnDeviceConversionMeasurementWithHashedEmailAddress(options: InitiateOnDeviceConversionMeasurementWithHashedEmailAddressOptions) => Promise<void>

Initiates on-device conversion measurement with a hashed email address.

Only available for iOS.

ParamType
optionsInitiateOnDeviceConversionMeasurementWithHashedEmailAddressOptions

Since: 7.2.0


initiateOnDeviceConversionMeasurementWithHashedPhoneNumber(...)

initiateOnDeviceConversionMeasurementWithHashedPhoneNumber(options: InitiateOnDeviceConversionMeasurementWithHashedPhoneNumberOptions) => Promise<void>

Initiates on-device conversion measurement with a hashed phone number.

Only available for iOS.

ParamType
optionsInitiateOnDeviceConversionMeasurementWithHashedPhoneNumberOptions

Since: 7.2.0


Interfaces

GetAppInstanceIdResult

PropTypeDescriptionSince
appInstanceIdstringThe app instance id. Not defined if FirebaseAnalytics.<a href="#consenttype">ConsentType</a>.ANALYTICS_STORAGE has been set to FirebaseAnalytics.<a href="#consentstatus">ConsentStatus</a>.DENIED.1.4.0

SetConsentOptions

PropTypeDescriptionSince
typeConsentTypeThe consent type.6.0.0
statusConsentStatusThe consent status.6.0.0

SetUserIdOptions

PropTypeSince
userIdstring | null0.1.0

SetUserPropertyOptions

PropTypeSince
keystring0.1.0
valuestring | null0.1.0

SetCurrentScreenOptions

PropTypeDescriptionDefaultSince
screenNamestring | null0.1.0
screenClassOverridestring | nullOnly available for Android and iOS.null0.1.0

LogEventOptions

PropTypeDescriptionSince
namestringThe event name.0.1.0
params{ [key: string]: any; }The optional event params.0.1.0

SetSessionTimeoutDurationOptions

PropTypeDescriptionDefaultSince
durationnumberDuration in seconds.18000.1.0

SetEnabledOptions

PropTypeSince
enabledboolean0.1.0

IsEnabledResult

PropTypeSince
enabledboolean0.1.0

LogTransactionOptions

PropTypeDescriptionSince
transactionIdstringThe StoreKit 2 Transaction.id value as a numeric string.8.2.0

InitiateOnDeviceConversionMeasurementWithEmailAddressOptions

PropTypeDescriptionSince
emailAddressstringThe email address to initiate on-device conversion measurement with.7.2.0

InitiateOnDeviceConversionMeasurementWithPhoneNumberOptions

PropTypeDescriptionSince
phoneNumberstringThe phone number to initiate on-device conversion measurement with.7.2.0

InitiateOnDeviceConversionMeasurementWithHashedEmailAddressOptions

PropTypeDescriptionSince
emailAddressToHashstringThe email address to initiate on-device conversion measurement with.7.2.0

InitiateOnDeviceConversionMeasurementWithHashedPhoneNumberOptions

PropTypeDescriptionSince
phoneNumberToHashstringThe phone number to initiate on-device conversion measurement with.7.2.0

Enums

ConsentType

MembersValueSince
AdPersonalization'AD_PERSONALIZATION'6.0.0
AdStorage'AD_STORAGE'6.0.0
AdUserData'AD_USER_DATA'6.0.0
AnalyticsStorage'ANALYTICS_STORAGE'6.0.0
FunctionalityStorage'FUNCTIONALITY_STORAGE'6.0.0
PersonalizationStorage'PERSONALIZATION_STORAGE'6.0.0

ConsentStatus

MembersValueSince
Granted'GRANTED'6.0.0
Denied'DENIED'6.0.0

Test your implementation

Here you can find more information on how to test the Firebase Analytics implementation using the DebugView.

FAQ

How can I verify that my events are being logged?

Use the DebugView in the Firebase console to inspect events as they are logged by your app. See the DebugView documentation for more information on how to test your Firebase Analytics implementation.

How can I disable analytics data collection?

You can enable or disable automatic data collection at runtime using the setEnabled(...) method; note that the value does not apply until the next run of the app. To disable data collection by default, follow the platform-specific instructions linked in the Installation section for Android and iOS.

Call the setConsent(...) method with a consent type (for example analytics storage or ad storage) and a consent status (granted or denied). This allows you to manage what data is collected based on the user's consent.

Why is the app instance ID not defined?

The appInstanceId returned by getAppInstanceId() is not defined if the analytics storage consent type has been set to denied via setConsent(...). Also note that getAppInstanceId() is only available on Android and iOS.

How do I disable Advertising ID or IDFA collection?

On Android, follow the Disable Advertising ID collection guide. On iOS, use the CapacitorFirebaseAnalytics/AnalyticsWithoutAdIdSupport pod instead of the CapacitorFirebaseAnalytics/Analytics pod if you are using CocoaPods, or enable the AnalyticsWithoutAdIdSupport package trait if you are using Swift Package Manager. See the Installation section for details.

Can I use this plugin with Ionic, React, Vue or Angular?

Yes, the plugin is framework-agnostic. It works in any Capacitor app regardless of the web framework, including Ionic with Angular, React, or Vue, as well as plain JavaScript projects.

Newsletter

Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our Capawesome Newsletter.

Changelog

See CHANGELOG.md.

License

See LICENSE.

[^1]: This project is not affiliated with, endorsed by, sponsored by, or approved by Google LLC or any of their affiliates or subsidiaries.

版本与发布记录

@capacitor-firebase/analytics 共发布 58 个版本,首次发布于 2022/03/20;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.6.0latest2026/10/319 天前
8.5.22026/09/171 个月前
8.5.12026/09/022 个月前
8.5.02026/08/312 个月前
8.4.02026/08/122 个月前
8.3.02026/06/045 个月前

其他发布通道:dev → 7.0.0-dev.1765901228。

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

@capacitor-firebase/analytics 是什么?

Capacitor plugin for Firebase Analytics on Android, iOS, and Web.

如何安装 @capacitor-firebase/analytics?

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

@capacitor-firebase/analytics 支持 TypeScript 吗?

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

@capacitor-firebase/analytics 支持 ESM 还是 CommonJS?

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

@capacitor-firebase/analytics 可以商用吗?

@capacitor-firebase/analytics 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@capacitor-firebase/analytics 的下载量有多少?

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

@capacitor-firebase/analytics 还在维护吗?

最新版本 8.6.0 发布于 2026/10/31(9 天前);至今共发布 58 个版本,其中过去 12 个月发布了 14 个;首次发布于 2022/03/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。