Expo includes support for the Google AdMob SDK for mobile advertising, including components for banner ads and imperative APIs for interstitial and rewarded video ads. expo-ads-admob is largely based of the react-native-admob module, as the documentation and questions surrounding that module may prove helpful. A simple example implementing AdMob SDK can be found here.
For the module to attribute interactions with ads to your AdMob app properly you will need to add a googleMobileAdsAppId property to app.json under [platform].config. More info on where to find the app ID can be found in this Google Support answer. A sample valid app.json would look like:
{"expo":{"name":"Ads Showcase",// ..."android":{// ..."config":{// ..."googleMobileAdsAppId":"ca-app-pub-3940256099942544~3347511713"// sample id, replace with your own}},"ios":{// ..."config":{// ..."googleMobileAdsAppId":"ca-app-pub-3940256099942544~1458002511"// sample id, replace with your own}}}}
import{AdMobBanner,AdMobInterstitial,PublisherBanner,AdMobRewarded,
setTestDeviceIDAsync,}from'expo-ads-admob';// Set global test device IDawaitsetTestDeviceIDAsync('EMULATOR');// Display a banner<AdMobBanner
bannerSize="fullBanner"
adUnitID="ca-app-pub-3940256099942544/6300978111"// Test ID, Replace with your-admob-unit-id
servePersonalizedAds // true or false
onDidFailToReceiveAdWithError={this.bannerError}/>// Display a DFP Publisher banner<PublisherBanner
bannerSize="fullBanner"
adUnitID="ca-app-pub-3940256099942544/6300978111"// Test ID, Replace with your-admob-unit-id
onDidFailToReceiveAdWithError={this.bannerError}
onAdMobDispatchAppEvent={this.adMobEvent}/>// Display an interstitialawaitAdMobInterstitial.setAdUnitID('ca-app-pub-3940256099942544/1033173712');// Test ID, Replace with your-admob-unit-idawaitAdMobInterstitial.requestAdAsync({ servePersonalizedAds:true});awaitAdMobInterstitial.showAdAsync();// Display a rewarded adawaitAdMobRewarded.setAdUnitID('ca-app-pub-3940256099942544/5224354917');// Test ID, Replace with your-admob-unit-idawaitAdMobRewarded.requestAdAsync();awaitAdMobRewarded.showAdAsync();
The default behavior of the Google Mobile Ads SDK is to serve personalized ads. If a user has consented to receive only non-personalized ads, you can configure the view to specify that only non-personalized ads should be requested. Adding servePersonalizedAds property causes non-personalized ads to be requested regardless of whether or not the user is in the EEA. The default is false — ads won't be personalized.
requests an interstitial and resolves when interstitialDidLoad or interstitialDidFailToLoad event fires. An optional options object argument may specify servePersonalizedAds: true value — then ads will be personalized.
showAdAsync()
shows an interstitial if it is ready and resolves when interstitialDidOpen event fires
getIsReadyAsync()
resolves with boolean whether interstitial is ready to be shown
Unfortunately, events are not consistent across iOS and Android. To have one unified API, new event names are introduced for pairs that are roughly equivalent.
iOS
this library
Android
interstitialDidReceiveAd
interstitialDidLoad
onAdLoaded
interstitial:didFailToReceiveAdWithError
interstitialDidFailToLoad
onAdFailedToLoad
interstitialWillPresentScreen
interstitialDidOpen
onAdOpened
interstitialDidFailToPresentScreen
interstitialWillDismissScreen
interstitialDidDismissScreen
interstitialDidClose
onAdClosed
interstitialWillLeaveApplication
interstitialWillLeaveApplication
onAdLeftApplication
Note that interstitialWillLeaveApplication and onAdLeftApplication are not exactly the same but share one event in this library.
| requestAdAsync(options) | (async) requests a rewarded ad. An optional options object argument may specify servePersonalizedAds: true value — then ad will be personalized. |
| showAdAsync() | (async) shows a rewarded if it is ready (async) |