Interstitial ads

A fullscreen ad format, interstitial ads are integrated in the app content at natural breaks, such as when the user transitions to the next game level or completes a conversion action.

Note

Examples showing how all the format types work are available in the demo project.

Entity

Description

HandleAdFailedToLoad

If HandleAdFailedToLoad() returns an error, don't attempt to load a new ad again using the same method.

adUnitId

Use:

  • Development mode to work with demo ad units.

  • Production mode to work with R-M-XXXXXX-Y (for the actual ID, check the Yandex Advertising Network interface). R-M-XXXXXX-Y is a template for your actual ad unit ID that will be used to receive various creatives.

Example of creating an interstitial ad

using UnityEngine;
using UnityEngine.Events;
using YandexMobileAds;
using YandexMobileAds.Base;
using System.Collections.Generic;
using System;

[AddComponentMenu("Yandex Ads/Interstitial Ad Component")]
public class InterstitialAdComponent : MonoBehaviour
{
    [Header("Ad Settings")]
    public string adName = "Default Interstitial Ad";

    [Header("Ad Unit IDs")]
    [Tooltip("Ad Unit ID for Android")]
    public string adUnitIdAndroid = "demo-interstitial-yandex";

    [Tooltip("Ad Unit ID for iOS")]
    public string adUnitIdiOS = "demo-interstitial-yandex";

    [Header("Configuration")]
    public bool autoLoading = true;
    public bool showAfterLoading = true;

    private Interstitial interstitial;
    private InterstitialAdLoader interstitialAdLoader;

    [System.Serializable]
    public class AdEvent : UnityEvent { }

    [System.Serializable]
    public class AdEventString : UnityEvent<string> { }

    [System.Serializable]
    public class AdEventWithAd : UnityEvent<Interstitial> { }

    [Header("Load Callbacks")]
    public AdEventWithAd OnAdLoaded;
    public AdEventString OnAdFailedToLoad;

    [Header("Interaction Callbacks")]
    public AdEventWithAd OnAdShown;
    public AdEventWithAd OnAdDismissed;
    public AdEventWithAd OnAdClicked;
    public AdEventString OnAdFailedToShow;
    public AdEventString OnImpression;

    private string CurrentAdUnitId
    {
        get
        {
#if UNITY_IOS
            return adUnitIdiOS;
#elif UNITY_ANDROID
            return adUnitIdAndroid;
#else
            Debug.LogWarning("Unsupported platform for Yandex Ads. Using Android Ad Unit ID by default.");
            return adUnitIdAndroid;
#endif
        }
    }

    private void Start()
    {
        if (!string.IsNullOrEmpty(CurrentAdUnitId))
        {
            SetupLoader();
            if (autoLoading)
            {
                ConfigureAd();
            }
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Debug.Log("InterstitialAd: Ad Unit ID is missing.");
        }
    }

    private void SetupLoader()
    {
        interstitialAdLoader = new InterstitialAdLoader();
    }

    private void ConfigureAd()
    {
        double lat = 60.0, lon = 30.0;

        Location location = new Location.Builder()
            .SetLatitude(lat)
            .SetLongitude(lon)
            .Build();

        AdTargeting targeting = new AdTargeting(
            age: "25",
            gender: Gender.MALE,
            contextTags: new List<string>() { "games", "unity", "test" },
            contextQuery: "user_search_query",
            location: location
        );

        AdRequest adRequest = new AdRequest(CurrentAdUnitId, targeting: targeting);
        interstitialAdLoader.LoadAd(
            adRequest: adRequest,
            onLoaded: ad =>
            {
                interstitial = ad;
                interstitial.OnAdShown += HandleAdShown;
                interstitial.OnAdDismissed += HandleAdDismissed;
                interstitial.OnAdClicked += HandleAdClicked;
                interstitial.OnAdFailedToShow += HandleAdFailedToShow;
                interstitial.OnAdImpression += HandleImpression;
                OnAdLoaded?.Invoke(interstitial);
                if (showAfterLoading)
                {
                    interstitial.Show();
                }
            },
            onFailed: args => OnAdFailedToLoad?.Invoke(args.Message));
    }

    public void Load()
    {
        ConfigureAd();
    }

    public void Show()
    {
        if (interstitial != null)
        {
            interstitial.Show();
        }
        else
        {
            Debug.Log("Failed to show ad. Ad object not loaded");
        }
    }

    public void OnDestroy()
    {
        if (interstitial != null)
        {
            interstitial.OnAdShown -= HandleAdShown;
            interstitial.OnAdDismissed -= HandleAdDismissed;
            interstitial.OnAdClicked -= HandleAdClicked;
            interstitial.OnAdFailedToShow -= HandleAdFailedToShow;
            interstitial.OnAdImpression -= HandleImpression;
            interstitial.Destroy();
            interstitial = null;
        }

        if (interstitialAdLoader != null)
        {
            interstitialAdLoader = null;
        }
    }

    #region Event Handlers

    private void HandleAdShown(object sender, EventArgs args) => OnAdShown?.Invoke(interstitial);

    private void HandleAdDismissed(object sender, EventArgs args) => OnAdDismissed?.Invoke(interstitial);

    private void HandleAdClicked(object sender, EventArgs args) => OnAdClicked?.Invoke(interstitial);

    private void HandleAdFailedToShow(object sender, AdFailureEventArgs args) => OnAdFailedToShow?.Invoke(args.Message);

    private void HandleImpression(object sender, ImpressionData impressionData) =>
        OnImpression?.Invoke(impressionData?.rawData ?? string.Empty);

    #endregion
}

Checking integration

Build and run your project. You can check if the integration is successful by searching the YandexAds keyword in Logcat in Android Studio:

[Integration] Ad type interstitial was integrated successfully
Previous
Next