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 with custom events, extend the Adapter class and implement the loadRewardedAd() method, delegating to a loader class.

  • The loader class handles loading the ad, implements MediationRewardedAd, and manages ad event callbacks to the Google Mobile Ads SDK.

  • Implement the showAd() method in the loader class to display the rewarded ad when requested.

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

  • A complete custom event rewarded ad example is available on GitHub for reference and modification.

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 loadRewardedAd() 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 loadRewardedAd() method in SampleRewardedCustomEventLoader.

To request a rewarded ad, create or modify a class that extends Adapter to implement loadRewardedAd(). Additionally, create a new class to implement MediationRewardedAd.

In our custom event example, SampleCustomEvent extends the Adapter class and then delegates to SampleRewardedCustomEventLoader.

Java

packagecom.google.ads.mediation.sample.customevent;
importcom.google.android.gms.ads.mediation.Adapter;
importcom.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;
importcom.google.android.gms.ads.mediation.MediationAdConfiguration;
importcom.google.android.gms.ads.mediation.MediationAdLoadCallback;
importcom.google.android.gms.ads.mediation.MediationRewardedAd;
importcom.google.android.gms.ads.mediation.MediationRewardedAdCallback;
...
publicclass SampleCustomEventextendsAdapter{
privateSampleNativeCustomEventLoadernativeLoader;
@Override
publicvoidloadRewardedAd(
@NonNullMediationRewardedAdConfigurationmediationRewardedAdConfiguration,
@NonNull
MediationAdLoadCallback<MediationRewardedAd,MediationRewardedAdCallback>
mediationAdLoadCallback){
rewardedLoader=
newSampleRewardedCustomEventLoader(
mediationRewardedAdConfiguration,mediationAdLoadCallback);
rewardedLoader.loadAd();
}
}

SampleRewardedCustomEventLoader is responsible for the following tasks:

  • Loading the rewarded ad

  • Implementing the MediationRewardedAd interface.

  • 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.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD). This parameter is typically an ad unit identifier that an ad network SDK requires when instantiating an ad object.

Java

packagecom.google.ads.mediation.sample.customevent;
importcom.google.android.gms.ads.mediation.Adapter;
importcom.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;
importcom.google.android.gms.ads.mediation.MediationAdLoadCallback;
importcom.google.android.gms.ads.mediation.MediationRewardedAd;
importcom.google.android.gms.ads.mediation.MediationRewardedAdCallback;
...
publicclass SampleRewardedCustomEventLoaderextendsSampleRewardedAdListener
implementsMediationRewardedAd{
/** Configuration for requesting the rewarded ad from the third-party network. */
privatefinalMediationRewardedAdConfigurationmediationRewardedAdConfiguration;
/**
 * A {@link MediationAdLoadCallback} that handles any callback when a Sample
 * rewarded ad finishes loading.
 */
privatefinalMediationAdLoadCallback<MediationRewardedAd,MediationRewardedAdCallback>
mediationAdLoadCallback;
/** Callback for rewarded ad events. */
privateMediationRewardedAdCallbackrewardedAdCallback;
/** Constructor. */
publicSampleRewardedCustomEventLoader(
@NonNullMediationRewardedAdConfigurationmediationRewardedAdConfiguration,
@NonNullMediationAdLoadCallback<MediationRewardedAd,MediationRewardedAdCallback>
mediationAdLoadCallback){
this.mediationRewardedAdConfiguration=mediationRewardedAdConfiguration;
this.mediationAdLoadCallback=mediationAdLoadCallback;
}
/** Loads the rewarded ad from the third-party ad network. */
publicvoidloadAd(){
// All custom events have a server parameter named "parameter" that returns
// back the parameter entered into the AdMob UI when defining the custom event.
Log.i("RewardedCustomEvent","Begin loading rewarded ad.");
StringserverParameter=mediationRewardedAdConfiguration
.getServerParameters()
.getString(MediationConfiguration
.CUSTOM_EVENT_SERVER_PARAMETER_FIELD);
Log.d("RewardedCustomEvent","Received server parameter.");
SampleAdRequestrequest=createSampleRequest(mediationRewardedAdConfiguration);
sampleRewardedAd=newSampleRewardedAd(serverParameter);
sampleRewardedAd.setListener(this);
Log.i("RewardedCustomEvent","Start fetching rewarded ad.");
sampleRewardedAd.loadAd(request);
}
publicSampleAdRequestcreateSampleRequest(
MediationAdConfigurationmediationAdConfiguration){
SampleAdRequestrequest=newSampleAdRequest();
request.setTestMode(mediationAdConfiguration.isTestRequest());
request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());
returnrequest;
}
}

Depending on whether the ad is successfully fetched or encounters an error, you would call either onSuccess() or onFailure(). onSuccess() is called by passing in an instance of the class that implements MediationRewardedAd.

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

Java

@Override
publicvoidonRewardedAdLoaded(){
rewardedAdCallback=mediationAdLoadCallback.onSuccess(this);
}
@Override
publicvoidonRewardedAdFailedToLoad(SampleErrorCodeerrorCode){
mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));
}

MediationRewardedAd requires implementing a showAd() method to display the ad:

Java

@Override
publicvoidshowAd(Contextcontext){
if(!(contextinstanceofActivity)){
rewardedAdCallback.onAdFailedToShow(
SampleCustomEventError.createCustomEventNoActivityContextError());
return;
}
Activityactivity=(Activity)context;
if(!sampleRewardedAd.isAdAvailable()){
rewardedAdCallback.onAdFailedToShow(
SampleCustomEventError.createCustomEventAdNotAvailableError());
return;
}
sampleRewardedAd.showAd(activity);
}

Forward mediation events to Google Mobile Ads SDK

Once onSuccess() is called, the returned MediationRewardedAdCallback object can then be used by the adapter to forward presentation events from the third-party SDK to Google Mobile Ads SDK. The SampleRewardedCustomEventLoader class extends the SampleAdListener interface to forward callbacks from the sample ad network to the 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 the Google Mobile Ads SDK. Here's an example of using callbacks:

Java

@Override
publicvoidonAdRewarded(finalStringrewardType,finalintamount){
RewardItemrewardItem=
newRewardItem(){
@Override
publicStringgetType(){
returnrewardType;
}
@Override
publicintgetAmount(){
returnamount;
}
};
rewardedAdCallback.onUserEarnedReward(rewardItem);
}
@Override
publicvoidonAdClicked(){
rewardedAdCallback.reportAdClicked();
}
@Override
publicvoidonAdFullScreen(){
rewardedAdCallback.onAdOpened();
rewardedAdCallback.onVideoStart();
rewardedAdCallback.reportAdImpression();
}
@Override
publicvoidonAdClosed(){
rewardedAdCallback.onAdClosed();
}
@Override
publicvoidonAdCompleted(){
rewardedAdCallback.onVideoComplete();
}

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.