アプリ起動時広告

このガイドは、アプリ起動時広告を統合するパブリッシャー様を対象としています。

アプリ起動時広告は、収益化を目指すパブリッシャー様向けの特別な広告フォーマットです アプリの読み込み画面ですアプリ起動時広告は、ユーザーはいつでも閉じることができます。 アプリ起動時広告は、ユーザーがアプリをフォアグラウンドに戻したときに表示できます。

アプリ起動時広告では、小さなブランディング領域が自動的に表示されるため、ユーザーはそのエリアにいることがわかる 説明します。アプリ起動時広告の例を以下に示します。

アプリ起動時広告を実装するには、大まかに次の手順を行う必要があります。

  1. 表示が必要になる前に広告を読み込むマネージャー クラスを作成します。
  2. アプリのフォアグラウンド イベント中にアドインを表示します。
  3. プレゼンテーション コールバックを処理する。

前提条件

必ずテスト広告でテストする

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

アプリ専用のテスト広告ユニット ID を使用すると、テスト広告を簡単に読み込むことができます オープン広告:

/21775744923/example/app-open

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

Mobile Ads SDK のテスト広告の仕組みについて詳しくは、テスト広告の仕組みに関するページ 広告

マネージャー クラスを実装する

広告はすぐに表示される必要があるため、必要な前に広告を読み込むことをおすすめします 表示されます。これにより、ユーザーがサイトにアクセスしたときに、すぐに広告を掲載できる準備が整います 説明します。マネージャー クラスを実装して、必要な場合に事前に広告リクエストできるようにしましょう。 広告を表示します

AppOpenAdManager という新しいシングルトン クラスを作成し、次のように入力します。 次のようになります。

Swift

class AppOpenAdManager: NSObject {
  var appOpenAd: GADAppOpenAd?
  var isLoadingAd = false.
  var isShowingAd = false

  static let shared = AppOpenAdManager()

  private func loadAd() async {
    // TODO: Implement loading an ad.
  }

  func showAdIfAvailable() {
    // TODO: Implement showing an ad.
  }

  private func isAdAvailable() -> Bool {
    // Check if ad exists and can be shown.
    return appOpenAd != nil
  }
}

Objective-C

@interface AppOpenAdManager ()
@property(nonatomic, strong) GADAppOpenAd *appOpenAd;
@property(nonatomic, assign) BOOL isLoadingAd;
@property(nonatomic, assign) BOOL isShowingAd;

@end

@implementation AppOpenAdManager

+ (nonnull AppOpenAdManager *)sharedInstance {
  static AppOpenAdManager *instance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    instance = [[AppOpenAdManager alloc] init];
  });
  return instance;
}

- (void)loadAd {
  // TODO: Implement loading an ad.
}

// Add this method to the .h file.
- (void)showAdIfAvailable {
  // TODO: Implement showing an ad.
}

- (BOOL)isAdAvailable {
  // Check if ad exists and can be shown.
  return self.appOpenAd != nil;
}

@end

広告を読み込む

次のステップでは、loadAd() メソッドに記入します。

Swift

private func loadAd() async {
  // Do not load ad if there is an unused ad or one is already loading.
  if isLoadingAd || isAdAvailable() {
    return
  }
  isLoadingAd = true

  do {
    appOpenAd = try await GADAppOpenAd.load(
      withAdUnitID: "/21775744923/example/app-open", request: GAMRequest())
  } catch {
    print("App open ad failed to load with error: \(error.localizedDescription)")
  }
  isLoadingAd = false
}

Objective-C

- (void)loadAd {
  // Do not load ad if there is an unused ad or one is already loading.
  if (self.isLoadingAd || [self isAdAvailable]) {
    return;
  }
  self.isLoadingAd = YES;

  [GADAppOpenAd loadWithAdUnitID:@"/21775744923/example/app-open"
                       request:[GAMRequest request]
             completionHandler:^(GADAppOpenAd *_Nullable appOpenAd, NSError *_Nullable error) {
               self.isLoadingAd = NO;
               if (error) {
                 NSLog(@"Failed to load app open ad: %@", error);
                 return;
               }
               self.appOpenAd = appOpenAd;
             }];
}

広告を表示

次のステップでは、showAdIfAvailable() メソッドに記入します。広告が表示されなかった場合 広告の読み込みが試行されます。

Swift

func showAdIfAvailable() {
  // If the app open ad is already showing, do not show the ad again.
  guard !isShowingAd else { return }

  // If the app open ad is not available yet but is supposed to show, load
  // a new ad.
  if !isAdAvailable() {
    Task {
      await loadAd()
    }
    return
  }

  if let ad = appOpenAd {
    isShowingAd = true
    ad.present(fromRootViewController: nil)
  }
}

