Android GAM Bidding-Only Integration - Multiformat Banner+Video+InAppNative

Back to Bidding-Only Integration

Starting with version 2.1.5 Prebid SDK supports the fully multiformat ad unit. It allows to run bid requests with any combination of banner, video, and native formats.

The following code demonstrates the integration of multiformat ad unit.

private fun createAd() {
    // random() only for test cases, in production use only one config id
    val configId = listOf(CONFIG_ID_BANNER, CONFIG_ID_VIDEO, CONFIG_ID_NATIVE).random()

    // Step 1: Create a PrebidAdUnit
    prebidAdUnit = PrebidAdUnit(configId)

    // Step 2: Create PrebidRequest
    val prebidRequest = PrebidRequest()

    // Step 3: Setup the parameters
    prebidRequest.setBannerParameters(createBannerParameters())
    prebidRequest.setVideoParameters(createVideoParameters())
    prebidRequest.setNativeParameters(createNativeParameters())

    // Step 4: Make a bid request
    val gamRequest = AdManagerAdRequest.Builder().build()
    prebidAdUnit?.fetchDemand(prebidRequest, gamRequest) {
        // Step 5: Load an Ad

        loadGam(gamRequest)
    }
}

private fun createBannerParameters(): BannerParameters {
    val parameters = BannerParameters()
    parameters.api = listOf(Signals.Api.MRAID_3, Signals.Api.OMID_1)

    return parameters
}

private fun createVideoParameters(): VideoParameters {
    return VideoParameters(listOf("video/mp4"))
}

private fun createNativeParameters(): NativeParameters {
    val assets = mutableListOf<NativeAsset>()

    val title = NativeTitleAsset()
    title.setLength(90)
    title.isRequired = true
    assets.add(title)

    val icon = NativeImageAsset(20, 20, 20, 20)
    icon.imageType = NativeImageAsset.IMAGE_TYPE.ICON
    icon.isRequired = true
    assets.add(icon)

    val image = NativeImageAsset(200, 200, 200, 200)
    image.imageType = NativeImageAsset.IMAGE_TYPE.MAIN
    image.isRequired = true
    assets.add(image)

    val data = NativeDataAsset()
    data.len = 90
    data.dataType = NativeDataAsset.DATA_TYPE.SPONSORED
    data.isRequired = true
    assets.add(data)

    val body = NativeDataAsset()
    body.isRequired = true
    body.dataType = NativeDataAsset.DATA_TYPE.DESC
    assets.add(body)

    val cta = NativeDataAsset()
    cta.isRequired = true
    cta.dataType = NativeDataAsset.DATA_TYPE.CTATEXT
    assets.add(cta)

    val nativeParameters = NativeParameters(assets)
    nativeParameters.addEventTracker(
        NativeEventTracker(
            NativeEventTracker.EVENT_TYPE.IMPRESSION,
            arrayListOf(NativeEventTracker.EVENT_TRACKING_METHOD.IMAGE)
        )
    )
    nativeParameters.setContextType(NativeAdUnit.CONTEXT_TYPE.SOCIAL_CENTRIC)
    nativeParameters.setPlacementType(NativeAdUnit.PLACEMENTTYPE.CONTENT_FEED)
    nativeParameters.setContextSubType(NativeAdUnit.CONTEXTSUBTYPE.GENERAL_SOCIAL)

    return nativeParameters
}

If you use Custom Native Ads follow the guide on how to implement processing of the ad response of the respective type. The following code snipet demonstrates how you can process the banner, video and in-banner native (Native Styles) ad resposnse:

private fun loadGam(gamRequest: AdManagerAdRequest) {
    val onBannerLoaded = OnAdManagerAdViewLoadedListener { adView ->
        showBannerAd(adView)
    }

    val onNativeLoaded = OnNativeAdLoadedListener { nativeAd ->
        showNativeAd(nativeAd, adWrapperView)
    }

    val onPrebidNativeAdLoaded = OnCustomFormatAdLoadedListener { customNativeAd ->
        showPrebidNativeAd(customNativeAd)
    }

    // Prepare the lisners for multiformat Ad Response
    val adLoader = AdLoader.Builder(this, AD_UNIT_ID)
        .forAdManagerAdView(onBannerLoaded, AdSize.BANNER, AdSize.MEDIUM_RECTANGLE)
        .forNativeAd(onNativeLoaded)
        .forCustomFormatAd(CUSTOM_FORMAT_ID, onPrebidNativeAdLoaded, null)
        .withAdListener(AdListenerWithToast(this))
        .withAdManagerAdViewOptions(AdManagerAdViewOptions.Builder().build())
        .build()

    adLoader.loadAd(gamRequest)
}

The methods managing the prebid and GAM ads:

