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.
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.
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 ..
1. Now select Android, and iOS apps one by one, and under the “App settings” menu item, you can find the “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 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;
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 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 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 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 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",
},
});
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 !!
The team at Mindbowser was highly professional, patient, and collaborative throughout our engagement. They struck the right balance between offering guidance and taking direction, which made the development process smooth. Although our project wasn’t related to healthcare, we clearly benefited...
Founder, Texas Ranch Security
Mindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork