A Comprehensive Guide to React Native Google Mobile Ads

In today’s mobile app development, monetization plays a critical role, and one of the most popular ways to monetize apps is through ads. Google Mobile Ads SDK offers a simple yet powerful solution to display ads in your React Native App. In this guide, we’ll walk through how to integrate the react-native-google-mobile-ads library into your project.

What are React Native Google Mobile Ads?

React Native library that provides a simple interface for integrating Google Mobile Ads into your mobile applications. It supports various ad formats, including banners, interstitials, and rewarded videos. By using this library, you can easily monetize your app and generate revenue.

Install Dependencies

To get started, you need to install the react-native-google-mobile-ads package along with its peer dependencies. Open your terminal and run the following command:

rn add react-native-google-mobile-ads

After the installation, link the native dependencies:

cd ios && pod install && cd ..

Setup Google AdMob Account

Create a Google AdMob Account

  • Visit AdMob: Go to the Google AdMob website.
  • Sign In: Log in to your Google account.
  • Create a New Account: If you don’t have an AdMob account, follow the prompts to create one.
  • Set Up Your Account: Provide necessary information about your business or personal account.

Create an Apps

  1. Click on Apps in the left menu of the dashboard where you can find the option to add a new app or create a new app.
  2. For setting up an app you need to select the platform and also need to select whether your app is available on any valid app store or not.

AdMob-SetupApp

AdMob-AddApp

Get and Set an App ID for the App

1. Now select Android, and iOS apps one by one, and under the “App settings” menu item, you can find the “App ID”:

app-id

2. In the app.json file, you need to add app IDs based on the platform as given below:

"react-native-google-mobile-ads": {
  "android_app_id": "ca-app-pub-3876927400784502~1784948553",
  "ios_app_id": "ca-app-pub-3876927400784502~5783903315"
},

Initialize the Google Mobile Ad SDK

Initialize the Google Mobile Ads SDK once at the app launch using the initialize method:

import { StyleSheet, Text, View } from "react-native";
import React, { useEffect } from "react";
import mobileAds from "react-native-google-mobile-ads";

import AppNavigator from "./navigation";

const App = () => {
  useEffect(() => {
    mobileAds()
     .initialize()
     .then((adapterStatuses) => {
       console.log("adapterStatuses", adapterStatuses);
     });
  }, []);

  return <AppNavigator />;
};

export default App;

Learn How to Integrate Google Mobile Ads into Your React Native App Now!

Displaying Ads in React Native

The Google Mobile Ads package supports four main ad types: App open, Interstitial, Rewarded, and Banner. We use different hooks provided by the SDK for each ad type. These hooks return various states and functions to control the ads, enabling effective management of their display and behavior.

Note: For testing different types of ads, you can use a Test ID. However, for production, you should use the actual ID from the Google AdMob dashboard found under “Ad units.”

App Open Ads

App open ads are designed for monetizing app load screens and can be shown when users launch or bring your app to the foreground. Since these ads can be closed at any time, it’s important to preload them so they’re ready to display when needed, ensuring they can be shown immediately when the app is opened.

For example, I’m displaying an ad on the splash screen using App open ads. We’re utilizing the useAppOpenAd hook provided by the SDK to handle the display of these ads.

import { StyleSheet, Text, View } from "react-native";
import React, { useEffect } from "react";
import { useAppOpenAd, TestIds } from "react-native-google-mobile-ads";
import { NavigationService } from "./NavigationService";

const Splash = () => {
  const { isLoaded, isOpened, isShowing, show, load, isClosed } = useAppOpenAd(
   TestIds.APP_OPEN
  );
 
  useEffect(() => {
    load();
  }, [load]);

  useEffect(() => {
    if (isLoaded) {
      show();
    }
  }, [isLoaded]);

  useEffect(() => {
    if (isClosed) {
      NavigationService.replace("Main");
    }
  }, [isClosed]);

  return (
    <View style={styles.container}>
      <Text>{"Splash screen"}</Text>
    </View>
  );
};

export default Splash;

const styles = StyleSheet.create({
 container: {
  flex: 1,
  justifyContent: "center",
  alignItems: "center",
 },
});

Interstitial Ads

Interstitial ads are full-screen ads that cover the app’s interface until dismissed by the user, typically shown at natural transition points in the app. For example, they can appear between game levels, after completing a task like submitting a form or making a purchase, or before/after watching a video. These ads are programmatically loaded and can be preloaded in the background to ensure they’re ready when needed.

For example, I’m displaying interstitial ads when a button is clicked. We’re using the useInterstitialAd hook provided by the SDK to manage these ads.

import { Button, StyleSheet, View } from "react-native";
import React, { useEffect } from "react";
import { TestIds, useInterstitialAd } from "react-native-google-mobile-ads";
import { NavigationService } from "./NavigationService";

const Interstitial = () => {
  const { isLoaded, isOpened, isShowing, show, load, isClosed } =
    useInterstitialAd(TestIds.INTERSTITIAL, {
      keywords: ["fashion", "clothing"],
    });

useEffect(() => {
  load();
}, [load]);

useEffect(() => {
  if (isClosed) {
    alert("Game will start soon....");
    NavigationService.goBack();
  }
}, [isClosed]);

return (
  <View style={styles.container}>
    <Button
       title="Start Game (with ad)"
       onPress={() => {
         if (isLoaded) {show();}
       }}
    />
  </View>
 );
};

