Through custom events, you can also monetize your application
with ad networks not directly supported by mediation.
A custom event is implemented through the GADCustomEventBanner
protocol.
Prerequisites
Before you can create custom events, you need to integrate the banner ad format into your app.
You may also want to first read about loading a banner ad and how mediation works.
Sample Ad Network
This guide demonstrates how to serve banners from the Sample Ad Network with the
SampleCustomEventBanner
custom event class.
The Sample Ad Network SDK is a mock SDK developed to illustrate what a real-life implementation of a custom event would look like. The SDK contains classes that are representative of the classes offered by most ad networks. See the complete sample SDK implementation for more information.
Banner custom event
In the following example, you first create a banner custom event within Ad Manager mediation. This requires that you define a custom event that points to that specific class in your application through the Ad Manager UI, then implement a banner custom event to return a view.
Define a custom event
Custom events must be defined in the Google Ad Manager UI. You can find instructions for setting up an Ad Manager yield group for mediation in this Help Center article.
Here's a screenshot showing some sample custom event settings:
The following table provides guidance on how to fill out these parameters.
Class Name | Enter the fully qualified name of the class that implements the custom event. If your class is implemented in Swift, you need to prefix the class name with the name of its app / framework module (for example, appName.className). Target name is required if you have multiple targets in your project or if the project name is different from the target name. With the target name, it would look like this: appName_targetName.className. In addition, remember to replace any non-alphanumeric characters such as dashes with underscores. For more details, see this example. |
---|---|
Label | Enter a unique name for the event. |
Parameter | If you wish to pass an argument to your custom event, enter the appropriate string. |
Request a banner
For custom event banner requests, the requestBannerAd:parameter:label:request:
method is called immediately after the custom event class is instantiated.
This method does not return anything. The assumption is that the custom event
will start an asynchronous ad fetch over the network. Your custom event should
act as a delegate to your SDK to listen to callbacks.
If your SDK does not support the given ad size or does not support banner ads,
call the customEventBanner:didFailAd:
method of the custom event delegate.
The serverParameter
and serverLabel
parameters correspond to the parameter
and label fields defined when creating a custom event in the
Ad Manager UI.
Here is an example implementation of requestBannerAd:parameter:label:request:
using the Sample Ad Network:
Swift
func requestBannerAd(adSize: GADAdSize, parameter serverParameter: String!,
label serverLabel: String!, request: GADCustomEventRequest!) {
// Create a banner view with the appropriate size.
bannerAd = SampleBanner(frame: CGRectMake(
0, 0, adSize.size.width, adSize.size.height))
bannerAd.delegate = self
bannerAd.adUnit = serverParameter
let adRequest = SampleAdRequest()
adRequest.testMode = request.isTesting
adRequest.keywords = request.userKeywords
bannerAd.fetchAd(adRequest)
}
Objective-C
- (void)requestBannerAd:(GADAdSize)adSize
parameter:(NSString *)serverParameter
label:(NSString *)serverLabel
request:(GADCustomEventRequest *)request {
// Create the bannerView with the appropriate size.
self.bannerAd =
[[SampleBanner alloc] initWithFrame:CGRectMake(0,
0,
adSize.size.width,
adSize.size.height)];
self.bannerAd.delegate = self;
self.bannerAd.adUnit = serverParameter;
SampleAdRequest *adRequest = [[SampleAdRequest alloc] init];
adRequest.testMode = request.isTesting;
adRequest.keywords = request.userKeywords;
[self.bannerAd fetchAd:adRequest];
}
Send ad network extras for custom event requests
In order to send ad network extras with the request for your custom event to
handle, you use the
GADRequest registerAdNetworkExtras:
function. You must create an instance of
GADCustomEventExtras
(which conforms to the
GADAdNetworkExtras
protocol) in order for a GADCustomEventRequest.additionalParameters
property to be populated. To pass in your extras, call
GADCustomEventExtras setExtras:forLabel:
,
passing in your extras as a dictionary and the label of your custom event that
you defined in the Ad Manager UI.
Here is a code snippet which shows how to pass a SampleExtra
parameter for our
SampleCustomEvent
label defined earlier:
Swift
let request = GAMRequest()
let extras = GADCustomEventExtras()
extras.setExtras(["SampleExtra": true], forLabel: "SampleCustomEvent")
request.register(extras)
Objective-C
GAMRequest *request = [GAMRequest request];
GADCustomEventExtras *extras = [[GADCustomEventExtras alloc] init];
[extras setExtras:@{@"SampleExtra": @(YES)} forLabel:@"SampleCustomEvent"];
[request registerAdNetworkExtras:extras];
If you don't register an instance of GADCustomEventExtras
for a custom event
request, the additionalParameters
property of the GADCustomEventRequest
will
be nil
.
Notify Ad Manager mediation
Implement the ad listener for your network and invoke the relevant callbacks
on the custom event's delegate to send messages back to mediation. The following
example implements the Sample Ad Network's SampleBannerAdDelegate
interface
to send these messages:
Swift
/// Type property for Sample Ad Network custom event error domain.
static let customEventErrorDomain = "com.google.CustomEvent"
// Sent when banner ad has loaded.
func bannerDidLoad(banner: SampleBanner!) {
delegate.customEventBanner(self, didReceiveAd: banner)
}
// Sent when banner has failed to load.
func banner(banner: SampleBanner!, didFailToLoadAdWithErrorCode error: SampleErrorCode) {
let nsError = NSError(domain: SampleCustomEventBanner.customEventErrorDomain,
code: error.rawValue, userInfo: nil)
delegate.customEventBanner(self, didFailAd: nsError)
}
// Sent when a banner is clicked and an external application is launched
func bannerWillLeaveApplication(banner: SampleBanner!) {
delegate.customEventBannerWasClicked(self)
delegate.customEventBannerWillLeaveApplication(self)
}
Objective-C
/// Constant for Sample Ad Network custom event error domain.
static NSString *const customEventErrorDomain = @"com.google.CustomEvent";
// Sent when banner ad has loaded.
- (void)bannerDidLoad:(SampleBanner *)banner {
[self.delegate customEventBanner:self didReceiveAd:banner];
}
// Sent when banner has failed to load.
- (void)banner:(SampleBanner *)banner
didFailToLoadAdWithErrorCode:(SampleErrorCode)errorCode {
NSError *error = [NSError errorWithDomain:customEventErrorDomain
code:errorCode
userInfo:nil];
[self.delegate customEventBanner:self didFailAd:error];
}
// Sent when a banner is clicked and an external application is launched.
- (void)bannerWillLeaveApplication:(SampleBanner *)banner {
[self.delegate customEventBannerWasClicked:self];
[self.delegate customEventBannerWillLeaveApplication:self];
}
To import Objective-C files into your Swift project, you must create an Objective-C bridging header.
Mediation callbacks
Ad Manager mediation supports the following callbacks:
Method | When to call |
---|---|
customEventBanner:didReceiveAd: |
The banner request succeeded. |
customEventBanner:didFailAd: |
The banner request failed. |
customEventBannerWillPresentModal: |
The banner will present a full screen modal view. |
customEventBannerWillDismissModal: |
The banner will dismiss a full screen modal view. |
customEventBannerDidDismissModal: |
The banner did dismiss a full screen modal view. |
customEventBannerWillLeaveApplication: |
The banner caused the user to leave the app. |
customEventBannerWasClicked: |
The banner was clicked. |