Interstitial advertising

Interstitial advertising is a full-screen ad format embedded within the app content during natural pauses, such as transitioning between game levels or completing a target action.

When the app renders an interstitial ad, the user has two options: click the ad and proceed to the advertiser site or close the ad and go back to the app.

In interstitial ads, user attention is fully focused on the ads, which is why the impression cost is higher.

This guide will show how to integrate interstitial ads into Unity apps. In addition to code examples and instructions, it contains format-specific recommendations and links to additional resources.

Prerequisite

  1. Follow the process in Quick start for integrating the Yandex Mobile Ads Unity Plugin.
  2. Make sure you're running the latest Yandex Mobile Ads Unity Plugin version. If you're using mediation, make sure you're also running the latest version of the unified build.

Implementation

Key steps for integrating interstitial ads:

  1. Create an InterstitialAdLoader ad loader and register the listeners for ad loading events.
  2. Set the ad loading parameters in AdRequestConfiguration.
  3. Load the ad with the InterstitialAdLoader.LoadAd(AdRequestConfiguration) method.
  4. Pass additional settings if you're using Adfox.
  5. Register listeners for events where users interact with your ad.
  6. Show the ad by calling the Interstitial.Show() method.
  7. Release the resources

Features of interstitial ads integration

  1. If you received an error in the onAdFailedToLoad event, don't try to load a new ad again. If there's no other option, limit the number of ad load retries. That will help avoid constant unsuccessful requests and connection issues when limitations arise.

  2. We recommend using a single instance of InterstitialAdLoader for all ad loads to improve performance.

Step-by-step guide

  1. Create an InterstitialAdLoader object in the C# script attached to GameObject, then register ad load event listeners.

    using UnityEngine;
    using YandexMobileAds;
    using YandexMobileAds.Base;
    
    public class YandexMobileAdsInterstitialDemoScript : MonoBehaviour
    {
        private InterstitialAdLoader interstitialAdLoader;
        private Interstitial interstitial;
    
        private void SetupLoader()
        {
            interstitialAdLoader = new InterstitialAdLoader();
            interstitialAdLoader.OnAdLoaded += HandleInterstitialLoaded;
            interstitialAdLoader.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
            // ...
        }
    
        public void HandleInterstitialLoaded(object sender, InterstitialAdLoadedEventArgs args)
        {
            // The ad was loaded successfully. Now you can handle it.
            interstitial = args.Interstitial;
        }
    
        public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
        {
            // Ad {args.AdUnitId} failed for to load with {args.Message}
            // Attempting to load a new ad from the OnAdFailedToLoad event is strongly discouraged.
        }
    }
    
  2. Set up the ad loading parameters in AdRequestConfiguration.

    string adUnitId = "demo-interstitial-yandex"; // replace with "R-M-XXXXXX-Y"
    AdRequestConfiguration adRequestConfiguration = new AdRequestConfiguration.Builder(adUnitId).Build();
    

    AdUnitId: A unique identifier that is issued in the Partner interface and looks like this: R-M-XXXXXX-Y.

    Tip

    For testing purposes, you can use the demo unit ID: "demo-interstitial-yandex". Before publishing your ad, make sure to replace the demo unit ID with a real ad unit ID.

    You can expand the ad request parameters through AdRequestConfiguration.Builder by passing user interests, contextual app data, location details, or other data. Delivering additional contextual data in the request can significantly improve your ad quality.

  3. Load the ad with the LoadAd method, passing AdRequestConfiguration as an argument.

    interstitialAdLoader.LoadAd(adRequestConfiguration);
    
  4. Register listeners for events where users interact with your ad.

    using System;
    
    // ...
    interstitial.OnAdClicked += HandleAdClicked;
    interstitial.OnAdShown += HandleInterstitialShown;
    interstitial.OnAdFailedToShow += HandleInterstitialFailedToShow;
    interstitial.OnAdImpression += HandleImpression;
    interstitial.OnAdDismissed += HandleInterstitialDismissed;
    // ...
    
    public void HandleAdClicked(object sender, EventArgs args)
    {
        // Called when a click is recorded for an ad.
    }
    
    public void HandleInterstitialShown(object sender, EventArgs args)
    {
        // Called when ad is shown.
    }
    
    public void HandleInterstitialFailedToShow(object sender, AdFailureEventArgs args)
    {
        // Called when an InterstitialAd failed to show.
    }
    
    public void HandleInterstitialDismissed(object sender, EventArgs args)
    {
        // Called when ad is dismissed.
    }
    
    public void HandleImpression(object sender, ImpressionData impressionData)
    {
        // Called when an impression is recorded for an ad.
    }
    
  5. Show the ad by calling Show() on the Interstitial object.

    if (interstitial != null)
    {
        interstitial.Show();
    }
    
  6. Call Destroy() on ads that are shown and clear links that are no longer used on the current screen.

    That releases the resources and prevents memory leaks.

    public void DestroyInterstitial()
    {
        if (interstitial != null)
        {
            interstitial.Destroy();
            interstitial = null;
        }
    }
    

