跳转至正文

Firebase Performance Capacitor 插件

用于 Firebase Performance Monitoring 的 Capacitor 插件

安装

bun add @capgo/capacitor-firebase-performance bunx cap sync
npm、pnpm 或 yarn
  • npm install @capgo/capacitor-firebase-performance
  • pnpm add @capgo/capacitor-firebase-performance
  • yarn add @capgo/capacitor-firebase-performance

指南

如何在 Capacitor 中使用 Firebase Performance

使用 @capgo/capacitor-firebase-performance

用于 Firebase Performance Monitoring 的 Capacitor 插件。

安装

bun add @capgo/capacitor-firebase-performance
bunx cap sync

bunx cap sync 将插件的原生 code 复制到你的原生项目中。每次升级插件后都要重新运行。

导入

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

API 概览

方法 描述
startTrace 开始追踪。
stopTrace 停止追踪。
incrementMetric 以原子方式将所选追踪中指定名称的指标增加... incrementBy 值。
setEnabled 启用或禁用性能监控。将在应用下次启动时生效。
isEnabled 决定是否启用性能监控。
putAttribute 为 trace 设置自定义属性值。
getAttribute 获取 trace 自定义属性的值。
getAttributes 获取 trace 的所有自定义属性及其对应的值。
removeAttribute 根据名称从 trace 中移除指定的自定义属性。
putMetric 设置自定义指标的值。
getMetric 根据名称获取自定义指标的值。
record 根据名称和配置参数记录一条 trace。

示例

startTrace()

开始追踪。

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

await FirebasePerformance.startTrace({ traceName: 'trace' });

stopTrace()

停止追踪。

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

await FirebasePerformance.stopTrace({ traceName: 'trace' });

incrementMetric()

以原子方式将所选追踪中指定名称的指标增加... incrementBy 值。

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

await FirebasePerformance.incrementMetric({
  traceName: 'trace',
  metricName: 'metric',
});

setEnabled()

启用或禁用性能监控。将在应用下次启动时生效。

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

await FirebasePerformance.setEnabled({ enabled: true });

isEnabled()

决定是否启用性能监控。

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

const result = await FirebasePerformance.isEnabled();
console.log(result);

putAttribute()

为 trace 设置自定义属性值。

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

await FirebasePerformance.putAttribute({
  traceName: 'trace',
  attribute: "experiment",
  value: "A",
});

上表列出了 12 个核心方法。监听器、版本辅助方法以及每个方法的完整接口约定,详见 GitHub 仓库.

完整参考文档

继续阅读:使用 @capgo/capacitor-firebase-performance

如果你正在使用 使用 @capgo/capacitor-firebase-performance 要规划原生插件工作,请将其与 Capgo 插件目录 查看 Capgo 插件目录中的产品工作流, Capgo 提供的 Capacitor 插件 查看由 Capgo 提供的 Capacitor 插件中的实现细节, 添加或更新插件 查看添加或更新插件中的实现细节, Ionic Enterprise 插件替代方案 查看 Ionic Enterprise 插件替代方案中的产品工作流,以及 Capgo Native Build 查看 Capgo Native Builds 中的产品工作流。

常见问题

Firebase Performance 插件常见问题

如何在 Capacitor 应用中安装 Firebase Performance 插件?

运行 "bun add @capgo/capacitor-firebase-performance"(或 "npm install @capgo/capacitor-firebase-performance"),然后运行 "bunx cap sync",让 iOS 和 Android 项目加载原生 code。再在应用的 code 中从 "@capgo/capacitor-firebase-performance" 导入。

@capgo/capacitor-firebase-performance 支持 React、Vue 和 Angular 吗?

支持。Firebase Performance 是一个 Capacitor 包,因此适用于任何运行在 Capacitor 中的 Web 框架,包括 Ionic、React、Vue、Angular、Svelte 和纯 JavaScript。

@capgo/capacitor-firebase-performance 支持哪个版本的 Capacitor?

Capgo 插件遵循 Capacitor 的主版本号:请使用与 Capacitor 主版本号一致的插件主版本(例如插件 v8 对应 Capacitor 8)。GitHub README 中的兼容性表列出了仍在维护的版本。

@capgo/capacitor-firebase-performance 是免费开源的吗?

是的。code 的源码已在 GitHub 公开,地址为 https://github.com/Cap-go/capacitor-firebase/tree/main/packages/performance,也可以直接从 npm 免费安装。欢迎提交 Bug 报告和 Pull Request。

使用 Firebase Performance 的 code 能否在不经过 App Store 审核的情况下更新?

安装或升级插件会改动原生 code,因此需要重新做一次商店构建。之后,调用该插件的 JavaScript、HTML 和 CSS 改动就能通过 Capgo live updates 立即发布。

无需等待 App Store 审核,即可推送 Firebase Performance 的变更

只要插件已包含在你的商店版本中,Capgo live updates 就能在几分钟内把 JavaScript、HTML 和 CSS 的改动推送给用户。

开始使用 Capgo