Integrating Facebook Audience Network with Mediation

This guide is intended for publishers who want to use the Google Mobile Ads SDK to load and display ads from the Facebook Audience Network via mediation. It covers how to add Facebook ads to an ad unit's mediation configuration, how to set up Automatic data collection, and how to integrate the Facebook Audience Network SDK and adapter into an Android app.

Supported ad formats and features

The Ad Manager mediation adapter for the Facebook Audience Network has the following capabilities:

Formats
Banners
Interstitials
Rewarded video
Rewarded Video (new APIs)
Native Ads - Custom Rendering
Features
Smart banners
Automatic Data Collection
Native Video

Requirements

  • Android SDK 4.0 (API level 14) or higher
  • Google Play services 17.2.0 or later

Step 1: Set up Facebook Audience Network

Sign up and Log in to the Business Manager Start page.

Click Create a new Business Manager account and fill out the required fields with your business details.

Create a Facebook property

Once you've filled out the required information, you'll be prompted to create a property for your app. Enter the desired name of the property for your app and click Go to Monetization Manager.

Next, select Android as the platform to monetize.

Indicate whether or not your app is live and click Submit.

You will be prompted to create an ad placement for your application. Select a format, fill out the form and click Next.

The next step presents an overview of the placement you created. Check the This app is using mediation checkbox and take note of the Placement ID, which will be used when setting up your AdMob Ad Unit ID.

In addition to the Placement ID, you'll also need the Facebook System User Access Token to set up your AdMob Ad Unit ID for mediation.

Create a Facebook application

Log in to your Facebook developer account. Navigate to the My Apps page by clicking the My Apps button in the top-right corner. Add your app to the Facebook console by clicking the Add a New App button.

Fill out the form and click Create App ID to add your app to Facebook.

Note the App ID of the app you created. This value is used in the next step. You can always find your App ID from the My Apps page.

Generate a system user access token

In order to set up Ad Network Optimization for Facebook Audience Network, you'll need to generate a system user access token and associate it with your property.

To generate a system user access token, sign in to Facebook Business Manager, click the Business Settings icon and navigate to System Users on the left navigation bar. If you don't already have a system user, click Add to create one.

Fill out the System User Name field, select Regular System User as the System User Role, and click Create System User.

To import your apps to Facebook Business, navigate to Accounts > Apps. Click Add and select Add an app.

Enter the App ID created from the previous step and click the Add App to continue.

Navigate to Data Sources > Properties. Take note of the Property ID as this will be needed in the next step. Select the property you created in the previous step and click Add People.

Select the System User created earlier, turn on Admin Access and click Assign.

Click Add Assets, select your app from the list to associate it to the property and click Add.

Navigate back to System Users, select the user you created, and click Generate New Token.

Select your app from the list, add read_audience_network_insights as the Available Scopes for the token to be generated, and click Generate Token.

Finally, take note of your System User Access Token.

Step 2: Configure mediation settings for your Ad Manager ad unit

Sign in to your Ad Manager account. Navigate to Delivery > Yield groups and click the New yield group button.

Enter a unique Name for your yield group, set the Status to Active, select your Ad Format, and set the Inventory type to Mobile App. Under the Targeting > Inventory section, select the Ad Unit ID to which you want to add mediation.

Next, click the Add yield partner button.

If you already have a Yield partner for the Facebook Audience Network, you can simply select it. Otherwise, select Create a new yield partner.

Select Facebook as the Ad network and enter a unique Name. Enable Mediation and turn on Automatic data collection, and enter the System User Access Token and Property ID obtained in the previous section. App Access Token is deprecated and can be left blank. Click Save when done.

Once the Yield partner is selected, choose Mobile SDK mediation as the Integration type, Android as the Platform, and Active as the Status. Enter the Placement ID obtained in the previous section and a Default CPM value.

Click Save at the bottom of the page when done.

Using rewarded video ads

