Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-admob

Package Overview
Dependencies
Maintainers
4
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-admob

A react-native component for Google AdMob banners and interstitials

  • 2.0.0-alpha.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
79
increased by29.51%
Maintainers
4
Weekly downloads
 
Created
Source

npm version

react-native-admob

A react-native module for Google AdMob GADBanner, DFPBanner and GADInterstitial (react-native v0.19.0 or newer required).

The banner is implemented as a component while the interstitial has an imperative API.

Installation

  1. npm i react-native-admob -S
  2. react-native link
  3. Add the Google Mobile Ads SDK to your Xcode project with CocoaPods or manually. This is only needed for iOS and guarantees your app is using the newest AdMob version.
Manual Installation
iOS
  1. npm i react-native-admob -S
  2. Add the Google Mobile Ads SDK to your Xcode project with CocoaPods or manually. This is only needed for iOS and guarantees your app is using the newest AdMob version.
  3. Add react-native-admob static library to your Xcode project like explained here. (Step 3 of this guide is not needed)
Android
  1. npm i react-native-admob -S
  2. Make the following additions to the given files:

android/settings.gradle

include ':react-native-admob'
project(':react-native-admob').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-admob/android')

android/app/build.gradle

dependencies {
   // ...
   compile project(':react-native-admob')
}

MainApplication.java

In MainAplication.java on top, where imports are:

import com.sbugert.rnadmob.RNAdMobPackage;

Under protected List<ReactPackage> getPackages() {:

  return Arrays.<ReactPackage>asList(
    new MainReactPackage(),
    new RNAdMobPackage()
  );

Usage

import {
  AdMobBanner,
  AdMobInterstitial,
  PublisherBanner,
  AdMobRewarded,
} from 'react-native-admob'

// Display a banner
<AdMobBanner
  adSize="fullBanner"
  adUnitID="your-admob-unit-id"
  testDevices={[AdMobBanner.simulatorId]}
  onDidFailToReceiveAdWithError={error => console.error(error)}
/>

// Display a DFP Publisher banner
<PublisherBanner
  adSize="fullBanner"
  adUnitID="your-admob-unit-id"
  testDevices={[PublisherBanner.simulatorId]}
  onDidFailToReceiveAdWithError={error => console.error(error)}
  onAdmobDispatchAppEvent={event => console.log(event.name, event.info)}
/>

// Display an interstitial
AdMobInterstitial.setAdUnitID('your-admob-unit-id');
AdMobInterstitial.setTestDevices([AdMobInterstitial.simulatorId]);
AdMobInterstitial.requestAd().then(() => AdMobInterstitial.showAd());

// Display a rewarded ad
AdMobRewarded.setAdUnitID('your-admob-unit-id');
AdMobRewarded.requestAd().then(() => AdMobRewarded.showAd());

For a full example reference to the example project.

Reference

AdMobBanner
adSize property

Corresponding to iOS framework banner size constants

Prop valueDescriptionSize
bannerStandard Banner for Phones and Tablets320x50
largeBannerLarge Banner for Phones and Tablets320x100
mediumRectangleIAB Medium Rectangle for Phones and Tablets300x250
fullBannerIAB Full-Size Banner for Tablet468x60
leaderboardIAB Leaderboard for Tablets728x90
smartBannerPortraitSmart Banner for Phones and Tablets (default)Screen width x 32
smartBannerLandscapeSmart Banner for Phones and TabletsScreen width x 32

Note: There is no smartBannerPortrait and smartBannerLandscape on Android. Both prop values will map to smartBanner

Events as function props

Corresponding to Ad lifecycle event callbacks

Prop
onAdViewDidReceiveAd()
onDidFailToReceiveAdWithError(error)
onAdViewWillPresentScreen()
onAdViewWillDismissScreen()
onAdViewDidDismissScreen()
onAdViewWillLeaveApplication()
PublisherBanner

Same as AdMobBanner, except it has an extra event prop:

| Prop | | |onAdmobDispatchAppEvent(event) | Event has a name and info property |

This handles App events that AdMob/DFP can send back to the app. More info here: https://developers.google.com/mobile-ads-sdk/docs/dfp/ios/banner#app_events

And also has an additional validAdSizes property, which accepts an array of ad sizes which may be eligible to be served.

AdMobInterstitial
Methods
NameDescription
setAdUnitID(adUnitID)sets the AdUnit ID for all future ad requests.
setTestDevices(devices)sets the devices which are served test ads
requestAd()requests an interstitial and returns a promise, which resolves on load and rejects on error
showAd()shows an interstitial and returns a promise, which resolves when ready and otherwise rejects
isReady(callback)calls callback with boolean whether interstitial is ready to be shown

For simulators/emulators you can use AdMobInterstitial.simulatorId for the test device ID.

Events

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.

iOSthis libraryAndroid
interstitialDidReceiveAdinterstitialDidLoadonAdLoaded
interstitial:didFailToReceiveAdWithErrorinterstitialDidFailToLoadonAdFailedToLoad
interstitialWillPresentScreeninterstitialDidOpenonAdOpened
interstitialDidFailToPresentScreen
interstitialWillDismissScreen
interstitialDidDismissScreeninterstitialDidCloseonAdClosed
interstitialWillLeaveApplicationinterstitialWillLeaveApplicationonAdLeftApplication

Note that interstitialWillLeaveApplication and onAdLeftApplication are not exactly the same but share one event in this library.

AdMobRewarded

Opens a rewarded AdMob ad.

Methods
NameDescription
setAdUnitID(adUnitID)sets the AdUnit ID for all future ad requests
setTestDevices(devices)sets the devices which are served test ads
requestAd()requests an rewarded ad and returns a promise, which resolves on load and rejects on error
showAd()shows an rewarded ad and returns a promise, which resolves when ready and otherwise rejects
isReady(callback)calls callback with boolean whether interstitial is ready to be shown
Events
iOSthis libraryAndroid
rewardBasedVideoAd:didRewardUserWithRewardrewardedVideoDidRewardUseronRewarded
rewardBasedVideoAdDidReceiveAdrewardedVideoDidLoadonRewardedVideoAdLoaded
rewardBasedVideoAd:didFailToLoadWithErrorrewardedVideoDidFailToLoadonRewardedVideoAdFailedToLoad
rewardBasedVideoAdDidOpenrewardedVideoDidOpenonRewardedVideoAdOpened
rewardBasedVideoAdDidCloserewardedVideoDidCloseonRewardedVideoAdClosed
rewardBasedVideoAdWillLeaveApplicationrewardedVideoWillLeaveApplicationonRewardedVideoAdLeftApplication

TODO

  • Support Ad Targeting
  • Also use interstitial event names for banner

FAQs

Package last updated on 10 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc