<br />

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](https://developers.google.com/ad-manager/mobile-ads-sdk/android/rewarded-video).

## Fetching ad metadata

To know more about served ads, listen for ad metadata changes on rewarded video
ads.

`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:

### Kotlin

```kotlin
RewardedAd.load(
AdRequest.Builder("AD_UNIT_ID").build(),
object : AdLoadCallback<RewardedAd> {
  override fun onAdLoaded(ad: RewardedAd) {
    ...
    ad.adEventCallback = object : RewardedAdEventCallback {
      override fun onAdMetadataChanged() {
        val metadata = ad.adMetadata
        val adId = metadata.getString("AdId")
      }
    }
  }
})
```

### Java

```java
RewardedAd.load(
  new AdRequest.Builder("AD_UNIT_ID").build(),
  new AdLoadCallback<RewardedAd>() {
    @Override
    public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
      Rewarded.this.rewardedAd = rewardedAd;
        ...
        rewardedAd.setAdEventCallback(new RewardedAdEventCallback() {
            @Override
            public void onAdMetadataChanged() {
                Bundle metadata = rewardedAd.getAdMetadata();
                String adId = metadata.getString("AdId");
            }
        });
      };
    };
  );
```

Replace `AD_UNIT_ID` ad unit. For example,
`/21775744923/example/rewarded` for rewarded ads.

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. |

> [!NOTE]
> **Note:** Only VAST Wrapper ads will have `Wrappers` key in the ad metadata. VAST Inline ads do not contain `Wrappers` metadata.