|
| 1 | +# Mediation |
| 2 | + |
| 3 | +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. |
| 4 | +AdMob Mediation helps maximize your fill rate and increase your monetization by sending ad requests to multiple networks to verify you find the best |
| 5 | +available network to serve ads. |
| 6 | + |
| 7 | +This guide walks you through configuring mediation using the `react-native-google-mobile-ads` library. |
| 8 | + |
| 9 | +## Configure mediation in the AdMob UI |
| 10 | + |
| 11 | +To configure mediation in the AdMob UI, complete the following steps: |
| 12 | + |
| 13 | +1. Choose an ad source from the network details table ([Android](https://developers.google.com/admob/android/choose-networks#network_details) | [iOS](https://developers.google.com/admob/ios/choose-networks#network_details)). |
| 14 | +2. Follow steps 1-2 of that ad source's integration guide. |
| 15 | + |
| 16 | +## Install mediation adapters |
| 17 | + |
| 18 | +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 |
| 19 | +adapter must be integrated in the Android and iOS layer of your application. |
| 20 | + |
| 21 | +To choose the adapters you want to install, see Integrate open source and versioned adapters |
| 22 | +([Android](https://developers.google.com/admob/android/choose-networks#integrate_open-source_and_versioned_adapters) | [iOS](https://developers.google.com/admob/ios/choose-networks#integrate_open-source_and_versioned_adapters)). |
| 23 | + |
| 24 | +<Tabs groupId="framework" values={[{label: 'React Native', value: 'bare'}, {label: 'Expo', value: 'expo'}]}> |
| 25 | +<TabItem value="bare"> |
| 26 | + |
| 27 | +Android |
| 28 | +- Add the adapter's dependency to your app-level `android/app/build.gradle` file. |
| 29 | + |
| 30 | +iOS |
| 31 | +- Add the adapter's pod to your `ios/Podfile`. |
| 32 | + |
| 33 | +</TabItem> |
| 34 | +<TabItem value="expo"> |
| 35 | + |
| 36 | +Add native dependencies using the [`expo-build-properties`](https://docs.expo.dev/versions/latest/sdk/build-properties) plugin. |
| 37 | + |
| 38 | +Install the plugin if you haven't already: |
| 39 | + |
| 40 | +```bash |
| 41 | +npx expo install expo-build-properties |
| 42 | +``` |
| 43 | + |
| 44 | +Add the plugin to your `app.json` or `app.config.js` file, specifying the Android dependency or iOS pod for the adapter. |
| 45 | + |
| 46 | +```json |
| 47 | +// app.json |
| 48 | +{ |
| 49 | + "expo": { |
| 50 | + "plugins": [ |
| 51 | + [ |
| 52 | + "expo-build-properties", |
| 53 | + { |
| 54 | + "android": { |
| 55 | + "dependencies": [ |
| 56 | + // Example: Add the AppLovin mediation adapter for Android. Replace {X.Y.Z.A} with the specific adapter version you are integrating. |
| 57 | + "com.google.ads.mediation:applovin:{X.Y.Z.A}" |
| 58 | + ] |
| 59 | + }, |
| 60 | + "ios": { |
| 61 | + "pods": [ |
| 62 | + { |
| 63 | + // Example: Add the AppLovin mediation adapter for iOS. |
| 64 | + "name": "GoogleMobileAdsMediationAppLovin" |
| 65 | + } |
| 66 | + ] |
| 67 | + } |
| 68 | + } |
| 69 | + ] |
| 70 | + ] |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +Rebuild your development client to include the new native dependencies: |
| 76 | + |
| 77 | +```bash |
| 78 | +npx expo prebuild --clean |
| 79 | +npx expo run:android |
| 80 | +npx expo run:ios |
| 81 | +``` |
| 82 | + |
| 83 | +</TabItem> |
| 84 | +</Tabs> |
| 85 | + |
| 86 | +## Verify your setup with ad inspector |
| 87 | + |
| 88 | +To open the ad inspector from your React Native application, see [Ad inspector](https://docs.page/invertase/react-native-google-mobile-ads/ad-inspector) in the |
| 89 | +`react-native-google-mobile-ads` documentation. |
| 90 | + |
| 91 | +## Call Android / iOS code from React Native |
| 92 | + |
| 93 | +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. |
| 94 | + |
| 95 | +<Tabs groupId="framework" values={[{label: 'React Native', value: 'bare'}, {label: 'Expo', value: 'expo'}]}> |
| 96 | +<TabItem value="bare"> |
| 97 | + |
| 98 | +This involves writing native code in both the Android and iOS layer. |
| 99 | + |
| 100 | +**Android** |
| 101 | + |
| 102 | +Create a `Module` class to contain your native logic and a `Package` class to register that module with your application. |
| 103 | + |
| 104 | +1. Create the native module. This class contains the methods you want to call from JavaScript. |
| 105 | + |
| 106 | +Example file: `android/app/src/main/java/com/your-app-name/` |
| 107 | + |
| 108 | +The following example creates a **Module** class to use with the Google Mobile Ads mediation adapter for AppLovin: |
| 109 | + |
| 110 | +``` |
| 111 | +package com.your-app-name |
| 112 | +
|
| 113 | +import android.util.Log |
| 114 | +import com.facebook.react.bridge.ReactApplicationContext |
| 115 | +import com.facebook.react.bridge.ReactContextBaseJavaModule |
| 116 | +import com.facebook.react.bridge.ReactMethod |
| 117 | +// Import the necessary third-party SDKs. |
| 118 | +import com.applovin.sdk.AppLovinPrivacySettings |
| 119 | +
|
| 120 | +class GoogleMobileAdsMediationAppLovinModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { |
| 121 | +
|
| 122 | + // The React layer interfaces with the value of this API. |
| 123 | + override fun getName() = "GoogleMobileAdsMediationAppLovin" |
| 124 | +
|
| 125 | + // This exposes the method to the React layer. |
| 126 | + @ReactMethod |
| 127 | + fun setHasUserConsent(hasUserConsent: Boolean) { |
| 128 | + AppLovinPrivacySettings.setHasUserConsent(hasUserConsent) |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +2. Create the package |
| 134 | + |
| 135 | + This class registers your module so React Native can find it. |
| 136 | + |
| 137 | +Example file: `android/app/src/main/java/com/your-app-name/` |
| 138 | + |
| 139 | +``` |
| 140 | +package com.your-app-name |
| 141 | +
|
| 142 | +import com.facebook.react.ReactPackage |
| 143 | +import com.facebook.react.bridge.NativeModule |
| 144 | +import com.facebook.react.bridge.ReactApplicationContext |
| 145 | +import com.facebook.react.uimanager.ViewManager |
| 146 | +import java.util.Collections |
| 147 | +
|
| 148 | +class GoogleMobileAdsMediationAppLovinPackage : ReactPackage { |
| 149 | + override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> { |
| 150 | + return listOf(GoogleMobileAdsMediationAppLovinModule(reactContext)) |
| 151 | + } |
| 152 | +
|
| 153 | + override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> { |
| 154 | + return Collections.emptyList() |
| 155 | + } |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +3. Register the package |
| 160 | + |
| 161 | +Add your new package in your `MainApplication` file: |
| 162 | + |
| 163 | +``` |
| 164 | +override fun getPackages(): List<ReactPackage> = |
| 165 | + PackageList(this).packages.apply { |
| 166 | + // Add your custom package here. |
| 167 | + add(GoogleMobileAdsMediationAppLovinPackage()) |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +**iOS** |
| 172 | + |
| 173 | +Create a header (`.h`) and an implementation (`.m`) file. The following example creates an `GoogleMobileAdsMediationAppLovin` class: |
| 174 | + |
| 175 | +Example file: `ios/GoogleMobileAdsMediationAppLovin.h` |
| 176 | + |
| 177 | +``` |
| 178 | +#import <React/RCTBridgeModule.h> |
| 179 | +
|
| 180 | +@interface GoogleMobileAdsMediationAppLovin : NSObject <RCTBridgeModule> |
| 181 | +@end |
| 182 | +``` |
| 183 | + |
| 184 | +Example file: `ios/GoogleMobileAdsMediationAppLovin.m` |
| 185 | + |
| 186 | +``` |
| 187 | +#import "GoogleMobileAdsMediationAppLovin.h" |
| 188 | +#import <Foundation/Foundation.h> |
| 189 | +// Import the necessary third-party SDKs. |
| 190 | +#import <AppLovinSDK/AppLovinSDK.h> |
| 191 | +
|
| 192 | +@implementation GoogleMobileAdsMediationAppLovin |
| 193 | +
|
| 194 | +// The React layer interfaces with the class name, e.g. GoogleMobileAdsMediationAppLovin, by default. |
| 195 | +RCT_EXPORT_MODULE(); |
| 196 | +
|
| 197 | +// This exports the method to the React layer. |
| 198 | +RCT_EXPORT_METHOD(setHasUserConsent:(BOOL)hasUserConsent) |
| 199 | +{ |
| 200 | + [ALPrivacySettings setHasUserConsent:hasUserConsent]; |
| 201 | +} |
| 202 | +
|
| 203 | +@end |
| 204 | +``` |
| 205 | + |
| 206 | +**Import native modules** |
| 207 | + |
| 208 | +In the JavaScript file where you want to call the native method, import `NativeModules` from the `react-native` library: |
| 209 | + |
| 210 | +``` |
| 211 | +import { NativeModules } from 'react-native' |
| 212 | +``` |
| 213 | + |
| 214 | +You can then access your module directly from the `NativeModules` object. The name you use here must exactly match the name |
| 215 | +exposed by your native code. |
| 216 | + |
| 217 | +The following example uses the `GoogleMobileAdsMediationAppLovin` sample class: |
| 218 | + |
| 219 | +``` |
| 220 | +const { GoogleMobileAdsMediationAppLovin } = NativeModules |
| 221 | +``` |
| 222 | + |
| 223 | +Once you have the module object, you can work with your object just as you would in Javascript. |
| 224 | + |
| 225 | +``` |
| 226 | +GoogleMobileAdsMediationAppLovin.setHasUserConsent(true) |
| 227 | +``` |
| 228 | + |
| 229 | +</TabItem> |
| 230 | +<TabItem value="expo"> |
| 231 | + |
| 232 | +1. **Create an [Expo module](https://docs.expo.dev/modules/get-started/)** with the `create-expo-module` tool. |
| 233 | + |
| 234 | +Run the following command from your project's root directory: |
| 235 | + |
| 236 | +``` |
| 237 | +npx create-expo-module --local |
| 238 | +``` |
| 239 | + |
| 240 | +When prompted, give your module a name. This will scaffold a new module in a modules/ directory at your project root. |
| 241 | + |
| 242 | +2. **Add your native Android and iOS code**. The following example creates a module to use with the |
| 243 | +Google Mobile Ads mediation adapter for AppLovin: |
| 244 | + |
| 245 | +Android |
| 246 | + |
| 247 | +Open the generated Kotlin file for your module and add your custom method. The module and package are already set up for you. |
| 248 | + |
| 249 | +File: `modules/google-mobile-ads-mediation-applovin/android/src/main/java/.../GoogleMobileAdsMediationApplovinModule.kt` |
| 250 | + |
| 251 | +``` |
| 252 | +// ... existing imports |
| 253 | +import com.applovin.sdk.AppLovinPrivacySettings |
| 254 | +
|
| 255 | +class GoogleMobileAdsMediationApplovinModule : Module() { |
| 256 | + override fun definition() = ModuleDefinition { |
| 257 | + // ... existing definition |
| 258 | +
|
| 259 | + // Add your custom method here. |
| 260 | + Function("setHasUserConsent") { hasUserConsent: Boolean -> |
| 261 | + AppLovinPrivacySettings.setHasUserConsent(hasUserConsent) |
| 262 | + } |
| 263 | + } |
| 264 | +} |
| 265 | +``` |
| 266 | + |
| 267 | +Then, add the dependency in your module's `build.gradle` file. |
| 268 | + |
| 269 | +File: `modules/google-mobile-ads-mediation-applovin/android/build.gradle` |
| 270 | + |
| 271 | +``` |
| 272 | +// Groovy |
| 273 | +
|
| 274 | +dependencies { |
| 275 | + // Add the third-party SDK dependency. Replace {X.Y.Z.A} with the specific adapter version you are integrating. |
| 276 | + implementation 'com.google.ads.mediation:applovin:{X.Y.Z.A}' |
| 277 | +} |
| 278 | +``` |
| 279 | + |
| 280 | +iOS |
| 281 | + |
| 282 | +Open the generated Swift file for your module and add your custom method. |
| 283 | + |
| 284 | +File: `modules/google-mobile-ads-mediation-applovin/ios/GoogleMobileAdsMediationApplovinModule.swift` |
| 285 | + |
| 286 | +``` |
| 287 | +// ... existing imports |
| 288 | +import AppLovinSDK |
| 289 | +
|
| 290 | +public class GoogleMobileAdsMediationApplovinModule: Module { |
| 291 | + public func definition() -> ModuleDefinition { |
| 292 | + // ... existing definition |
| 293 | +
|
| 294 | + // Add your custom method here |
| 295 | + Function("setHasUserConsent") { (hasUserConsent: Bool) in |
| 296 | + ALPrivacySettings.setHasUserConsent(hasUserConsent) |
| 297 | + } |
| 298 | + } |
| 299 | +} |
| 300 | +``` |
| 301 | + |
| 302 | +Next, add the dependency in your module's `.podspec` file. |
| 303 | + |
| 304 | +File: `modules/google-mobile-ads-mediation-applovin/google-mobile-ads-mediation-applovin.podspec` |
| 305 | + |
| 306 | +``` |
| 307 | +# Add the third-party pod dependency. |
| 308 | +s.dependency 'GoogleMobileAdsMediationAppLovin' |
| 309 | +``` |
| 310 | + |
| 311 | +3. **Install and rebuild**. After creating the module and adding the code, install it in your project and rebuild your development client. |
| 312 | + |
| 313 | +4. **Call the code from JavaScript**. You can now import and use the module directly in your code. |
| 314 | + |
| 315 | +``` |
| 316 | +import { setHasUserConsent } from 'google-mobile-ads-mediation-applovin' |
| 317 | +
|
| 318 | +// Call your custom native method. |
| 319 | +setHasUserConsent(true) |
| 320 | +``` |
| 321 | + |
| 322 | +</TabItem> |
| 323 | +</Tabs> |
0 commit comments