OpenWrap-InApp Developer Guide: Android InApp Header Bidding for MoPub

Document created by catherine.racette on Aug 8, 2017Last modified by catherine.racette on Aug 24, 2017
Version 5Show Document
  • View in full screen mode

(Version 1.2.1)

Introduction

In-App Header Bidding allows publishers to allocate ad inventory using a technology and methodology that bypasses inefficiencies that have kept apps from finding the best prices.

 

In App Header bidding is the process of sending out an initial ad call to one or more buyers to solicit an exact price for inclusion in the ad server’s inventory allocation process.

 

At a high level this is a 3-step process:

  1. The prefetch/header bidding ad call needs to be initiated before requesting ad from Primary SDK.
  2. Buyers get first look at the inventory and submit a pre-bid price.
  3. This pre-bid price then becomes the bid floor for the ad server call, which checks if any other buyers in the same priority tier can beat it. As a result, inventory gets allocated to the highest yielding buyer while respecting other publisher controls that may have been set up in the ad server. 

Header bidding primarily benefits publishers who use third party SDKs (e.g., MoPub/DFP) to manage their ads. However, MoPub doesn’t allow publishers to effectively pull in programmatic buyers that compete against marketplace and direct deals, so publishers are limited to which buyers they can utilize.


When a screen in the publisher's app is loaded, the code reaches out to PubMatic for bids before the ad server’s direct sales are called.

 

The diagram below shows how PubMatic OpenWrap - InApp header bidder integrates with publisher’s app.

 

This header bidding arrangement accomplishes several things:

  • All the demand sources are bidding at the same time, which replaces waterfall’s preferential ordering of buyers.
  • Advertisers have a chance at obtaining the best ad inventory.
  • Stats have shown publisher gains as high as 50 percent in CPMs when moving to header bidding setup. 

Supported Ad Format

  • Simple Banner

Prerequisites 

Before continuing with the in-app code integration, use the guidelines in the following MoPub SDK documentation to get started with MoPub and banner integration for Android.

 

 

In-App Code Integration

Use the instructions below to complete the in-app code integration.

  1. Integrating the PubMatic Sample Code in the Publisher’s Application
  2. Adding Permissions to the Manifest File
  3. Creating a Header Bidding Request
  4. Prefetching Bids Using PMPrefetchManager
  5. Handling Prefetched Bids from PMPrefetchManager
  6. Receiving Winning Bids Using MoPub’s Custom Event (Adapter) Class
  7. Get Rendered Ad from Prefetched Creative
  8. Using HeaderBiddingBannerHelper (Optional)
  9. Setting Refresh Interval (Optional)
  10. App debugging

 

Integrating the PubMatic Sample Code in the Publisher’s Application

Copy the contents of the “core” package to the Publisher’s Android application. Refer to the code, available in PubMatic’s Developer Portal, for faster and easier integration.

Import the following classes to integrate the Header Bidding request:

import com.pubmatic.headerbidding.core.PMAdSize; 
import com.pubmatic.headerbidding.core.PMBannerImpression;
import com.pubmatic.headerbidding.core.PMBannerPrefetchRequest;
import com.pubmatic.headerbidding.core.PMBid;
import com.pubmatic.headerbidding.core.PMPrefetchManager;
import com.pubmatic.headerbidding.core.PubMaticBannerAdRequest;
import com.pubmatic.headerbidding.core.PubMaticConstants;

Adding Permissions to the Manifest File

Declare the following security permissions in your manifest file (AndroidManifest.xml).

 

Table 1. Basic Permissions for the Manifest File

PermissionDescription and Manifest File Entry
InternetAccess the Internet. Required for ad-content download.
<uses-permission android:name="android.permission.INTERNET" />
Network StateAccess the Network state. Required for retrieving network details from ConnectivityManager
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Creating a Header Bidding Request

  1. Create Impression objects that contain information about specific ad slots such as impressionId (Unique identifier), slot name, sizes and slot index. Separate object of PMBannerImpression class must be created for each ad slot. 
PMBannerImpression bannerImpression = new PMBannerImpression(<ImpressionId>, <SlotName>, <AdSizes>,
<SlotIndex>);
  • ImpressionId: Used to identify unique ad slot/banner of the MoPub screen. This should match with imp_id configured in step 2 in the Ad Server Set-Up in the MoPub section below.
  • SlotName: A string identifier. It should be the same slot name mapped to PubMatic ad tag. Please refer to mapping in an Excel sheet in the PubMatic Setup section below (Internal only).
  • SlotIndex: Required when the same slot name is used multiple times on the screen. For example, if there are two slot names on the screen, testAd (Above the fold) and testAd (Below the fold), then slotindex can be used to differentiate between slots with same name.
  • AdSizes: Can be provided to request an ad with different sizes.
    Page Break
  1. Create a prefetch request object by specifying publisher id and impressions, from step 1, to prefetch bids. It is required to make an ad request to PubMatic against impression objects.

    PMBannerPrefetchRequest adRequest = PMBannerPrefetchRequest.initHBRequestForImpression
    (<Context>, <pubId>, List<PMBannerImpression>);

     

  2. Specify targeting information on the prefetch request object such as store URL, app domain, gender, ethnicity, etc.

    Sample code for creating ad request & setting basic parameters:
