Monetize Your React Native & Expo Apps With Google Admob

Banner Ads, Interstitial Ads, Reward Ads, and More… for Android & iOS applications

Monetization in react native and expo

Project setup, Package Requirements, and Code

  • Start your expo project
  • If you visit the official documentation of Expo Admob, you will see that they clearly state that the Expo package for AdMob is deprecated for Expo SDK version 46 and above. Therefore, I am going to use the React Native Google Mobile Ads package.
  • Install the AdMob package
# Install the admob module
npm install react-native-google-mobile-ads
  • If you are an iOS user, you have to add this plugin to your app.json file. However, I am testing AdMob on an Android device, so I don’t need that.
{
"expo": {
"plugins": [
[
"expo-build-properties",
{
"ios": {
"useFrameworks": "static"
}
}
]
]
}
}
  • Setup your Google Admob account
  • We will use the Ad Unit Id from the Ad settings.
  • Now, If you are an expo user then you have to add the following line of react-native-google-mobile-ads block
// <project-root>/app.json
{
"expo": {
// ...
},
"react-native-google-mobile-ads": {
"android_app_id": "ca-app-pub-xxxxxxxx~xxxxxxxx",
"ios_app_id": "ca-app-pub-xxxxxxxx~xxxxxxxx"
}
}
  • Replace the default Android and iOS app ids with the real Google AdMob Ad unit.
  • In this instance, I am using native code in an Expo application. However, Expo Go does not support native code, so you will not be able to test this application on Expo Go. Don’t worry, though — Expo has a solution. You can use either the Prebuild or Development build method to test apps that use native code. The Prebuild method uses Android Studio or Xcode, depending on which operating system you are using. That’s why I prefer to work with the Development build method.
  • Build your expo app using the Development build method. You can refer to this video for step by step guide.
  • Sometimes the Development build fails due to the internal server issue. You just have to retry the command

Banner Ad

Add the Banner component in the return section of the Add section of your code.

return (
<BannerAd
unitId={adUnitId}
size={BannerAdSize.FULL_BANNER}
requestOptions={{
requestNonPersonalizedAdsOnly: true,
}}
/>
);

Interstitial Ads

import React, { useEffect, useState } from 'react';
import { Button } from 'react-native';
import { InterstitialAd, AdEventType, TestIds } from 'react-native-google-mobile-ads';

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

const interstitial = InterstitialAd.createForAdRequest(adUnitId, {
requestNonPersonalizedAdsOnly: true,
keywords: ['fashion', 'clothing'],
});

function App() {
const [loaded, setLoaded] = useState(false);

useEffect(() => {
const unsubscribe = interstitial.addAdEventListener(AdEventType.LOADED, () => {
setLoaded(true);
});

// Start loading the interstitial straight away
interstitial.load();

// Unsubscribe from events on unmount
return unsubscribe;
}, []);

// No advert ready to show yet
if (!loaded) {
return null;
}

return (
<Button
title="Show Interstitial"
onPress={() => {
interstitial.show();
}}
/>
);
}

Rewards Ad

import React, { useEffect, useState } from 'react';
import { Button } from 'react-native';
import { RewardedAd, RewardedAdEventType, TestIds } from 'react-native-google-mobile-ads';

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

const rewarded = RewardedAd.createForAdRequest(adUnitId, {
requestNonPersonalizedAdsOnly: true,
keywords: ['fashion', 'clothing'],
});

function App() {
const [loaded, setLoaded] = useState(false);

useEffect(() => {
const unsubscribeLoaded = rewarded.addAdEventListener(RewardedAdEventType.LOADED, () => {
setLoaded(true);
});
const unsubscribeEarned = rewarded.addAdEventListener(
RewardedAdEventType.EARNED_REWARD,
reward => {
console.log('User earned reward of ', reward);
},
);

// Start loading the rewarded ad straight away
rewarded.load();

// Unsubscribe from events on unmount
return () => {
unsubscribeLoaded();
unsubscribeEarned();
};
}, []);

// No advert ready to show yet
if (!loaded) {
return null;
}

return (
<Button
title="Show Rewarded Ad"
onPress={() => {
rewarded.show();
}}
/>
);
}
  • If you are confused or lost, I would recommend watching a step-by-step video tutorial. Few minutes of setup and you will be ready to test the application.

Why do I suggest watching more videos?

I am from India and I love writing code and sharing it with the community. We all know that money is the ultimate motivation for work, and the Medium payment system uses Stripe, which has been in preview mode in India for the past 4–5 years. I waited a year on the Medium platform, but in the end, nothing worked out.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store