インタースティシャル広告

インタースティシャル広告は、ユーザーが閉じるまでアプリのインターフェース上に全画面表示される広告です。通常は、次のアクティビティに移行する前やゲームレベルをクリアした後の合間など、アプリの操作中に画面が切り替わるタイミングで表示されます。アプリにインタースティシャル広告が表示されると、ユーザーは広告をタップしてリンク先 URL に移動するか、広告を閉じてアプリに戻るかを選択することになります。 事例紹介

このガイドでは、iOS アプリにインタースティシャル広告を組み込む方法について説明します。

前提条件

常にテスト広告でテストする

アプリの開発とテストでは必ずテスト広告を使用し、配信中の実際の広告は使用しないでください。実際の広告を使用すると、アカウントが停止される可能性があります。

テスト広告を読み込む際は、次に示す iOS インタースティシャル向けのテスト専用広告ユニット ID を使うと簡単です。
/21775744923/example/interstitial

この ID は、すべてのリクエストに対してテスト広告を返すように構成されており、アプリのコーディング、テスト、デバッグで自由に使うことができます。なお、アプリを公開する前に、必ずテスト用 ID をご自身の広告ユニット ID に置き換えてください。

Mobile Ads SDK のテスト広告の仕組みについて詳しくは、テスト広告をご覧ください。

実装

インタースティシャル広告を組み込む主な手順は、以下のとおりです。

  1. 広告を読み込みます。
  2. コールバックを登録する
  3. 広告を表示する

広告を読み込む

広告の読み込みは、GAMInterstitialAd クラスの load(adUnitID:request) メソッドを使用して行います。

Swift

fileprivate func loadInterstitial() async {
  do {
    interstitial = try await AdManagerInterstitialAd.load(
      with: "/21775744923/example/interstitial", request: AdManagerRequest())
    interstitial?.fullScreenContentDelegate = self
  } catch {
    print("Failed to load interstitial ad with error: \(error.localizedDescription)")
  }
}

SwiftUI

import GoogleMobileAds

class InterstitialViewModel: NSObject, FullScreenContentDelegate {
  private var interstitialAd: InterstitialAd?

