Skip to main content

Using Firebase Analytics

Once installed, you can access the firebase_analytics plugin by importing it in your Dart code:

import 'package:firebase_analytics/firebase_analytics.dart';

Before using Firebase Analytics, you must first have ensured you have initialized FlutterFire.

To create a new Firebase Analytics instance, call the instance getter on FirebaseAnalytics:

FirebaseAnalytics analytics = FirebaseAnalytics.instance;

By default, this allows you to interact with Firebase Analytics using the default Firebase App used whilst installing FlutterFire on your platform.

If you'd like to use a secondary Firebase Analytics, use the instanceFor method - note multi-app support is only available on the web:

// Only supported on web!
FirebaseApp secondaryApp = Firebase.app('SecondaryApp');
FirebaseAnalytics analytics = FirebaseAnalytics.instanceFor(app: secondaryApp);

Logging events#

The plugin supports logging both custom and predefined events to Analytics.

Custom events#

You can log custom events by calling the logEvent method. The method accepts an event name (which will be shown in the Analytics dashboard), along with any additional parameters for the event.

await FirebaseAnalytics.instance
.logEvent(
name: 'view_product',
parameters: {
'product_id': 1234,
}
);

Note; if the event name is reserved or starts with firebase_, an error will be thrown.

Predefined events#

The plugin exposes a number of methods which accept a set of parameters which Firebase Analytics expects when a given event is called. For example, the logBeginCheckout event can be called when the user starts a checkout flow on your application. It accepts the value, currency, checkout items and coupon values:

await FirebaseAnalytics.instance
.logBeginCheckout(
value: 10.0,
currency: 'USD',
items: [
Item(
item_name: 'Socks',
item_id: 'xjw73ndnw',
price: '10.0'
),
],
coupon: '10PERCENTOFF'
);

The Item class provided above is a class which represents some item which is sent to Analytics. It accepts a number of predefined parameters which will be used for analysis on the dashboard. Although optional, to get the best experience from Firebase Analytics it is best to ensure as many relevant parameters are provided.

You can learn more about the Item class here.

Below is a table containing all predefined events available from the plugin:

MethodDescription
logAddPaymentInfoThis event signifies that a user has submitted their payment information to your app.
logAddShippingInfoThis event signifies that a user has submitted their shipping information to your app.
logAddToCartThis event signifies that an item was added to a cart for purchase.
logAddToWishlistThis event signifies that an item was added to a wish list. Use this event to identify popular gift items in your app.
logAdImpressionThis event signifies that an item was added to a wish list. Use this event to identify popular gift items in your app.
logAppOpenLogs that the application has been opened.
logBeginCheckoutThis event signifies that a user has begun the process of checking out.
logCampaignDetailsLog this event to supply the referral details of a re-engagement campaign.
logEarnVirtualCurrencyThis event tracks the awarding of virtual currency in your app.
logGenerateLeadLog this event when a lead has been generated in the app to understand the efficacy of your install and re-engagement campaigns.
logJoinGroupLog this event when a user joins a group such as a guild, team or family.
logLevelUpThis event signifies that a player has leveled up in your gaming app.
logLevelStartThis event signifies that a player has started a level in your gaming app.
logLevelEndThis event signifies that a player has ended a level in your gaming app.
logPostScoreLog this event when the user posts a score in your gaming app.
logPurchaseThis event signifies that an item(s) was purchased by a user.
logRemoveFromCartThis event signifies that an item(s) was removed from a cart.
logScreenViewThis event signifies a screen view. Use this when a screen transition occurs.
logSelectItemThis event signifies that an item was selected by a user from a list.
logSelectPromotionThis event signifies that a user has selected a promotion offer.
logViewCartThis event signifies that a user has viewed their cart. Use this to analyze your purchase funnel.
logSearchApps that support search features can use this event to contextualize search operations by supplying the appropriate, corresponding parameters.
logSelectContentThis general purpose event signifies that a user has selected some content of a certain type in an app.
logShareApps with social features can log the Share event to identify the most viral content.
logSignUpThis event indicates that a user has signed up for an account in your app.
logSpendVirtualCurrencyThis event tracks the sale of virtual goods in your app and can help you identify which virtual goods are the most popular objects of purchase.
logTutorialBeginThis event signifies the start of the on-boarding process in your app.
logTutorialCompleteUse this event to signify the user's completion of your app's on-boarding process
logUnlockAchievementLog this event when the user has unlocked an achievement in your game.
logViewItemThis event signifies that some content was shown to the user.
logViewItemListLog this event when the user has been presented with a list of items of a certain category.
logViewPromotionThis event signifies that a promotion was shown to a user.
logViewSearchResultsLog this event when the user has been presented with the results of a search.
logRefundThis event signifies that a refund was issued.

Screen tracking#

Use setCurrentScreen to track each unique screen the user is selecting and build a picture of the most relevant screens in your application:

await FirebaseAnalytics.instance
.setCurrentScreen(
screenName: 'Products'
);

Default Parameters#

Use the setDefaultEventParameters method if you have common parameters which need to be sent with each event:

await FirebaseAnalytics.instance
.setDefaultEventParameters({
version: '1.2.3'
});

Setting user properties#

Use the setUserId method to set the current analytics session to assign events to the user's id:

await FirebaseAnalytics.instance
.setUserId({
id: 'xxxxxxx'
});

The id can be set to null to remove the ID.

If you have any user properties to attach to the user, call the setUserProperty method:

await FirebaseAnalytics.instance
.setUserProperty({
name: 'age',
value: 18
});

You can provide a null value to remove any previous properties.

Resetting data#

To clear all data associated with the current session, use the resetAnalyticsData method:

await FirebaseAnalytics.instance
.resetAnalyticsData();