Documentation

Overview

Loading native ads consists of several stages:

  1. Loading BidMachine NativeRequest
  2. Setting up AdManagerAdRequest according to loaded BidMachine NativeRequest
  3. Loading AdManager NativeAd with configured AdManagerAdRequest throught AdManager AdLoader
  4. Loading BidMachine NativeAd
  5. Showing BidMachine NativeAd

❗️

Auto-Refresh

Auto-refresh is not supported. For this type of integration, all of the above steps must be repeated.

Loading BidMachine NativeRequest

Create a new NativeRequest instance with a specific AdRequestListener.
Call request on the NativeRequest instance to start loading.
When the onRequestSuccess callback is triggered (it means that the request has been successfully loaded) - you can proceed to setting up AdManagerAdRequest.

// Create new BidMachine NativeRequest instance
NativeRequest nativeRequest = new NativeRequest.Builder()
        .setListener(new NativeRequest.AdRequestListener() {
            @Override
            public void onRequestSuccess(@NonNull NativeRequest nativeRequest,
                                         @NonNull AuctionResult auctionResult) {
                runOnUiThread(() -> loadAdManagerNative());
            }
        })
        .build();

// Request an ad from BidMachine without loading it
nativeRequest.request(...);

Setting up AdManagerAdRequest

Steps to set up AdManagerAdRequest by BidMachine NativeRequest:

  1. Create a new AdManagerAdRequest instance
AdManagerAdRequest adManagerAdRequest =
    AMBidMachineUtils.createAdManagerRequest(nativeRequest);
  1. Create a new AdManagerAdRequest.Builder instance
AdManagerAdRequest.Builder adManagerAdRequestBuilder =
    AMBidMachineUtils.createAdManagerRequestBuilder(nativeRequest);
AdManagerAdRequest adManagerAdRequest = adManagerAdRequestBuilder.build();
  1. Fill an existing AdManagerAdRequest.Builder instance
AdManagerAdRequest.Builder adManagerAdRequestBuilder =
    new AdManagerAdRequest.Builder();
AMBidMachineUtils.appendRequest(adManagerAdRequestBuilder, nativeRequest);
AdManagerAdRequest adManagerAdRequest = adManagerAdRequestBuilder.build();

Loading AdManager NativeAd

When configuring the AdManager NativeAd, add an extra check to the top of onNativeAdLoaded callback in the OnNativeAdLoadedListener to determine if BidMachine has won/lost the mediation. Also, call notifyMediationWin/notifyMediationLoss on the NativeRequest instance when BidMachine wins/loses the mediation.

Use the AdManagerAdRequest that was created during the previous step to load the AdManager NativeAd.

AdLoader adManagerNativeAdLoader = new AdLoader.Builder(...)
        .forNativeAd(nativeAd -> {
            // Checking whether it is BidMachine or not
            if (AMBidMachineUtils.isBidMachineNative(nativeAd)) {
                // Notify BidMachine about win
                bidMachineNativeRequest.notifyMediationWin();

                // Load BidMachine ad object, before show BidMachine ad
                loadBidMachineNative();
            } else {
                // Notify BidMachine about loss
                bidMachineNativeRequest.notifyMediationLoss();
            }
        })
        .withAdListener(...)
        .build();

adManagerNativeAdLoader.loadAd(adManagerAdRequest);

Loading BidMachine NativeAd

If BidMachine has won the mediation, the next step will be loading BidMachine NativeAd.

NativeAd bidMachineNativeAd = new NativeAd(...);
bidMachineNativeAd.setListener(...);
bidMachineNativeAd.load(nativeRequest);

Show BidMachine NativeAd

After loading BidMachine NativeAd, register native ad and show it by adding it to ViewGroup.

🚧

Don't forget to check if it can be shown by executing canShow on the NativeAd instance before showing

if (bidMachineNativeAd != null && bidMachineNativeAd.canShow()) {
    NativeAdContentLayout nativeAdContentLayout = (NativeAdContentLayout) getLayoutInflater()
            .inflate(R.layout.item_bidmachine_native_ad, adContainer, false);
    nativeAdContentLayout.bind(bidMachineNativeAd);
    nativeAdContentLayout.registerViewForInteraction(bidMachineNativeAd);

    adContainer.removeAllViews();
    adContainer.addView(nativeAdContentLayout);
}

📘

Sample App

You can find Sample App with BidMachine and AdManager here: Github