Using Firebase Analytics in Flutter

Using Firebase Analytics in Flutter

Practical advice when using Firebase Analytics in Flutter development

Practical advice when using Firebase Analytics in Flutter development

Setup

Getting up and running with Firebase Analytics for Flutter is pretty simple and is well described in the documentation, so I won’t repeat it all here. Once your project is setup the code to track page views is as simple as:

FirebaseAnalytics analytics = FirebaseAnalytics();
MaterialApp(
home: MyAppHome(),
navigatorObservers: [
FirebaseAnalyticsObserver(analytics: analytics),
],
);

Problem

Simple right, so what’s the problem? Well, the problem is if you navigate using anything like these approaches, then you won’t actually see any screen_view events in your dashboard:

 Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyPage()),
);
}
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => MyPage()),
);
}

So why is that? What’s the problem? In short, the problem is that these are not named routes. The reason this is a problem you can see by looking at the implementation of FirebaseAnalyticsObserver:

 String defaultNameExtractor(RouteSettings settings) => settings.name;
class FirebaseAnalyticsObserver extends RouteObserver<PageRoute<dynamic>> {
FirebaseAnalyticsObserver({
@required this.analytics,
this.nameExtractor = defaultNameExtractor,
Function(PlatformException error) onError,
}) : _onError = onError;
void _sendScreenView(PageRoute<dynamic> route) {
final String screenName = nameExtractor(route.settings);
if (screenName != null) {
analytics.setCurrentScreen(screenName: screenName).catchError(
(Object error) {
if (_onError == null) {
debugPrint('$FirebaseAnalyticsObserver: $error');
} else {
_onError(error);
}
},
test: (Object error) => error is PlatformException,
);
}
}

FirebaseAnalyticsObserver calls _sendScreenView for each navigation and uses the defaultNameExtractor to determine the screen name. If that resolves to null, then no event gets logged. The defaultNameExtractor just uses the name property of the supplied RouteSettings. And this is the problem; in the navigation examples I gave above there are no RouteSettings, so the screenName ends up as null and nothings gets logged.

Solution

Thankfully, the solution is pretty simple. Either use named routes when navigating, or supply a RouteSettings instance with your navigation such as:

 Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyPage(),
settings: RouteSettings(name: 'MyPage'),
),
);
}
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => MyPage().
settings: RouteSettings(name: 'MyPage'),
),
);
}

Extra Tip

If you want to see the events in Firebase Console quickly and reliably while debugging, use DebugView.

Android

To enable Analytics Debug mode on an Android device, execute the following command line:

adb shell setprop debug.firebase.analytics.app <package_name>

This behavior persists until you explicitly disable Debug mode by executing the following command line:

adb shell setprop debug.firebase.analytics.app .none.

iOS

To enable Analytics Debug mode on your development device, specify the following command line argument in Xcode :

-FIRDebugEnabled

This behavior persists until you explicitly disable Debug mode by specifying the following command line argument:

-FIRDebugDisabled

Note: Before using DebugView, you should ensure that your device time is accurate. A skewed device clock will result in delayed or missing events in your Analytics reports. For more information about using Firebase, check out the docs on flutter.dev.

firebase flutter mobile-apps

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

ECommerce Mobile App Development | Ecommerce Mobile App Development Services

We are leading ecommerce mobile application development company. Hire our ecommerce mobile app developer for your custom Ecommerce project at competitive rates. **Know about [Top ECommerce Mobile App Development...

What will be the impact on your pocket if you opt for Flutter app development?

We cannot imagine the modern world without mobile apps as apps have become an integral part of our lifestyle. We have mobile apps for all our daily needs right from food to travel to medicines to movie or hotel bookings and many more. Mobile app development is indeed playing a prominent part in making the world fast-paced. Almost everything can be managed through mobile apps as they are rich in features and impeccable in making the life of user easy. The high performance of mobile apps has made it inevitable for businesses to opt for them or lag behind in …

Mobile App development Company in Melbourne Australia

If you are seeking online **[mobile app development Melbourne Australia](http://www.efrog.com.au/ "mobile app development Melbourne Australia")**? Then Efrog Pty Ltd is the best online choice for you we are an award-winning company in Australia....

Top Mobile App Development Company in Texas

AppClues Studio is a Top Mobile App Development Company in Texas. We offer comprehensive mobile app development services for businesses that are ready to tackle innovation head-on. We specialize in building mobile apps that leverage IoT, AR/VR...

Top Mobile App Development Company in Washington

AppClues Studio is an experienced mobile app development company in Washington. We have an experienced team of mobile app developers who are proficient in developing mobile apps for Android & iOS using state-of-the-art mobile app development...