リワード広告のカスタム イベント

前提条件

カスタム イベントの設定を完了します。

リワード広告をリクエストする

ウォーターフォール メディエーション チェーンでカスタム イベント広告申込情報に到達すると、 クラス名で loadRewardedAd() メソッドが呼び出されます。 カスタム ディメンションを イベントをご覧ください。この例では このメソッドは SampleCustomEvent にあり、その後 loadRewardedAd() を呼び出します。 メソッド(SampleRewardedCustomEventLoader 内)

リワード広告をリクエストするには、Adapter を拡張するクラスを作成または変更して、 loadRewardedAd() を実装する。さらに、新しいクラスを作成して実装し、 MediationRewardedAd

このカスタム イベントの例では、 SampleCustomEventAdapter クラスを拡張してから、デリゲートします。 SampleRewardedCustomEventLoader

Java

package com.google.ads.mediation.sample.customevent;

import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAd;
import com.google.android.gms.ads.mediation.MediationRewardedAdCallback;
...

public class SampleCustomEvent extends Adapter {

  private SampleNativeCustomEventLoader nativeLoader;

  @Override
  public void loadRewardedAd(
      @NonNull MediationRewardedAdConfiguration mediationRewardedAdConfiguration,
      @NonNull
          MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
              mediationAdLoadCallback) {
    rewardedLoader =
        new SampleRewardedCustomEventLoader(
            mediationRewardedAdConfiguration, mediationAdLoadCallback);
    rewardedLoader.loadAd();
  }
}

SampleRewardedCustomEventLoader は次のタスクを行います。

  • リワード広告の読み込み

  • MediationRewardedAd インターフェースの実装

  • 広告イベント コールバックを受信して Google Mobile Ads SDK に報告する

アド マネージャーの UI で定義されているオプション パラメータは、 広告設定に含まれていますこのパラメータには、 adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)。 このパラメータは通常、広告ネットワーク SDK で実装される 渡す必要があります。

Java

package com.google.ads.mediation.sample.customevent;

import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAd;
import com.google.android.gms.ads.mediation.MediationRewardedAdCallback;
...

public class SampleRewardedCustomEventLoader extends SampleRewardedAdListener
    implements MediationRewardedAd {

  /** Configuration for requesting the rewarded ad from the third-party network. */
  private final MediationRewardedAdConfiguration mediationRewardedAdConfiguration;

  /**
   * A {@link MediationAdLoadCallback} that handles any callback when a Sample
   * rewarded ad finishes loading.
   */
  private final MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
      mediationAdLoadCallback;

  /** Callback for rewarded ad events. */
  private MediationRewardedAdCallback rewardedAdCallback;

  /** Constructor. */
  public SampleRewardedCustomEventLoader(
      @NonNull MediationRewardedAdConfiguration mediationRewardedAdConfiguration,
      @NonNull MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
              mediationAdLoadCallback) {
    this.mediationRewardedAdConfiguration = mediationRewardedAdConfiguration;
    this.mediationAdLoadCallback = mediationAdLoadCallback;
  }

  /** Loads the rewarded ad from the third-party ad network. */
  public void loadAd() {
    // All custom events have a server parameter named "parameter" that returns
    // back the parameter entered into the AdMob UI when defining the custom event.
    Log.i("RewardedCustomEvent", "Begin loading rewarded ad.");
    String serverParameter = mediationRewardedAdConfiguration
        .getServerParameters()
        .getString(MediationConfiguration
        .CUSTOM_EVENT_SERVER_PARAMETER_FIELD);
    Log.d("RewardedCustomEvent", "Received server parameter.");
    SampleAdRequest request = createSampleRequest(mediationRewardedAdConfiguration);
    sampleRewardedAd = new SampleRewardedAd(serverParameter);
    sampleRewardedAd.setListener(this);
    Log.i("RewardedCustomEvent", "Start fetching rewarded ad.");
    sampleRewardedAd.loadAd(request);
  }

  public SampleAdRequest createSampleRequest(
      MediationAdConfiguration mediationAdConfiguration) {
    SampleAdRequest request = new SampleAdRequest();
    request.setTestMode(mediationAdConfiguration.isTestRequest());
    request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());
    return request;
  }
}

広告が正常に取得されたか、エラーが発生したかに応じて、 呼び出すと、 onSuccess() または onFailure()onSuccess() は、次の実装を実装するクラスのインスタンスを渡すことで呼び出されます。 MediationRewardedAd

通常、これらのメソッドは 実装する必要があります。この例では、サンプル SDK が 関連するコールバックを含む SampleAdListener がある

Java

@Override
public void onRewardedAdLoaded() {
  rewardedAdCallback = mediationAdLoadCallback.onSuccess(this);
}

@Override
public void onRewardedAdFailedToLoad(SampleErrorCode errorCode) {
    mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));
}

MediationRewardedAd では、表示する showAd() メソッドを実装する必要があります。 次を行います。

Java

@Override
public void showAd(Context context) {
  if (!(context instanceof Activity)) {
    rewardedAdCallback.onAdFailedToShow(
        SampleCustomEventError.createCustomEventNoActivityContextError());
    return;
  }
  Activity activity = (Activity) context;

  if (!sampleRewardedAd.isAdAvailable()) {
    rewardedAdCallback.onAdFailedToShow(
        SampleCustomEventError.createCustomEventAdNotAvailableError());
    return;
  }
  sampleRewardedAd.showAd(activity);
}

メディエーション イベントを Google Mobile Ads SDK に転送する

onSuccess() が呼び出されると、返された MediationRewardedAdCallback そのオブジェクトを使用して、プレゼンテーション イベントを Google Mobile Ads SDK にリンクできます。「 SampleRewardedCustomEventLoader クラスは SampleAdListener を拡張します。 インターフェースを使用して、サンプル広告ネットワークから Google モバイルにコールバックを転送します。 。

カスタム イベントは、できるだけ多くのコールバックを これにより、アプリはこれらの同等のイベントを Google Mobile Ads SDK があります。コールバックの使用例を次に示します。

Java

@Override
public void onAdRewarded(final String rewardType, final int amount) {
  RewardItem rewardItem =
      new RewardItem() {
        @Override
        public String getType() {
          return rewardType;
        }

        @Override
        public int getAmount() {
          return amount;
        }
      };
  rewardedAdCallback.onUserEarnedReward(rewardItem);
}

@Override
public void onAdClicked() {
  rewardedAdCallback.reportAdClicked();
}

@Override
public void onAdFullScreen() {
  rewardedAdCallback.onAdOpened();
  rewardedAdCallback.onVideoStart();
  rewardedAdCallback.reportAdImpression();
}

@Override
public void onAdClosed() {
  rewardedAdCallback.onAdClosed();
}

@Override
public void onAdCompleted() {
  rewardedAdCallback.onVideoComplete();
}

これで、リワード広告のカスタム イベントの実装が完了しました。完全な サンプルを GitHub。 すでにサポートされている広告ネットワークで使用するか、 カスタム イベントのリワード広告を表示します。