Anh Tran dev notes

Production bugs from mobile and backend work, written up with the fix and how it was checked.

Note 06 / Analytics / Ads / Flutter

Measuring ad show rate per placement in GA4: four events, one gate per trigger and a denominator trap

How to get a per-unit show rate (impressions divided by fills) out of Firebase Analytics for Flutter and React Native apps. The event contract, why GA4 cannot backfill custom dimensions, and the (not set) bug that made a rate read 100%.

By
Trần Thế Anh, Software Engineer
Published
Work done
October 2026, Flutter and React Native apps
Stack
Firebase Analytics, GA4, BigQuery export, Dart, TypeScript
Reading
6 min, 1,173 words

AdMob reports a show rate per ad unit, but only for AdMob. If you want the same number next to your own funnel (which screen, which trigger, how long the user waited), you have to emit it yourself. This note describes a small event contract that was reused across several apps, the places it can silently go wrong, and the order of setup that keeps GA4 from throwing data away.

The definition

Show rate is ad_impression divided by ad_loaded: of the ads that were filled, how many reached the screen. It follows the AdMob definition, so numbers can be compared. It is not fill rate, which needs a request event this contract does not have.

Four events, fixed names

Event When Parameters
ad_loaded the SDK reports a fill placement
ad_impression the SDK reports the ad is on screen placement, ad_type
ad_load_failed a load error placement, code
ad_gate a fullscreen trigger ended placement, result, wait_ms

placement is the ad slot name from the team’s ad-ID sheet, with a prefix per format (inter_, native_, banner_, reward_, open_). ad_type is the format. The names are fixed on purpose: a dashboard that reads ad_impression breaks quietly if one app calls it adImpression.

Rules that matter more than the code

The impression comes from the SDK callback only. In Flutter that is the onAdImpression callback of the ad, or FullScreenContentCallback.onAdImpression for fullscreen formats. Logging it at “widget mounted” or “show() returned” counts ads the user never saw and makes the rate look better than reality.

One ad_gate per fullscreen trigger, ended exactly once. A trigger is the moment the app decides to show an interstitial: a back press, a finished task. The gate records what happened, in one of these results: shown, shown_late (there was no ready ad and the user waited for one), show_failed, or skipped_<reason>. The skip reason is turned into a stable code from the text of the function that decided to skip, such as an interval cap or a paywall screen. A tiny class makes “exactly once” hard to get wrong:

class Gate {
  Gate(this.placement) : _clock = Stopwatch()..start();
  final String placement;
  final Stopwatch _clock;
  bool waited = false;
  bool _ended = false;

  void shown() => _end(waited ? 'shown_late' : 'shown');
  void showFailed() => _end('show_failed');
  void skipped(String code) => _end('skipped_$code');

  void _end(String result) {
    if (_ended) return;
    _ended = true;
    log('ad_gate', {
      'placement': placement,
      'result': result,
      'wait_ms': _clock.elapsedMilliseconds,
    });
  }
}

That is a short illustration of the idea rather than the shipped file. The point is the _ended flag: whichever path calls first wins, and the second call is a no-op, so a double callback cannot double the count.

Fire and forget, errors swallowed. Every call to logEvent runs unawaited inside a try/catch. Analytics failing must never fail an ad.

Revenue events must not reuse the name ad_impression. Paid-event handlers (the ones that carry value) are tempting places to log ad_impression again. That makes a second event of the same name with no placement, and then every ratio is wrong. Call it ad_revenue or similar, or give it a placement.

Call from one central place. The ads service and the banner/native widgets log; the screens do not. Scattering the calls across screens is how a trigger ends up with zero gates or two.

React Native: use the modular API

In the React Native Firebase package, the namespaced style analytics().logEvent(...) is deprecated and, from v22 on, does nothing. An app lost its events that way. The working form is modular:

import { getAnalytics, logEvent } from '@react-native-firebase/analytics';

export function adLoaded(placement: string) {
  void logEvent(getAnalytics(), 'ad_loaded', { placement }).catch(() => {});
}

If an app seems to log events but the console shows none, check which call style it uses before looking anywhere else.

Register the custom dimensions before the first event

GA4 only lets you break events down by a parameter after you register it as a custom dimension, and the registration applies from the day you make it. Nothing is backfilled. So placement, ad_type and result have to be registered when the property is created, even before an app sends them (the form accepts a name that has not been seen yet). Register them three weeks late and three weeks of events show up under “(not set)”, which cannot be split by unit afterwards.

The denominator trap

The second ordering problem is in the arithmetic. Events from before the dimension was registered, or from old app versions that never sent placement, land in the (not set) row. If you divide per row you get a per-unit number. If you also include (not set) in the totals you get a number that belongs to no unit.

The bug that surfaced it: a custom dimension has values only from the day it is registered, and (not set) was being counted as a real outcome. In the gate breakdown, events from before registration fell into the “other” group, and the “skipped by gate” box read 100% on the day the dimension was registered. The lesson that came out of it: (not set) means unknown. Drop it from both the numerator and the denominator of every ratio, never fold it into an “other” or “failed” group, and show the unattributed events on their own line so they stay visible.

Two platforms, two blocks

Android and iOS show different ads, different caps and different load behaviour, and some placements exist on one platform only (for instance a back-press interstitial that has no iOS equivalent). Add them up and the number says nothing about either. The dashboard shows two blocks, and a placement that does not exist on a platform gets no gate on that platform.

Where the numbers come from

GA4’s own reporting API is the fallback. The primary source is the BigQuery export, because it keeps the full parameter history without sampling or cardinality limits. Turning the export on needs the pay-as-you-go plan, which costs money, so it is a decision to put in front of whoever owns the budget, not a switch for the developer to flip.

Checking it

Before calling the work done:

  1. Open DebugView (or the verbose Firebase log on the device) and look for all four events with a placement, on Android and on iOS.
  2. Trigger each fullscreen path once and count ad_gate. Each trigger should produce exactly one.
  3. Run a code check that fails when an event name is changed or a revenue event reuses ad_impression.
  4. Wait a day or two after a build reaches users. A build already in the stores will not have the events, so an empty dashboard right after the change is correct.

I did not run any of this while writing the article. The contract and the lessons come from the project’s analytics notes and the template code, which I read; the example code above is rewritten, not copied.