@capgo/capacitor-facebook-analytics

中文文档
8.1.19发布于 2026/10/15MPL-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 1.3万

Capacitor plugin for Meta/Facebook App Events analytics.

原始使用文档
文档目录19 节
  1. ➡️ Get started for free
  2. Key features
  3. Install
  4. Usage
  5. Native Setup
  6. iOS
  7. Android
  8. Troubleshooting
  9. "FacebookAnalytics" plugin is not implemented on android
  10. API
  11. initAppEvents()
  12. logEvent(...)
  13. logPurchase(...)
  14. enableAdvertiserTracking()
  15. disableAdvertiserTracking()
  16. getAdvertiserTrackingStatus()
  17. getPluginVersion()
  18. Interfaces
  19. Type Aliases

@capgo/capacitor-facebook-analytics

Send Meta (Facebook) App Events from your Capacitor app on iOS and Android to measure installs, purchases and conversions for your Meta ad campaigns.

Capgo - Build Capacitor apps with AI, ship updates instantly

Capgo: push fixes to your Capacitor users in minutes, build signed iOS and Android apps without a Mac, and roll back in one click.

➡️ Get started for free

14-day unlimited free trial. No credit card required

🤖 Building with an AI coding agent? Install Capgo skills so Claude Code, Codex, Cursor, Copilot, or Gemini CLI know how to install and use @capgo/capacitor-facebook-analytics in your Capacitor app: npx skills add Cap-go/capgo-skills. Then ask your agent: "Add @capgo/capacitor-facebook-analytics to my Capacitor app and wire it up." Agent setup guides · Hosted MCP

@capgo/capacitor-facebook-analytics for Capacitor apps

Key features

  • App Events: initAppEvents() starts the Facebook SDK and activates App Events.
  • Custom events: logEvent() logs any event with parameters.
  • Purchases: logPurchase() logs revenue with currency.
  • Advertiser tracking: enableAdvertiserTracking(), disableAdvertiserTracking() and getAdvertiserTrackingStatus(). On iOS 17 and later the status reflects App Tracking Transparency. On iOS 16 and earlier the setters change the Facebook SDK flag.
  • Platforms: iOS and Android. Uses the official Facebook SDKs. Event logging is not available on web.

Install

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

npx skills add https://github.com/cap-go/capacitor-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-facebook-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:

bun add @capgo/capacitor-facebook-analytics
bunx cap sync

Usage

import {
  FacebookAnalytics,
  FacebookEventName,
  FacebookEventParameterName,
} from '@capgo/capacitor-facebook-analytics';

await FacebookAnalytics.enableAdvertiserTracking();
await FacebookAnalytics.initAppEvents();

await FacebookAnalytics.logEvent({
  event: FacebookEventName.CompletedRegistration,
  params: {
    [FacebookEventParameterName.RegistrationMethod]: 'email',
  },
});

await FacebookAnalytics.logEvent({
  event: FacebookEventName.AddedToCart,
  valueToSum: 19.99,
  currency: 'USD',
  params: {
    [FacebookEventParameterName.ContentType]: 'product',
    [FacebookEventParameterName.ContentId]: 'sku-123',
  },
});

await FacebookAnalytics.logPurchase({
  amount: 9.99,
  currency: 'USD',
});

Native Setup

Configure the Meta app id and client token in your native app. This plugin does not create those values for you.

iOS

Add your Meta values to the app Info.plist:

<key>FacebookAppID</key>
<string>YOUR_FACEBOOK_APP_ID</string>
<key>FacebookClientToken</key>
<string>YOUR_FACEBOOK_CLIENT_TOKEN</string>
<key>FacebookDisplayName</key>
<string>YOUR_APP_NAME</string>

When advertiser tracking is allowed by your consent flow, call enableAdvertiserTracking() before logging events.

If automatic Meta App Event logging is disabled (FacebookAutoLogAppEventsEnabled = false) so initialization can wait for consent, call initAppEvents() only after that consent and ATT flow. On iOS this initializes FBSDK before activating App Events. On Android, FacebookInitProvider normally performs basic sdkInitialize() at startup while AutoInitEnabled and AutoLogAppEventsEnabled control full initialization and automatic event logging; initAppEvents() completes basic init when needed (for example if the provider was removed) and then calls activateApp(). Do not call ApplicationDelegate.shared.initializeSDK() from AppDelegate in that flow; it would start Meta before those gates pass.

Android

Add your Meta values to the app AndroidManifest.xml:

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id" />
<meta-data android:name="com.facebook.sdk.ClientToken" android:value="@string/facebook_client_token" />

Add the string resources in android/app/src/main/res/values/strings.xml:

<string name="facebook_app_id">YOUR_FACEBOOK_APP_ID</string>
<string name="facebook_client_token">YOUR_FACEBOOK_CLIENT_TOKEN</string>

Troubleshooting

"FacebookAnalytics" plugin is not implemented on android