In the settings for your rewarded video ad unit, provide values for the reward amount and reward type. Then, to ensure you provide the same reward to the user no matter which ad network is served, check the Override reward settings from third-party ad networks when using mediation box.

If you don't apply this setting, the Facebook adapter defaults to a reward of type "" (empty string) with a value of 1. The Facebook SDK does not provide specific reward values for its rewarded video ads.

Step 3: Import the Facebook Audience Network SDK and adapter

Add the following implementation dependencies with the latest versions of the Facebook Audience Network SDK and adapter in the app-level build.gradle file:

repositories {
    jcenter()
}

...
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.ads.mediation:facebook:5.7.0.0'
}
...

Manual integration

Download the latest version of the Facebook Audience Network SDK for Android and extract the AudienceNetwork.aar under the AudienceNetwork/bin folder and add it to your project.

Download the latest .aar file of the adapter from Bintray, located under the Files tab, and add it to your project.

Step 4: Additional code required

Follow Facebook's Android network security config guide to modify your network security config file to support media caching.

Step 5: Test your implementation

See the Testing Audience Network Implementation guide guide for detailed instructions on how to enable Facebook test ads.

That's it! You now have a working mediation integration with Facebook's Audience Network.

Optional steps

Under the Google EU User Consent Policy, you must ensure that certain disclosures are given to, and consents obtained from, users in the European Economic Area (EEA) regarding the use of device identifiers and personal data. This policy reflects the requirements of the EU ePrivacy Directive and the General Data Protection Regulation (GDPR). When seeking consent, you must identify each ad network in your mediation chain that may collect, receive, or use personal data and provide information about each network's use. Google currently is unable to pass the user's consent choice to such networks automatically.

Please review Facebook's guidance for information about GDPR and Facebook advertising.

Using native ads

Some Facebook native ad assets don't map one to one to Google native ad assets. Such assets are passed back to the publisher in a bundle in getExtras() method in UnifiedNativeAd. Here's a code example showing how to extract these assets:

Example:

Java

Bundle extras = unifiedNativeAd.getExtras();
if (extras.containsKey(FacebookAdapter.KEY_SOCIAL_CONTEXT_ASSET)) {
    String socialContext = extras.get(FacebookAdapter.KEY_SOCIAL_CONTEXT_ASSET);
    ...
}

Kotlin

val extras = unifiedNativeAd.getExtras()
if (extras.containsKey(FacebookAdapter.KEY_SOCIAL_CONTEXT_ASSET)) {
   var socialContext = extras.get(FacebookAdapter.KEY_SOCIAL_CONTEXT_ASSET)
   ...
}

Using Facebook Native Ads without a MediaView

If your app renders native ads without a MediaView asset, Facebook Audience network requires that you implement their native banner ad format.

By default, the Facebook adapter requests native ads , which require a MediaView asset. To use Facebook's native banner ads instead, you must:

  • Upgrade to version 5.4.1.0 or higher of the Facebook adapter.
  • Pass an extra parameter into the ad request indicating you want to request a native banner ad, as shown below.

Java

Bundle extras = new FacebookExtras()
       .setNativeBanner(true)
       .build();

PublisherAdRequest request =  new PublisherAdRequest.Builder()
       .addNetworkExtrasBundle(FacebookAdapter.class, extras)
       .build();

Kotlin

var extras = FacebookExtras()
       .setNativeBanner(true)
       .build()

var adrequest = PublisherAdRequest.Builder()
       .addNetworkExtrasBundle(FacebookAdapter::class.java, extras)
       .build()

Ad rendering

The Facebook adapter returns its native ads as UnifiedNativeAd objects. It populates the following fields for a UnifiedNativeAd.

Field Populated by Facebook adapter
Headline
Image error
Body
App icon
Call to action
Advertiser Name
Star rating
Store
Price

The Facebook adapter does not provide direct access to the main image asset for its native ads. Instead, the adapter populates the MediaView with a video or an image.

