Amazon_Android_Banner_AdMob - imobile/app-mediation GitHub Wiki

※以下の文書はAmazon Publisher ServicesのドキュメントをAmazonの許諾を得て転載したものです。

AdMob

Adding Banners on AdMob

Before you start, be sure to download the APS AdMob SDK and include the APSAdMobAdapter.aar adapter into your project. Please refer to the Android SDK initialization page for more information.

To add a banner ad:

  • Once an APS ad is returned to the user, the onSuccess() method will execute passing in a DTBAdResponse object.
  • The DTBAdResponse object’s getRenderingBundle() method contains the necessary APS bid information.
  • Use AdMob’s addCustomEventExtrasBundle() method to pass in the AmazonCustomBannerEvent adapter name and the Bundle object with the APS bid information.
  • Call AdMob’s loadAd() function to send the ad request.
final DTBAdRequest loader = new DTBAdRequest();
loader.setSizes(new DTBAdSize(Your_Slot_Width, Your_Slot_Height, "Your_Amazon_SlotID"));
loader.loadAd(new DTBAdCallback() {

    // No APS bid returned
    @Override
    public void onFailure(AdError adError) {
        Log.e("AdError", "Oops banner ad load has failed: " + adError.getMessage());
       /*** 
        Please implement the logic to send an ad request without our parameters if 
        you want to show ads from other ad networks when APS request fails.
        ***/
    }

    // APS bid returned
    @Override
    public void onSuccess(DTBAdResponse dtbAdResponse) {
      // Find the adMob adView you have created
      adView = findViewById(R.id.adView);

      // Add bid parameters to Bundle
      Bundle bundle = dtbAdResponse.getRenderingBundle();

      // Append bid parameters to AdMob request
      @SuppressWarnings("unchecked")
      AdRequest request = new AdRequest.Builder().addCustomEventExtrasBundle(AmazonCustomBannerEvent.class, bundle).build();
      // Make request to AdMob
      adView.loadAd(request); 
    } 
});

Note:

  • If the ad request fails, onFailure() will be called with an error object. Here you can send an ad request to your ad mediator.
  • If you have ad slots that auto refresh ads, please refer to Auto Refresh of Ads documentation. (※アイモバイル註:広告をリフレッシュさせる事で収益性が高まります。推奨秒数45秒~60秒)
  • If you haven’t set up line items in your AdMob account at this point, please refer to Setting up Custom Events in AdMob. (※アイモバイル註:この作業は弊社営業担当が実施します)
  • For testing, use the following test ids:
    • Amazon app key: b478e6a7750d4bcc8d4a4e53c04d6fab
    • Amazon 320×50 ad size slot uuid: e0c7923c-4885-4c8d-87dd-57bb637365f7
    • AdMob ad unit id: ca-app-pub-8104633956190749/4931221308
  • Build and run the app. You will see a test banner ad at the bottom of the screen.

mmb_banner_example-1

Adding Smart Banner ads

If you’re using AdMob’s Smart Banner, you must define the AdRegistration.SlotGroup object in your project’s MainActivity. This will allow you to send one APS bid request for either 320×50 or 728×90 without calling the DTBAdRequest.setSizes() function for each size.

Instantiate the AdRegistration.SlotGroup object. Give a unique name to the SlotGroup (e.g. “Your_SlotGroup_Name”) so that this can be retrieved later when instantiating the DTBAdRequest. If this is left blank, the slot group name will be “default.”

Add the 320×50 and 728×90 slots by passing the width, height, and corresponding slotID. A mismatch between the width, height, and slotID will result in an error.

@Override
protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  /*** 
  Give a unique name to the SlotGroup (e.g. “Your_SlotGroup_Name”) so that this can be 
  retrieved later when instantiating the DTBAdRequest.
   ***/ 
  AdRegistration.SlotGroup group = new AdRegistration.SlotGroup("Your_SlotGroup_Name");

  /***
  Add your 320x50 and 728x90 slots to the slot group.
   ***/
  group.addSlot(new DTBAdSize(320, 50, "Your_Amazon_320x50_SlotId"));
  group.addSlot(new DTBAdSize(728, 90, "Your_Amazon_728x90_SlotId"));
  AdRegistration.addSlotGroup(group);
}

In your Smart Banner Activity, you’ll need to:

  • Instantiate the DTBAdRequest object, and set the slot group using the DTBAdRequest.setSlotGroup() function. The slot group name must correspond with the name defined earlier.
  • Request for an APS bid by calling the DTBAdRequest.loadSmartBanner() function and passing the DTBAdCallBack() object.

The following DTBAdCallBack() methods must be defined to handle each APS bid response:

  • onSuccess() – APS returns a bid
    • Define a Bundle object, and append the APS bid parameters using DTBAdResponse.getRenderingBundle().
    • Instantiate the AdMob AdRequest object, and pass the AmazonCustomBannerEvent class and the newly defined Bundle as custom event extras.
    • Call AdMob’s loadAd() function to send the AdMob ad request.
  • onFailure() – APS doesn’t return a bid
    • Call AdMob’s loadAd() function as usual without appending the APS bid parameters. This will ensure the AdMob request will proceed even if APS doesn’t bid.
final DTBAdRequest loader = new DTBAdRequest();
// Reference to the slot group instantiated in MainActivity.java
loader.setSlotGroup("Your_SlotGroup_Name");

try {
  loader.loadSmartBanner(new DTBAdCallback() {

    // No APS bid returned
    @Override
    public void onFailure(AdError adError) {
    Log.e("APP", "Failed to load the ad" + adError.getMessage());
    /*** 
    Please implement the logic to send an ad request without our parameters if you want to
    show ads from other ad networks when APS ad request fails.
     ***/
    }
 
    // APS bid returned - send custom event info to AdMob in ad request
    @Override
    public void onSuccess(DTBAdResponse dtbAdResponse) {
      // Find the adMob smart banner view you have created
      smartAdView = findViewById(R.id.smartAdView);

      // Pass 'true' to getRenderingBundle() to indicate this is a Smart Banner; add bid parameters to bundle
      Bundle bundle = dtbAdResponse.getRenderingBundle(true);

      // Append bid parameters to AdMob request
      @SuppressWarnings("unchecked")
      AdRequest request = new AdRequest.Builder().addCustomEventExtrasBundle(APSAdMobCustomBannerEvent.class, bundle).build();

      // Make request to AdMob
      smartAdView.loadAd(request);
    }
  });
} catch (DTBLoadException ex) {
  Log.e("APP", "Failed to load the ad" + ex.getMessage());
}