Testing interstitial ad integration

Using demo ad units for ad testing

We recommend using test ads to test your interstitial ad integration and your app itself.

For guaranteed return of test ads for every ad request, we have created a special demo ad placement ID. Use it to check your ad integration.

Demo adUnitId: demo-interstitial-yandex.

Warning

Make sure that before placing the application in the store, you replaced the demo ad placement ID with a real one, obtained in Partner Interface.

You can find the list of available demo ad placement IDs in the Demo ad units for testing section.

Testing ad integration

You can check your interstitial ad integration using the SDK's built-in analyzer.

The tool makes sure your interstitial ads are integrated properly and outputs a detailed report to the log. To view the report, search for the "YandexAds" keyword in the Logcat tool used for Android application debugging.

adb logcat -v brief '*:S YandexAds'

If the integration is successful, you will see the following message:

adb logcat -v brief '*:S YandexAds'
mobileads$ adb logcat -v brief '*:S YandexAds'
I/YandexAds(13719): [Integration] Ad type interstitital was integrated successfully

If you're having problems integrating interstitial ads, you'll get a detailed report on the issues and recommendations for how to fix them.

Using demo ad units for ad testing

We recommend using test ads to test your ad integration and your app itself.

To guarantee that test ads are returned for every ad request, we created a special demo ad placement ID. Use it to check your ad integration.

Demo adUnitId: demo-interstitial-yandex.

Warning

Make sure that before placing the application in the store, you replaced the demo ad placement ID with a real one, obtained in Partner Interface.

You can find the list of available demo ad placement IDs in the Demo ad units for testing section.

Testing ad integration

You can test your ad integration using the native Console tool.

To view detailed logs, call the YMAMobileAds class's enableLogging method.

YMAMobileAds.enableLogging()

To view SDK logs, go to the Console tool and set sybsystem = com.yandex.mobile.ads.sdk. You can also filter logs by category and error level.

If you're having problems integrating ads, you'll get a detailed report on the issues and recommendations for how to fix them.

Tips

Ad preloading

Loading an ad may take several seconds, depending on the number of ad networks connected in mobile mediation and the user's internet speed. We recommend preloading ads before displaying them.

You can call ad loading in the Awake method for gameObject. Make sure to preserve the object between scenes using the DontDestroyOnLoad() call. That ensures the ad that's loaded is not deleted with gameObject on scene change.

private void Awake()
{
    SetupLoader();
    RequestInterstitial();
    DontDestroyOnLoad(gameObject);
}

Along with that, you can bind loading of the next ad to the callback functions run when ad show completes or fails. Like this:

