How to integrate google analytics in the flutter app for both Android and IOS?
It’s the first article of the series to learn the integration process of Google Analytics with Flutter app for both Android and IOS platform concerning different flavors.
In this first article we will learn to integrate Google Analytics to our Flutter app.
So, let’s start :
Step 1: Login to the Google Analytics Console using your Email ID and create an analytics account.
Step 1.1: For the starters, provide an account name say ‘Analytics’
Then fill user property. In Google Analytics, each property provides a unique tracking ID which we can associate with a website, mobile application, blog, etc., to track it’s events. A google analytics account can contain one or more properties.
Note: If you don’t want to use Universal Analytics property then skip next step.
Step 1.2: To enable or create a Google Universal Analytics property click on “Show advanced options” below property setup and select “Create a Universal Analytics property” option.
Following complete Business information and click on create.
Step 2: To integrate Google Analytics in our Flutter app we will be using Firebase SDK which works as a data layer to feed data for Google Analytics in a App+Web property. App+Web property is now updated and renamed as GA4 property.
Note:- Those who want to use google universal analytics in the project, they need to integrate TAG Manager to this project which we will be covering later in this series.
So to integrate Firebase to our Flutter project we need to create a Firebase project in the Firebase console. So, login into the Firebase console using the same Email ID which you have used to create the Google Analytics project and create a new Firebase project.
Step 2.1: For this, click on the Add project card in the Firebase Console and provide a project name.
Click on Continue, make sure Google Analytics option is selected on the next page and continue. You can also select the Google Analytics projects which you created in the previous step.
Note: We can always add a google analytics project later so if you don’t allow it right away or you already have a project with analytics disable or want to add a different account, that’s fine too. You can check this article to integrate the analytics project to your firebase project using Firebase project settings.
Step 3: Link your Firebase project to your Flutter project.
Step 3.1: For that you need to add an app in the Firebase project. Let’s start with android first.
Step 3.1.1: Go to “Project Overview” in firebase console, click on Add app and select Android.
Step 3.1.2: Add your package name, you can find your package name in either your (App) build.gradle file or in manifest.xml file.
Your package name should look like this.
Add this ID in the Android Package name field. You can skip the optional fields (you can edit them later) and click on Register app.
Now download the config file and put it inside your Project/Android/app folder.
Now add below dependency to your Project/pubspec.yaml file and execute pub.get command to add this dependency to your project.
And that’s all, Firebase Analytics has been added to your project. Firebase SDK will start collecting your analytical data and send them to the firebase console which will be shared with your Google Analytics account. Great, but wait, don’t celebrate so fast we still have to update IOS files.
Step 3.2.1: Let’s add the Firebase project to the IOS.
Following you need to click on the Add app again and then select IOS app.
Enter the IOS bundle ID. To find your app IOS bundle ID. Open XCode select runner inside the target section then inside the General section you can find your IOS bundle id in the Bundle identifier.
You can skip the optional fields and register the app. Now download the config file and move your config file into the root of your Xcode project. If prompted, select to add the config file to all targets.
And that’s all. As we already added “firebase_analytics: ^6.3.0” in our pubspec.yaml file in the previous steps. The Firebase SDK is already there.
Launch your app in an emulator or real device and the console will be updated after a few minutes.
Note: Make sure the time zone of your device, firebase, and google analytics are same, otherwise it will create lag in analytics report creation and the real-time section of google analytics or stream view of firebase analytics will not be updated properly.
Great , now we have successfully integrated Google Analytics in our Flutter app for both Android and IOS. Great Job guys😃.