Flutter and Google Analytics: How to effortlessly integrate

Abdur Mohammed
4 min readFeb 12, 2023

--

Flutter How To: Simple | To the Point| Short

Flutter Dev Level: Intermediate

1.0 Objective

  • Track User interaction with your Flutter app using Google Analytics

2.0 Sample Project Github Link

https://github.com/AbdurM/flutter_google_analytics_example

3.0 The Steps

Step 1: Create a firebase account if you don’t have one

Step 2: Click on create new project (if you don’t have one)

Step 3: Enable google Analytics while creating the project

Step 4: Create or choose an existing google analytics account.

Step 5: Add flutter packages to the app

  • Go to your code editor (VS Code in my case) or open root of your project inside the terminal.
  • Run this command to add firebase core
flutter pub add firebase_core 
  • then run this command to add firebase analytics.
flutter pub add firebase_analytics

Step 6: Add your app to the firebase project

  • Run the following command while still being in the root of your project.
flutterfire configure
  • Select the relevant project (In our example, we created it in Step 1- 4)
  • Select relevant platforms
  • Press Y and proceed when asked.
  • Once done, the above file will be generated.

Step 7: Initialize firebase

  • Add the following two lines of code before run app in the main method.
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();

Step 8: Start tracking events

  • Wherever you want to track events, import the firebase_analytics plugin
import 'package:firebase_analytics/firebase_analytics.dart';
  • Now you can use the log methods on the analytics instance to log events. Example below
FirebaseAnalytics.instance.logEvent(name: 'increment_button_press');

IMPORTANT: There are only few formats that google analytics accept the log event name in. I would recommend snake_case (Example: ‘sample_event_name’). Please note that if the format that you follow is’nt from the list then it would not be logged.

  • There’s an array of log events available

Step 9: Debug Events

  • Usually, events are uploaded roughly every hour to save battery and network usage.
  • However, to debug or to verify if everything is working as expected, you can use debug view to upload events almost immediately.
  • To enable analytics debug mode on Android, run the following command in your terminal, while your device is connected / Emulator is running.
adb shell setprop debug.firebase.analytics.app PACKAGE_NAME
  • Note: You can find the package name in AndroidManifest.xml

Step 10: TEST TEST TEST

  • Go back to cloud console.
  • On the left navigation tab, under analytics click on Debug View
  • Run your flutter app
  • Within debug view, select your debug device
  • Start interacting with the app see the logs coming through

Woohoo! You have now integrated google analytics with your Flutter app.

That’s all for this one. Hope it helped :-)

Github link:
https://github.com/AbdurM/flutter_google_analytics_example

Please find more flutter articles here: https://medium.com/@rafayk66/abdurs-flutter-articles-c201eff334b8

You can find me on
LinkedInhttps://www.linkedin.com/in/knowabdur
Twitter: — https://twitter.com/AbdurDeveloper

#flutter #flutterdeveloper #AbdurDeveloper #fluttercommunity

--

--

Abdur Mohammed

React Native| Flutter | Senior Software Engineer | Sydney, Australia