top of page
Alejandro Carrazana

How to set up Google Analytics in a Flutter app

Google Analytics is a valueable tool to understand how users interact with your Flutter application. With Google Analytics, developers can answer key questions such as: How many users are using the application?; What are the most popular screens or sections?; How much time do users spend in the application?

This information is crucial for making informed decisions and improving the user experience in the application. Here, we'll go over how to set up Google Analytics in a mobile application.


What is Google Analytics?


Google Analytics is a free web analytics service provided by Google that allows website and mobile app owners to collect, analyze, and monitor data about user behavior and traffic.


Google Analytics provides a better understanding of how users interact with content to inform decisions improving user experience and site or app performance. Google Analytics offers a wide variety of reports and analysis tools that reveal detailed information about traffic, conversions, user retention, and much more.


Advantages of Google Analytics in a Flutter app


The benefits of using Google Analytics in a Flutter application are as follows:

  1. Real-time analytics: Get real-time insights into how users are interacting with your app.

  2. Accurate data: Track and analyze key metrics such as installs, user sessions, retention rate, time spent, and more.

  3. Custom tracking: Set up custom events to track specific actions that your users perform in your app.

  4. Easy integration: The Google Analytics library for Flutter is easy to install and configure, meaning you can start collecting and analyzing data in minutes.

  5. Detailed user information: Get detailed information about users, including their demographic characteristics and how they interact with your app.

  6. Improve user experience: Use the data collected by Google Analytics to improve the user experience and adjust your app to better meet their needs.

  7. Integration with other tools: Google Analytics integrates with other Google tools, such as AdWords and BigQuery, for a more complete view of your app and users.

In summary, Google Analytics provides valuable information to improve user experience and the performance of your Flutter application.


Implementation of Google Analytics in a Flutter Application


To implement Google Analytics in a Flutter application, you must have previously set up the Firebase project and enabled the Google Analytics option. We recommend the article "How to Set Up Firebase in a Flutter Application" if you're not familiar with this topic already.


Additionly, you'll need to add the firebase_analytics package to the Flutter project within the dependencies in the pubspec.yaml file. This library enables the simple use of the Firebase Analytics API.


Next, we create a class called FirebaseAnalyticsService within the firebase_analytics_service.dart file, where we will have a variable that corresponds to the instance of Firebase Analytics and an object of the FirebaseAnalyticsObserver class, which can track the screens visited by the user.

import 'package:firebase_analytics/firebase_analytics.dart';

class FirebaseAnalyticsService {
 static FirebaseAnalytics analytics = FirebaseAnalytics.instance;
 static FirebaseAnalyticsObserver observer =
     FirebaseAnalyticsObserver(analytics: analytics);
}

Next, in the main class of the application, inside the MaterialApp widget, we assign the observer object.

class OctaApp extends StatelessWidget {
 const OctaApp({Key? key}) : super(key: key);
 @override
 Widget build(BuildContext context) {
   return MaterialApp(
     ...
     navigatorObservers: [
       FirebaseAnalyticsService.observer,
     ],
    ...
   );
 }
}

Next, insert the google-services.json file in the application configuration for Android; this file is downloaded from the Firebase project and placed inside the app directory of the project. In the app/build.gradle file, the following plugin must be added:

apply plugin: 'com.google.gms.google-services'

For iOS, you need to place the GoogleService-Info.plist file, which can also be downloaded from Firebase, in the root directory of your Xcode project.


With this, the basic configuration of Google Analytics in the Flutter application is completed.


There are additional configurations for events that can be set for Google Analytics, such as the use of the logEvent function that records a custom event with the given name and parameters.

static Future<void> sendAnalyticsEvent() async {
 await analytics.logEvent(
   name: 'test_event',
   parameters: <String, dynamic>{
     'string': 'string',
     'int': 42,
     'long': 12345678910,
     'double': 42.0,
     'bool': true.toString(),
   },
 );
}

To learn more, check out the official documentation on GitHub.


Where can I view the information provided by Google Analytics in Firebase?


Firebase offers two options for viewing the information provided by Google Analytics:

  • General overview menu: You can quickly observe the daily active users and retention.

  • Analytics menu: you can see a general summary of data, real-time events, conversions, audiences, custom definitions, tracking of releases, and debug events.


For more detailed information, we recommend going directly to the Google Analytics platform.


Follow us for more information on developing applications with Flutter!

bottom of page