Setup

This guide is intended for publishers that are interested in using Google Mobile Ads mediation. We'll walk through getting a mediation adapter set up with your current Android app and setting up additional request parameters.

Prerequisites

Add mediation adapters to your project

Include network adapters and SDKs

Once you know which networks you want to serve ads from, you can download the SDK and adapter for those networks from the mediation networks page. Some of these SDKs already include a Google Mobile Ads adapter.

In Android Studio, include the JAR files in your project's libs folder. Make sure that your build.gradle file includes

compile fileTree(dir: 'libs', include: ['*.jar'])

Configure the AndroidManifest.xml file

Add entries to your AndroidManifest.xml file as required by each ad network you intend to use. Instructions from each network can be found from the mediation networks page. Follow instructions related to modifying your AndroidManifest.xml file.

Your app doesn't need to call any ad networks—the Google Mobile Ads SDK calls the third party ad network adapters to fetch third party ads on your behalf. If you don't wish to specify any additional request parameters, then you're done! Otherwise, read on to learn how to provide more information to the mediating ad networks.

Initialize your ad object with an Activity instance

In the constructor for a new ad object (for example, AdView), you must pass in an object of type Context. This Context is passed on to other ad networks when using mediation. Some ad networks require a more restrictive Context that is of type Activity, and may not be able to serve ads without an Activity instance. Therefore, we recommend passing in an Activity instance when initializing ad objects to ensure a consistent experience with your mediated ad networks.

Specify additional request parameters (Optional)

You can optionally add targeting information (such as location, birthday, gender, and COPPA preferences) that can be used by networks to serve more finely targeted ads.

Location targeting

Location targeting information may also be specified in the PublisherAdRequest. In this example, the location is set before loading the ad request:

Location location = new Location("");
// New York
location.setLatitude(40.764316);
location.setLongitude(-73.977245);
location.setAccuracy(100);
PublisherAdRequest adRequest = new PublisherAdRequest.Builder()
    .setLocation(location)
    .build();

Besides location, you can also target on birthday and gender, as well as set COPPA tags.

Demographic information

DFP also provides methods for specifying demographic information like gender and birthday. These are passed along to all networks that accept them. Here is an example:

PublisherAdRequest adRequest = new PublisherAdRequest.Builder()
    .setGender(PublisherAdRequest.GENDER_FEMALE)
    .setBirthday(new GregorianCalendar(1985, 1, 1).getTime())  // January 1, 1985
    .build();

Child-directed treatment in mediation

DFP Mediation facilitates compliance with the Children's Online Privacy Protection Act (COPPA).

Set tagForChildDirectedTreatment(true) in the Google Mobile Ads SDK to indicate whether you want your content treated as child-directed for the purposes of COPPA or not. Google makes this signal available to third-party ad networks in Mediation to facilitate COPPA compliance. See Tag an ad request for child-directed treatment (TFCD) for more information.

DFP simply serves as a platform. The advertising relationship is between the mobile app developer and the third-party ad network, so it's the developer's responsibility to ensure that each third-party ad network serves ads that treat the developer's content as child-directed for purposes of COPPA.

Network-specific parameters

Some networks also support other request parameters that are specific to their network. Refer to the "v1" or "v2" instructions below to learn how to provide these other parameters to the networks. If you are unsure if the adapter is v1 or v2, try calling addNetworkExtrasBundle with the adapter class (as detailed in v2 below). If the adapter compiles, then it's a v2 adapter; otherwise, it's a v1 adapter. Note that you can use both v1 and v2 adapters in the same app.

v1

You can pass extra parameters to specific mediation networks by using the addNetworkExtras() method of the PublisherAdRequest. The addNetworkExtras() method receives an instance of a class that implements NetworkExtras.

Each network defines its own extras class. The following table shows the names of these classes for some of the networks.

Ad Network Additional Parameters Class
Millennial Media com.google.ads.mediation.millennial.MillennialAdapterExtras
InMobi com.google.ads.mediation.inmobi.InMobiAdapterExtras

For example, both Millennial Media and InMobi allow specifying the income of the user to provide more relevant ads. In order to make the mediation framework pass an income when requesting an ad from these networks, you could use the following code:

import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdView;
import com.google.ads.mediation.inmobi.InMobiAdapterExtras;
import com.google.ads.mediation.millennial.MillennialAdapterExtras;

/* … */

    /* Set parameters common to all networks in ad request. */

    // Millennial Media extra parameters.
    MillennialAdapterExtras millennialExtras = new MillennialAdapterExtras();
    millennialExtras.setIncomeInUsDollars(65000);

    // InMobi extra parameters.
    InMobiAdapterExtras inMobiExtras = new InMobiAdapterExtras();
    inMobiExtras.setIncome(65000);

    // Create the ad request with these extra parameters.
    PublisherAdRequest adRequest = new PublisherAdRequest.Builder()
        .addNetworkExtras(millennialExtras)
        .addNetworkExtras(inMobiExtras)
        .build();

    // Finally, request the ad.
    adView.loadAd(adRequest);
v2

You can pass extra parameters to specific mediation networks by using the addNetworkExtrasBundle() method on PublisherAdRequest.Builder. Pass the adapter class to which you want to send parameters, and a Bundle of values for the adapter to consume.

In most cases, if an adapter supports extra parameters it has either constants to represent appropriate keys for the bundle or some method to help generate a valid bundle to pass to their network.

FAQ

Do Ad Listeners still work with mediation?
Yes, they still work with all mediation networks.
Do v1 mediation adapters work with Google Play services?
Yes. The Mobile Ads SDK included in Google Play services is designed to maintain backwards compatibility with our v1 mediation adapters.
Suppose I add a new network to my mediation configuration for the next version of my app. How does that affect the current version of my app that doesn't include this network?
The SDK detects that the adapter isn't there and fails gracefully; the request goes to the next network in the mediation waterfall.
What does the "Could not instantiate mediation adapter: x.y.z.SomeAdapter" error message mean?
Your project is likely missing the adapter library that contains this class. You need to include network adapters and SDKs for all ad networks you're mediating.

Send feedback about...

SDK for DFP Users on Android
Need help? Visit our support page.