For Native ads, Facebook Audience Network SDK itself populates its icon view with an image asset. To avoid potential null pointer errors, the Facebook adapter returns an empty NativeAd.Image if your app calls UnifiedNativeAd.getIcon(). Facebook Audience Network SDK always returns 0 for mediaview width and height, so the Facebook Adapter always returns 0 for getMediaContent().getAspectRatio(). The Facebook Audience Network SDK will fix this issue in a future release.

Impression and click tracking

Google Mobile Ads SDK uses the Facebook Audience Network SDK's callbacks for impression and click tracking, so the reports on both dashboards should match up with few or no discrepancies.

Facebook Android Mediation Adapter Changelog

5.8.0.0

  • Verified compatibility with Facebook SDK v5.8.0.
  • Updated the minimum required Google Mobile Ads SDK version to 19.0.1.

Built and tested with:

  • Google Mobile Ads SDK version 19.0.1.
  • Facebook SDK version 5.8.0.

5.7.1.1

  • Added support for Facebook Audience Network adapter errors.
  • Added descriptive error codes and reasons for adapter load/show failures.

Built and tested with:

  • Google Mobile Ads SDK version 18.3.0.
  • Facebook SDK version 5.7.1.

5.7.1.0

  • Verified compatibility with Facebook SDK v5.7.1.
  • Added support for Facebook Native Banner ads when using open bidding.
  • Native ads now use 'Drawable' for the icon asset.
  • Added support for waterfall mediation.

Built and tested with:

  • Google Mobile Ads SDK version 18.3.0.
  • Facebook SDK version 5.7.1.

5.7.0.0

  • Verified compatibility with Facebook SDK v5.7.0.

Built and tested with:

  • Google Mobile Ads SDK version 18.3.0.
  • Facebook SDK version 5.7.0.

5.6.1.0

  • Verified compatibility with Facebook SDK v5.6.1.
  • Updated the minimum required Google Mobile Ads SDK version to 18.3.0.

Built and tested with:

  • Google Mobile Ads SDK version 18.3.0.
  • Facebook SDK version 5.6.1.

5.6.0.0

  • Verified compatibility with Facebook SDK v5.6.0.
  • Updated Facebook Adapter to use AdChoicesView.

Built and tested with:

  • Google Mobile Ads SDK version 18.2.0.
  • Facebook SDK version 5.6.0.

5.5.0.0

  • Verified compatibility with Facebook SDK v5.5.0.

5.4.1.1

  • Fixed an issue that causes a crash when Native Ads are removed.

5.4.1.0

  • Verified compatibility with Facebook SDK v5.4.1.
  • Added support for Facebook Native Banner Ads for waterfall mediation.
    • Use setNativeBanner() from the FacebookExtras class to request for Native Banner Ads.
  • Fixed an issue that caused Smart Banner Ad requests to fail.
  • Fixed an issue where Rewarded Video Ads were not forwarding the onAdClosed() event in some cases where the app was backgrounded while the video was in progress.
  • Migrated the adapter to AndroidX.
  • Updated the minimum required Google Mobile Ads SDK version to 18.1.1.

5.4.0.0

  • Verified compatibility with Facebook SDK v5.4.0.

5.3.1.2

  • Fixed a bug where Facebook open bidding failed to initialize due to "No placement IDs found".

5.3.1.1

  • Updated native RTB ads impression tracking.
  • Updated the minimum required Google Mobile Ads SDK version to 17.2.1.

5.3.1.0

  • Added open bidding capability to the adapter for banner, interstitial, rewarded and native ads.
  • Verified compatibility with Facebook SDK v5.3.1.

5.3.0.0

  • Updated mediation service name for Google Mobile Ads.
  • Added adapter version to the initialization call.
  • Verified compatibility with Facebook SDK v5.3.0.

5.2.0.2

  • Added support for flexible banner ad sizes.

5.2.0.1

  • Updated adapter to support new open-beta Rewarded API.
  • Updated the minimum required Google Mobile Ads SDK version to 17.2.0.

