Skip to main content

firebase_analytics

PackageKindProvides the roleNeedsChoose with
smf_firebase_analyticsinfrastructureAnalyticsfirebase_core-m firebase_analytics

firebase_analytics records what the users of the app do, and the screens they see, in Google Analytics for Firebase with the firebase_analytics package.

What it adds to the app​

The module adds firebase_analytics: ^12.6.0 to the dependencies, and lib/core/analytics/firebase_analytics_service.dart with the service on FirebaseAnalytics.instance. In an app with a router, that file also has logFirebaseScreenView, the listener of the screen.

Whichever module provides it, the analytics role adds lib/core/analytics/analytics_service.dart with:

  • the AnalyticsService interface: logEvent logs an event with its parameters, logSignIn and logSignUp log that the user signed in or signed up, setUserId and setUserProperty describe the user and clear what they set with null, and setAnalyticsCollectionEnabled turns the collection of analytics data on or off;
  • createAnalyticsService(), which returns the analytics service of the app, a service that forwards every call to the services of all the modules that provide analytics.

An event goes to logEvent of Firebase Analytics. A sign-in and a sign-up become its standard login and sign_up events, with the method in their method parameter.

Screen views​

When the app has a router, the module gives it logFirebaseScreenView, a listener of the screen the user sees. The router calls it once for each change of that screen, as the router role promises: the first screen, a page that a navigation shows or that shows again, the page of the tab the user switches to, and the page on top when it shows another location. For each call, the listener logs a screen_view event:

  • The screen_name is the full name of the route, such as home.home. The fallback start screen of an app with no route to start on is logged as /. The error screen of the router is not logged, because the location it could not show may hold anything, such as a token of a link.
  • Only the pages of the router count. Pages that the app shows with a navigator directly, such as with Navigator.push, and dialogs are not logged.
  • Nothing waits for a screen view, so an error of the platform in logging one is printed rather than left to the handler of uncaught errors, which a module of crash reporting would report.

Without a router, the module records only what the app logs itself.

Getting the service​

The service is created on first use, without waiting, so the app starts as fast as it would without it. With a module of dependency injection, the role registers it in the container as a lazy singleton, and a feature takes it with resolve in its composition file. Without a container, the code of the app calls createAnalyticsService().

Native setup​

The module adds nothing to the native projects. Firebase Analytics needs no setup of its own besides the setup of Firebase, which firebase_core runs after generation.

Choosing it​

smf create asks which modules provide the analytics of the app, and the answer may be none. To choose it without the question, name it in -m, and firebase_core comes with it:

smf create my_app -m firebase_analytics