private fun showBannerAd(adView: AdManagerAdView) {
    adWrapperView.addView(adView)
    AdViewUtils.findPrebidCreativeSize(adView, object : AdViewUtils.PbFindSizeListener {
        override fun success(width: Int, height: Int) {
            adView.setAdSizes(AdSize(width, height))
        }

        override fun failure(error: PbFindSizeError) {}
    })
}

private fun showNativeAd(ad: NativeAd, wrapper: ViewGroup) {
    val nativeContainer = View.inflate(wrapper.context, R.layout.layout_native, null)

    val icon = nativeContainer.findViewById<ImageView>(R.id.imgIcon)
    val iconUrl = ad.icon?.uri?.toString()
    if (iconUrl != null) {
        ImageUtils.download(iconUrl, icon)
    }

    val title = nativeContainer.findViewById<TextView>(R.id.tvTitle)
    title.text = ad.headline

    val image = nativeContainer.findViewById<ImageView>(R.id.imgImage)
    val imageUrl = ad.images.getOrNull(0)?.uri?.toString()
    if (imageUrl != null) {
        ImageUtils.download(imageUrl, image)
    }

    val description = nativeContainer.findViewById<TextView>(R.id.tvDesc)
    description.text = ad.body

    val cta = nativeContainer.findViewById<Button>(R.id.btnCta)
    cta.text = ad.callToAction

    wrapper.addView(nativeContainer)
}

private fun showPrebidNativeAd(customNativeAd: NativeCustomFormatAd) {
    AdViewUtils.findNative(customNativeAd, object : PrebidNativeAdListener {
        override fun onPrebidNativeLoaded(ad: PrebidNativeAd) {
            inflatePrebidNativeAd(ad)
        }

        override fun onPrebidNativeNotFound() {
            Log.e("PrebidAdViewUtils", "Find native failed: native not found")
        }

        override fun onPrebidNativeNotValid() {
            Log.e("PrebidAdViewUtils", "Find native failed: native not valid")
        }
    })
}

private fun inflatePrebidNativeAd(ad: PrebidNativeAd) {
    val nativeContainer = View.inflate(this, R.layout.layout_native, null)

    val icon = nativeContainer.findViewById<ImageView>(R.id.imgIcon)
    ImageUtils.download(ad.iconUrl, icon)

    val title = nativeContainer.findViewById<TextView>(R.id.tvTitle)
    title.text = ad.title

    val image = nativeContainer.findViewById<ImageView>(R.id.imgImage)
    ImageUtils.download(ad.imageUrl, image)

    val description = nativeContainer.findViewById<TextView>(R.id.tvDesc)
    description.text = ad.description

    val cta = nativeContainer.findViewById<Button>(R.id.btnCta)
    cta.text = ad.callToAction

    ad.registerView(nativeContainer, Lists.newArrayList(icon, title, image, description, cta), null)

    adWrapperView.addView(nativeContainer)
}

Step 1: Create a PrebidAdUnit

Initialize the PrebidAdUnit with the following properties:

  • configId - an ID of the Stored Impression on the Prebid Server

Step 2: Create a PrebidRequest

Create the instance of PrebidRequest initializing it with respective ad format parameters.

In addition you can set the following properties of the PrebidRequest.

Step 3: Setup the parameters

For each ad format you should create a respective configuration parameter:

BannerParameters

Using the BannerParameters object you can customize the bid request for banner ads.

Starting from PrebidMobile 2.1.0 the BannerBaseAdUnit.Parameters class is deprecated. Use BannerParameters instead.

adSizes

Defines the OpenRTB banner.formats array.

interstitialMinWidthPerc and interstitialMinHeightPerc

For interstitials only, these define which sizes Prebid Server will choose to send to bidders. See Prebid Server interstitial support. If this option is used, you’ll need to set the size to 1x1.

api

The api property is dedicated to adding values for API Frameworks to bid response according to the OpenRTB 2.6 spec. The supported values for GMA SDK integration are:

  • 3 or Signals.Api.MRAID_1 : MRAID-1 support signal
  • 5 or Signals.Api.MRAID_2 : MRAID-2 support signal
  • 6 or Signals.Api.MRAID_3 : MRAID-3 support signal
  • 7 or Signals.Api.OMID_1 : signals OMSDK support

VideoParameters

Using the VideoParameters object you can customize the bid request for video ads.

placement

Not needed for Instream video integration, which uses placement=1 and plcmt=1.

The OpenRTB 2.6 Placement Type for the auction can be expressed as an integer array or you can use an enum for easier readability.

  • 2 or InBanner : In-Banner placement exists within a web banner that leverages the banner space to deliver a video experience as opposed to another static or rich media format. The format relies on the existence of display ad inventory on the page for its delivery.
  • 3 or InArticle : In-Article placement loads and plays dynamically between paragraphs of editorial content; existing as a standalone branded message.
  • 4 or InFeed : In-Feed placement is found in content, social, or product feeds.
  • 5 or Slider, Floating or Interstitial : Open RTB supports one of three values for option 5 as either Slider, Floating or Interstitial. If an enum value is supplied in placement, bidders will receive value 5 for placement type and assume to be interstitial with the instl flag set to 1.

