Ad Metadata

This guide is intended for publishers integrating Ad Metadata with the Google Mobile Ads SDK for Android.

Prerequisites

  • Minimum version 17.0.0 of the Google Mobile Ads SDK.
  • Complete the steps in Rewarded Video.

Fetching ad metadata

Some apps may want to know more about ads that were served. You can listen for ad metadata changes on rewarded video ads by calling RewardedVideoAd.setAdMetadataListener() with an implementation of AdMetadataListener. Once AdMetadataListener.onAdMetadataChanged() is called, you can check RewardedVideo.getAdMetadata() for an ad's metadata.

onAdMetadataChanged() is called just after an ad loads or when its metadata changes asynchronously after it loads. It is not guaranteed that an ad's metadata will be available at the time the ad is loaded, so we recommend waiting for this callback before accessing an ad's metadata.

Here is a code example showing how to retrieve the ad metadata:

Java

RewardedAd.load(this, "/21775744923/example/rewarded", new AdManagerAdRequest.Builder().build(),
    new RewardedAdLoadCallback() {
      @Override
      public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
        mRewardedAd = rewardedAd;
        rewardedAd.setOnAdMetadataChangedListener(new OnAdMetadataChangedListener() {
          @Override
          public void onAdMetadataChanged() {
            Bundle metadata = rewardedAd.getAdMetadata();
            String adId = metadata.getString("AdId");
          }
        });
      }
    });

Kotlin

RewardedAd.load(this, "/21775744923/example/rewarded", AdManagerAdRequest.Builder().build(),
                    object : RewardedAdLoadCallback() {
      override fun onAdLoaded(rewardedAd: RewardedAd) {
        mRewardedAd = rewardedAd
        rewardedAd.onAdMetadataChangedListener = OnAdMetadataChangedListener {
          val metadata = rewardedAd.adMetadata
          val adId = metadata.getString("AdId")
        }
      }
    })

After retrieving the metadata, you can check the Bundle for the keys you care about. Different types of ads may have different ad metadata keys associated with them. VAST video ads have the following keys:

Key Type Description
AdId String The ID of the ad, empty if not available.
AdTitle String The title, empty if not specified.
CreativeDurationMs Integer The selected creative duration in milliseconds, -1 if non-linear.
TraffickingParameters String Trafficking parameters, empty if not available.
DealId String The first deal ID present in the wrapper chain for the current ad, starting from the top--empty if not available.
AdSystem String The source ad server of the ad, empty if not available.
CreativeId String The ID of the selected creative for the ad, empty if not available.
MediaURL String The URL of the selected media.
Wrappers Array The array is populated with elements beginning at the innermost wrapper ad (close to the inline ad) moving outwards to the outermost wrapper ad. Each element in the array is a dictionary that contains the following keys and values.
AdId
String. Ad ID used for wrapper ad, empty if not available.
AdSystem
String. Ad system used for wrapper ad, empty if not available.
CreativeId
String. Creative ID used for wrapper ad, empty if not available.