@capacitor-firebase/performance

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

Capacitor plugin for Firebase Performance Monitoring on Android, iOS, and Web.

原始使用文档
文档目录39 节
  1. Use Cases
  2. Compatibility
  3. Guides
  4. Installation
  5. Android
  6. iOS
  7. Configuration
  8. Demo
  9. Usage
  10. Start and stop a trace
  11. Record custom metrics
  12. Add custom attributes to a trace
  13. Enable or disable performance monitoring
  14. Record a trace manually
  15. API
  16. startTrace(...)
  17. stopTrace(...)
  18. incrementMetric(...)
  19. setEnabled(...)
  20. isEnabled()
  21. putAttribute(...)
  22. getAttribute(...)
  23. getAttributes(...)
  24. removeAttribute(...)
  25. putMetric(...)
  26. getMetric(...)
  27. record(...)
  28. Interfaces
  29. Type Aliases
  30. FAQ
  31. How do I measure the duration of a specific task in my app?
  32. What are the requirements for custom trace names?
  33. What is the difference between a metric and an attribute?
  34. Can users disable performance monitoring at runtime?
  35. Why does the record method not work on Android and iOS?
  36. Related Plugins
  37. Newsletter
  38. Changelog
  39. License

Capacitor Firebase Performance Monitoring Plugin

Unofficial Capacitor plugin for Firebase Performance Monitoring.[^1]

Use Cases