5.2.0.0

  • Verified compatibility with Facebook SDK v5.2.0.

5.1.1.1

  • Updated the adapter to populate Advertiser Name for Unified Native Ads.

5.1.1.0

  • Replaced AdChoices View with AdOptions View.
  • Verified compatibility with Facebook SDK v5.1.1

5.1.0.1

  • Fixed an ANR issue caused by 'getGMSVersionCode()'.

5.1.0.0

  • Initialize Facebook SDK for each ad format.

5.0.1.0

  • Verified compatibility with Facebook SDK v5.0.1.

5.0.0.1

  • Updated the adapter to create the rewarded ad object at ad request time.

5.0.0.0

  • Verified compatibility with Facebook SDK v5.0.0.

4.99.3.0

  • Verified compatibility with Facebook SDK v4.99.3.

4.99.1.1

  • Fixed a bug where the Ad Choices icon is not shown for Unified Native Ads.
  • Fixed a bug where the adapter would throw an exception when trying to download images.

4.99.1.0

  • Verified compatibility with Facebook SDK v4.99.1.

4.28.2.1

  • Updated the adapter to invoke the onRewardedVideoComplete() ad event.

4.28.2.0

  • Verified compatibility with Facebook SDK v4.28.2.

4.28.1.1

  • Fixed an issue where clicks are not being registered for Unified Native Ads.

4.28.1.0

  • Verified compatibility with Facebook SDK v4.28.1.

4.28.0.0

  • Verified compatibility with Facebook SDK v4.28.0.

4.27.1.0

  • Verified compatibility with Facebook SDK v4.27.1.

4.27.0.0

  • Verified compatibility with Facebook SDK v4.27.0.

4.26.1.0

  • Verified compatibility with Facebook SDK v4.26.1.
  • Updated the Adapter project for Android Studio 3.0

4.26.0.0

  • Added support for rewarded video ads.
  • Added support for native video ads.
  • Verified compatibility with Facebook SDK v4.26.0.

4.25.0.0

  • Fixed an issue where incorrectly sized banners were being returned.
  • Updated the adapter's view tracking for native ads to register individual asset views with the Facebook SDK rather than the entire ad view. This means that background (or "whitespace") clicks on the native ad will no longer result in clickthroughs.
  • Verified compatibility with Facebook SDK v4.25.0.

4.24.0.0

  • Verified compatibility with Facebook SDK v4.24.0.

4.23.0.0

  • Verified compatibility with Facebook SDK v4.23.0.

4.22.1.0

  • Verified compatibility with Facebook SDK v4.22.1.

4.22.0.0

  • Updated the adapter to make it compatible with Facebook SDK v4.22.0.

4.21.1.0

  • Verified compatibility with Facebook SDK v4.21.1.

4.21.0.0

  • Verified compatibility with Facebook SDK v4.21.0.

4.20.0.0

  • Updated the minimum supported Android API level to 14+.
  • Verified compatibility with Facebook SDK v4.20.0.

4.19.0.0

  • Verified compatibility with Facebook SDK v4.19.0.

4.18.0.0

  • Verified compatibility with Facebook SDK v4.18.0.

4.17.0.0

  • Added support for native ads.

4.15.0.0

  • Changed the version naming system to [FAN SDK version].[adapter patch version].
  • Updated the minimum required FAN SDK to v4.15.0.
  • Updated the minimum required Google Mobile Ads SDK to v9.2.0.
  • Fixed a bug where Facebook's click callbacks for interstitial ads weren't forwarded correctly.
  • The adapter now also forwards onAdLeftApplication when an ad is clicked.

1.2.0

  • Fixed a bug that so that AdSize.SMART_BANNER is now a valid size.

1.1.0

  • Added support for full width x 250 format when request is for AdSize.MEDIUM_RECTANGLE

1.0.1

  • Added support for AdSize.SMART_BANNER

1.0.0

  • Initial release