Skip to content
Tutorials Center
My learning

Topics

Lesson 55 of 71 ยท Shipping

Crash Reporting and Analytics

Find out what breaks in production: Sentry crash reporting, source maps, analytics events, feature flags, and privacy and consent rules for mobile apps.

When a web page breaks, you can often reproduce it in your own browser. When a phone app breaks, it happens on someone else's device, on an operating system version you do not own, with no console in sight. Most users never report it. They simply delete the app. If you do not measure what happens in production, you will not know.

This lesson covers the three tools that close that gap: crash and error reporting (what broke, and where), analytics (what people actually do) and feature flags (turning things on and off without a release), plus the privacy rules that go with collecting any of it.

Read-only and stand-in. Sentry and analytics services send data from a real app to a server, so their setup is shown in plain code blocks. The TryIts are stand-ins that run in a browser: an event logger with a consent gate, and a tiny stack-trace decoder that uses a source map. SDK options change between versions, so follow the vendor's current guide when you install.

Why release builds need monitoring

In development, errors appear in LogBox (see Debugging and Performance Profiling). In a release build, that is gone. A thrown error in your JavaScript can show a blank screen or close the app, and the only evidence is in the user's pocket. A monitoring service gives each failure a record with the message, the stack trace, the device, the OS and app version, and the steps the user took just before.

flowchart LR
    A["Error on a phone"] --> B["SDK adds context<br/>device, version, breadcrumbs"]
    B --> C["Sent to the service"]
    C --> D["Grouped with similar errors"]
    D --> E["Alert and a readable stack trace"]

One crash becomes one issue, with a count of how many people it hit.

The grouping step matters most. You do not want 4,000 emails for 4,000 crashes. You want one issue that says "affects 4,000 users, started in version 2.4.0".

Crash reporting with Sentry

Sentry is a common choice (others include Bugsnag, Firebase Crashlytics and Datadog). In an Expo project the wizard does most of the setup:

Terminal
npx @sentry/wizard@latest -i reactNative

The core of the setup is a few lines at the entry of your app:

TSX
import * as Sentry from '@sentry/react-native';

Sentry.init({
  dsn: process.env.EXPO_PUBLIC_SENTRY_DSN,
  enabled: !__DEV__,
  environment: __DEV__ ? 'development' : 'production',
  tracesSampleRate: 0.2, // performance traces for 20% of sessions
  beforeSend(event) {
    if (event.user) delete event.user.email; // never send what you do not need
    return event;
  },
});

export default Sentry.wrap(App);

Points worth knowing:

  • The DSN is the address the SDK sends events to. It is public by design (it can only submit events, not read them), so it is fine in the bundle. Compare that with the auth token used to upload source maps, which is a secret that must live only in your build environment (an EAS secret), as discussed in Mobile App Security.
  • Sentry.wrap(App) catches errors in your component tree and in JavaScript. The SDK also catches native crashes (a segfault in a native library, an out-of-memory kill, an "app not responding" on Android), which JavaScript error handling can never see.
  • Capture handled errors yourself where it helps: Sentry.captureException(error) inside a catch, or inside an error boundary (see Error Boundaries).
  • Add context sparingly: Sentry.setUser({ id }) with an internal id, tags such as the subscription plan, and breadcrumbs (automatic trail of taps, navigation and network calls leading up to the error).
  • Use beforeSend to scrub personal data before it leaves the device. Do not rely on every developer remembering.

Sentry also offers performance monitoring (slow screens, slow requests, startup time) and session replay. They cost money and carry more privacy weight, so enable them deliberately and sample rather than recording everything.

Source maps: readable stack traces

In a release build your JavaScript is bundled, minified and compiled to Hermes bytecode. The raw stack trace points at positions inside index.android.bundle, which no human can read. A source map is a file that translates those positions back into your original files, lines and function names. You must upload the source maps for every build and every OTA update, and they must match the release exactly.

The Sentry Expo integration uploads them for you during the build when it has the auth token (SENTRY_AUTH_TOKEN set as an EAS secret). For OTA updates published with EAS Update, check Sentry's current guide for the upload step; updates have their own bundles and need their own maps (see Over-the-Air Updates and Release Strategy). Native crashes need their own symbol files (dSYMs on iOS, mapping files on Android) for the same reason.

The release name and build number in the SDK must match what you uploaded, or Sentry cannot find the right map and the trace stays unreadable. When a trace looks like gibberish, a missing or mismatched source map is the first suspect.

Analytics: what do people actually do?

Crash reporting tells you what broke. Analytics tells you how the app is used: how many people finish onboarding, where they leave the checkout, which feature nobody touches. Services include PostHog, Amplitude, Mixpanel and Firebase Analytics.

The model is simple: you send events, each with a name and a few properties.

TSX
track('add_to_cart', { productId: 'sku_42', price: 19.9, currency: 'EUR' });
track('screen_view', { screen: 'Checkout' });

