To discuss and provide feedback on our products, join the official AdMob Discord channel in the Google Advertising and Measurement Community server.

Rewarded ads custom events

  • To request a rewarded ad through a custom event, implement the GADMediationAdapter and loadRewarded:adConfiguration:completionHandler: methods.

  • Create a separate class that implements GADMediationRewardedAd to handle ad loading, the GADMediationRewardedAd protocol, and ad event callbacks to the Google Mobile Ads SDK.

  • Use the GADMediationRewardedLoadCompletionHandler to notify the Google Mobile Ads SDK about ad load success or failure.

  • Forward ad events from the third-party ad network SDK to the Google Mobile Ads SDK using the GADMediationRewardedAdEventDelegate object.

  • The provided code snippets and the full example on GitHub can be used to create or modify custom events for rewarded ads.

Select platform: Android iOS

Prerequisites

Complete the custom events setup.

Request a rewarded ad

When the custom event line item is reached in the waterfall mediation chain, the loadRewarded:adConfiguration:completionHandler: method is called on the class name you provided when creating a custom event. In this case, that method is in SampleCustomEvent, which then calls the loadRewarded:adConfiguration:completionHandler: method in SampleCustomEventRewarded.

To request a rewarded ad, create or modify a class that implements GADMediationAdapter and loadRewarded:adConfiguration:completionHandler:. If a class that extends GADMediationAdapter already exists, implement loadRewarded:adConfiguration:completionHandler: there. Additionally, create a new class to implement GADMediationRewardedAd.

In our custom event example, SampleCustomEvent implements the GADMediationAdapter interface and then delegates to SampleCustomEventRewarded.

Swift

importGoogleMobileAds
classSampleCustomEvent:NSObject,MediationAdapter{
fileprivatevarrewardedAd:SampleCustomEventRewarded?
...
funcloadRewarded(
foradConfiguration:MediationRewardedAdConfiguration,
completionHandler:@escapingGADMediationRewardedLoadCompletionHandler
){
self.rewardedAd=SampleCustomEventRewarded()
self.rewardedAd?.loadRewarded(
for:adConfiguration,completionHandler:completionHandler)
}
}

Objective-C

#import "SampleCustomEvent.h"
@implementation SampleCustomEvent
...
SampleCustomEventRewarded*sampleRewarded;
- (void)loadRewardedForAdConfiguration:
(GADMediationRewardedAdConfiguration*)adConfiguration
completionHandler:
(GADMediationRewardedLoadCompletionHandler)
 completionHandler{
sampleRewarded=[[SampleCustomEventRewardedalloc]init];
[sampleRewardedloadRewardedForAdConfiguration:adConfiguration
completionHandler:completionHandler];
}

SampleCustomEventRewarded is responsible for the following tasks:

  • Loading the rewarded ad.

  • Implementing the GADMediationRewardedAd protocol.

  • Receiving and reporting ad event callbacks to Google Mobile Ads SDK.

The optional parameter defined in the AdMob UI is included in the ad configuration. The parameter can be accessed through adConfiguration.credentials.settings[@"parameter"]. This parameter is typically an ad unit identifier that an ad network SDK requires when instantiating an ad object.

Swift

classSampleCustomEventRewarded:NSObject,MediationRewardedAd{
/// The Sample Ad Network rewarded ad.
varnativeAd:SampleRewarded?
/// The ad event delegate to forward ad rendering events to Google Mobile Ads SDK.
vardelegate:MediationRewardedAdEventDelegate?
/// Completion handler called after ad load.
varcompletionHandler:GADMediationRewardedLoadCompletionHandler?
funcloadRewarded(
foradConfiguration:MediationRewardedAdConfiguration,
completionHandler:@escapingGADMediationRewardedLoadCompletionHandler
){
rewarded=SampleRewarded.init(
adUnitID:adConfiguration.credentials.settings["parameter"]as?String)
rewarded?.delegate=self
letadRequest=SampleAdRequest()
adRequest.testMode=adConfiguration.isTestRequest
self.completionHandler=completionHandler
rewarded?.fetchAd(adRequest)
}
}

Objective-C

#import "SampleCustomEventRewarded.h"
@interface SampleCustomEventRewarded()<SampleRewardedAdDelegate,
GADMediationRewardedAd>{
/// The sample rewarded ad.
SampleRewarded*_rewardedAd;
/// The completion handler to call when the ad loading succeeds or fails.
GADMediationRewardedLoadCompletionHandler_loadCompletionHandler;
/// The ad event delegate to forward ad rendering events to Google Mobile Ads SDK.
id<GADMediationRewardedAdEventDelegate>_adEventDelegate;
}
@end
-(void)loadRewardedAdForAdConfiguration:(GADMediationRewardedAdConfiguration*)adConfiguration
completionHandler:
(GADMediationRewardedLoadCompletionHandler)completionHandler{
__blockatomic_flagcompletionHandlerCalled=ATOMIC_FLAG_INIT;
__blockGADMediationRewardedLoadCompletionHandleroriginalCompletionHandler=
[completionHandlercopy];
_loadCompletionHandler=^id<GADMediationRewardedAdEventDelegate>(
_Nullableid<GADMediationRewardedAd>ad,NSError*_Nullableerror){
// Only allow completion handler to be called once.
if(atomic_flag_test_and_set(&completionHandlerCalled)){
returnnil;
}
id<GADMediationRewardedAdEventDelegate>delegate=nil;
if(originalCompletionHandler){
// Call original handler and hold on to its return value.
delegate=originalCompletionHandler(ad,error);
}
// Release reference to handler. Objects retained by the handler will also be released.
originalCompletionHandler=nil;
returndelegate;
};
NSString*adUnit=adConfiguration.credentials.settings[@"parameter"];
_rewardedAd=[[SampleRewardedAdalloc]initWithAdUnitID:adUnit];
_rewardedAd.delegate=self;
SampleAdRequest*adRequest=[[SampleAdRequestalloc]init];
adRequest.testMode=adConfiguration.isTestRequest;
[_rewardedAdfetchAd:adRequest];
}