PMBannerPrefetchRequest adRequest = PMBannerPrefetchRequest.initHBRequestForImpression(mContext, <Publisher ID>, bannerImpressions); 
adRequest.setAWT(PubMaticBannerAdRequest.AWT_OPTION.WRAPPED_IN_JS);
adRequest.addKeyword("entertainment");
adRequest.addKeyword("sports");
adRequest.setEthnicity(PubMaticBannerAdRequest.ETHNICITY.ASIAN_AMERICAN);
adRequest.setDoNotTrack(false);
adRequest.setApplicationPaid(false);
// Designated market area (DMA) code of the user.
// This field is applicable for US users only. For example, dma=734
adRequest.setDMA("734");
adRequest.setCoppa(true);
adRequest.setOrmmaComplianceLevel(1);
adRequest.setAppCategory("Game");
adRequest.setIABCategory("IAB1-1,IAB1-7");

For additional parameter details, please refer Table 3 in the ‘PubMatic Prefetch API Parameter Details’ section below.

       

Prefetching Bids Using PMPrefetchManager

  1. Create an object of PMPrefetchListener:

 

PMPrefetchManager.PMPrefetchListener listener = new
PMPrefetchManager.PMPrefetchListener () {
@Override
public void onBidsFetched(Map<String, PMBid> hBResponse) {
Log.d(TAG, "onBidsFetched");
// Header bidding completed.
// Now send the custom data to MoPub.
requestMoPubAd(hBResponse);
}
@Override
public void onBidsFailed(String errorMessage) {
Log.d(TAG, "Header Bidding failed. " + errorMessage);
// Get on with requesting MoPub for ads without HB data.
requestMoPubAd(null);
}
};
  1. Create Prefetch Manager object by specifying the context and set PMPrefetchListener instance. PMPrefetchListener has callback methods for success & failure case of prefetch bid request.
PMPrefetchManager pmPrefetchManager =
PMPrefetchManager.getInstance(<Context>);

pmPrefetchManager.setPrefetchListener(listener);
  1. Set a network timeout using setNetworkTimeout (<timeout in seconds>) method on PMPrefetchManager instance. Default network timeout is 3 seconds, defined as NETWORK_TIMEOUT_SECONDS in PubMaticConstants.java. It can be customized as required. It defines the maximum timeout required to get the response from PubMatic ad server. The Ad server (DFP/MoPub) call will be sent; Either PubMatic responds before this timeout or when the set timeout lapses.

    pmPrefetchManager.setNetworkTimeout(<TIMEOUT>); 
  2. Call prefetchCreatives() on instance of PMPrefetchManager created in step 2.

pmPrefetchManager.prefetchCreatives(<PMBannerPrefetchRequest>);

Handling Prefetched Bids from PMPrefetchManager

  1. Upon success, the Prefetch Manager will respond with a callback onBidsFetched(Map<String, PMBid>) (in PMPrefetchListener class) with a map of PMBid objects.

    The keys for this map will be the impression ids. The PMBid object will contain impression ids and bid information.
  2. Get an instance of MoPubView class (in MoPub SDK) providing it with all the targeting information using setKeywords().
//Include string variables in class 
private final String BID = "bid";
private final String BID_ID = "bidid";
private final String WDEAL_ID = "wdeal";
private final String BID_STATUS = "bidstatus";
private final int BID_DECIMAL_ROUNDOFF= 1;

//Fetch the winning bid details for the impressionId & send to MoPub
PMBid pubResponse = hBResponse.get(<impressionId>);
StringBuilder requestBuilder;

if (pubResponse != null) {

requestBuilder = new StringBuilder();

if(!TextUtils.isEmpty(pubResponse.getDealId())) {
requestBuilder.append(WDEAL_ID+":"+ pubResponse.getDealId());
requestBuilder.append(",");
}

if(!TextUtils.isEmpty(pubResponse.getImpressionId())) {
requestBuilder.append(BID_ID+":"+pubResponse.getImpressionId());
requestBuilder.append(",");
}

String price = String.valueOf(pubResponse.getPrice());

if(!TextUtils.isEmpty(price)) {
double bidPrice = Double.valueOf(price);

if(bidPrice > 0.0d) {
requestBuilder.append(BID+":"+price+",");
requestBuilder.append(BID_STATUS+":1");
} else
requestBuilder.append(BID_STATUS+":0");
}
mopubView.setKeywords(requestBuilder.toString());
}
mopubView.setAdUnitId(<Mopub Ad Unit>);