  func loadAd() async {
    do {
      interstitialAd = try await InterstitialAd.load(
        with: "ca-app-pub-3940256099942544/4411468910", request: Request())
      interstitialAd?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load interstitial ad with error: \(error.localizedDescription)")
    }
  }

Objective-C

[GAMInterstitialAd loadWithAdManagerAdUnitID:@"/21775744923/example/interstitial"
                                     request:[GAMRequest request]
                           completionHandler:^(GAMInterstitialAd *ad, NSError *error) {
                             if (error) {
                               NSLog(@"Failed to load interstitial ad with error: %@",
                                     [error localizedDescription]);
                               return;
                             }
                             self.interstitial = ad;
                             self.interstitial.fullScreenContentDelegate = self;
                           }];

コールバックを登録する

表示イベントに関する通知を受け取るには、返された広告の GADFullScreenContentDelegate to thefullScreenContentDelegate` プロパティを割り当てる必要があります。

Swift

interstitial?.fullScreenContentDelegate = self

SwiftUI

interstitialAd?.fullScreenContentDelegate = self

Objective-C

self.interstitial.fullScreenContentDelegate = self;

GADFullScreenContentDelegate プロトコルは、広告の表示が成功または失敗した場合のコールバックと、広告が閉じられた場合のコールバックを処理します。次のコードは、プロトコルを実装する方法を示しています。

Swift

func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func ad(_ ad: FullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
  print("\(#function) called with error: \(error.localizedDescription)")
  // Clear the interstitial ad.
  interstitial = nil
}

func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
  // Clear the interstitial ad.
  interstitial = nil
}

SwiftUI

func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called")
}

func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
  // Clear the interstitial ad.
  interstitialAd = nil
}

Objective-C

- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)ad:(id<GADFullScreenPresentingAd>)ad
    didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
  // Clear the interstitial ad.
  self.interstitial = nil;
}

- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
  // Clear the interstitial ad.
  self.interstitial = nil;
}

GAMInterstitialAd は使い捨てオブジェクトです。つまり、インタースティシャル広告を一度表示すると、再度表示することはできません。おすすめの方法は、GADFullScreenContentDelegateadDidDismissFullScreenContent: メソッドで別のインタースティシャル広告を読み込んでおくことです。この方法では、前のインタースティシャル広告の表示が終了したらすぐに次のインタースティシャル広告を読み込めます。

広告を表示する

インタースティシャルは、アプリの操作が一時中断するタイミングで表示される必要があります。ゲームレベルをクリアした後の合間やタスクが完了した直後などが適しています。

Swift

ad.present(from: self!)

SwiftUI

ビューの UI イベントをリッスンして、広告を表示するタイミングを判断します。

var body: some View {
  // ...
  }
  .onChange(of: countdownTimer.isComplete) { newValue in
    showGameOverAlert = newValue
  }
  .alert(isPresented: $showGameOverAlert) {
    Alert(
      title: Text("Game Over"),
      message: Text("You lasted \(countdownTimer.countdownTime) seconds"),
      dismissButton: .cancel(
        Text("OK"),
        action: {
          viewModel.showAd()
        }))

ビューモデルからインタースティシャル広告を表示します。

func showAd() {
  guard let interstitialAd = interstitialAd else {
    return print("Ad wasn't ready.")
  }

  interstitialAd.present(from: nil)
}

Objective-C

[self.interstitial presentFromRootViewController:self];

ベスト プラクティス

インタースティシャル広告が自分のアプリに適しているかどうかをよく見極めましょう。
インタースティシャル広告は、自然な移行ポイント(画面の切り替わりなど)があるアプリに適しています。 画像の共有やゲームレベルのクリアなど、アプリ内で特定のタスクが完了したタイミングがこうした切り替わりポイントになります。ユーザーも流れの中断を想定しているため、インタースティシャル広告を表示しても操作性を損ないません。アプリのワークフローのどの時点でインタースティシャル広告を表示するか、ユーザーがそれにどう反応しそうかをよく検討してください。
インタースティシャル広告を表示する際には必ずアクションを一時停止しましょう。
インタースティシャル広告には、テキスト、イメージ、動画などのさまざまな種類があります。アプリにインタースティシャル広告を表示する際は、アプリで一部のリソースの使用を停止して、広告でそのリソースを利用できるようにすることが重要です。たとえば、インタースティシャル広告を表示するための呼び出しを行う場合は、アプリで行っている音声出力を一時停止してください。音声出力は adDidDismissFullScreenContent: イベント ハンドラで再開できます。このハンドラはユーザーが広告の操作を終了すると呼び出されます。また、広告の表示中は、計算の集中処理(ゲームループなど)を一時的に停止することをご検討ください。こうすることで、グラフィックの読み込みが遅れたり、反応が停止したり、動画が途切れたりしなくなります。
十分な読み込み時間を確保しましょう。
インタースティシャル広告を適切なタイミングで表示することも大事ですが、それと同様に読み込みの待ち時間を短くすることも重要です。広告の読み込みを事前に完了しておくと、表示の段階で待ち時間が発生しません。
過度に広告を表示しないよう注意しましょう。
インタースティシャル広告の表示頻度を増やすことで収益の向上が見込める一方、ユーザー エクスペリエンスが損なわれ、クリック率の低下につながります。ユーザーがアプリを楽しめなくなるほど頻繁に広告を表示しないでください。
読み込み完了コールバックを使用してインタースティシャルを表示しないようにしましょう。
このイベントを使用すると、ユーザー エクスペリエンスが低下する可能性があります。代わりに、広告を表示する前にプリロードして、その広告が表示可能な状態かどうかを GAMInterstitialAdcanPresentFromRootViewController:error: メソッドで確認してください。

GitHub の例

インタースティシャル広告の完全な例を、ご希望の言語で確認する:

次のステップ