Google Analytics is a free app measurement solution that provides insights into app usage and user engagement.

At the heart of Firebase is Google Analytics, a free and unlimited analytics solution. Analytics integrates across Firebase features and gives you unlimited reporting for up to 500 distinct events that you can define using the Firebase SDK. Analytics reports help you clearly understand how your users are performing, allowing you to make informed decisions regarding app marketing and performance optimization.

Create a new Firebase project

From within the Firebase dashboard, select the “Create New Project” button and give the project a name:

Next, we are given the option to enable Google Analytics, we need to review and accept the terms and conditions before creating the project.

After pressing Continue, your project will be created and resources will be provisioned. You will then be directed to the dashboard for the new project.

Add Google Analytics Firebase for Android

Step 1: Register the app

To add Android support to our Flutter app, select the Android logo from the dashboard. This brings us to the following screen:

The most important thing here is to match the Android package name you choose here with the name inside your app. Its structure is implemented as follows: com.example.app

Once you've decided on a name, go to android/app/build.gradle and update the applicationId to match this:

...
defaultConfig {
    // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
    applicationId 'com.example.flutterfirebaseexample'
    ...
}
...

You can skip the signing and debugging key at this stage. Select “App registration” to continue.

Step 2: Download the configuration file

The next step is to add the Firebase configuration file to our Flutter project. This is important because it contains API keys and other important information for Firebase to use.

Select “Download google-services.json” from this page.

Once you have it, get the file and place it inside the android/app folder in your Flutter project.

Step 3: Add Firebase SDK

Now we need to update your Gradle configuration to include the Google Services plugin. Open android/build.gradle and modify it to include the following:

buildscript {
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
  }
  dependencies {
    ...
    // Add this line
    classpath 'com.google.gms:google-services:4.3.6'
  }
}

allprojects {
  ...
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    ...
  }
}

Finally, update the app-level file at android/app/build.gradle to include the following:

apply plugin: 'com.android.application'
// Add this line
apply plugin: 'com.google.gms.google-services'

dependencies {
  // Import the Firebase Analytics
  implementation 'com.google.firebase:firebase-analytics:19.0.1'

  // Add the dependencies for any other desired Firebase products
  // https://firebase.google.com/docs/android/setup#available-libraries
}

With this update, we're mainly applying the Google services plug-in as well as looking at how other Firebase Flutter plugins can be enabled like Analytics.

From here, run your app on your Android device or emulator. If everything is working properly, you will get the following message in the dashboard:

Add Google Analytics Firebase for IOS

Step 1: Register the app

To add IOS support to our Flutter app, select the IOS logo from the dashboard, we need to add the iOS bundle ID which I am keeping the same as Android for consistency:

Then you need to make sure this matches by opening the iOS project in Xcode at ios/Runner/Runner.xcodeproj and changing the Bundle identifier in General:

Click “Apply Registration” to move to the next screen.

Step 2: Download the configuration file

In this step, we need to download the configuration file and add it to our Xcode project.

Download GoogleService-Info.plist and drag it to the root folder of your Xcode project in the Runner :

Be sure to move this file within Xcode to create the proper file references.