Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sbugert/react-native-admob

A react-native component for Google AdMob banners
https://github.com/sbugert/react-native-admob

Last synced: about 8 hours ago
JSON representation

A react-native component for Google AdMob banners

Awesome Lists containing this project

README

        

# react-native-admob [![npm](https://img.shields.io/npm/v/react-native-admob.svg)](https://www.npmjs.com/package/react-native-admob) [![npm (next)](https://img.shields.io/npm/v/react-native-admob/next.svg)](https://www.npmjs.com/package/react-native-admob)

### ⚠️ Please note, the master branch tracks development of version 2 of this library, which is currently in beta. For version 1 please check out the [1.x branch](https://github.com/sbugert/react-native-admob/tree/1.x).

A react-native module for Google AdMob Banners, Interstitials, Rewarded Videos and also DFP Banners.

The banner types are implemented as components while the interstitial and rewarded video have an imperative API.

## Installation

You can use npm or Yarn to install the latest beta version:

**npm:**

npm i --save react-native-admob@next

**Yarn:**

yarn add react-native-admob@next

In order to use this library, you have to link it to your project first. There's excellent documentation on how to do this in the [React Native Docs](https://facebook.github.io/react-native/docs/linking-libraries-ios.html#content).

Alternatively for iOS you can install the library with CocoaPods by adding a line to your `Podfile`;

pod 'react-native-admob', path: '../node_modules/react-native-admob'

### iOS

For iOS you will have to add the [Google Mobile Ads SDK](https://developers.google.com/admob/ios/quick-start#import_the_mobile_ads_sdk) to your Xcode project.

### Android

On Android the AdMob library code is part of Play Services, which is automatically added when this library is linked.

But you still have to manually update your `AndroidManifest.xml`, as described in the [Google Mobile Ads SDK documentation](https://developers.google.com/admob/android/quick-start#update_your_androidmanifestxml).

## Usage

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

// Display a banner
console.error(error)}
/>

// Display a DFP Publisher banner
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](Example).

## Reference

### AdMobBanner

#### Properties

##### `adSize`

_Corresponding to [iOS framework banner size constants](https://developers.google.com/admob/ios/banner)_



Value
Description
Availability
Size (WxH)




banner
Standard Banner
Phones and Tablets
320x50


largeBanner
Large Banner
Phones and Tablets
320x100


mediumRectangle
IAB Medium Rectangle
Phones and Tablets
300x250


fullBanner
IAB Full-Size Banner
Tablets
468x60


leaderboard
IAB Leaderboard
Tablets
728x90



smartBannerPortrait

smartBannerLandscape

Smart Banner
Phones and Tablets
Screen 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](https://developers.google.com/admob/android/banner#ad_events)_

### 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 library
iOS
Android




adLoaded
interstitialDidReceiveAd
onAdLoaded


adFailedToLoad
interstitial:didFailToReceiveAdWithError
onAdFailedToLoad


adOpened
interstitialWillPresentScreen
onAdOpened


adFailedToOpen
interstitialDidFailToPresentScreen
Not supported


adClosed
interstitialWillDismissScreen
onAdClosed


adLeftApplication
interstitialWillLeaveApplication
onAdLeftApplication

### 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 library
iOS
Android




adLoaded
rewardBasedVideoAdDidReceiveAd
onRewardedVideoAdLoaded


adFailedToLoad
rewardBasedVideoAd:didFailToLoadWithError
onRewardedVideoAdFailedToLoad


rewarded
rewardBasedVideoAd:didRewardUserWithReward
onRewarded


adOpened
rewardBasedVideoAdDidOpen
onRewardedVideoAdOpened


videoStarted
rewardBasedVideoAdDidStartPlaying
onRewardedVideoStarted


videoCompleted
rewardBasedVideoAdDidCompletePlaying
rewardedVideoAdVideoCompleted


adClosed
rewardBasedVideoAdDidClose
onRewardedVideoAdClosed


adLeftApplication
rewardBasedVideoAdWillLeaveApplication
onRewardedVideoAdLeftApplication

---

## TODO

- [ ] Support [Ad Targeting](https://developers.google.com/admob/ios/targeting) (RFC: [#166](https://github.com/sbugert/react-native-admob/pull/166))