React Native Google Mobile Ads
repository·main·Indexed 21 days ago
https://github.com/invertase/react-native-google-mobile-adsA React Native wrapper for the Google Mobile Ads SDK (version 16.4.0) that enables AdMob monetization for iOS and Android apps. It provides full TypeScript support and supports multiple ad formats, including App open, Banner (anchored adaptive, inline adaptive, collapsible, and fixed size), Native, Interstitial, Rewarded, and Rewarded Interstitial ads. The library is compatible with both the Old Architecture and the React Native New Architecture.
What's inside react-native-google-mobile-ads
- React Native Google Mobile Ads is a React Native wrapper around the native Google-Mobile-Ads SDKs for both iOS and Android. It allows developers to monetize their applications using AdMob. The library is designed with three core principles: extensive testing (>95% coverage), first-class TypeScript support, and comprehensive documentation.
Supported Ad Formats in react-native-google-mobile-ads
mainThe
react-native-google-mobile-adsmodule provides integration for six different types of advertisements. Once you have completed the initial setup, you can implement the following formats:- App Open Ads: Ads that appear when a user opens or switches back to the app.
- Interstitial Ads: Full-screen ads that cover the interface of the app.
- Rewarded Ads: Ads that reward users with in-app benefits after watching.
- Banner Ads: Small display ads that occupy a portion of the screen.
- Rewarded Interstitial Ads: A hybrid format that combines the full-screen nature of interstitials with the reward mechanism of rewarded ads.
- Native Ads: Ads that can be customized to match the look and feel of your app's UI.
Supported Ad formats
mainThe module supports six distinct types of advertisements:
- App Open Ads: Full-screen ads shown when an app is opened or brought to the foreground.
- Banner Ads: Component-based ads displayed within the UI layout.
- Native Ads: Component-based ads that can be styled to match the app's native look and feel.
- Interstitial Ads: Full-screen ads that interrupt the user flow (e.g., between levels in a game).
- Rewarded Ads: Full-screen ads that provide users with an in-app reward after watching.
- Rewarded Interstitial Ads: A hybrid format that combines the properties of interstitial and rewarded ads.
Understand Ad serving modes based on consent
mainGoogle ads are categorized into several modes depending on user consent:
- Personalized ads
- Non-personalized ads
- Limited ads
- Technical ads
It is critical to understand how these modes affect ad delivery. The
AdsConsenthelper provides the tools to request consent, but the developer is responsible for ensuring the resulting consent status is correctly handled and reflected throughout the application logic.Manage ad instances via adUnitId
mainThe
adUnitidparameter passed to the hook controls the lifecycle of the ad instance:- Setting/Changing ID: If the
adUnitidis changed, a new ad instance is created and the previous one is destroyed. - Setting to
null: IfadUnitidis set tonull, no ad instance is created and any existing instance is destroyed.
- Setting/Changing ID: If the
Supported Ad Formats
mainThe library supports several Google AdMob ad formats:
- App open: Ads intended for app load screens.
- Banner: Rectangular ads that can be anchored (top/bottom) or inline with content. Supported types include:
- Anchored adaptive: Full-width, auto-height, and always on-screen.
- Inline adaptive: Larger, taller banners intended for scrolling content.
- Collapsible: Improved performance for anchored ads.
- Fixed size (legacy): Specific dimensions like Banner (320x50), Large banner (320x100), Medium rectangle (300x250), full banner (468x60), and leaderboard (728x90).
- Native: Customizable ads that match the app's design and layout.
- Interstitial: Full-page ads placed at natural app transitions.
- Rewarded: Incentivized ads where users watch videos, playables, or surveys to earn in-app items.
- Rewarded Interstitial: Incentivized ads that appear automatically during natural transitions without requiring explicit user opt-in.
Use Ad Inspector for real-time ad analysis
mainAd Inspector is an in-app overlay for authorized devices that allows real-time analysis of test ad requests. It is used to:
- Inspect ad units
- Verify if ads are filling as expected
- Identify and resolve errors
- View Open Bidding and mediation details per request
- Test individual third-party Open Bidding or waterfall mediation ad sources
- Verify configuration for Open Bidding and waterfall mediation
Handle App Open Ad expiration and cold starts
mainWhen implementing App Open Ads, keep these constraints in mind:
- Ad Expiration: Ad references in the app open beta will time out after four hours. Ads rendered more than four hours after the request time may be invalid and won't earn revenue.
- Cold Starts: On a cold start (when the app is launched but not previously in memory), you won't have a preloaded ad ready. To avoid a bad user experience where an ad appears unexpectedly after the user has already started interacting with the app, use a loading screen to load app assets and only show the ad from that loading screen. If the app finishes loading and reaches main content before the ad is ready, do not show the ad.
Implement full-screen ad flow with hooks
mainTo display an ad, you typically follow this lifecycle:
- Initialize: Call the hook with an Ad Unit ID.
- Load: Call the
load()function (often inside auseEffect) to fetch the ad from the network. - Show: When a user triggers an action, check
isLoaded. Iftrue, callshow(). Iffalse, proceed with your fallback logic (e.g., navigating to the next screen). - Handle Closure: Listen to the
isClosedstate to perform actions after the user dismisses the ad.
import { useInterstitialAd, TestIds } from 'react-native-google-mobile-ads'; export default function App({ navigation }) { const { isLoaded, isClosed, load, show } = useInterstitialAd(TestIds.INTERSTITIAL); useEffect(() => { // Start loading the interstitial straight away load(); }, [load]); useEffect(() => { if (isClosed) { // Action after the ad is closed navigation.navigate('NextScreen'); } }, [isClosed, navigation]); return ( <View> <Button title="Navigate to next screen" onPress={() => { if (isLoaded) { show(); } else { // No advert ready to show yet navigation.navigate('NextScreen'); } }} /> </View> ); }Migrate AdsConsent to the User Messaging Platform (UMP) SDK
mainIn version 5 and later,
react-native-google-mobile-adstransitioned from the old Consent SDK to Google's User Messaging Platform (UMP) SDK. This change aligns with IAB standards and Apple's App Tracking Transparency (ATT) requirements.Key Changes in the AdsConsent API:
requestInfoUpdate(): Now accepts an optionalAdsConsentInfoOptionsobject instead of a list ofpublisherIds. It returns a newAdsConsentInfointerface.showForm(): No longer accepts parameters. It returns anAdsConsentFormResultinterface containing the newAdsConsentStatus.- Removed Methods:
getAdProviders,getStatus, andsetStatushave been removed. - Removed Testing/Debug Methods:
addTestDevices,setDebugGeography, andsetTagForUnderAgeOfConsentare removed. Their functionality is now handled via theAdsConsentInfoOptionsobject passed torequestInfoUpdate(). - New Method:
getUserChoices()can be used to inspect specific consent choices made by the user.
Important Note on Consent Status
Unlike the old SDK, the new
AdsConsentStatusprimarily indicates whether you should show the consent modal to a user, rather than providing detailed user preference information directly through the status enum.Install react-native-google-mobile-ads
mainInstall the module based on your project type:
React Native (Bare):
npm install react-native-google-mobile-adsExpo:
npx expo install react-native-google-mobile-adsNote for Android: Before releasing, you must select "Yes, my app contains ads" in the Google Play Console under "Policy and programmes" > "App content" > "Manage".
npm install react-native-google-mobile-ads # or npx expo install react-native-google-mobile-adsConfigure Server Side Verification (SSV) for Rewarded Ads
mainWhile the
EARNED_REWARDevent is triggered on the client, you can use Server Side Verification (SSV) to confirm rewards on your backend. This prevents client-side tampering.To use SSV:
- Configure your SSV callback URL in the Google AdMob dashboard.
- When creating the ad request, pass
userIdandcustomDatavia theserverSideVerificationOptionsfield in theRequestOptionsobject. - AdMob will call your server with these parameters, along with reward details (amount, item, etc.) and a signature for verification.
Note: You must verify the incoming requests on your server to ensure they are genuine.
const rewardedAd = RewardedAd.createForAdRequest(adUnitId, { serverSideVerificationOptions: { userId: '9999', customData: 'my-custom-data', }, });