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
MediationAdapterBasein thecom.applovin.mediation.adapterspackage 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 initializes:
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 initializes:
- 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
MaxAdViewAdapterinterface in yourcom.mycompanyname.MyNetworkMediationAdapterclass. - 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
MaxAdViewAdapterListenermethods. 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
MaxInterstitialAdapterinterface in yourcom.mycompanyname.MyNetworkMediationAdapterclass. - 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
MaxInterstitialAdapterListenermethods. 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
MaxRewardedAdapterinterface in yourcom.mycompanyname.MyNetworkMediationAdapterclass. - 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
MaxRewardedAdapterListenermethods. 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 appropriateMaxRewardamount 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
MaxNativeAdAdapterinterface in yourcom.mycompanyname.MyNetworkMediationAdapterclass. - Override the
loadNativeAd()method which requests an ad. - Call the appropriate
MaxNativeAdAdapterListenermethods. 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 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
.mand.hextending fromALMediationAdapterin 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))completionHandlermethod 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. CallcompletionHandlerafter you initialize the ad network, and pass that handler one of the following values:- While the ad network SDK initializes:
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(withnullas the error message) - If the ad network explicitly initialized, but without a status:
MAAdapterInitializationStatusInitializedUnknown
- While the ad network SDK initializes:
- Implement the
- (NSString *)SDKVersionmethod. Implement code that returns the network SDK version string from that method. - Implement the
- (NSString *)adapterVersionmethod. 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)destroymethod. Implement clean-up logic for the network SDK objects in that method.
Banner ads
- Implement the
MAAdViewAdapterprotocol in yourMyNetworkMediationAdapterclass. - Implement the
- (void)loadAdViewAdForParameters:(id<MAAdapterResponseParameters>)parameters adFormat:(MAAdFormat *)adFormat andNotify:(id<MAAdViewAdapterDelegate>)delegatemethod which requests an ad. Call theparameters.thirdPartyAdPlacementIdentifiermethod to get the Placement ID you need for your ad logic. - Call the appropriate
MAAdViewAdapterDelegatemethods. 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
MAInterstitialAdapterprotocol in yourMyNetworkMediationAdapterclass. - Declare the
- (void)loadInterstitialAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MAInterstitialAdapterDelegate>)delegatemethod which requests an ad. Call theparameters.thirdPartyAdPlacementIdentifiermethod to get the placement ID you need for your ad logic. - Declare the
- (void)showInterstitialAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MAInterstitialAdapterDelegate>)delegatemethod which shows the loaded ad. Call theparameters.thirdPartyAdPlacementIdentifiermethod 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
MAInterstitialAdapterDelegatemethods. 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
MARewardedAdapterprotocol in yourMyNetworkMediationAdapterclass. - Declare the
- (void)loadRewardedAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MARewardedAdapterDelegate>)delegate;method which requests an ad. Call theparameters.thirdPartyAdPlacementIdentifiermethod 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.thirdPartyAdPlacementIdentifiermethod 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
MARewardedAdapterDelegatemethods. 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 appropriateMARewardamount 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
MANativeAdAdapterprotocol in yourMyNetworkMediationAdapterclass. - Implement the
- (void)loadNativeAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MANativeAdAdapterDelegate>)delegate;method which requests an ad. - Call the appropriate
MANativeAdAdapterDelegatemethods. 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 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.