The Firebase Performance Monitoring plugin is typically used to gain insight into the performance characteristics of your app, for example:

  • Custom code traces: Measure how long specific tasks in your app take, such as loading data or processing an image.
  • Custom metrics: Count performance-related events within a trace, such as cache hits or retries.
  • Performance segmentation: Add custom attributes to your traces, such as a user id, to segment your performance data.
  • Privacy compliance: Enable or disable performance monitoring at runtime, for example based on the user's consent.

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/performance` 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/performance firebase
npx cap sync

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

Android

See Add the Performance Monitoring plugin to your app and follow the instructions to set up your app correctly.

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:

  • $firebasePerfVersion version of com.google.firebase:firebase-perf (default: 22.0.4)

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/performance": {
            "symlink": true
          }
        }
      }
    }
  }
}

Attention: SPM packageOptions support requires Capacitor CLI 8.4.0+.

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 start and stop traces, record custom metrics, add custom attributes, toggle performance monitoring, and record a trace manually.

Start and stop a trace

Start a custom code trace to measure how long a specific task in your app takes and stop it as soon as the task is completed:

import { FirebasePerformance } from '@capacitor-firebase/performance';

const startTrace = async () => {
  await FirebasePerformance.startTrace({ traceName: 'test_trace' });
};

const stopTrace = async () => {
  await FirebasePerformance.stopTrace({ traceName: 'test_trace' });
};

Record custom metrics

Set the value of a custom metric for a trace, increment it atomically, or read its current value. Note that metric values are floored down to the nearest integer:

import { FirebasePerformance } from '@capacitor-firebase/performance';

const putMetric = async () => {
  await FirebasePerformance.putMetric({
    traceName: 'test_trace',
    metricName: 'item_cache_hit',
    num: 1,
  });
};

const incrementMetric = async () => {
  await FirebasePerformance.incrementMetric({
    traceName: 'test_trace',
    metricName: 'item_cache_hit',
    incrementBy: 1,
  });
};

const getMetric = async () => {
  const result = await FirebasePerformance.getMetric({
    traceName: 'test_trace',
    metricName: 'item_cache_hit',
  });
  return result.value;
};

Add custom attributes to a trace

Set custom attributes on a trace, for example a user id, to segment your performance data. You can also read or remove them again:

import { FirebasePerformance } from '@capacitor-firebase/performance';

const putAttribute = async () => {
  await FirebasePerformance.putAttribute({
    traceName: 'test_trace',
    attribute: 'user_id',
    value: '123',
  });
};

const getAttribute = async () => {
  const result = await FirebasePerformance.getAttribute({
    traceName: 'test_trace',
    attribute: 'user_id',
  });
  return result.attributes;
};

const getAttributes = async () => {
  const result = await FirebasePerformance.getAttributes({ traceName: 'test_trace' });
  return result.attributes;
};

const removeAttribute = async () => {
  await FirebasePerformance.removeAttribute({
    traceName: 'test_trace',
    attribute: 'user_id',
  });
};

Enable or disable performance monitoring

Enable or disable performance monitoring at runtime, for example based on the user's consent. The setting is applied with the next start of the app:

import { FirebasePerformance } from '@capacitor-firebase/performance';

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

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

Record a trace manually

Record a trace for a task that has already been completed by providing its start time and duration. Only available on Web:

import { FirebasePerformance } from '@capacitor-firebase/performance';

const record = async () => {
  await FirebasePerformance.record({
    traceName: 'test_trace',
    startTime: Date.now(),
    duration: 1000,
    options: {
      metrics: {
        item_cache_hit: 1,
      },
      attributes: {
        user_id: '123',
      },
    },
  });
};

API

startTrace(...)

startTrace(options: StartTraceOptions) => Promise<void>

Starts a trace.

ParamType
optionsStartTraceOptions

Since: 0.1.0


stopTrace(...)

stopTrace(options: StopTraceOptions) => Promise<void>

Stops a trace.

ParamType
optionsStopTraceOptions

Since: 0.1.0


incrementMetric(...)

incrementMetric(options: IncrementMetricOptions) => Promise<void>

Atomically increments the metric with the given name for the selected trace by the incrementBy value.

ParamType
optionsIncrementMetricOptions

Since: 0.1.0


setEnabled(...)

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

Enables or disables performance monitoring. Will be applied with the next start of the app.

ParamType
optionsSetEnabledOptions

Since: 0.1.0


isEnabled()

isEnabled() => Promise<IsEnabledResult>

Determines whether performance monitoring is enabled or disabled.

Returns: Promise<IsEnabledResult>

Since: 0.1.0


putAttribute(...)

putAttribute(options: PutAttributeOptions) => Promise<void>

Sets a custom attribute of a trace to a given value.

ParamType
optionsPutAttributeOptions

Since: 6.3.0


getAttribute(...)

getAttribute(options: GetAttributeOptions) => Promise<GetAttributeResult>

Returns the value of a custom attribute of a trace.

ParamType
optionsGetAttributeOptions

Returns: Promise<GetAttributeResult>

Since: 6.3.0


getAttributes(...)

getAttributes(options: GetAttributesOptions) => Promise<GetAttributesResult>

Gets the all the custom attributes of a trace with their values.

ParamType
optionsGetAttributesOptions

Returns: Promise<GetAttributesResult>

Since: 6.3.0


removeAttribute(...)

removeAttribute(options: RemoveAttributeOptions) => Promise<void>

Removes a custom attribute from a trace given its name.

ParamType
optionsGetAttributeOptions

Since: 6.3.0


putMetric(...)

putMetric(options: PutMetricOptions) => Promise<void>

Sets the value of a custom metric.

ParamType
optionsPutMetricOptions

Since: 6.3.0


getMetric(...)

getMetric(options: GetMetricOptions) => Promise<GetMetricResult>

Get the value of a custom metric by name.

ParamType
optionsGetMetricOptions

Returns: Promise<GetMetricResult>

Since: 6.3.0


record(...)

record(options: RecordOptions) => Promise<void>

Records a trace given its name and options.

Only available on web.

ParamType
optionsRecordOptions

Since: 6.3.0


Interfaces

StartTraceOptions

PropTypeDescriptionSince
traceNamestringCustom trace name. Names for custom code traces must meet the following requirements: no leading or trailing whitespace, no leading underscore (_) character, and max length is 100 characters.0.1.0

StopTraceOptions

PropTypeDescriptionSince
traceNamestringName of the trace that was set with startTrace.0.1.0

IncrementMetricOptions

PropTypeDescriptionDefaultSince
traceNamestringName of the trace that was set with startTrace.0.1.0
metricNamestringName of the metric to be incremented.0.1.0
incrementBynumberAmount by which the metric has to be incremented.10.1.0

SetEnabledOptions

PropTypeDescriptionSince
enabledbooleanShould performance monitoring be enabled.0.1.0

IsEnabledResult

PropTypeDescriptionSince
enabledbooleantrue if performance monitoring is enabled, otherwise false.0.1.0

PutAttributeOptions

PropTypeDescriptionSince
traceNamestringName of the trace to set its attribute.6.3.0
attributestringName of the attribute to set its value.6.3.0
valuestringThe value to set to the attribute.6.3.0

GetAttributeResult

PropTypeDescriptionSince
valuestring | nullThe value of the custom attribute.6.3.0

GetAttributeOptions

PropTypeDescriptionSince
traceNamestringName of the trace to set its attribute.6.3.0
attributestringName of the attribute to retrieve its value.6.3.0

GetAttributesResult

PropTypeDescriptionSince
attributes{ [key: string]: string; }A map of all custom attributes of a trace with their values.6.3.0

GetAttributesOptions

PropTypeDescriptionSince
traceNamestringName of the trace to get its attributes.6.3.0

PutMetricOptions

PropTypeDescriptionSince
traceNamestringName of the trace to set its metric.6.3.0
metricNamestringThe metric name.6.3.0
numnumberThe value to set for the metric. The given value is floored down to the nearest integer.6.3.0

GetMetricResult

PropTypeDescriptionSince
valuenumberThe value of the metric if exists.6.3.0

GetMetricOptions

PropTypeDescriptionSince
traceNamestringName of the trace to get its metric.6.3.0
metricNamestringThe metric name.6.3.0

RecordOptions

PropTypeDescriptionSince
traceNamestringName of the trace to record.6.3.0
startTimenumberStart time of the trace since epoch in milliseconds.6.3.0
durationnumberThe duration of the trace in milliseconds.6.3.0
options{ metrics?: { [key: string]: number; }; attributes?: { [key: string]: string; }; }An optional object that holds optional maps of custom metrics and attributes.6.3.0

Type Aliases

RemoveAttributeOptions

GetAttributeOptions

FAQ

How do I measure the duration of a specific task in my app?

Start a custom code trace with startTrace(...) before the task begins and stop it with stopTrace(...) as soon as the task is completed, as shown in the usage example above. You can enrich the trace with custom metrics and attributes before stopping it.

What are the requirements for custom trace names?

Names for custom code traces must not contain leading or trailing whitespace, must not start with an underscore (_), and may have a maximum length of 100 characters.

What is the difference between a metric and an attribute?

A metric is a numeric value of a trace, for example a counter for cache hits, which is set with putMetric(...) or incremented with incrementMetric(...). Note that metric values are floored down to the nearest integer. An attribute is a string value of a trace, for example a user id, which is set with putAttribute(...) and can be used to segment your performance data.

Can users disable performance monitoring at runtime?

Yes, call setEnabled(...) to enable or disable performance monitoring, for example based on the user's consent. The setting is applied with the next start of the app. Use isEnabled() to determine whether performance monitoring is currently enabled or disabled.

Why does the record method not work on Android and iOS?

The record(...) method is only available on Web. On Android and iOS, use startTrace(...) and stopTrace(...) to measure the duration of a task instead.

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/performance 共发布 51 个版本,首次发布于 2022/03/20;过去 12 个月发布了 13 个版本。最新版本附带 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.1765887457。

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

@capacitor-firebase/performance 是什么?

Capacitor plugin for Firebase Performance Monitoring on Android, iOS, and Web.

如何安装 @capacitor-firebase/performance?

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

@capacitor-firebase/performance 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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