How to pass predefined custom targeting values in ad request (Only for GAM)

Below are the steps to pass predefined custom targeting value in ad request for GAM (Supported by all ad formats)

1

Create AdRequestConfiguration

Java

Java
AdRequestConfiguration.Builder configuration = AdRequestConfiguration.Companion.builder(context, "Your_placement_name");

Kotlin

Kotlin
val configuration = AdRequestConfiguration.builder(context, "Your_placement_name")
2

Add predefined ad targeting parameters to the AdRequest

chevron-rightSingle key with single valuehashtag
configuration.addCustomTargetingValue("YOUR_KEY","YOUR_VALUE");
chevron-rightSingle key with multiple valueshashtag
configuration.addCustomTargetingValue("YOUR_KEY",List<String>);
chevron-rightMultiple keys with single valuehashtag
configuration.addCustomTargetingValue("YOUR_KEY","YOUR_VALUE")
             .addCustomTargetingValue("YOUR_KEY","YOUR_VALUE");
chevron-rightMultiple keys with multiple valueshashtag
configuration.addCustomTargetingValue("YOUR_KEY",List<String>)
             .addCustomTargetingValue("YOUR_KEY",List<String>);
3
circle-info

Custom ad request can be created for all the ad types.

Here is an example of implementation with banner ad.

4

Call loadAd() method as per below format

AdSterAdLoader.Companion.builder().withAdsListener(new MediationAdListener() {
    @Override
    public void onBannerAdLoaded(@NonNull MediationBannerAd ad) {
        super.onBannerAdLoaded(ad);
        // Show banner ad here
    }

    @Override
    public void onFailure(@NonNull AdError adError) {
        // Handle failure here
    }
}).withAdsEventsListener(new AdEventsListener() {
    @Override
    public void onAdClicked() {
        //Handle ad click here
    }

    @Override
    public void onAdImpression() {
        //Handle ad impression here
    }
    
    @Override
    public void onAdRevenuePaid(double revenue, @NotNull String adUnitId, @NotNull String network) {
        // Callback which provides revenue and the network which provided it
    }
}).build().loadAd(configuration.build());

5

Inside the onBannerAdLoaded callback method invoke getView() method of MediationnBannerAd object to add an AdSter banner view to the given layout as shown below

container.removeAllViews();
container.addView(ad.getView());
6

Cleanup

Call MediationBannerAd.destroy() When activity/fragment is destroyed or detached.

Last updated