Good habits:

  • Wrap the vendor. Call your own track() function and let it call the SDK. Switching vendors, adding consent checks and scrubbing data then happen in one place.
  • Name events consistently. One style (snake_case, object then action) and a short written list of events with their properties. An analytics setup without a naming plan turns into a swamp.
  • Track questions, not everything. Decide what you want to learn first ("where do users abandon checkout?") and track the few events that answer it.
  • Keep properties small and non-personal: ids and categories, never emails, names, phone numbers, addresses or free text typed by users.
  • Send screen views from your navigation container's state change callback so every screen is covered without per-screen code (see Navigation Concepts for Mobile).

Feature flags

A feature flag is a switch controlled from a server: if (flags.newCheckout) .... Flags let you:

  • release code turned off, then enable it for 5% of users, then everyone,
  • keep a kill switch to disable a broken feature in seconds, without a store build,
  • run A/B tests, and show a feature only to testers or one country.

Rules of thumb: always give every flag a default value that is safe when the app is offline or the flag service is down, cache the last known values, and delete flags once a feature is fully released, because every old flag is a branch of code to maintain. Flags do not replace OTA updates: a flag changes behavior of code that is already in the app, while an update changes the code.

Three tools, three questions

Crash reporting What broke, for whom, and where?
Analytics What do people actually do?
Feature flags Can I switch this on or off safely?
1 / 2

Collecting data about people is regulated, and the stores check it. This is not legal advice, but these rules of thumb keep most apps out of trouble:

  • Consent. In the EU and UK (GDPR and related rules) and in many other regions, analytics that identify or track users generally need the person's consent before collection starts. Crash reporting is often treated more leniently, but still must not collect personal data you do not need. Ask a lawyer for your situation.
  • Opt-out must work. If a user withdraws consent, stop sending events immediately, and delete whatever is still queued on the device.
  • Apple. The App Store requires you to describe your data collection in the App Privacy section (the "nutrition label") and bundles from certain SDKs need a privacy manifest. Only if you track users across other companies' apps and websites do you need the App Tracking Transparency prompt (NSUserTrackingUsageDescription). Plain first-party analytics does not trigger it.
  • Google Play. The Data safety form must truthfully list what you collect and share, including through third-party SDKs.
  • Know your SDKs. Every analytics or crash SDK you add is part of your privacy disclosure. Read what each one collects by default.

Wrong answers on these forms can get an app update rejected or an app removed, so treat them as part of the release checklist from Builds with EAS and App Store Submission.

This stand-in is not a real analytics SDK. It shows the decisions your own track() wrapper should make: drop events when there is no consent, remove personal properties, batch events and send them together, and delete the queue when consent is revoked. It uses a pure reducer so the behavior is easy to follow.

Analytics wrapper: consent, scrubbing and batching

Try this order: press Track sign_up first (it is dropped, because there is no consent), grant consent, then track all three events. The third one fills the batch and sends it, and the email property is gone from the sent data. Then track one more event and revoke consent: the queue is emptied.

Stand-in: decoding a stack trace

This plain-JavaScript stand-in imitates what the monitoring service does with a source map: it takes a frame position in the bundle and finds the closest earlier mapping on the same line. Real source maps use a compact encoded format (VLQ); the table below is a readable simplification.

JS Source map lookup for stack frames

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first three frames land in your own files, and the last two show the two ways a frame stays unreadable: a position with no mapping, and a framework frame that has no line in your source. The service does this lookup for you, but only if you uploaded the matching map.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why are release builds in need of crash reporting?

    In production you cannot see the user's screen or console. A reporting service collects the error, device and version details for you.
  2. 2. Which value is safe to ship in the app bundle?

    The DSN can only submit events and is meant to be public. The auth token can manage your project and belongs in the build environment only.
  3. 3. A stack trace shows index.android.bundle:1:48211 instead of your file names. What is the most likely cause?

    Source maps translate bundle positions into your original files. A missing or mismatched map leaves the raw positions.
  4. 4. A user revokes analytics consent. What should your tracking wrapper do?

    Withdrawing consent must stop collection immediately, including data waiting on the device.
  5. 5. What is a sensible default for a feature flag when the flag service is unreachable?

    Flags must work offline. Use a safe default and cache the last known values.

Summary

  • Monitoring turns anonymous failures on other people's phones into grouped issues with stack traces, versions and devices.
  • Sentry's SDK catches JavaScript and native crashes; the DSN is public, the upload auth token is a secret.
  • Upload matching source maps for every build and every OTA update, or traces stay unreadable.
  • For analytics, wrap the vendor, name events consistently, track questions rather than everything and never send personal data in properties.
  • Feature flags let you ship code switched off and keep a kill switch; give each a safe default and remove it after rollout.
  • Respect consent and fill in the Apple and Google privacy forms honestly; revoking consent must stop collection.

You now know how to build, ship and watch a React Native app. Put it all together in Project: Todo App.