# Crash reporting and analytics

Two roles of an app can have several providers at once: crash reporting and analytics. The code of the app reports an error through one `CrashReporter` and logs an event through one `AnalyticsService`, and each forwards the call to every module that provides its role.

| Part | Role | Modules | What they bring |
| --- | --- | --- | --- |
| Crash reports | Crash reporting | [`firebase_crashlytics`](https://doc.saymyframe.com/modules/firebase-crashlytics.md), [`sentry`](https://doc.saymyframe.com/modules/sentry.md) | The errors that the app does not handle and those that its code reports, to Firebase Crashlytics, to Sentry, or to both |
| Analytics | Analytics | [`firebase_analytics`](https://doc.saymyframe.com/modules/firebase-analytics.md), [`posthog`](https://doc.saymyframe.com/modules/posthog.md) | The events that the app logs and, in an app with a router, the screens that the user sees, in Google Analytics for Firebase, in PostHog, or in both |

```bash
smf create my_app -m firebase_crashlytics,sentry,firebase_analytics,posthog
```

`firebase_crashlytics` and `firebase_analytics` bring [`firebase_core`](https://doc.saymyframe.com/modules/firebase-core.md), which sets up Firebase; see [Firebase](https://doc.saymyframe.com/guides/firebase.md). `sentry` and `posthog` read their keys from `.env`, so they bring [`dart_define`](https://doc.saymyframe.com/modules/dart-define.md) while that module is the only provider of the values in `.env`; see [Values in .env](https://doc.saymyframe.com/guides/env.md).

## One service for several providers

The crash reporting role generates `lib/core/crash_reporting/crash_reporter.dart`, and the analytics role `lib/core/analytics/analytics_service.dart`. `createCrashReporter()` and `createAnalyticsService()` return the one service of the app, which calls each provider on its own:

- A provider that throws, or whose future fails, keeps no other from the call, and its failure reaches neither the code that called nor the handlers of the errors that nothing catches. A debug build prints it, such as `The crash reporter SentryCrashReporter failed: ...` or `The analytics service PostHogAnalyticsService failed: ...`.
- Each analytics provider gets its own copy of the parameters of a call.
- A provider whose factory throws, or whose start fails, is left out, and the app starts and works with the others. A debug build prints why, such as `initSentryCrashReporter() failed, so the app works without it: ...`. So an app whose `.env` has no DSN of Sentry or no token of PostHog starts and runs without that provider.

Sentry and PostHog start asynchronously, so `bootstrap()` awaits `initCrashReporting()` and `initAnalytics()` of the two files, which start them, before the app reports or logs anything. Crashlytics and Firebase Analytics are created on first use, without waiting. With a DI container, each role registers its one service in it; see [Services and state](https://doc.saymyframe.com/guides/services.md).

The code of the app never calls an SDK. It reports with `recordError()` of `CrashReporter`, and logs with `logEvent()` of `AnalyticsService`; the guide for coding agents of the app says the same.

## Errors that nothing catches

`installCrashReporting()`, which `bootstrap()` calls once the crash reporters are ready, reports the errors of the main isolate that the app does not handle, through `FlutterError.onError` and `PlatformDispatcher.instance.onError`. The handlers belong to the role, so a provider whose SDK installs handlers of its own turns them off: the start of Sentry removes its `OnErrorIntegration` and puts `FlutterError.onError` back. So each error reaches each crash reporter once. See [Services and state](https://doc.saymyframe.com/guides/services.md#crash-reporting) for what the handlers cover.

The native crashes stay with the SDKs of the providers. Next to Crashlytics, Sentry records the native crashes that it catches too: an uncaught exception of Java or Kotlin reaches both, and a signal of native code on Android may reach Sentry alone.

## Screens

In an app with a router, each analytics module gives the router a listener of the screen that the user sees, and the router calls each listener on its own. `firebase_analytics` logs a `screen_view` event and `posthog` a `$screen` event, each under the full name of the route of the screen, such as `home.home`. Neither records the error screen of the router, whose location may hold anything, such as a token of a link. Without a router, the modules record only what the app logs.

## Which build an event came from

Every app has the [flavors](https://doc.saymyframe.com/guides/flavors.md) `dev`, `staging` and `prod`, and a debug build sends its events to Sentry and to PostHog too. Sentry gives each event the flavor of the build as its environment, and PostHog gives each event the property `app_flavor`, except the first events of a new install, which it records while it starts. So filter on the flavor to see the release alone. To keep debug builds out of Sentry altogether, its page gives [one line](https://doc.saymyframe.com/modules/sentry.md#what-reaches-sentry).

## The collection switch

`setAnalyticsCollectionEnabled()` of `AnalyticsService` is the one switch of collection, for every provider of the app:

- Once a call with `false` completes, nothing that the app logs from then on, and nothing that an SDK records itself from then on, such as a screen, a session or an install, reaches the backend of a provider, until a call with `true` completes.
- What the app logged before the call may still leave with a batch that a provider had queued. PostHog, for example, keeps its queue of events on the device and does not empty it when it opts out.
- Every provider keeps the setting on the device, so what the code chose holds from the first moment of every later start, before any code of the app runs. No other call of the service changes it.

Off means that nothing recorded while collection is off is sent. It does not mean that no SDK writes to the device: an SDK keeps at least the setting there. Crash reports are another role, which this switch does not stop. What an SDK asks its backend for its own configuration is not covered either.

### Before the code chose

Until the code first calls the switch on a device, the app collects as `analyticsCollectedFromFirstStart` in `lib/core/analytics/analytics_service.dart` says. `smf create` writes that constant from `--analytics-collection`:

| Value | `analyticsCollectedFromFirstStart` | The app |
| --- | --- | --- |
| `on`, the default | `true` | Collects from its first start, until its code turns collection off. |
| `off` | `false` | Collects nothing until its code turns collection on, as once the user agreed. |

A run in a terminal asks once the modules of the app are known, when the app has a module of analytics:

```text
Does the app collect analytics before the user agrees?
  Yes, from its first start
  No: only once your code turns it on, as after the user agrees
```

Enter takes the first answer, and a run without a terminal takes `on`. To choose without the question:

```bash
smf create my_app -m firebase_analytics,posthog --analytics-collection=off
```

In a debug build of an app with `off`, `bootstrap()` prints at each start:

```text
Analytics: this app collects nothing on a device until its code turns collection on there with setAnalyticsCollectionEnabled(true), as once the user agreed. See the section Analytics of README.md.
```

Firebase Analytics starts before any Dart code runs and records the first open of the app and the start of a session there. So in an app with `off`, `firebase_analytics` also turns its collection off by default in the native projects: `firebase_analytics_collection_enabled` is `false` in `android/app/src/main/AndroidManifest.xml`, and `FIREBASE_ANALYTICS_COLLECTION_ENABLED` is `false` in `ios/Runner/Info.plist`. An app with `on` has neither line. PostHog starts from Dart, so the constant alone decides what it does before the code chose, and it needs no such line.

To change what the app does before its code chose, change `analyticsCollectedFromFirstStart` together with those native lines, or generate the app again with the other value. On iOS, PostHog keeps nothing when the code chose what the constant says already, so an app that changes the constant from `false` to `true` collects on the devices of iOS whose users declined before. In Firebase Analytics, an install whose user turns collection on later records its `first_open` event then, with the parameter `deferred_analytics_collection`.

### Consent

The README of the app, in its section "Analytics", says what consent asks of the app. In the European Economic Area, the ePrivacy rules generally need the consent of the user before an app stores or reads analytics data on the device. In the UK, the Privacy and Electronic Communications Regulations, as the Data (Use and Access) Act 2025 amended them, let some analytics run without consent: storage whose only purpose is statistics on how the app is used, to improve it, as long as the user gets clear information about it and a simple and free way to object. The guidance of the ICO tells what qualifies.

Where consent is needed, generate the app with `--analytics-collection=off`, ask the user in a screen of your own, such as a page of the onboarding, and turn collection on once the user agreed:

```dart
await createAnalyticsService().setAnalyticsCollectionEnabled(true);
```

With a DI container, take the service from the container instead. SMF shows no consent dialog. Apple's App Review Guideline 5.1.1(ii) asks for an easily accessible and understandable way to withdraw consent: give the settings screen a switch that calls `setAnalyticsCollectionEnabled()`, as an [entry of your own](https://doc.saymyframe.com/guides/preferences.md#the-settings-screen). The App Store and Google Play also ask for a link to a privacy policy in the listing of the app and inside the app: say there what the analytics of the app collect, and what the crash reporters send. The section of each module in the README of the app says what its service gets.

This is not legal advice.

## Finish the setup

Each module has its steps by hand, which the README of the app lists in its section "Finish the setup":

| Module | Step |
| --- | --- |
| `firebase_analytics` | [Turn on Google Analytics in the Firebase project](https://doc.saymyframe.com/setup/firebase-analytics-google-analytics.md), unless it is on already |
| `sentry` | [Create a Flutter project in Sentry and copy its DSN into .env](https://doc.saymyframe.com/setup/sentry-dsn.md) |
| `posthog` | [Create a PostHog project and copy its token and host into .env](https://doc.saymyframe.com/setup/posthog-project-token.md) |

`firebase_crashlytics` needs only the setup of Firebase, which `smf create` runs; see [Firebase](https://doc.saymyframe.com/guides/firebase.md).

## In tests

A test that runs `bootstrap()` with Sentry runs in real time, since the start of sentry_flutter never completes in the fake time of a widget test, and it sets `configureSentryForTests` to keep the events and send none; see [`sentry`](https://doc.saymyframe.com/modules/sentry.md#in-tests). posthog_flutter has no mocks of its own, so the tests of the app replace its platform side; see [`posthog`](https://doc.saymyframe.com/modules/posthog.md#in-tests).

What the two roles promise holds with every provider. SMF's CI checks the switch of collection in running apps: with `off`, nothing is collected at the first start, no session either, and a setting that the code chose holds at the next start. On the real packages, the test of `firebase_analytics` reads its native lines, and that of `posthog` checks the setting that PostHog keeps at the next start. See [Contributing](https://doc.saymyframe.com/contributing.md#the-roles-at-run-time).