This usually means Capacitor did not register the native Android plugin. Check the following:

  1. Run bunx cap sync after installing or upgrading the plugin.
  2. Confirm @capgo/capacitor-facebook-analytics is listed in the Android sync output.
  3. Add the Meta ApplicationId and ClientToken entries from the Android setup section above.
  4. Upgrade to @capgo/[email protected] or newer if you are on an older build.

If Meta SDK setup is missing, event calls can still reject with a clear error, but the plugin itself should register on Android once sync succeeds.

API

Facebook App Events analytics bridge.

initAppEvents()

initAppEvents() => Promise<void>

Initialize the Facebook SDK and activate App Events.

Call this when automatic app event logging is disabled and you want to start sending events after your consent / ATT flow.

On iOS this initializes FBSDK on the main thread, then activates App Events.

On Android, FacebookInitProvider normally runs basic SDK setup via FacebookSdk.sdkInitialize() at app start. com.facebook.sdk.AutoInitEnabled controls whether that path also calls FacebookSdk.fullyInitialize() (server communication). com.facebook.sdk.AutoLogAppEventsEnabled controls automatic activateApp() logging. When the provider was removed (for example by @capgo/capacitor-social-login) or basic init never completed, this method calls sdkInitialize() and waits for its InitializeCallback, then enables full initialization via setAutoInitEnabled(true) before calling activateApp(). activateApp() alone is not enough when the SDK was never initialized or only basic initialization completed.

Do not initialize Facebook from AppDelegate for consent-gated apps; call this method after the user grants advertising measurement consent.


logEvent(...)

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

Log a Facebook App Event.

ParamType
optionsLogEventOptions

logPurchase(...)

logPurchase(options: LogPurchaseOptions) => Promise<void>

Log a Facebook purchase event.

ParamType
optionsLogPurchaseOptions

enableAdvertiserTracking()

enableAdvertiserTracking() => Promise<void>

Enable advertiser tracking.

On iOS 16 and below this sets Settings.shared.isAdvertiserTrackingEnabled. On iOS 17 and above FBSDK v17+ reads App Tracking Transparency directly. On Android this enables advertiser ID collection.


disableAdvertiserTracking()

disableAdvertiserTracking() => Promise<void>

Disable advertiser tracking.

On iOS 16 and below this sets Settings.shared.isAdvertiserTrackingEnabled. On iOS 17 and above FBSDK v17+ reads App Tracking Transparency directly. On Android this disables advertiser ID collection.


getAdvertiserTrackingStatus()

getAdvertiserTrackingStatus() => Promise<AdvertiserTrackingStatusResult>

Get the current advertiser tracking status.

On iOS 17 and above this reflects App Tracking Transparency authorization.

Returns: Promise<AdvertiserTrackingStatusResult>


getPluginVersion()

getPluginVersion() => Promise<PluginVersionResult>

Returns the platform implementation version marker.

Returns: Promise<PluginVersionResult>


Interfaces

LogEventOptions

PropTypeDescription
eventstringFacebook standard event name or a custom app event name.
valueToSumnumberOptional numeric value to sum for this event.
currencystringOptional ISO 4217 currency code for value-bearing standard events. This is forwarded as Facebook's fb_currency event parameter.
paramsFacebookEventParamsOptional event parameters.

LogPurchaseOptions

PropTypeDescription
amountnumberPurchase amount.
currencystringISO 4217 currency code.
paramsFacebookEventParamsOptional purchase parameters.

AdvertiserTrackingStatusResult

PropTypeDescription
statusbooleanCurrent advertiser tracking status.

PluginVersionResult

PropTypeDescription
versionstringVersion identifier returned by the platform implementation.

Type Aliases

FacebookEventName

Facebook standard event names.

(typeof FacebookEventName)[keyof typeof FacebookEventName]

FacebookEventParams

Event parameters keyed by Facebook standard parameter names or custom names.

Record<FacebookEventParameterName | string, FacebookEventParamValue>

Record

Construct a type with a set of properties K of type T

{ [P in K]: T; }

FacebookEventParamValue

Values accepted by the native Facebook App Events SDKs.

Boolean values are converted to Facebook toggle strings: true becomes "1" and false becomes "0".

string | number | boolean | null

FacebookEventParameterName

Facebook standard event parameter names.

(typeof FacebookEventParameterName)[keyof typeof FacebookEventParameterName]

版本与发布记录

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

版本发布日期距今
8.1.19latest2026/10/1525 天前
8.1.182026/10/1327 天前
8.1.172026/10/091 个月前
8.1.162026/10/081 个月前
8.1.142026/10/081 个月前
8.1.152026/10/081 个月前

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

@capgo/capacitor-facebook-analytics 是什么?

Capacitor plugin for Meta/Facebook App Events analytics.

如何安装 @capgo/capacitor-facebook-analytics?

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

@capgo/capacitor-facebook-analytics 支持 TypeScript 吗?

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

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

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

@capgo/capacitor-facebook-analytics 可以商用吗?

@capgo/capacitor-facebook-analytics 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

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

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

@capgo/capacitor-facebook-analytics 还在维护吗?

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