Mediation
AdMob Mediation is a feature lets you serve ads to your apps from multiple sources, including the AdMob Network and third-party ad sources, in one place. AdMob Mediation helps maximize your fill rate and increase your monetization by sending ad requests to multiple networks to verify you find the best available network to serve ads.
This guide walks you through configuring mediation using the react-native-google-mobile-ads
library.
Configure mediation in the AdMob UI
To configure mediation in the AdMob UI, complete the following steps:
Install mediation adapters
For the Google Mobile Ads SDK to communicate with third-party ad networks, you need to include a mediation adapter for each network in your app. Each adapter must be integrated in the Android and iOS layer of your application.
To choose the adapters you want to install, see Integrate open source and versioned adapters (Android | iOS).
Android
- Add the adapter's dependency to your app-level
android/app/build.gradle
file.
iOS
- Add the adapter's pod to your
ios/Podfile
.
Verify your setup with ad inspector
To open the ad inspector from your React Native application, see Ad inspector in the
react-native-google-mobile-ads
documentation.
Call Android / iOS code from React Native
A third-party mediation adapter might have specific Android or iOS APIs that aren't exposed to your Reach Native code. In these cases, you can create a custom "bridge" to expose that native functionality.
This involves writing native code in both the Android and iOS layer.
Android
Create a Module
class to contain your native logic and a Package
class to register that module with your application.
- Create the native module. This class contains the methods you want to call from JavaScript.
Example file: android/app/src/main/java/com/your-app-name/
The following example creates a Module class to use with the Google Mobile Ads mediation adapter for AppLovin:
package com.your-app-name
import android.util.Log
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
// Import the necessary third-party SDKs.
import com.applovin.sdk.AppLovinPrivacySettings
class GoogleMobileAdsMediationAppLovinModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
// The React layer interfaces with the value of this API.
override fun getName() = "GoogleMobileAdsMediationAppLovin"
// This exposes the method to the React layer.
@ReactMethod
fun setHasUserConsent(hasUserConsent: Boolean) {
AppLovinPrivacySettings.setHasUserConsent(hasUserConsent)
}
}
-
Create the package
This class registers your module so React Native can find it.
Example file: android/app/src/main/java/com/your-app-name/
package com.your-app-name
import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager
import java.util.Collections
class GoogleMobileAdsMediationAppLovinPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf(GoogleMobileAdsMediationAppLovinModule(reactContext))
}
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return Collections.emptyList()
}
}
- Register the package
Add your new package in your MainApplication
file:
override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
// Add your custom package here.
add(GoogleMobileAdsMediationAppLovinPackage())
}
iOS
Create a header (.h
) and an implementation (.m
) file. The following example creates an GoogleMobileAdsMediationAppLovin
class:
Example file: ios/GoogleMobileAdsMediationAppLovin.h
#import <React/RCTBridgeModule.h>
@interface GoogleMobileAdsMediationAppLovin : NSObject <RCTBridgeModule>
@end
Example file: ios/GoogleMobileAdsMediationAppLovin.m
#import "GoogleMobileAdsMediationAppLovin.h"
#import <Foundation/Foundation.h>
// Import the necessary third-party SDKs.
#import <AppLovinSDK/AppLovinSDK.h>
@implementation GoogleMobileAdsMediationAppLovin
// The React layer interfaces with the class name, e.g. GoogleMobileAdsMediationAppLovin, by default.
RCT_EXPORT_MODULE();
// This exports the method to the React layer.
RCT_EXPORT_METHOD(setHasUserConsent:(BOOL)hasUserConsent)
{
[ALPrivacySettings setHasUserConsent:hasUserConsent];
}
@end
Import native modules
In the JavaScript file where you want to call the native method, import NativeModules
from the react-native
library:
import { NativeModules } from 'react-native'
You can then access your module directly from the NativeModules
object. The name you use here must exactly match the name
exposed by your native code.
The following example uses the GoogleMobileAdsMediationAppLovin
sample class:
const { GoogleMobileAdsMediationAppLovin } = NativeModules
Once you have the module object, you can work with your object just as you would in Javascript.
GoogleMobileAdsMediationAppLovin.setHasUserConsent(true)