public void HandleInterstitialFailedToShow(object sender, EventArgs args)
{
    // Called when an InterstitialAd failed to show.

    // Clear resources after Ad dismissed.
    DestroyInterstitial();

    // Now you can preload the next interstitial ad.
    RequestInterstitial();
}

public void HandleInterstitialDismissed(object sender, EventArgs args)
{
    // Called when ad is dismissed.

    // Clear resources after Ad dismissed.
    DestroyInterstitial();

    // Now you can preload the next interstitial ad.
    RequestInterstitial();
}

If you cache ads on too many screens that are unlikely to be shown, your ad effectiveness could drop. For example, if users complete 2-3 game levels per session, you shouldn't cache ads for 6-7 screens. Your ad viewability could decrease otherwise, and the advertising system might deprioritize your app.

To make sure you're finding a good balance for your app, track the "Share of impressions" or "Share of visible impressions" metric in the partner interface. If it's under 20%, you should probably revise your caching algorithm. The higher the percentage of impressions, the better.

Full code example

using System;
using UnityEngine;
using UnityEngine.UI;
using YandexMobileAds;
using YandexMobileAds.Base;

public class YandexMobileAdsInterstitialDemoScript : MonoBehaviour
{
    private InterstitialAdLoader interstitialAdLoader;
    private Interstitial interstitial;
    private Button button;

    private void Awake()
    {
        SetupLoader();
        RequestInterstitial();
        DontDestroyOnLoad(gameObject);
        button.onClick.AddListener(ShowInterstitial);
    }

    private void SetupLoader()
    {
        interstitialAdLoader = new InterstitialAdLoader();
        interstitialAdLoader.OnAdLoaded += HandleInterstitialLoaded;
        interstitialAdLoader.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
    }

    private void RequestInterstitial()
    {
        string adUnitId = "demo-interstitial-yandex"; // replace with "R-M-XXXXXX-Y"
        AdRequestConfiguration adRequestConfiguration = new AdRequestConfiguration.Builder(adUnitId).Build();
        interstitialAdLoader.LoadAd(adRequestConfiguration);
    }

    private void ShowInterstitial()
    {
        if (interstitial != null)
        {
            interstitial.Show();
        }
    }

    public void HandleInterstitialLoaded(object sender, InterstitialAdLoadedEventArgs args)
    {
        // The ad was loaded successfully. Now you can handle it.
        interstitial = args.Interstitial;

        // Add events handlers for ad actions
        interstitial.OnAdClicked += HandleAdClicked;
        interstitial.OnAdShown += HandleInterstitialShown;
        interstitial.OnAdFailedToShow += HandleInterstitialFailedToShow;
        interstitial.OnAdImpression += HandleImpression;
        interstitial.OnAdDismissed += HandleInterstitialDismissed;
    }

    public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        // Ad {args.AdUnitId} failed for to load with {args.Message}
        // Attempting to load a new ad from the OnAdFailedToLoad event is strongly discouraged.
    }

    public void HandleInterstitialDismissed(object sender, EventArgs args)
    {
        // Called when ad is dismissed.

        // Clear resources after Ad dismissed.
        DestroyInterstitial();

        // Now you can preload the next interstitial ad.
        RequestInterstitial();
    }

    public void HandleInterstitialFailedToShow(object sender, EventArgs args)
    {
        // Called when an InterstitialAd failed to show.

        // Clear resources after Ad dismissed.
        DestroyInterstitial();

        // Now you can preload the next interstitial ad.
        RequestInterstitial();
    }

    public void HandleAdClicked(object sender, EventArgs args)
    {
        // Called when a click is recorded for an ad.
    }

    public void HandleInterstitialShown(object sender, EventArgs args)
    {
        // Called when ad is shown.
    }

    public void HandleImpression(object sender, ImpressionData impressionData)
    {
        // Called when an impression is recorded for an ad.
    }

    public void DestroyInterstitial()
    {
        if (interstitial != null)
        {
            interstitial.Destroy();
            interstitial = null;
        }
    }
}

Additional resources