Page Summary
-
Rewarded interstitial ads offer rewards for automatically appearing ads during app transitions without requiring user opt-in.
-
Initialize the Google Mobile Ads SDK at app launch before loading ads.
-
The main steps for integrating rewarded interstitial ads include loading, optionally validating SSV, showing with a reward callback, listening for events, cleaning up, and preloading the next ad.
-
Rewarded interstitial ad objects are single-use, so a new one must be loaded for each impression.
Rewarded interstitial is a type of incentivized ad format that lets you offer rewards for ads that appear automatically during natural app transitions. Unlike rewarded ads, users aren't required to opt in to view a rewarded interstitial.
Prerequisites
Before you continue, set up Google Mobile Ads Unity Plugin.
Always test with test ads
The following sample code contains an ad unit ID which you can use to request test ads. It's been specially configured to return test ads rather than production ads for every request, making it safe to use.
However, after you've registered an app in the AdMob web interface and created your own ad unit IDs for use in your app, explicitly configure your device as a test device during development.
Android
ca-app-pub-3940256099942544/1033173712
iOS
ca-app-pub-3940256099942544/4411468910
Initialize Google Mobile Ads Unity Plugin
Before loading ads, have your app initialize Google Mobile Ads Unity Plugin by calling
MobileAds.Initialize(). This needs to be done only once, ideally at app launch.
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize Google Mobile Ads Unity Plugin.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
}
If you're using mediation, wait until the callback occurs before loading ads as this will ensure that all mediation adapters are initialized.
Load the rewarded interstitial ad
To load a rewarded interstitial ad, use the static Load method on the
RewardedInterstitialAd class. The loaded RewardedInterstitialAd object is
provided as a parameter in the completion handler. The following example loads a
rewarded interstitial ad:
Replace AD_UNIT_ID with your ad unit ID.
[Optional] Validate server-side verification (SSV) callbacks
If your app requires extra data in server-side verification
callbacks, use the custom data feature of rewarded interstitial ads. Any string
value set on a rewarded interstitial ad object is passed to the custom_data
query parameter of the SSV callback. If no custom data value is set, the
custom_data query parameter value isn't included in the SSV callback.
The following code sample demonstrates how to set the SSV options after the rewarded interstitial ad is loaded:
Replace SAMPLE_CUSTOM_DATA_STRING with your custom data.
If you want to set the custom reward string, you must do so before showing the ad.
Show the rewarded interstitial ad with reward callback
When presenting your ad, you must provide a callback to handle the reward for
the user. Ads can only be shown once per load. Use the CanShowAd method to
verify that the ad is ready to be shown.
The following code displays a rewarded interstitial ad:
if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
{
rewardedInterstitialAd.Show((Reward reward) =>
{
// The ad was shown and the user earned a reward.
Debug.Log($"Rewarded interstitial ad rewarded user: {reward.Type}, {reward.Amount}");
});
}
Listen to rewarded interstitial ad events
To further customize the behavior of your ad, you can hook into a number of events in the ad's lifecycle. The following example listens for ad events:
rewardedInterstitialAd.OnAdPaid += (AdValue adValue) =>
{
// Raised when the ad is estimated to have earned money.
Debug.Log($"Rewarded interstitial ad paid {adValue.Value} {adValue.CurrencyCode}.");
};
rewardedInterstitialAd.OnAdImpressionRecorded += () =>
{
// Raised when an impression is recorded for an ad.
Debug.Log("Rewarded interstitial ad recorded an impression.");
};
rewardedInterstitialAd.OnAdClicked += () =>
{
// Raised when a click is recorded for an ad.
Debug.Log("Rewarded interstitial ad was clicked.");
};
rewardedInterstitialAd.OnAdFullScreenContentOpened += () =>
{
// Raised when the ad opened full screen content.
Debug.Log("Rewarded interstitial ad full screen content opened.");
};
rewardedInterstitialAd.OnAdFullScreenContentClosed += () =>
{
// Raised when the ad closed full screen content.
Debug.Log("Rewarded interstitial ad full screen content closed.");
};
rewardedInterstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
// Raised when the ad failed to open full screen content.
Debug.LogError($"Rewarded interstitial ad failed to open full screen content: {error}");
};
Clean up the rewarded interstitial ad
When you're finished with a RewardedInterstitialAd object, make sure to call
the Destroy method before dropping your reference to it:
if (rewardedInterstitialAd != null)
{
rewardedInterstitialAd.Destroy();
}
This notifies the plugin that the object is no longer used and the memory it occupies can be reclaimed. Failure to call this method results in memory leaks.
Preload the next rewarded interstitial ad
A RewardedInterstitialAd object is a one-time-use object. When a rewarded
interstitial ad is shown, the object can't be used again. To request another
rewarded interstitial ad, load a new RewardedInterstitialAd object.
To prepare a rewarded interstitial ad for the next impression opportunity,
preload the rewarded interstitial ad once the OnAdFullScreenContentClosed or
OnAdFullScreenContentFailed ad event is raised:
Replace AD_UNIT_ID with your ad unit ID.
Additional resources
- HelloWorld example: A minimal implementation of all ad formats.