Ads sometimes fail to load due to not having a request ready. (Unity game)

106 views
Skip to first unread message

Umut Selbaş

unread,
Dec 2, 2022, 6:59:57 AM12/2/22
to Google Mobile Ads SDK Developers
Hello there, ads on my unity game sometimes work as intended however at other times requests fails so I decided to optimize the requests. here is how it should work:

send ad request, if fails wait for 3 seconds and send another one, if not when the time comes show the ad.

Here is the short version of the code:

private void Start()
    {
        MobileAds.Initialize(InitializationStatus => { });
        RequestInterstitial();
    }

public void Update()
    {
            if (interstitial.IsLoaded())
            {
                interstitial.Show();
            }
    }

    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MobileAds.Initialize(InitializationStatus => { });

        RequestInterstitial();

        StartCoroutine(ExampleCoroutine());

        Debug.Log("ad failed and rerequested");
    }

    IEnumerator ExampleCoroutine()
    {
        yield return new WaitForSeconds(3);
    }

    public void HandleOnAdLoaded(object sender, EventArgs args)
    {
        Debug.Log("ad requested");
    }

    private void RequestInterstitial()
    {
#if UNITY_ANDROID
        string adUnitId = "test";
#elif UNITY_IPHONE
        string adUnitId = "test";
#else
        string adUnitId = "unexpected_platform";
#endif

        // Initialize an InterstitialAd.
        this.interstitial = new InterstitialAd(adUnitId);

        this.interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;

        this.interstitial.OnAdLoaded += HandleOnAdLoaded;

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the interstitial with the request.
        this.interstitial.LoadAd(request);
    }

Mobile Ads SDK Forum Advisor

unread,
Dec 2, 2022, 1:34:39 PM12/2/22
to oyuna...@gmail.com, google-adm...@googlegroups.com
Hi Umut,

As per checking on your post, I could see that you've shared an approach on how to handle when an ad request fails due to lack of ad inventory. In addition to this, we always ask our publishers to set a threshold on the number of an ad request to a given time. For example, publisher may request an ad for up to 3 times if the ad-server didn't return any ad, and if it reaches this threshold, we recommend to wait at least 5 to 10 minutes before requesting for another ad again.

Regards,
Google Logo
Teejay Wennie
Mobile Ads SDK Team
 


ref:_00D1U1174p._5004Q2ghqsN:ref

Umut Selbaş

unread,
Dec 3, 2022, 9:33:11 AM12/3/22
to Google Mobile Ads SDK Developers
Thank you for your answer, 3 times is fine. How can I do it? My code attempts to send ads constantly with no delay. What I want to do is to send 3 requests with 3 seconds of delay each.

Mobile Ads SDK Forum Advisor

unread,
Dec 4, 2022, 10:54:07 PM12/4/22
to oyuna...@gmail.com, google-adm...@googlegroups.com

Hi Umut,

 

Thank you for your response.

 

As per our documentation, the code snippet you've provided should be working as what you've intended to work with the help also of coroutine as it was requesting only after the ad failed to load. The one of the best practices, is that the loading the ad in advance by calling loadAd() before you intend to call show() can ensure that your app has a fully loaded interstitial ad at the ready when the time comes to display one.

 

In addition to that, kindly note that if your app is automatically refreshing ads, make sure ad requests are not made when the screen is off. Also, if users navigate to and from pages with ads in an app over a short period of time, a new ad request should not be made sooner than the recommended 60 second rate. You may also check this recommended interstitial implementations for more information.

 

Regards,

Google Logo
Princess Pamela
Mobile Ads SDK Team
 


ref:_00D1U1174p._5004Q2ghqsN:ref
Reply all
Reply to author
Forward
0 new messages