How to add Google Analytics to Flutter app for both Android & IOS with different flavor?

In this article, we will learn to link Google Analytics with our Flutter app for different flavors.

Why we need different flavors?

As a developer, we have to work with different environments like DEV, QA, PROD.

Sometimes we have to work with different (Paid/Free) versions of the app and we want to isolate this analytics data from one another.

For that, we will be using flavors to create different versions of the app. Let’s name each of them for our convenience say prod and dev.

Till now in this series, we have created a flutter project and added google analytics to it for both IOS and Android. Let’s continue with that by adding flavors in it.

So let’s start:

Let’s first add different flavor to android.

Open your (App) build.gradle file and add this code below ‘defaultConfig’.

flavorDimensions “flavor”

productFlavors {

prod {

dimension “flavor”

applicationId “com.example.analytics”

// signingConfig prodSigningVariable

// versionCode 1

// versionName “0.0.1”

}

dev {

dimension “flavor”

applicationIdSuffix “.dev”

versionNameSuffix “-dev”

// signingConfig devSigningVariable

// versionCode 1

// versionName “0.0.1”

}

}

In the above code we have created two flavors prod and dev.

Thus creating two app package id denoting each environment/flavor:
1. com.example.analytics
2. com.example.analytics.dev

Now let’s move to the Firebase Console, click on Add app icon and select an android app.

Put the “com.example.analytics.dev” in the Android Package Name field and register app.

Now download the google-services.json file and add this to your android/app folder.

That’s all, you have created different environments in android and linked it to firebase.

You can see all your apps in the General Tap of Project Settings. And if you want to give a custom name to this app, app nickname is the field you are looking for.

Good Job. Now let’s move to IOS.

Open your Firebase console and click on the Add app Icon and select an IOS app. Enter a new bundle ID like “com.example.analytics.dev” and register the app like we did in android.
Now download the json file. At this moment you should have two GoogleServices-Info.plist, one for the prod and another for the dev environment. That’s all from the Firebase side, let’s move to flutter project.

Creating flavors in IOS require more work then android. As it’s been properly explained in another article and to keep this article short. I suggest you follow this link to create multiple flavors in IOS.

Note: In the above link they are creating flavors and linking them to different firebase projects. So you can also see a different approach using multiple firebase projects dedicated to specific flavors of app whereas in this article we are using the same firebase project with multiple App’s. Which is a better approach, well it depends upon the use-case.

Now, I will assume you have created multiple IOS flavors and linked them to your firebase projects. Let’s filter out this data.

The 1st arrow pointed to a button which will open the Analytics Filter Panel. The 2nd arrow pointed towards a link which will open your Google Analytics Console.

In Firebase and Google Analytics each, app or flavor will be denoted as a stream.

You can use these streams to filter your data referring to individual flavor. You can also create an audience in analytics for proper filtering of your data. I will cover data filtering later in this series.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store