See About AdMob Mediation for a walk-through of what mediation is and how to use the AdMob Mediation UI. Then, continue reading these instructions for adding mediation code to your app.
Add mediation to your project
1. Integrate AdMob ads
Follow the same instructions used for integrating AdMob ads into your app. To integrate non-interstitial ads (banner size, leaderboard size, and so on), see Banner Ads. To integrate interstitial ads (full-screen ads that mask all other content), see Interstitial Ads.
Once you've completed these integration steps, you'll make a few small modifications described below to change your AdMob ad placement into a mediation placement that can show ads from multiple networks.
2. Add network adapters and SDKs
You're now ready to download and add to your project the adapters and SDKs of all the ad networks you'd like to serve ads from. To find links to them, see Mediation Networks.
To add the downloaded network adapters/SDKs in Xcode, right-click on your project and click Add Files to project.
3. Include network configurations
You need to include any frameworks, compiler flags, or linker flags that your chosen networks require. For your convenience, the mediation networks page has links to each network's instructions.
There is no need to write additional code to create ad views from each ad network. Mediation invokes each ad network's adapter and SDK as necessary to create ads. Below, you can see how to set up mediation listeners that notify you of ad events for all of your networks.
That's it!
Optional setup
Specify additional request parameters
Demographic targeting
You can optionally add the user's location, gender, and birthday to your requests.
These are not required
but can be used by networks to serve more finely targeted ads.
GADRequest
provides methods for setting birthday and location,
and a property for setting gender.
These parameters are forwarded to all networks that you mediate.
Here is an example:
Objective-C
GADRequest *request = [GADRequest request];
request.gender = kGADGenderFemale;
NSDateComponents *components = [[NSDateComponents alloc] init];
components.month = 1;
components.day = 1;
components.year = 1985;
request.birthday = [[NSCalendar currentCalendar] dateFromComponents:components];
Swift
let request = GADRequest()
request.gender = .Female
let components = NSDateComponents()
components.month = 1
components.day = 1
components.year = 1985
request.birthday = NSCalendar.currentCalendar().dateFromComponents(components)
Network-specific parameters
Some networks also support other request parameters that are specific to their network.
These are not required
but can be used by networks to serve more finely targeted ads.
Ad networks with extra parameters expose a class in their adapter
that extends GADExtras
.
Let's say there is a sample ad network that exposes an income parameter. That ad network may provide a class that looks like this:
Objective-C
@interface SampleExtras : GADExtras
@property(nonatomic, assign) NSInteger income;
@end
Swift
class SampleExtras: GADExtras {
var income = 0
...
}
If you're mediating this network, you can provide the income to that particular
network by passing an instance of their extras class to
registerAdNetworkExtras:
:
Objective-C
SampleExtras *sampleExtras = [[SampleExtras alloc] init];
sampleExtras.income = 65000;
[request registerAdNetworkExtras:sampleExtras];
Swift
let sampleExtras = SampleExtras()
sampleExtras.income = 65000
request.registerAdNetworkExtras(sampleExtras)
Request agent (for third-party libraries)
Third-party libraries that reference the Mobile Ads SDK
should call the setRequestAgent()
method
to denote the platform from which the ad request originated.
For example,
if a third-party ad network called "Cool Ad Network"
mediates requests to the Mobile Ads SDK
and wants to tag its requests for Analytics purposes,
it can do so like this:
Objective-C
GADRequest *request = [GADRequest request];
request.requestAgent = @"Cool Ad Network"
Swift
let request = GADRequest()
request.requestAgent = "Cool Ad Network"
Set up event notification
To be notified of ad lifecycle events like impressions, you can implement
a GADBannerViewDelegate
. When using AdMob Mediation, this delegate is
notified of events from all the networks you're mediating. For example,
impressions from any ad network are reported through the
GADBannerViewDelegate
method adViewDidReceiveAd:()
.
See the Ad Events guide for instructions on how to register for ad events.
Check the value of adNetworkClassName
You can optionally check the adNetworkClassName
property of GADBannerView
,
which returns the class name of the ad network that fetched the current banner.
This property can be checked once adViewDidReceiveAd
is called:
Objective-C
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView {
NSLog(@"Banner adapter class name: %@", bannerView.adNetworkClassName);
}
Swift
func adViewDidReceiveAd(bannerView: GADBannerView!) {
print("Banner adapter class name: \(bannerView.adNetworkClassName)")
}
Similarly, the adNetworkClassName
property of GADInterstitial
can be
checked once interstitialDidReceiveAd
is called:
Objective-C
- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {
NSLog(@"Interstitial adapter class name: %@", interstitial.adNetworkClassName);
}
Swift
func interstitialDidReceiveAd(ad: GADInterstitial!) {
print("Interstitial adapter class name: \(ad.adNetworkClassName)")
}
For ads returned from AdMob, adNetworkClassName
returns
GADMAdapterGoogleAdMobAds
. For ads fetched via
custom events, it returns
GADMAdapterCustomEvents
.
FAQ
- What if I want to mediate with a network, but there is no supported adapter for it?
- You can write your own mediation adapters using Custom Events.
- Does
GADBannerViewDelegate
still work with mediation? - Yes, it still works with all mediation networks.
- I want to add a new network to my mediation configuration for the next version of my app. How will that affect the current version of my app that doesn't include this network?
- The SDK detects when an adapter isn't there and fails gracefully; the request then goes to the next network in the mediation waterfall.
- What does the "<Google> Cannot find an ad network adapter with the name(s): (GADMSomeAdapter)" error message mean?
- Your project is likely missing the adapter library that contains this class. You need to add network adapters and SDKs for all ad networks you're mediating.