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-beta.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
4
Created
Source

react-native-admob npm version

⚠️ Please note, the master branch tracks development of version 2 of this library, which is currently in alpha. For version 1 please check out the 1.x branch.

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 react-native-admob
  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]}
  onAdFailedToLoad={error => console.error(error)}
/>

// Display a DFP Publisher banner
<PublisherBanner
  adSize="fullBanner"
  adUnitID="your-admob-unit-id"
  testDevices={[PublisherBanner.simulatorId]}
  onAdFailedToLoad={error => console.error(error)}
  onAppEvent={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

Properties
adSize

Corresponding to iOS framework banner size constants

ValueDescriptionAvailabilitySize (WxH)
bannerStandard BannerPhones and Tablets320x50
largeBannerLarge BannerPhones and Tablets320x100
mediumRectangleIAB Medium RectanglePhones and Tablets300x250
fullBannerIAB Full-Size BannerTablets468x60
leaderboardIAB LeaderboardTablets728x90
smartBannerPortrait
smartBannerLandscape
Smart BannerPhones and TabletsScreen width x 32|50|90

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

onAdLoaded

Accepts a function. Called when an ad is received.

onAdFailedToLoad

Accepts a function. Called when an ad request failed.

onAdOpened

Accepts a function. Called when an ad opens an overlay that covers the screen.

onAdClosed

Accepts a function. Called when the user is about to return to the application after clicking on an ad.

onAdLeftApplication

Accepts a function. Called when a user click will open another app (such as the App Store), backgrounding the current app.

onSizeChange

Accepts a function. Called when the size of the banner changes. The function is called with an object containing the width and the height.

Above names correspond to the Ad lifecycle event callbacks

PublisherBanner

Properties

Same as AdMobBanner, with the addition of 2 extra properties:

onAppEvent

Accepts a function. Called when DFP sends an event back to the app.

These events may occur at any time during the ad's lifecycle, even before onAdLoaded is called. The function is called with an object, containing the name of the event and an info property, containing additional information.

More info here: https://developers.google.com/mobile-ads-sdk/docs/dfp/ios/banner#app_events

validAdSizes

An array of ad sizes which may be eligible to be served.

AdMobInterstitial

In comparison to the AdMobBanner and PublisherBanner which have a declaritive API, the AdMobInterstitial has an imperative API.

Methods
setAdUnitID(adUnitID)

Sets the AdUnit ID for all future ad requests.

setTestDevices(devices)

Sets the devices which are served test ads.

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

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 an ad is going to be shown, rejects when the ad is not ready to be shown.

isReady(callback)

Calls callback with a boolean value whether the interstitial is ready to be shown.

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.

This libraryiOSAndroid
adLoadedinterstitialDidReceiveAdonAdLoaded
adFailedToLoadinterstitial:didFailToReceiveAdWithErroronAdFailedToLoad
adOpenedinterstitialWillPresentScreenonAdOpened
adFailedToOpeninterstitialDidFailToPresentScreenNot supported
adClosedinterstitialWillDismissScreenonAdClosed
adLeftApplicationinterstitialWillLeaveApplicationonAdLeftApplication

AdMobRewarded

In comparison to the AdMobBanner and PublisherBanner which have a declaritive API, the AdMobRewarded has an imperative API, just like the AdMobInterstitial.

Methods
setAdUnitID(adUnitID)

Sets the AdUnit ID for all future ad requests.

setTestDevices(devices)

Sets the devices which are served test ads.

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

requestAd()

Requests a rewarded ad and returns a promise, which resolves on load and rejects on error.

showAd()

Shows a rewarded ad and returns a promise, which resolves when an ad is going to be shown, rejects when the ad is not ready to be shown.

isReady(callback)

Calls callback with a boolean value whether the rewarded ad is ready to be shown.

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.

This libraryiOSAndroid
adLoadedrewardBasedVideoAdDidReceiveAdonRewardedVideoAdLoaded
adFailedToLoadrewardBasedVideoAd:didFailToLoadWithErroronRewardedVideoAdFailedToLoad
rewardedrewardBasedVideoAd:didRewardUserWithRewardonRewarded
adOpenedrewardBasedVideoAdDidOpenonRewardedVideoAdOpened
videoStartedrewardBasedVideoAdDidStartPlayingonRewardedVideoStarted
adClosedrewardBasedVideoAdDidCloseonRewardedVideoAdClosed
adLeftApplicationrewardBasedVideoAdWillLeaveApplicationonRewardedVideoAdLeftApplication

TODO

FAQs

Package last updated on 03 Oct 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