App open ads

Note

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

Example of creating an app open ad

  1. Initialize the SDK when the application starts.

    YandexAds.initialize(this) {
        // Now you can use ads
    }
    
    YandexAds.initialize(this, () -> {
        // Now you can use ads
    });
    
  2. Create and configure the AppOpenAdLoader ad loader.

    You need the ad unit ID from the Yandex Advertising Network interface (AD_UNIT_ID).

    You can extend the ad request via AdRequest.Builder() by passing user interests, page context, location, or other data. Extra contextual data in the request can significantly improve ad quality.

    val appOpenAdLoader: AppOpenAdLoader = AppOpenAdLoader(application)
    val AD_UNIT_ID = "R-M-XXXXXX-Y" // for debugging you can use "demo-appopenad-yandex"
    val adRequest = AdRequest.Builder(AD_UNIT_ID).build()
    
    final AppOpenAdLoader appOpenAdLoader = new AppOpenAdLoader(application);
    final String AD_UNIT_ID = "R-M-XXXXXX-Y"; // for debugging you can use "demo-appopenad-yandex"
    final AdRequest adRequest = new AdRequest.Builder(AD_UNIT_ID).build();
    
  3. Set an AppOpenAdLoadListener on the loader to receive success or failure load notifications.

    val appOpenAdLoadListener = object : AppOpenAdLoadListener {
       override fun onAdLoaded(appOpenAd: AppOpenAd) {
           // The ad was loaded successfully. Now you can show loaded ad.
           this@Activity.appOpenAd = appOpenAd
       }
    
       override fun onAdFailedToLoad(adRequestError: AdRequestError) {
           // Ad failed to load with AdRequestError.
           // Attempting to load a new ad from the onAdFailedToLoad() method is strongly discouraged.
       }
    }
    
    AppOpenAdLoadListener appOpenAdLoadListener = new AppOpenAdLoadListener() {
        @Override
        public void onAdLoaded(@NonNull final AppOpenAd appOpenAd) {
            // The ad was loaded successfully. Now you can show loaded ad.
            mAppOpenAd = appOpenAd;
        }
    
        @Override
        public void onAdFailedToLoad(@NonNull final AdRequestError adRequestError) {
            // Ad failed to load with AdRequestError.
            // Attempting to load a new ad from the onAdFailedToLoad() method is strongly discouraged.
        }
    };
    
  4. Load the ad using loadAd(AdRequest, AppOpenAdLoadListener).

     private fun loadAppOpenAd() {
        appOpenAdLoader.loadAd(adRequest, appOpenAdLoadListener)
     }
    
     private void loadAppOpenAd() {
         appOpenAdLoader.loadAd(adRequest, appOpenAdLoadListener);
     }
    
  5. Use LifecycleEventObserver to handle application state changes and show the ad when the app is opened.

    val processLifecycleObserver = DefaultProcessLifecycleObserver(
        onProcessCameForeground = ::showAppOpenAd
    )
    ProcessLifecycleOwner.get().lifecycle.addObserver(processLifecycleObserver)
    
    final DefaultProcessLifecycleObserver processLifecycleObserver = new DefaultProcessLifecycleObserver() {
        @Override
        public void onProcessCameForeground() {
            showAppOpenAd();
        }
    };
    
    ProcessLifecycleOwner.get().getLifecycle().addObserver(processLifecycleObserver);
    
  6. Before showing the ad, set the ad event callback listener AppOpenAdEventListener.

    private inner class AdEventListener : AppOpenAdEventListener {
        override fun onAdShown() {
            // Called when ad is shown.
        }
    
        override fun onAdFailedToShow(adError: AdError) {
            // Called when ad failed to show.
        }
    
        override fun onAdDismissed() {
            // Called when ad is dismissed.
            // Clean resources after dismiss and preload new ad.
            clearAppOpenAd()
            loadAppOpenAd()
        }
    
        override fun onAdClicked() {
            // Called when a click is recorded for an ad.
        }
    
        override fun onAdImpression(impressionData: ImpressionData?) {
            // Called when an impression is recorded for an ad.
            // Get Impression Level Revenue Data in argument.
        }
    }
    
    private val appOpenAdEventListener = AdEventListener()
    appOpenAd?.setAdEventListener(appOpenAdEventListener)
    
    AppOpenAdEventListener appOpenAdEventListener = new AppOpenAdEventListener() {
       @Override
       public void onAdShown() {
           // Called when ad is shown.
       }
    
       @Override
       public void onAdFailedToShow(@NonNull final AdError adError) {
           // Called when ad failed to show.
       }
    
       @Override
       public void onAdDismissed() {
           // Called when ad is dismissed.
           // Clean resources after dismiss and preload new ad.
           clearAppOpenAd();
           loadAppOpenAd();
       }
    
       @Override
       public void onAdClicked() {
           // Called when a click is recorded for an ad.
       }
    
       @Override
       public void onAdImpression(@Nullable final ImpressionData impressionData) {
           // Called when an impression is recorded for an ad.
       }
    };
    
    if (mAppOpenAd != null) {
       mAppOpenAd.setAdEventListener(appOpenAdEventListener);
    }
    
  7. Show the ad using the show method.

    private fun showAppOpenAd() {
        appOpenAd?.show(activity)
    }
    
    private void showAppOpenAd() {
        if (mAppOpenAd != null) {
           mAppOpenAd.show(activity);
        }
    }
    
  8. Release resources.

    This is required to prevent memory leaks.

    private fun clearAppOpenAd() {
        appOpenAd?.setAdEventListener(null)
        appOpenAd = null
    }
    
    private void clearAppOpenAd() {
        if (mAppOpenAd != null) {
            mAppOpenAd.setAdEventListener(null);
            mAppOpenAd = null;
        }
    }
    

Checking integration

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

[Integration] Ad type App Open was integrated successfully