Building a Custom Adapter
You can write a custom adapter if you want to support an ad network that is not listed in the AppLovin MAX Mediation Matrix. The following instructions show you how to do this.
Android
Implement a subclass of com.applovin.mediation.adapters.MediationAdapterBase
.
This subclass enables the interface between your mediation adapter and the AppLovin MAX SDK.
That interface handles functions of your SDK such as initialization, versioning, and resource cleanup.
- Create a subclass of
MediationAdapterBase
in thecom.applovin.mediation.adapters
package of your application. The name of this class should contain the network name (for example,com.mycompanyname.MyNetworkMediationAdapter
). - Override the
initialize(final MaxAdapterInitializationParameters parameters, final Activity activity, final OnCompletionListener onCompletionListener)
method. Implement code that initializes your ad network SDK. If you need to access the app ID set in the UI, you can do so viaparameters.getServerParameters().getString( "app_id", null );
. This logic executes in parallel with the initialization of the AppLovin SDK. CallonCompletionListener.onCompletion()
after you initialize the ad network, and pass that completion handler one of the following values:- While the ad network SDK is initializing:
InitializationStatus.INITIALIZING
- If the ad network SDK initializes:
InitializationStatus.INITIALIZED_SUCCESS
- If the ad network SDK fails to initialize:
InitializationStatus.INITIALIZED_FAILURE
(with a relevant error message) - If the ad network SDK does not have an initialization callback status:
InitializationStatus.DOES_NOT_APPLY
- If the ad network explicitly initialized, but without a status:
InitializationStatus.INITIALIZED_UNKNOWN
- While the ad network SDK is initializing:
- Override the
getSdkVersion()
method. Implement code that returns the network SDK version string from that method. - Override the
getAdapterVersion()
method. Implement code that returns the version number of the mediation adapter from that method. All AppLovin’s adapters use a five-number versioning scheme: The leftmost four numbers correspond to the network SDK version. The last number denotes the minor version number, which refers to the adapter release. - Override the
onDestroy()
method. Implement the clean-up logic for the network SDK objects in that method.
Banner Ads
- Implement the
MaxAdViewAdapter
interface in yourcom.mycompanyname.MyNetworkMediationAdapter
class. - Override the
loadAdViewAd()
method which requests an ad. Call theparameters.getThirdPartyAdPlacementId()
method to get the placement ID you need for your ad logic. - Call the appropriate
MaxAdViewAdapterListener
methods. They notify the AppLovin MAX SDK about banner lifecycle events:- When an ad loads, call
MaxAdViewAdapterListener.onAdViewAdLoaded()
. - When an ad load fails, call
MaxAdViewAdapterListener.onAdViewAdLoadFailed()
with an appropriateMaxAdapterError
. - Optionally, call the following banner lifecycle event notifications as appropriate:
onAdViewAdClicked()
,onAdViewAdCollapsed()
,onAdViewAdDisplayed()
,onAdViewAdDisplayFailed()
,onAdViewAdExpanded()
,onAdViewAdHidden()
.
- When an ad loads, call
Interstitials
- Implement the
MaxInterstitialAdapter
interface in yourcom.mycompanyname.MyNetworkMediationAdapter
class. - Override the
loadInterstitialAd()
method which requests an ad. Call theparameters.getThirdPartyAdPlacementId()
method to get the placement ID you need for your ad logic. - Override the
showInterstitialAd()
method which shows the loaded ad. Call theparameters.getThirdPartyAdPlacementId()
method to get the placement ID you need for your ad logic. If the ad is not ready, callMaxInterstitialAdapterListener.onInterstitialAdDisplayFailed( MaxAdapterError.AD_NOT_READY )
. - Call the appropriate
MaxInterstitialAdapterListener
methods. They notify the AppLovin MAX SDK about interstitial lifecycle events:- When an ad loads, call
MaxInterstitialAdapterListener.onInterstitialAdLoaded()
. - When an ad load fails, call
MaxInterstitialAdapterListener.onInterstitialAdLoadFailed()
with an appropriateMaxAdapterError
. - When an ad is hidden, call
MaxInterstitialAdapterListener.onInterstitialAdHidden()
. - Optionally, call the following interstitial lifecycle event notifications as appropriate:
onInterstitialAdClicked()
,onInterstitialAdDisplayFailed()
.
- When an ad loads, call
Rewarded Ads
- Implement the
MaxRewardedAdapter
interface in yourcom.mycompanyname.MyNetworkMediationAdapter
class. - Override the
loadRewardedAd()
method which requests an ad. Call theparameters.getThirdPartyAdPlacementId()
method to get the placement ID you need for your ad logic. - Override the
showRewardedAd()
method which shows the loaded ad. Call theparameters.getThirdPartyAdPlacementId()
method to get the placement ID you need for your ad logic. If the ad is not ready, callMaxRewardedAdapterListener.onRewardedAdDisplayFailed( MaxAdapterError.AD_NOT_READY )
. - Call the appropriate
MaxRewardedAdapterListener
methods. They notify the AppLovin MAX SDK about rewarded video lifecycle events:- When an ad loads, call
MaxRewardedAdapterListener.onRewardedAdLoaded()
. - When an ad load fails, call
MaxRewardedAdapterListener.onRewardedAdLoadFailed()
with an appropriateMaxAdapterError
. - When an ad starts playing, call
MaxRewardedAdapterListener.onRewardedAdDisplayed()
. - If a reward should be presented to the user, call
MaxRewardedAdapterListener.onUserRewarded()
with an appropriateMaxReward
amount and currency. If no amount is available, default toMaxReward.DEFAULT_AMOUNT
. AppLovin recommends that you call this immediately beforeMaxRewardedAdapterListener.onRewardedAdHidden()
. - When an ad is hidden, call
MaxRewardedAdapterListener.onRewardedAdHidden()
. - Optionally, call the following rewarded video lifecycle event notification as appropriate:
onRewardedAdClicked()
.
- When an ad loads, call
Native Ads
- Implement the
MaxNativeAdAdapter
interface in yourcom.mycompanyname.MyNetworkMediationAdapter
class. - Override the
loadNativeAd()
method which requests an ad. - Call the appropriate
MaxNativeAdAdapterListener
methods. They notify the AppLovin MAX SDK about native ad lifecycle events:- When an ad loads, call
MaxNativeAdAdapterListener.onNativeAdLoaded()
. - When an ad load fails, call
MaxNativeAdAdapterListener.onNativeAdLoadFailed()
with an appropriateMaxAdapterError
. - When an ad displays, call
MaxNativeAdAdapterListener.onNativeAdDisplayed()
. - When the user clicks on an ad, call
MaxNativeAdAdapterListener.onNativeAdClicked()
.
- When an ad loads, call
Privacy
MAX offers two boolean flags that indicate whether the user provides privacy consent.
They are Boolean
objects.
Their value is null
if the user has indicated neither consent or no consent.
MAX passes the parameters
object into each method.
To get the value of the privacy flags, use the following methods of the parameters
object:
MaxAdapterParameters.hasUserConsent()
- for the GDPR consent flag. To determine if GDPR applies to the user, read the SDK Integration Guides > Platform > Overview > Privacy page of this documentation.
MaxAdapterParameters.isDoNotSell()
- for the multi-state targeted advertising consent flag. AppLovin does not have an API that indicates whether the user belongs in a region to which this applies.
iOS
Implement a subclass of ALMediationAdapter
.
This subclass enables the interface between your mediation adapter and the AppLovin MAX SDK.
That interface handles functions of your SDK such as initialization, versioning, and resource cleanup.
- Create a
.m
and.h
extending fromALMediationAdapter
in your project. The name of this class should contain the network name (for example,MyNetworkMediationAdapter
). - Implement the
- (void)initializeWithParameters:(id<MAAdapterInitializationParameters>)parameters completionHandler:(void (^)(MAAdapterInitializationStatus NSString *_Nullable))completionHandler
method Implement code that initializes your ad network SDK. You can retrieve the app ID set in the UI fromparameters.serverParameters[@"app_id"]
. This logic executes in parallel with the initialization of the AppLovin SDK. CallcompletionHandler
after you initialize the ad network, and pass that handler one of the following values:- While the ad network SDK is initializing:
MAAdapterInitializationStatusInitializing
- If the ad network SDK initializes:
MAAdapterInitializationStatusInitializedSuccess
- If the ad network SDK fails to initialize:
MAAdapterInitializationStatusInitializedFailure
(with a relevant error message) - If the ad network SDK does not have an initialization callback status:
MAAdapterInitializationStatusDoesNotApply
(withnull
as the error message) - If the ad network explicitly initialized, but without a status:
MAAdapterInitializationStatusInitializedUnknown
- While the ad network SDK is initializing:
- Implement the
- (NSString *)SDKVersion
method. Implement code that returns the network SDK version string from that method. - Implement the
- (NSString *)adapterVersion
method. Implement code that returns the version number of the mediation adapter from that method. All AppLovin’s adapters use a five-number versioning scheme: The leftmost four numbers correspond to the network SDK version. The last number denotes the minor version number, which refers to the adapter release. - Implement the
- (void)destroy
method. Implement clean-up logic for the network SDK objects in that method.
Banner Ads
- Implement the
MAAdViewAdapter
protocol in yourMyNetworkMediationAdapter
class. - Implement the
- (void)loadAdViewAdForParameters:(id<MAAdapterResponseParameters>)parameters adFormat:(MAAdFormat *)adFormat andNotify:(id<MAAdViewAdapterDelegate>)delegate
method which requests an ad. Call theparameters.thirdPartyAdPlacementIdentifier
method to get the Placement ID you need for your ad logic. - Call the appropriate
MAAdViewAdapterDelegate
methods. They notify the AppLovin MAX SDK about banner lifecycle events:- When an ad loads, call
-[MAAdViewAdapterDelegate didLoadAdForAdView]
. - When an ad load fails, call
-[MAAdViewAdapterDelegate didFailToLoadAdViewAdWithError:]
with an appropriateMAAdapterError
. - Optionally, call the following banner lifecycle event notifications as appropriate:
didClickAdViewAd
,didCollapseAdViewAd
,didDisplayAdViewAd
,didExpandAdViewAd
,didFailToDisplayAdViewAdWithError
,didHideAdViewAd
.
- When an ad loads, call
Interstitials
- Implement the
MAInterstitialAdapter
protocol in yourMyNetworkMediationAdapter
class. - Declare the
- (void)loadInterstitialAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MAInterstitialAdapterDelegate>)delegate
method which requests an ad. Call theparameters.thirdPartyAdPlacementIdentifier
method to get the placement ID you need for your ad logic. - Declare the
- (void)showInterstitialAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MAInterstitialAdapterDelegate>)delegate
method which shows the loaded ad. Call theparameters.thirdPartyAdPlacementIdentifier
method to get the placement ID you need for your ad logic. If the ad is not ready, call-[MAInterstitialAdapterDelegate didFailToDisplayInterstitialAdWithError: MAAdapterError.adNotReady]
. - Call the appropriate
MAInterstitialAdapterDelegate
methods. They notify the AppLovin MAX SDK about interstitial lifecycle events:- When an ad loads, call
-[MAInterstitialAdapterDelegate didLoadInterstitialAd:]
. - When an ad load fails, call
-[MAInterstitialAdapterDelegate didFailToLoadInterstitialAdWithError:]
with an appropriateMAAdapterError
. - When an ad displays, call
-[MAInterstitialAdapterDelegate didDisplayInterstitialAd]
. - When an ad is hidden, call
-[MAInterstitialAdapterDelegate didHideInterstitialAd:]
. - Optionally, call the following interstitial lifecycle event notifications as appropriate:
didClickInterstitialAd
,didFailToDisplayInterstitialAdWithError
.
- When an ad loads, call
Rewarded Ads
- Implement the
MARewardedAdapter
protocol in yourMyNetworkMediationAdapter
class. - Declare the
- (void)loadRewardedAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MARewardedAdapterDelegate>)delegate;
method which requests an ad. Call theparameters.thirdPartyAdPlacementIdentifier
method to get the placement ID you need for your ad logic. - Declare the
- (void)showRewardedAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MARewardedAdapterDelegate>)delegate;
method which shows the loaded ad. Call theparameters.thirdPartyAdPlacementIdentifier
method to get the placement ID you need for your ad logic. If the ad is not ready, call-[MARewardedAdapterDelegate didFailToDisplayRewardedAdWithError: MAAdapterError.adNotReady]
. - Call the appropriate
MARewardedAdapterDelegate
methods. They notify the AppLovin MAX SDK about rewarded video lifecycle events:- When an ad loads, call
-[MARewardedAdapterDelegate didLoadRewardedAd:]
. - When an ad load fails, call
-[MARewardedAdapterDelegate didFailToLoadRewardedAdWithError:]
with an appropriateMAAdapterError
. - When an ad starts playing, call
-[MARewardedAdapterDelegate didDisplayRewardedAd:]
. - If a reward should be presented to the user, call
-[MARewardedAdapterDelegate didRewardUserWithReward:]
with an appropriateMAReward
amount and currency. If no amount is available, default toMAReward.defaultAmount
. AppLovin recommends that you call this immediately before-[MARewardedAdapterDelegate didHideRewardedAd:]
. - When an ad is hidden, call
-[MARewardedAdapterDelegate didHideRewardedAd:]
. - Optionally, call the following rewarded video lifecycle event notification as appropriate:
didClickRewardedAd
.
- When an ad loads, call
Native Ads
- Implement the
MANativeAdAdapter
protocol in yourMyNetworkMediationAdapter
class. - Implement the
- (void)loadNativeAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MANativeAdAdapterDelegate>)delegate;
method which requests an ad. - Call the appropriate
MANativeAdAdapterDelegate
methods. They notify the AppLovin MAX SDK about native ad lifecycle events:- When an ad loads, call
-[MANativeAdAdapterDelegate didLoadAdForNativeAd:]
. - When an ad load fails, call
-[MANativeAdAdapterDelegate didFailToLoadNativeAdWithError:]
with an appropriateMAAdapterError
. - When an ad displays, call
-[MANativeAdAdapterDelegate didDisplayNativeAdWithExtraInfo:]
. - When the user clicks on an ad, call
-[MANativeAdAdapterDelegate didClickNativeAd:]
.
- When an ad loads, call
Privacy
MAX offers two boolean flags that indicate whether the user provides privacy consent.
They are NSNumber
objects.
Their value is nil
if the user has indicated neither consent or no consent.
MAX passes the parameters
object into each method.
To get the value of the privacy flags, use the following APIs of the parameters
object:
-[MAAdapterParameters hasUserConsent]
- for the GDPR consent flag. To determine if GDPR applies to the user, read the SDK Integration Guides > Platform > Overview > Privacy page of this documentation.
-[MAAdapterParameters isDoNotSell]
- for the multi-state targeted advertising consent flag. AppLovin does not have an API that indicates whether the user belongs in a region to which this applies.
Testing
You can test your custom adapter by using the MAX Demo App (see SDK Integration Guides > Platform > Testing Networks). To get an ad request, raise the CPM of your placement in the waterfall.
Troubleshooting
If your custom adapter does not appear in the waterfall as you expect, use the following troubleshooting checklist:
- Disable Test Mode (see SDK Integration Guides > Platform > Testing Networks > Test Mode)
- Make sure that the app package name, ad unit, and SDK key all match.
- Test with a physical device.
- Set the CPM of the custom network high enough that it can win in the waterfall.