Skip to main content

Firebase Analytics Capacitor plugin

Capacitor plugin for Firebase Analytics

Install

bun add @capgo/capacitor-firebase-analytics bunx cap sync
npm, pnpm or yarn
  • npm install @capgo/capacitor-firebase-analytics
  • pnpm add @capgo/capacitor-firebase-analytics
  • yarn add @capgo/capacitor-firebase-analytics

Guide

How to use Firebase Analytics in Capacitor

Using @capgo/capacitor-firebase-analytics

Capacitor plugin for Firebase Analytics.

Install

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

bunx cap sync copies the plugin's native code into your native projects. Run it again after every plugin upgrade.

Import

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

API at a glance

Method Description
getAppInstanceId Retrieves the app instance id.
getSessionId Retrieves the current session id (ga_session_id).
setConsent Sets the user's consent mode.
setUserId Sets the user ID property.
setUserProperty Sets a custom user property to a given value.
setCurrentScreen Sets the current screen name.
logEvent Logs an app event.
setSessionTimeoutDuration Sets the duration of inactivity that terminates the current session.
setEnabled Enables/disables automatic data collection. The value does not apply until the next run of the app.
isEnabled Returns whether or not automatic data collection is enabled.
resetAnalyticsData Clears all analytics data for this app from the device. Resets the app instance id.
logTransaction Logs a StoreKit 2 transaction.
initiateOnDeviceConversionMeasurementWithEmailAddress Initiates on-device conversion measurement with an email address.
initiateOnDeviceConversionMeasurementWithPhoneNumber Initiates on-device conversion measurement with a phone number.
initiateOnDeviceConversionMeasurementWithHashedEmailAddress Initiates on-device conversion measurement with a hashed email address.
initiateOnDeviceConversionMeasurementWithHashedPhoneNumber Initiates on-device conversion measurement with a hashed phone number.

Examples

getAppInstanceId()

Retrieves the app instance id.

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

const result = await FirebaseAnalytics.getAppInstanceId();
console.log(result);

getSessionId()

Retrieves the current session id (ga_session_id).

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

const result = await FirebaseAnalytics.getSessionId();
console.log(result);

setConsent()

Sets the user's consent mode.

import { FirebaseAnalytics, ConsentStatus, ConsentType } from '@capgo/capacitor-firebase-analytics';

await FirebaseAnalytics.setConsent({
  type: ConsentType.AdPersonalization,
  status: ConsentStatus.Granted,
});

setUserId()

Sets the user ID property.

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

await FirebaseAnalytics.setUserId({ userId: 'user-id-123' });

setUserProperty()

Sets a custom user property to a given value.

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

await FirebaseAnalytics.setUserProperty({
  key: 'key-123',
  value: 'value',
});

setCurrentScreen()

Sets the current screen name.

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

await FirebaseAnalytics.setCurrentScreen({ screenName: 'screen' });

The table above lists the 16 core methods. Listener and version helpers, and the full contract of each method, are documented in the GitHub repository.

Full reference

Keep going from Using @capgo/capacitor-firebase-analytics

If you are using Using @capgo/capacitor-firebase-analytics to plan native plugin work, connect it with Capgo Plugin Directory for the product workflow in Capgo Plugin Directory, Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, Adding or Updating Plugins for the implementation detail in Adding or Updating Plugins, Ionic Enterprise Plugin Alternatives for the product workflow in Ionic Enterprise Plugin Alternatives, and Capgo Native Builds for the product workflow in Capgo Native Builds.

FAQ

Firebase Analytics plugin FAQ

How do I install the Firebase Analytics plugin in a Capacitor app?

Run "bun add @capgo/capacitor-firebase-analytics" (or "npm install @capgo/capacitor-firebase-analytics"), then run "bunx cap sync" so the iOS and Android projects pick up the native code. Import it from "@capgo/capacitor-firebase-analytics" in your app code.

Does @capgo/capacitor-firebase-analytics work with React, Vue and Angular?

Yes. Firebase Analytics is a Capacitor package, so it works with any web framework that runs inside Capacitor, including Ionic, React, Vue, Angular, Svelte and plain JavaScript.

Which Capacitor version does @capgo/capacitor-firebase-analytics support?

Capgo plugins follow Capacitor's major version: use the plugin major version that matches your Capacitor major version (for example plugin v8 with Capacitor 8). The compatibility table in the GitHub README lists the maintained versions.

Is @capgo/capacitor-firebase-analytics free and open source?

Yes. The source code is public on GitHub at https://github.com/Cap-go/capacitor-firebase/tree/main/packages/analytics and the package is free to install from npm. Bug reports and pull requests are welcome.

Can I update code that uses Firebase Analytics without an App Store review?

Installing or upgrading the plugin changes native code, so it needs a new store build. After that, JavaScript, HTML and CSS changes that call the plugin can ship instantly with Capgo live updates.

Ship Firebase Analytics changes without waiting for app review

Once the plugin is in your store build, Capgo live updates push your JavaScript, HTML and CSS changes to users in minutes.

Start with Capgo