npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-facebook-ads

v4.2.4

Published

Native Facebook Ads for React Native

Downloads

6

Readme

react-native-fbads npm version

Facebook Ads

Facebook Audience SDK integration for React Native, available on iOS and Android. Features native, interstitial and banner ads.

Table of Contents

Installation

1. Install Javascript packages

RN >= 0.40

Install JavaScript packages:

$ react-native install react-native-facebook-ads
RN < 0.40

Install JavaScript packages:

$ react-native install [email protected]
Note:

If you got error when building iOS project, you can try installing the library manually as the instruction here: https://facebook.github.io/react-native/docs/linking-libraries-ios.html. After installing the library, you have to remove the line pod 'ReactNativeFBAds', :path => '../node_modules/react-native-fbads' inside Podfile.

2. Configure native projects

The react-native-fbads has been automatically linked for you, the next step will be downloading and linking the native Facebook SDK for both platforms.

2.1 iOS

Make sure you have the latest Xcode installed. Open the .xcodeproj in Xcode found in the ios subfolder from your project's root directory. Now, follow all the steps in the Getting Started Guide for Facebook SDK for iOS. Along with FBSDKCoreKit.framework, don't forget to import FBAudienceNetwork.framework.

Next, follow steps 1 and 3 from the Getting Started Guide for Facebook Audience. Once you have created the placement id, write it down and continue to next section.

2.2. Android

If you are using react-native-fbsdk you can follow their installation instructions. Otherwise, please follow official Getting Started Guide for Facebook SDK.

Usage

For detailed usage please check examples folder.

Interstitial Ads

Interstitial Ad is a type of an ad that displays full screen with media content. It has a dismiss button as well as the clickable area that takes user outside of your app.

They are displayed over your root view with a single, imperative call.

1. Showing ad

In order to show an ad, you have to import InterstitialAdManager and call showAd on it supplying it a placementId identifier, as in the below example:

import { InterstitialAdManager } from 'react-native-fbads';

InterstitialAdManager.showAd(placementId)
  .then(didClick => {})
  .catch(error => {})

Method returns a promise that will be rejected when an error occurs during a call (e.g. no fill from ad server or network error) and resolve when user either dimisses or interacts with the displayed ad.

Native Ads

Native Ad is a type of an ad that matches the form and function of your React Native interface.

1. Creating AdsManager

In order to start rendering your custom native ads within your app, you have to construct a NativeAdManager that is responsible for caching and fetching ads as you request them.

import { NativeAdsManager } from 'react-native-fbads';

const adsManager = new NativeAdsManager(placementId, numberOfAdsToRequest);

The constructor accepts two parameters:

  • placementId - which is an unique identifier describing your ad units,
  • numberOfAdsToRequest - which is a number of ads to request by ads manager at a time

2. Making ad component

After creating adsManager instance, next step is to wrap an arbitrary component that you want to use for rendering your custom advertises with a withNativeAd wrapper.

It's a higher order component that passes nativeAd via props to a wrapped component allowing you to actually render an ad!

class AdComponent extends React.Component {
  render() {
    return (
      <View>
        <Text>{this.props.nativeAd.description}</Text>
      </View>
    );
  }
}

export default withNativeAd(AdComponent);

For full details on the nativeAd object, please check flowtype definitions here

3. Rendering an ad

Finally, you can render your wrapped component from previous step and pass it adsManager of your choice.

class MainApp extends React.Component {
  render() {
    return (
      <View>
        <AdComponent adsManager={adsManager} />
      </View>
    );
  }
}

BannerView

BannerView is a component that allows you to display native banners (know as AdView).

Banners are available in 3 sizes:

  • standard (BANNER_HEIGHT_50)
  • large (BANNER_HEIGHT_90)
  • rectangle (RECTANGLE_HEIGHT_250)

1. Showing ad

In order to show an ad, you have to first import it BannerView from the package:

import { BannerView } from 'react-native-fbads';

Later in your app, you can render it like below:

function ViewWithBanner(props) {
  return (
    <View>
      <BannerView
        placementId="YOUR_BANNER_PLACEMENT_ID"
        type="standard"
        onPress={() => console.log('click')}
        onError={(err) => console.log('error', err)}
      />
    </View>
  );
}

API

NativeAdsManager

Provides a mechanism to fetch a set of ads and then use them within your application. The native ads manager supports giving out as many ads as needed by cloning over the set of ads it got back from the server which can be useful for feed scenarios. It's a wrapper for FBNativeAdsManager

disableAutoRefresh

By default the native ads manager will refresh its ads periodically. This does not mean that any ads which are shown in the application's UI will be refreshed but simply that requesting next native ads to render may return new ads at different times. This method disables that functionality.

adsManager.disableAutoRefresh();

setMediaCachePolicy

Sets the native ads manager caching policy. This controls which media from the native ads are cached before being displayed. The default is to not block on caching.

adsManager.setMediaCachePolicy('none' | 'icon' | 'image' | 'all');

Note: This method is a noop on Android

InterstitialAdManager

import { InterstitialAdManager } from 'react-native-fbads';

InterstitialAdManager is a manager that allows you to display interstitial ads within your app with a single call.

showAd

Loads an interstitial ad asynchronously and shows it full screen by attaching a view onto the current root view controller.

InterstitialAdManager.showAd('placementId')
  .then(...)
  .catch(...);

Promise will be rejected when there's an error loading ads from Facebook Audience network. It will resolve with a boolean indicating whether user didClick an ad or not.

On Android you have to add following activity to AndroidManifest.xml

<activity
  android:name="com.facebook.ads.InterstitialAdActivity"
  android:configChanges="keyboardHidden|orientation" />

Note: There can be only one showAd call being performed at a time. Otherwise, an error will be thrown.

AdSettings

import { AdSettings } from 'react-native-fbads';

AdSettings contains global settings for all ad controls.

currentDeviceHash

Constant which contains current device's hash id.

addTestDevice

Registers given device to receive test ads. When you run app on simulator, it should automatically get added. Use it to receive test ads in development mode on a standalone phone. Hash of the current device can be obtained from a debug log or AdSettings.currentDeviceHash constant.

All devices should be specified before any other action takes place, like AdsManager gets created.

AdSettings.addTestDevice('hash');

clearTestDevices

Clears all previously set test devices. If you want your ads to respect newly set config, you'll have to destroy and create an instance of AdsManager once again.

AdSettings.clearTestDevices();

setLogLevel

Sets current SDK log level.

AdSettings.setLogLevel('none' | 'debug' | 'verbose' | 'warning' | 'error' | 'notification');

Note: This method is a noop on Android.

setIsChildDirected

Configures the ad control for treatment as child-directed.

AdSettings.setIsChildDirected(true | false);

setMediationService

If an ad provided service is mediating Audience Network in their sdk, it is required to set the name of the mediation service

AdSettings.setMediationService('foobar');

setUrlPrefix

Sets the url prefix to use when making ad requests.

AdSettings.setUrlPrefix('...');

Note: This method should never be used in production

Running example

In order to see ads you will have to create your own placementId and use it instead of the one provided in the examples. This is our internal set up that doesn't work for any developers outside of Callstack.io organisation. This is because of Facebook not showing test ads to outside collaborators in the development mode.

1. Install dependencies

$ npm install

2. Start packager

Because of the way example project is set up (custom packager arguments), you'll have to start it explicitly before any other command

$ cd ./example && npm start

3. Run it on iOS / Android

$ cd ./example && npm run ios
$ cd ./example && npm run android

Credits

Some of the API explanations were borrowed from Facebook SDK documentation.