# Ads

> Interstitial and rewarded AdMob ads on Android, and the same calls compiling to nothing everywhere else, so ad code needs no #ifdef.

Source: https://omardev29.github.io/rmp-docs/manual/modules/ads.html

[`rmp::ads`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ads.html) shows Google AdMob ads of two kinds: *interstitials*, the full-screen ads between levels, and *rewarded* ads, which the player chooses to watch in exchange for something. They are real on Android once the game [turns them on](https://omardev29.github.io/rmp-docs/manual/modules/ads.html#configuring-admob), and do nothing on every other platform, so the same game code builds on all 17 targets without a single `#ifdef`.

## Every ad goes the same way

An ad has to be downloaded before it can be shown, and that takes seconds. So each kind is three calls — ask for one early, check whether it has arrived, show it — and a shown ad is used up:

|  | Interstitial | Rewarded |
|---|---|---|
| Start loading one | [`request_interstitial()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ads.html#request_interstitial) | [`request_rewarded()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ads.html#request_rewarded) |
| Has it loaded? | [`is_interstitial_loaded()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ads.html#is_interstitial_loaded) | [`is_rewarded_loaded()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ads.html#is_rewarded_loaded) |
| Show it | [`show_interstitial()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ads.html#show_interstitial) | [`show_rewarded()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ads.html#show_rewarded) |
| The reward |  | [`take_reward_earned()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ads.html#take_reward_earned) and [`reward_amount()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ads.html#reward_amount) |

Request as soon as you know you might want one — when the game starts, when a level starts — and again after each one is shown, because showing it consumes it. Showing when nothing has loaded does nothing. A request that fails — no connection, no ad to fill it — leaves `is_*_loaded()` false and is not retried for you; asking again is the game's decision.

## Interstitials

An interstitial belongs at a natural break, where the player has stopped anyway: between two levels, after a game over. Request it while the level is being played, and show it on the way out if it made it in time:

```cpp
class GameScene : public rmp::Scene {
    void _ready() override { rmp::ads::request_interstitial(); } // loads while the level is played
};

class GameOverScene : public rmp::Scene {
    void _draw() override {
        rmp::ui::begin();
        if (rmp::ui::button("Play again")) {
            if (rmp::ads::is_interstitial_loaded()) rmp::ads::show_interstitial();
            rmp::Scene::change<GameScene>();
        }
        rmp::ui::end();
    }
};
```

An ad that has not arrived is simply skipped: the player is never kept waiting for one.

## Rewarded ads

A rewarded ad is a deal with the player — watch this, get that — and the deal is only kept when the player watched enough of it. So the reward does not come from `show_rewarded()`. It arrives later, through `take_reward_earned()`, which is true once for each reward earned and clears itself; `reward_amount()` is then the amount set on the ad unit in AdMob. A player who closes the ad early gets nothing, and no code has to say so.

```cpp
class ShopScene : public rmp::Scene {
    void _ready() override { rmp::ads::request_rewarded(); }

    void _update(float delta) override {
        if (rmp::ads::take_reward_earned()) {
            _coins += rmp::ads::reward_amount();
            rmp::ads::request_rewarded(); // the next one, for next time
        }
    }

    void _draw() override {
        rmp::ui::begin();
        rmp::ui::text("Coins: " + std::to_string(_coins));
        if (rmp::ads::is_rewarded_loaded()) {
            if (rmp::ui::button("Watch an ad for coins")) rmp::ads::show_rewarded();
        } else {
            rmp::ui::button("Ad not ready", { .enabled = false });
        }
        rmp::ui::end();
    }

    int _coins = 0;
};
```

Two things in it are worth copying:

- **The button is only live when there is an ad.** A button that does nothing when pressed reads as a broken game; a disabled one says "not yet" honestly.
- **The reward is polled, every frame.** The flag waits until it is taken, so it does not matter on which frame the ad network reports it.

Do not put progress behind an ad that may never load. On a bad connection `is_rewarded_loaded()` can stay false for a long time, and a player who cannot continue without an ad is a player who uninstalls.

[examples/ads/02_rewarded](https://omardev29.github.io/rmp-docs/examples/ads/02_rewarded.html) is this flow, end to end; [examples/ads/01_interstitial](https://omardev29.github.io/rmp-docs/examples/ads/01_interstitial.html) shows both kinds side by side.

## Everywhere but Android

Off Android — the desktops, the web, iOS — every call compiles and does nothing: `is_*_loaded()` is always false, `take_reward_earned()` never fires and `reward_amount()` is 0. The code above runs unchanged on a laptop: the buttons say "not ready" and the rest of the game is there to work on. Ads on iOS are not implemented.

`rmp::ads` forwards each call to a C function in `<admob.h>`, one for each of the calls above, with the same meaning and the same no-op off Android. A C source file in the game, which has no namespace to call into, calls them directly — and so does a game written entirely in [plain C](https://omardev29.github.io/rmp-docs/manual/raylib/plain-c.html#ads-in-plain-c), which has `<admob.h>` on its include path on every target:

```c
#include <admob.h>

void level_finished(void) {
    if (IsInterstitialAdLoaded()) ShowInterstitialAd();
    RequestInterstitialAd();
}

void every_frame(int *coins) {
    if (TakeRewardEarned()) *coins += GetRewardAmount();
}
```

## Configuring AdMob

Ads are off until the game turns them on: `[android.admob] enabled` is `false` by default, and `rmp new` writes `false` into a new game's `.toml`. (The framework's own `.toml` has it on, so that its CI builds the ads path.) Turning them on is the switch and three ids, in `[android.admob]`:

```toml
[android.admob]
enabled         = true
app_id          = "ca-app-pub-3940256099942544~3347511713"
interstitial_id = "ca-app-pub-3940256099942544/1033173712"
rewarded_id     = "ca-app-pub-3940256099942544/5224354917"
```

`app_id`

Your AdMob app's id. It goes into the Android manifest of the release build.

`interstitial_id`, `rewarded_id`

The ad unit each kind is loaded from.

These are Google's test ids The three ids above are the defaults, and they are the ones Google publishes for testing: they serve test ads. Replace them with your own before you publish. Clicking real ads from your own build is invalid traffic, and invalid traffic is how AdMob accounts get suspended.

Your ids reach the release build only. The Android debug build — the one `rmp android` makes, the one you tap through yourself, and the one Firebase Test Lab's Robo crawler taps at random — carries Google's test ids whatever the `.toml` says, the app id in its manifest included, so it shows test ads even after your own ids are in. A release build carries your ids and serves real ads wherever it is installed, your own phone included, so do not tap the ads in it.

Each id is a string, in quotes, whether ads are on or not. While they are on, each one is also checked when the configuration is read: it cannot be empty, and it has to have AdMob's shape — `ca-app-pub-` and sixteen digits, then `~` and ten digits for the app id, or `/` and ten digits for an ad unit. The Google Mobile Ads library stops the app at startup over an app id it cannot read, so a wrong one is refused before a build starts, at its line, with the reason. An app id pasted where an ad unit goes is named for what it is:

```toml
[android.admob]
enabled         = true
interstitial_id = "ca-app-pub-3940256099942544~3347511713"
```

```text
  raylib_multiplatform.toml:3: interstitial_id = "ca-app-pub-3940256099942544~3347511713"
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  configure: error: [android.admob] interstitial_id = 'ca-app-pub-3940256099942544~3347511713' is not an AdMob ad unit id.
                    An AdMob ad unit id looks like ca-app-pub-<16 digits>/<10 digits> -- this one has a '~', which makes it the app id.
                    Copy it from the AdMob console, or set [android.admob] enabled = false.
```

### With ads off

```toml
[android.admob]
enabled = false
```

With `enabled = false`, the default, AdMob is not in the Android build at all: no Google Mobile Ads library, no advertising-id permission, nothing started when the app opens, and nothing to declare about advertising ids in Google Play's Data safety form. The game's code does not change: the calls keep compiling and do nothing, exactly as they do on desktop. The same happens on its own when `android` is not among the targets the game builds, however it was left out — there is no Android build to put ads in.

With ads on, the app asks for the internet permission whatever `[android.permissions] internet` says, because an ad has to be downloaded.

`enabled` is a switch, and only `true` or `false` is accepted:

```toml
[android.admob]
enabled = "no"
```

```text
  raylib_multiplatform.toml:2: enabled = "no"
                               ^^^^^^^^^^^^^^

  configure: error: [android.admob] enabled = 'no' must be true or false.
```

## Consent

Not implementedShowing ads to players in the European Economic Area or the United Kingdom requires a consent platform certified by Google, and the framework does not ship one. With ads on, that traffic is served badly or not at all until the game adds one. Google's own, the User Messaging Platform, is already inside the Google Mobile Ads library the build links, so adding it is a consent request and a form in the Android code, not a new dependency.
