# `firebase_analytics`

| Package | Kind | Provides the role | Needs | Choose with |
| --- | --- | --- | --- | --- |
| [`smf_firebase_analytics`](https://pub.dev/packages/smf_firebase_analytics) | infrastructure | Analytics | [`firebase_core`](https://doc.saymyframe.com/modules/firebase-core.md) | `-m firebase_analytics` |

`firebase_analytics` records what the users of the app do, and the screens they see, in [Google Analytics for Firebase](https://firebase.google.com/docs/analytics) with the [firebase_analytics](https://pub.dev/packages/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. It calls each of them on its own and gives each its own copy of the parameters, so a service that fails, or that changes the parameters, affects no other.

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.

In the [guide for coding agents](https://doc.saymyframe.com/guides/coding-agents.md) of the app, the analytics role describes the service under "Analytics". In an app with a router, the module adds that screen views need no code.

## 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](https://doc.saymyframe.com/extending/role-provider.md#following-the-router): 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 in the app besides the setup of Firebase, which [`firebase_core`](https://doc.saymyframe.com/modules/firebase-core.md) runs after generation.

## Google Analytics in the project

The events of the app reach Google Analytics only once the Firebase project is linked to it, and a project that `flutterfire configure` creates is not. Until then, the app logs its events, but they show up nowhere. So every app of the module lists the step by hand `firebase_analytics.google_analytics`, which turns on Google Analytics in the project, unless it is on already. The link belongs to the project, so repeat this in each Firebase project of your flavors. The README of the app has it under "Finish the setup", and the run ends with the link to its guide. The [click-by-click guide](https://doc.saymyframe.com/setup/firebase-analytics-google-analytics.md) shows each screen.

## 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:

```bash
smf create my_app -m firebase_analytics
```