Whether the ad is successfully fetched or encounters an error, you would call GADMediationRewardedLoadCompletionHandler. In the event of success, pass through the class that implements GADMediationRewardedAd with a nil value for the error parameter; in the event of failure, pass through the error you encountered.

Typically, these methods are implemented inside callbacks from the third-party SDK your adapter implements. For this example, the Sample SDK has a SampleRewardedAdDelegate with relevant callbacks:

Swift

funcrewardedDidLoad(_interstitial:SampleRewarded){
iflethandler=completionHandler{
delegate=handler(self,nil)
}
}
funcrewarded(
rewarded:SampleRewarded,didFailToLoadAdWitherrorCode:SampleErrorCode
){
leterror=
SampleCustomEventUtils.SampleCustomEventErrorWithCodeAndDescription(
code:SampleCustomEventErrorCode
.SampleCustomEventErrorAdLoadFailureCallback,
description:
"Sample SDK returned an ad load failure callback with error code: \(errorCode)"
)
iflethandler=completionHandler{
delegate=handler(nil,error)
}
}

Objective-C

- (void)rewardedDidLoad:(SampleRewarded*)rewarded{
_adEventDelegate=_loadCompletionHandler(self,nil);
}
- (void)rewarded:(SampleInterstitial*)rewarded
didFailToLoadAdWithErrorCode:(SampleErrorCode)errorCode{
NSError*error=SampleCustomEventErrorWithCodeAndDescription(
SampleCustomEventErrorAdLoadFailureCallback,
[NSStringstringWithFormat:@"Sample SDK returned an ad load failure "
@"callback with error code: %@",
errorCode]);
_adEventDelegate=_loadCompletionHandler(nil,error);
}

GADMediationrewardedAd requires implementing a present(viewController:) method to display the ad:

Swift

funcpresent(fromviewController:UIViewController){
ifletrewarded=rewarded,rewarded.isRewardedLoaded{
rewarded.show()
}
}

Objective-C

- (void)presentFromViewController:(UIViewController*)viewController{
if([_rewardedAdisRewardedLoaded]){
[_rewardedAdshow];
}else{
NSError*error=SampleCustomEventErrorWithCodeAndDescription(
SampleCustomEventErrorAdNotLoaded,
[NSStringstringWithFormat:
@"The rewarded ad failed to present because the ad was not loaded."]);
[_adEventDelegatedidFailToPresentWithError:error]
}
}

Forward mediation events to Google Mobile Ads SDK

Once you've called GADMediationRewardedLoadCompletionHandler with a loaded ad, the returned GADMediationRewardedAdEventDelegate delegate object can then be used by the adapter to forward presentation events from the third-party SDK to Google Mobile Ads SDK. The SampleCustomEventRewarded class implements the SampleRewardedAdDelegate protocol to forward callbacks from the sample ad network to Google Mobile Ads SDK.

It's important that your custom event forwards as many of these callbacks as possible, so that your app receives these equivalent events from Google Mobile Ads SDK. Here's an example of using callbacks:

Swift

funcrewardedAdDidPresent(_rewarded:SampleRewardedAd){
delegate?.willPresentFullScreenVideo()
delegate?.didStartVideo()
}
funcrewardedAdUserDidEarnReward(_rewarded:SampleRewardedAd){
AdRewardaReward=AdReward("",rewarded)
delegate.didRewardUser()
}

Objective-C

- (void)rewardedAdDidPresent:(SampleRewardedAd*)rewardedAd{
[_adEventDelegatewillPresentFullScreenView];
[_adEventDelegatedidStartVideo];
}
- (void)rewardedAd:(nonnullSampleRewardedAd*)rewardedAd
userDidEarnReward:(NSUInteger)reward{
GADAdReward*aReward=[[GADAdRewardalloc]
initWithRewardType:@""
rewardAmount:[NSDecimalNumbernumberWithUnsignedInt:reward]];
[_adEventDelegatedidRewardUserWithReward];
}

This completes the custom events implementation for rewarded ads. The full example is available on GitHub. You can use it with an ad network that is already supported or modify it to display custom event rewarded ads.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月15日 UTC.