Rewarded interstitial is a type of incentivized ad format that allows 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
- Unity plugin 5.4.0 or higher.
- Complete the Get started guide.
Implementation
The primary steps to integrate rewarded interstitial ads are as follows:
- Load an ad
- [Optional] Validate SSV callbacks
- Display the ad and handle the reward event
- Use ad event callbacks
Load an ad
Loading an ad is accomplished using the static LoadAd()
method on the
RewardedInterstitialAd
class. The load method requires an ad unit ID, an
AdRequest
object, and a completion handler which gets called when ad loading
succeeds or fails. The loaded RewardedInterstitialAd
object is provided as a
parameter in the completion handler. The example below shows how to load a
RewardedInterstitialAd
.
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
private RewardedInterstitialAd rewardedInterstitialAd;
...
public void Start()
{
...
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
RewardedInterstitialAd.LoadAd(adUnitId, request, adLoadCallback);
}
private void adLoadCallback(RewardedInterstitialAd ad, string error)
{
if (error == null)
{
rewardedInterstitialAd = ad;
}
}
}
[Optional] Validate server-side verification (SSV) callbacks
Apps that require extra data in server-side verification
callbacks should use the custom data feature of rewarded ads. Any string value
set on a rewarded 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 won't be present in the SSV callback.
The following code sample demonstrates how to set the SSV options after the rewarded interstitial ad is loaded.
void HandleRewardedInterstitialAdLoaded(RewardedInterstitialAd ad,
AdFailedToLoadEventArgs error)
{
// Create and pass the SSV options to the rewarded interstitial ad.
var options = new ServerSideVerificationOptions
.Builder()
.SetCustomData("SAMPLE_CUSTOM_DATA_STRING")
.Build()
ad.SetServerSideVerificationOptions(options);
}
If you want to set the custom reward string, you must do so before showing the ad.
Display the ad and handle the reward event
When presenting your ad, you must provide a callback to handle the reward for the user.
The following code presents the best method for displaying a rewarded interstitial ad.
public void ShowRewardedInterstitialAd()
{
if (rewardedInterstitialAd != null)
{
rewardedInterstitialAd.Show(userEarnedRewardCallback);
}
}
private void userEarnedRewardCallback(Reward reward)
{
// TODO: Reward the user.
}
Use ad event callbacks
To further customize the behavior of your ad, you can hook into a number of events. Listen for these events by registering a delegate for the appropriate event handler.
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
private RewardedInterstitialAd rewardedInterstitialAd;
...
public void Start()
{
...
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
RewardedInterstitialAd.LoadAd(adUnitId, request, adLoadCallback);
}
private void adLoadCallback(RewardedInterstitialAd ad, string error)
{
if (error == null)
{
rewardedInterstitialAd = ad;
rewardedInterstitialAd.OnAdFailedToPresentFullScreenContent += HandleAdFailedToPresent;
rewardedInterstitialAd.OnAdDidPresentFullScreenContent += HandleAdDidPresent;
rewardedInterstitialAd.OnAdDidDismissFullScreenContent += HandleAdDidDismiss;
rewardedInterstitialAd.OnPaidEvent += HandlePaidEvent;
}
}
private void HandleAdFailedToPresent(object sender, AdErrorEventArgs args)
{
MonoBehavior.print("Rewarded interstitial ad has failed to present.");
}
private void HandleAdDidPresent(object sender, EventArgs args)
{
MonoBehavior.print("Rewarded interstitial ad has presented.");
}
private void HandleAdDidDismiss(object sender, EventArgs args)
{
MonoBehavior.print("Rewarded interstitial ad has dismissed presentation.");
}
private void HandlePaidEvent(object sender, AdValueEventArgs args)
{
MonoBehaviour.print(
"Rewarded interstitial ad has received a paid event.");
}
}