Below is the utility method to round off the bid price up to ‘x’ decimal places. In the current sample code, it has been rounded up to 1 decimal place and can be customized as required. Make sure the similar line item has been configured in MoPub portal. Please refer to the Ad Server Setup section in this document.

private String getRoundoffPrice(double value, int places) { 
    if (places < 0)
        throw new IllegalArgumentException();

    String formatter = "%."+places+"f";
    return String.format(formatter, value);
  1. Disable the AutoRefresh feature for the MoPubView.
    mopubView.setAutorefreshEnabled(false); 
  2. Call loadAd() from MoPubView class object.
    mopubView.loadAd(); 

Receiving Winning Bids Using MoPub's Custom Event (Adapter) Class

Publisher needs to implement a custom event adapter inheriting MoPub’s CustomEventBanner. For every winning bid, this adapter will receive callback where serverExtras contains the impressionId for which the header bid has won. Assuming it has been configured in MoPub portal along with the full path of this adapter class.

 

@Override 
protected void loadBanner(Context context, final CustomEventBannerListener
customEventBannerListener, Map<String, Object> localExtras, Map<String, String>
serverExtras) {

String winningImpressionId = serverExtras.get("adunit");
}


Get Rendered Ad from Prefetched Creative

Call getPubMaticAdView() on PMPrefetchManager instance passing the impressionId and size received in the previous step. In case of success, this method will return a rendered creative in a webView & fire impression trackers.

Define these variables in class:

private CustomEventBannerListener mListener; 
private WebView mPubmaticAdView;

Sample code to render Ad from prefetched creative:

@Override 
    protected void loadBanner(Context context, final CustomEventBannerListener
customEventBannerListener, Map<String, Object> localExtras, Map<String, String> serverExtras) {
        mListener = customEventBannerListener;
        String winningImpressionId = serverExtras.get("adunit");

        if(!TextUtils.isEmpty(winningImpressionId)) {

            PMPrefetchManager headerBiddingManager = PMPrefetchManager.getInstance(context);
            mPubmaticAdView = headerBiddingManager.getPubMaticAdView(winningImpressionId);

            //Return Pubmatic AdView to MoPub
            if (mPubmaticAdView != null)
                mListener.onBannerLoaded(mPubmaticAdView);
            else
                mListener.onBannerFailed(MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR);
        } else
            mListener.onBannerFailed(MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR);
    }

As mentioned above, Call onBannerLoaded() on the custom event listener instance if a valid ad is returned (i.e. webview is not null) else call onBannerFailed() with error code.

As a part of cleanup of resources, it is recommended to free the Ad objects. Below is a sample code:

@Override 
protected void onInvalidate() {

mListener = null;

if(mPubmaticAdView!=null) {
ViewGroup parent = (ViewGroup)mPubmaticAdView.getParent();
if(parent!=null)
parent.removeView(mPubmaticAdView);
mPubmaticAdView.destroy();
mPubmaticAdView = null;
}
}

Using HeaderBiddingBannerHelper (Optional)

The MoPubHeaderBiddingBannerHelper class will be a part of Publisher's app. This class can be used as a convenient way to integrate PubMatic's header bidding solution with MoPub.


Customize this class to integrate any other header bidding partner in addition to PubMatic.

 

  1. Create an AdSlotInfo object for each ad slot by providing slot name, list of ad sizes and PublisherAdView.

    MoPubHeaderBiddingBannerHelper.AdSlotInfo adSlotInfo = new MoPubHeaderBiddingBannerHelper
    .AdSlotInfo(<SlotName>, <List[AdSizes]>, <MoPubView>);
  2. Create a list of AdSlotInfo objects.
  3. Create an object of MoPubHeaderBiddingBannerHelper giving it the context and the ad slot list created in step 2. Now call execute.
    MoPubHeaderBiddingBannerHelper hbBannerHelper = new MoPubHeaderBiddingBannerHelper (<Context>, <AdSlotInfoList>); 
    hbBannerHelper.execute();

Setting Refresh Interval (Optional)

This solution provides an optional feature to auto-refresh the banner ad after a specified interval.

The default value for refresh interval is 0 seconds, i.e., by default auto-refresh feature is disabled. The publisher can set an appropriate refresh interval by calling setRefreshInterval(<value in sec>) of PMPrefetchManager. Expected value should be in range of 12 to 120.

 

Table 2. Ad Refresh Behavior on Setting Refresh Interval Value

Value (X) in secondsBehavior
X<=0Ad will not refresh 
X > 0 & X <= 12 Ad will get refreshed after every 12 seconds. 
X > 12 & X <=120 Ad will get refreshed after every X seconds
X > 120Ad will get refreshed after every 120 seconds

 

Note: To use this feature, please disable the ad server’s refresh functionality. Please refer to the MoPub portal documentationfor additional information.

App debugging

Header bidding code logs the request & response from the PubMatic server in debug mode. This debug log can help to see if PubMatic is providing proper bids or not. Below is a sample log from the Header bidding sample application.

 

PubMatic Prefetch API Parameter Details

Please refer the following setter functions for sending the targeting parameter values.

 

Table 3. PMBannerPrefetchRequest APIs/Setters

S. noMethodDescription
1setStoreURL(String storeUrl)URL of the app store from where a user can download this application. This URL must match the storeurl that is whitelisted in the UI.
2setAppDomain(String appDomain)Indicates the domain of the mobile application
3setApplicationPaid(boolean paid)

Indicates whether the mobile application is a paid version or not. Possible values are:

false - Free version
true - Paid version

4setAWT(AWT_OPTION awtOption)

Indicates whether the tracking URL has been wrapped or not in the creative tag.

Possible options are:

 

DEFAULT - Indicates that the tracking URL is sent separately in the response JSON as tracking_url. In this case, the tracking_url field is absent in the JSON response.

 

WRAPPED_IN_IFRAME - Indicates that the tracking_url value is wrapped in an Iframe and appended to the creative_tag.

 

WRAPPED_IN_JS - Indicates that the tracking_url value is wrapped in a JS tag and appended to the creative_tag.

5setPMZoneId(String pmZoneId)This parameter is used to pass a zone ID for reporting.
6setEthnicity(ETHNICITY ethnicity)Numeric code of ethnicity
7setIncome(String income)User's income or income range in dollars (whole numbers). For example, inc=50000 or 50000-75000. 
8setYearOfBirth(String yearOfBirth)Visitor's birth year as a four-digit integer. For example, 1975.
9setGender(String gender)Gender of the visitor. Possible values are:
M - Male
F - Female
O - Others
10setCity(String city)City of the user. For example, city=New York
11setZip(String zip)Home zip code if the visitor is present in the U.S.; otherwise it indicates the postal code. 
12setCoppa(boolean coppa)

Indicates whether the visitor is COPPA-specific or not. For COPPA (Children's Online Privacy Protection Act) compliance, if the visitor's age is below 13, then such visitors should not be served targeted ads.

 

Possible options are:
false - Indicates that the visitor is not COPPA-specific and can be served targeted ads.
true - Indicates that the visitor is COPPA-specific and should be served only COPPA-compliant ads.

 

The United States Federal Trade Commission has written a comprehensive FAQ on complying with COPPA at http://business.ftc.gov/documents/Complying-with-COPPA-Frequently-Asked-Questions.

13setOrmmaComplianceLevel(int ormmaaComplianceLevel)Minimum compliance level. Possible values are 0, 1 and 2.
14setIABCategory(String iabCategory)

List of IAB content categories for the overall site/application. Refer the "Table 6.1 Content Categories" in the Open RTB 2.1 / 2.2 specifications document.

If the site/application falls under multiple IAB categories, you can send categories separated by comma

15addKeyword(String keyword)list of keywords indicating the consumer's interests or intent. 
16setLocation(Location location)Location object
17setDoNotTrack(boolean state)If set, it restricts user and device targeting. Default value is false.
This can also be set by end user, on enabling the “Opt out of Ads Personaization” option in an android phone.
Settings > Google > Ads > Opt out of Ads Personalization
18setAndroidAidEnabled(boolean isAndroidAidEnabled)

If set, it uses advertisement id otherwise it uses android id for targeting.

 

This has effect only if DoNotTrack is set to false.

19setAppCategory(String appCategory)Application primary category as displayed on storeurl page for the respective platform.
20setUdidHash(HASHING_TECHNIQUE hash)

Based on the given hashing type, UDID value hashed & sent to the ad server.

 

Note: If setAndroidAidEnabled(true) is invoked & hashing type is also set then hashed as well as raw advertising id would be sent to ad server.

Note: For more details about these parameters, please refer to HTTP Parameters Details (SSP Ad Server) 

    

PubMatic: See PubMatic Setup - Internal to PubMatic for details on Admin UI set up.

Attachments

    Outcomes