Objective-C

- (void)showAdIfAvailable {
  // If the app open ad is already showing, do not show the ad again.
  if (self.isShowingAd) {
    return;
  }

  // If the app open ad is not available yet but is supposed to show, load a
  // new ad.
  if (![self isAdAvailable]) {
    [self loadAd];
    return;
  }

  self.isShowingAd = YES;
  [self.appOpenAd presentFromRootViewController:nil];
}

アプリのフォアグラウンド イベント中に広告を表示する

アプリケーションがアクティブになったら showAdIfAvailable() を呼び出して、次の場合に広告を表示します。 新しい関数がロードされます

UISceneDelegate

Swift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  // ...

  func applicationDidBecomeActive(_ application: UIApplication) {
    // Show the app open ad when the app is foregrounded.
    AppOpenAdManager.shared.showAdIfAvailable()
  }
}

Objective-C

@implementation AppDelegate
// ...

- (void) applicationDidBecomeActive:(UIApplication *)application {
  // Show the app open ad when the app is foregrounded.
  [AppOpenAdManager.sharedInstance showAdIfAvailable];
}

@end

プレゼンテーション コールバックを処理する

アプリにアプリ起動時広告を表示する場合は、 GADFullScreenContentDelegate: 特定のプレゼンテーション イベントを処理します。イン 最初にアプリ起動時広告をリクエストしたら 次にアプリ起動時広告をリクエストします 終了します。

AppOpenAdManager クラスに以下を追加します。

Swift

class AppOpenAdManager: NSObject, GADFullScreenContentDelegate {
  // ...

  private func loadAd() async {
    // Do not load ad if there is an unused ad or one is already loading.
    if isLoadingAd || isAdAvailable() {
      return
    }
    isLoadingAd = true

    do {
      appOpenAd = try await GADAppOpenAd.load(
        withAdUnitID: "/21775744923/example/app-open", request: GAMRequest())
      appOpenAd?.fullScreenContentDelegate = self
    } catch {
      print("App open ad failed to load with error: \(error.localizedDescription)")
    }
    isLoadingAd = false
  }

  // ...

  // MARK: - GADFullScreenContentDelegate methods

  func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("App open ad will be presented.")
  }

  func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    appOpenAd = nil
    isShowingAd = false
    // Reload an ad.
    Task {
      await loadAd()
    }
  }

  func ad(
    _ ad: GADFullScreenPresentingAd,
    didFailToPresentFullScreenContentWithError error: Error
  ) {
    appOpenAd = nil
    isShowingAd = false
    // Reload an ad.
    Task {
      await loadAd()
    }
  }
}

Objective-C

@interface AppOpenAdManager () <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADAppOpenAd *appOpenAd
@property(nonatomic, assign) BOOL isLoadingAd;
@property(nonatomic, assign) BOOL isShowingAd;

@end

@implementation AppOpenAdManager

// ...

- (void)loadAd {
  // Do not load ad if there is an unused ad or one is already loading.
  if (self.isLoadingAd || [self isAdAvailable]) {
    return;
  }
  self.isLoadingAd = YES;

  [GADAppOpenAd loadWithAdUnitID:@"/21775744923/example/app-open"
                       request:[GAMRequest request]
             completionHandler:^(GADAppOpenAd *_Nullable appOpenAd, NSError *_Nullable error) {
              self.isLoadingAd = NO;
               if (error) {
                 NSLog(@"Failed to load app open ad: %@", error);
                 return;
               }
               self.appOpenAd = appOpenAd;
               self.appOpenAd.fullScreenContentDelegate = self;
             }];
}

- (BOOL)isAdAvailable {
  // Check if ad exists and can be shown.
  return self.appOpenAd != nil;
}

// ...

#pragma mark - GADFullScreenContentDelegate methods

- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
  NSLog(@"App open ad is will be presented.");
}

- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
  self.appOpenAd = nil;
  self.isShowingAd = NO;
  // Reload an ad.
  [self loadAd];
}

- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
    didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
  self.appOpenAd = nil;
  self.isShowingAd = NO;
  // Reload an ad.
  [self loadAd];
}

@end

広告の有効期限に関する考察

期限切れの広告を表示しないようにするには、アプリのデリゲートにメソッドを追加します。 広告参照が読み込まれてからの経過時間を確認できます。

AppOpenAdManager に、loadTime という Date プロパティを追加し、 プロパティの値を指定します。次に、次の場合に true を返すメソッドを追加します。 広告の読み込みから一定時間経過していない確認事項 広告を表示する前に、広告参照の有効性を確認する。