export default Interstitial;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
text: {
    color: "black",
    fontSize: 18,
    fontWeight: "bold",
  },
});

Rewarded Ads

Rewarded ads offer users rewards, like in-game currency or premium content, in exchange for engaging with a video or interactive ad. For example, in a game, a rewarded ad might allow users to earn extra lives or in-game currency by watching a video. These full-screen ads cover the app’s interface until dismissed and are managed through the Google AdMob dashboard. The reward is given only after users complete the required action, such as watching the ad or interacting with it.

For example, I’m displaying rewarded ads when a button is clicked, and then showing a message to the user to inform them that they have earned a reward.

import React, { useEffect, useState } from "react";
import { Button, StyleSheet, Text, View } from "react-native";
import { TestIds, useRewardedAd } from "react-native-google-mobile-ads";

function Rewarded() {
  const [rewardEarned, setRewardEarned] = useState(false);
  const {
      isLoaded,
      isOpened,
      isShowing,
      show,
      load,
      isClosed,
      isEarnedReward,
      error,
      reward,
   } = useRewardedAd(TestIds.REWARDED, {
      keywords: ["fashion", "clothing"],
   });

   useEffect(() => {
      load();
   },  [load]);

   useEffect(() => {
      if (isClosed) {
        load();
      }
   },  [isClosed]);

   useEffect(() => {
      if (error) {
        load();
      }
   },  [error]);

   useEffect(() => {
      if (isEarnedReward && reward) {
        setRewardEarned(true);
      }
   }, [isEarnedReward]);

   const showRewardedAd = () => {
      if (isLoaded) {
        show();
      } else {
        load();
      }
   };

   if (!isLoaded) {
     return null;
   }
   return (
     <View style={styles.container}>
       {rewardEarned ? (
         <Text style={styles.text}>
           Congratulations! You've earned a reward!
         </Text>
       ) : (
         <Button title="Watch Rewarded Ad" onPress={showRewardedAd} />
       )}
     </View>
   );
}

export default Rewarded;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
  text: {
    color: "black",
    fontSize: 18,
    fontWeight: "bold",
  },
});

Banner Ads

Banner ads are small, rectangular ads that appear at the top or bottom of an app’s screen, remaining visible without interrupting the user experience. They are ideal for non-intrusive areas and static screens, such as dashboards or home screens. For example, you might place a banner ad at the bottom of a game’s main menu, allowing users to view ads while interacting with the app without disruption. Unlike Interstitial or Rewarded Ads, banner ads are less intrusive, fitting seamlessly into the app’s layout.

For example, I’m displaying banner ads on the main routing screen, where they fit neatly into a small space without requiring additional room.

import { StyleSheet, Text, TouchableOpacity, View } from "react-native";
import React from "react";
import { useNavigation } from "@react-navigation/native";
import {
  BannerAd,
  BannerAdSize,
  TestIds,
}  from "react-native-google-mobile-ads";

const adUnitId = __DEV__? TestIds.BANNER : "ca-app-pub-xxxxxxxxxxxxx/yyyyyyyyyyyyyy";

interface ButtonProps {
  title: string;
  routeName: string;
}

const Main = () => {
  const navigation = useNavigation();

  const RenderButtons: React.FC<ButtonProps> = ({
    title,
    routeName,
  }): React.JSX.Element => {
    return (
      <TouchableOpacity
        style={styles.button}
        onPress={() => {
          navigation.navigate(routeName);
        }}
      >
          <Text style={styles.text}>{title}</Text>
        </TouchableOpacity>
    );
};

return (
  <View style={styles.container}>
    <View style={styles.buttonWrap}>
       <RenderButtons title={`"Interstitial Ads"`} routeName="Interstitial" />
       <RenderButtons title={`Show "Rewarded Ads"`} routeName="Rewarded" />
    </View>
    <BannerAd
       unitId={adUnitId}
       onAdClosed={() => console.log("closed")}
       size={BannerAdSize.BANNER}
    />
   </View>
  );
};

export default Main;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    paddingBottom: 30,
  },
 button: {
    backgroundColor: "blue",
    borderRadius: 10,
    paddingVertical: 15,
    marginVertical: 10,
    width: 250,
    alignItems: "center",
    justifyContent: "center",
 },
 text: {
    color: "white",
    fontSize: 16,
    fontWeight: "bold",
 },
 buttonWrap: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center",
 },
});
coma

Conclusion

In this blog, we covered Google Mobile Ads, how to use them with React Native, and how to set up an account. We discussed the different types of ads and the best times to use each one based on your needs. This integration helps you monetize your app and generate revenue.

Ads are a great way to make money from your app, and with this setup, you can efficiently use banners, interstitials, and rewarded ads. By understanding these concepts, you’ll be able to implement Google Mobile Ads effectively in your React Native project.

!! Enjoy, Keep, and Do Delicious Coding !!

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?