Notes:

  • VideoInterstitialAdUnit and rewarded video ads will default to placement= 5 if no placement value is supplied.

api

The api property is dedicated to adding values for API Frameworks to bid response according to the OpenRTB 2.6 spec. The supported values for GMA SDK integration are:

  • 1 or Signals.Api.VPAID_1 : VPAID 1.0
  • 2 or Signals.Api.VPAID_2 : VPAID 2.0
  • 3 or Signals.Api.MRAID_1 : MRAID-1 support signal
  • 5 or Signals.Api.MRAID_2 : MRAID-2 support signal
  • 6 or Signals.Api.MRAID_3 : MRAID-3 support signal
  • 7 or Signals.Api.OMID_1 : signals OMSDK support

maxBitrate

Integer representing the OpenRTB 2.6 maximum bit rate in Kbps.

minBitrate

Integer representing the OpenRTB 2.6 minimum bit rate in Kbps.

maxDuration

Integer representing the OpenRTB 2.6 maximum video ad duration in seconds.

minDuration

Integer representing the OpenRTB 2.6 minimum video ad duration in seconds.

mimes

Array of strings representing the supported OpenRTB 2.6 content MIME types (e.g., “video/x-ms-wmv”, “video/mp4”). Required property.

playbackMethod

Array of OpenRTB 2.6 playback methods. If none are specified, any method may be used. Only one method is typically used in practice. It is strongly advised to use only the first element of the array.

  • 1 or Signals.PlaybackMethod.AutoPlaySoundOn : Initiates on Page Load with Sound On
  • 2 or Signals.PlaybackMethod.AutoPlaySoundOff : Initiates on Page Load with Sound Off by Default
  • 3 or Signals.PlaybackMethod.ClickToPlay : Initiates on Click with Sound On
  • 4 or Signals.PlaybackMethod.MouseOver : Initiates on Mouse-Over with Sound On
  • 5 or Signals.PlaybackMethod.EnterSoundOn : Initiates on Entering Viewport with Sound On
  • 6 or Signals.PlaybackMethod.EnterSoundOff: Initiates on Entering Viewport with Sound Off by Default

protocols

Array or enum of OpenRTB 2.6 supported Protocols. Values can be one of:

  • 1 or Signals.Protocols.VAST_1_0 : VAST 1.0
  • 2 or Signals.Protocols.VAST_2_0 : VAST 2.0
  • 3 or Signals.Protocols.VAST_3_0 : VAST 3.0
  • 4 or Signals.Protocols.VAST_1_0_Wrapper : VAST 1.0 Wrapper
  • 5 or Signals.Protocols.VAST_2_0_Wrapper : VAST 2.0 Wrapper
  • 6 or Signals.Protocols.VAST_3_0_Wrapper : VAST 3.0 Wrapper
  • 7 or Signals.Protocols.VAST_4_0 : VAST 4.0
  • 8 or Signals.Protocols.VAST_4_0_Wrapper : VAST 4.0 Wrapper

NativeParameters

Using the NativeParameters object (with the PrebidRequest object) or the NativeRequest object, you can customize the bid request for native ads.

assets

The array of requested asset objects. Prebid SDK supports all kinds of assets according to the IAB spec except video.

eventtrackers

The array of requested native trackers. Prebid SDK supports inly image trackers according to the IAB spec.

version

Version of the Native Markup version in use. The default value is 1.2

context

The context in which the ad appears.

contextSubType

A more detailed context in which the ad appears.

placementType

The design/format/layout of the ad unit being offered.

placementCount

The number of identical placements in this Layout.

sequence

0 for the first ad, 1 for the second ad, and so on.

asseturlsupport

Whether the supply source/impression supports returning an assetsurl instead of an asset object. 0 or the absence of the field indicates no such support.

durlsupport

Whether the supply source / impression supports returning a dco url instead of an asset object. 0 or the absence of the field indicates no such support.

privacy

Set to 1 when the native ad supports buyer-specific privacy notice. Set to 0 (or field absent) when the native ad doesn’t support custom privacy links or if support is unknown.

ext

This object is a placeholder that may contain custom JSON agreed to by the parties to support flexibility beyond the standard defined in this specification

Step 4: Make a bid request

The fetchDemand method makes a bid request to the Prebid Server. You should provide a GAMRequest object to this method so Prebid SDK sets the targeting keywords of the winning bid for future ad requests.

Step 5: Load and Ad

Follow the GMA SDK documentation to combine the a banner and custom native ads int the app.

Further Reading