Skip to main content

Get started with Google Analytics

Notice

This page is archived and might not reflect the latest version of the FlutterFire plugins. You can find the latest information on firebase.google.com:

https://firebase.google.com/docs/analytics/get-started?platform=flutter

Google Analytics collects usage and behavior data for your app. The SDK logs two primary types of information:

  • Events: What is happening in your app, such as user actions, system events, or errors.
  • User properties: Attributes you define to describe segments of your user base, such as language preference or geographic location.

Analytics automatically logs some events and user properties; you don't need to add any code to enable them.

Before you begin#

  1. Install firebase_core and add the initialization code to your app if you haven't already.
  2. Add your app to your Firebase project in the Firebase console.

Add the Analytics SDK to your app#

  1. From the root of your Flutter project, run the following command to install the plugin:

    flutter pub add firebase_analytics
  2. Once complete, rebuild your Flutter application:

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

    import 'package:firebase_analytics/firebase_analytics.dart';
  4. Create a new Firebase Analytics instance by calling the instance getter on FirebaseAnalytics:

    FirebaseAnalytics analytics = FirebaseAnalytics.instance;

(Optional) Disable IDFA tracking#

To use Firebase Analytics without IDFA collection capability, open /ios/Podfile or /macos/Podfile and add the following global variable to the top of the file:

$FirebaseAnalyticsWithoutAdIdSupport = true

Learn more about IDFA in Apple's documentation:

(Optional) Disable Apple ad network attribution registration#

For your convenience, the SDK automatically registers your app with Apple for ad network attribution with SKAdNetwork. If you wish to disable this feature, set the value of GOOGLE_ANALYTICS_REGISTRATION_WITH_AD_NETWORK_ENABLED to NO (Boolean) in your app's Info.plist file.

Start logging events#

After you have created a FirebaseAnalytics instance, you can begin to log events with the library's log- methods.

Certain events are recommended for all apps; others are recommended for specific business types or verticals. You should send suggested events along with their prescribed parameters, to ensure maximum available detail in your reports and to benefit from future features and integrations as they become available. This section demonstrates logging a pre-defined event, for more information on logging events, see Log events.

The following code logs a checkout event:

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

Next steps#