Swift

class AppOpenAdManager: NSObject, GADFullScreenContentDelegate {
  var appOpenAd: GADAppOpenAd?
  var isLoadingAd = false.
  var isShowingAd = false
  var loadTime: Date?
  let fourHoursInSeconds = TimeInterval(3600 * 4)

  // ...

  private func loadAd() async {
    // Do not load ad if there is an unused ad or one is already loading.
    if isLoadingAd || isAdAvailable() {
      return
    }
    isLoadingAd = true

    do {
      appOpenAd = try await GADAppOpenAd.load(
        withAdUnitID: "/21775744923/example/app-open", request: GAMRequest())
      appOpenAd?.fullScreenContentDelegate = self
      loadTime = Date()
    } catch {
      print("App open ad failed to load with error: \(error.localizedDescription)")
    }
    isLoadingAd = false
  }

  private func wasLoadTimeLessThanFourHoursAgo() -> Bool {
    guard let loadTime = loadTime else { return false }
    // Check if ad was loaded more than four hours ago.
    return Date().timeIntervalSince(loadTime) < fourHoursInSeconds
  }

  private func isAdAvailable() -> Bool {
    // Check if ad exists and can be shown.
    return appOpenAd != nil && wasLoadTimeLessThanFourHoursAgo()
  }
}

Objective-C

static NSTimeInterval const fourHoursInSeconds = 3600 * 4;

@interface AppOpenAdManager () <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADAppOpenAd *appOpenAd
@property(nonatomic, assign) BOOL isLoadingAd;
@property(nonatomic, assign) BOOL isShowingAd;
@property(weak, nonatomic) NSDate *loadTime;

@end

@implementation AppOpenAdManager

// ...

- (void)loadAd {
  // Do not load ad if there is an unused ad or one is already loading.
  if (self.isLoadingAd || [self isAdAvailable]) {
    return;
  }
  self.isLoadingAd = YES;

  [GADAppOpenAd loadWithAdUnitID:@"/21775744923/example/app-open"
                       request:[GAMRequest request]
             completionHandler:^(GADAppOpenAd *_Nullable appOpenAd, NSError *_Nullable error) {
              self.isLoadingAd = NO;
               if (error) {
                 NSLog(@"Failed to load app open ad: %@", error);
                 return;
               }
               self.appOpenAd = appOpenAd;
               self.appOpenAd.fullScreenContentDelegate = self;
               self.loadTime = [NSDate date];
             }];
}

- (BOOL)wasLoadTimeLessThanFourHoursAgo {
  // Check if ad was loaded more than four hours ago.
  return [[NSDate Date] timeIntervalSinceDate:self.loadTime] < fourHoursInSeconds;
}

- (BOOL)isAdAvailable {
  // Check if ad exists and can be shown.
  return self.appOpenAd != nil && [self wasLoadTimeLessThanFourHoursAgo];
}

@end

コールド スタートと読み込み画面

このドキュメントでは、ユーザーが特定のアクションを メモリ内で一時停止されたときにアプリをフォアグラウンドで処理します。「コールド スタート」次の場合に発生: アプリが起動されたものの、以前にメモリ内で一時停止されていなかった場合。

コールド スタートの例として、ユーザーが初めてアプリを起動したときが挙げられます。 コールド スタートでは、読み込み済みのアプリ起動時広告を 表示されます。広告のリクエストから広告が届くまでの所要時間 ユーザーがそれまでにアプリを使用できなくなった場合も 背景情報のない広告を表示して驚かされました。エラーの原因となるため、この方法は ユーザーエクスペリエンスが低下します

コールド スタート時にアプリ起動時広告を使用する場合のおすすめの方法は、読み込み画面を使用することです ゲームアセットやアプリアセットを読み込むこと、 表示されます。アプリの読み込みが完了し、ユーザーをメイン アプリのコンテンツに広告が表示されないようにできます

ベスト プラクティス

Google はアプリの読み込み画面を収益化するためにアプリ起動時広告を作成しましたが、 おすすめの方法を念頭に置きながら 説明します。必ず次の作業を行ってください。

  • アプリ起動時広告は、ユーザーが アプリを数回起動させます。
  • ユーザーが待たされるはずの時間帯にアプリ起動時広告を表示 アプリが読み込まれるようにします。
  • アプリ起動時広告の下に読み込み画面があり、 広告が閉じられる前に読み込みを完了した場合、 adDidDismissFullScreenContent メソッドの読み込み画面。

GitHub 上のサンプル全体

Swift Objective-C

次のステップ

詳しくは、ユーザーのプライバシーについての説明をご覧ください。