Monday, March 18, 2013

Customizing Your AdMob Mediation Ad Requests

Ad networks take into account a variety of signals when targeting ads to your users. Generally speaking, the more information you provide to an ad network, the more accurately that network can target its ads, and the better those ads perform.

Many parameters, such as age, gender, and location, are commonly used by most ad networks. AdMob Mediation supports passing those parameters directly in the AdRequest; these parameters will be passed to the networks you’re mediating:

AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adRequest.setBirthday(new Date(2000, 1, 1));
adRequest.setGender(AdRequest.Gender.MALE);
adRequest.setLocation(location);

AdMob Mediation also supports passing specialized parameters to specific networks. Any custom parameters used by a specific ad network can be passed to an instance of that network adapter’s NetworkExtras object, which is then set on the AdRequest. Here is how you can customize the background and text colors for AdMob text ads, and set education level and number of children for a hypothetical Example ad network:

AdMobAdapterExtras adMobExtras = new AdMobAdapterExtras();
adMobExtras.addExtra("color_bg", "00FFFF");
adMobExtras.addExtra("color_text", "FF0000");
adRequest.setNetworkExtras(adMobExtras);

ExampleAdapterExtras exampleExtras = new ExampleAdapterExtras();
exampleExtras.setEducation(Education.BACHELORS);
exampleExtras.setNumberOfChildren(2);
adRequest.setNetworkExtras(exampleExtras);

AdMob Mediation will pass an adapter only the NetworkExtras object specific to that network. So in this case, the AdMob adapter will be provided with the AdMobAdapterExtras object, and the Example adapter will be provided with the ExampleAdapterExtras object. You can find the class name for each ad network’s NetworkExtras object in their respective adapter jar file.

Custom Events

You can also use CustomEventExtras to pass special parameters to any custom events that your app implements. Keep in mind that you can call AdRequest.setNetworkExtras() with only one instance of CustomEventExtras for all custom events that you implement. To make sure your custom event doesn’t access parameters meant for other custom events, we recommend you create a HashMap for each custom event, and pass in any necessary key-value pairs related to that custom event in that map.

CustomEventExtras customEventExtras = new CustomEventExtras();
HashMap customExtras1 = new HashMap();
customExtras1.put("key1", "value1");
customExtras1.put("key2", "value2");
customEventExtras.addExtra("customEvent1", customExtras1);
HashMap customExtras2 = new HashMap();
customExtras1.put("key1", "othervalue1");
customExtras1.put("key2", "othervalue2");
customEventExtras.addExtra("customEvent2", customExtras2);

Your custom event implementation just needs to check CustomEventExtras for the HashMap at whatever key that was designated for it - in this case customEvent1. You’ll use these parameters to construct your custom event.

HashMap extras =
    (HashMap) customEventExtras.getExtra("customEvent1");

Load the Ad

Once you’re done setting all targeting options, make sure to call loadAd with that request.

// This snippet assumes you have an AdView object named "adView".
adView.loadAd(adRequest);

If you have any questions or comments about AdMob, mediation, custom events, or targeting, we can field them in the forum. Also follow us on our Google+ page for ads-related updates.