广告事件

本指南介绍如何使用 GADBannerViewDelegateGADInterstitialDelegate 监听横幅广告和插页式广告的广告事件。借助这些事件,您可以跟踪广告生命周期事件,例如何时加载广告以及广告引起用户离开应用等。

前提条件

实用的入门信息

您可能需要阅读如何将插页式广告添加到项目中,这样才可以使用 GADInterstitialDelegate 监听插页式广告事件。

GADBannerViewDelegate 实现

注册横幅广告事件

要注册横幅广告事件,请将 DFPBannerView 上的 delegate 属性设置为实现 GADBannerViewDelegate 协议的对象。通常,实现横幅广告的类也可以用作委托类,在这种情况下委托属性可设置为 self

Objective-C

#import "DFPBannerView.h"
#import "GADBannerViewDelegate.h"

@interface ViewController ()<GADBannerViewDelegate>

@property(nonatomic, strong) DFPBannerView *bannerView;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.bannerView = [[DFPBannerView alloc] initWithAdSize:kGADAdSizeBanner];
  self.bannerView.delegate = self;
}

Swift

import UIKit
import GoogleMobileAds

class ViewController: UIViewController, GADBannerViewDelegate {

  var bannerView: DFPBannerView!

  override func viewDidLoad() {
    super.viewDidLoad()
    bannerView = DFPBannerView(adSize: kGADAdSizeBanner)
    bannerView.delegate = self
  }
}

实现横幅广告事件

GADBannerViewDelegate 中的每个方法都是可选方法,因此您只需实现所需的方法即可。以下示例实现了每个方法并将消息记录到控制台。

Objective-C

// Tells the delegate an ad request loaded an ad.
- (void)adViewDidReceiveAd:(DFPBannerView *)adView {
  NSLog(@"adViewDidReceiveAd");
}

// Tells the delegate an ad request failed.
- (void)adView:(DFPBannerView *)adView
    didFailToReceiveAdWithError:(GADRequestError *)error {
  NSLog(@"adView:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}

// Tells the delegate that a full screen view will be presented in response
// to the user clicking on an ad.
- (void)adViewWillPresentScreen:(DFPBannerView *)adView {
  NSLog(@"adViewWillPresentScreen");
}

// Tells the delegate that the full screen view will be dismissed.
- (void)adViewWillDismissScreen:(DFPBannerView *)adView {
  NSLog(@"adViewWillDismissScreen");
}

// Tells the delegate that the full screen view has been dismissed.
- (void)adViewDidDismissScreen:(DFPBannerView *)adView {
  NSLog(@"adViewDidDismissScreen");
}

// Tells the delegate that a user click will open another app (such as
// the App Store), backgrounding the current app.
- (void)adViewWillLeaveApplication:(DFPBannerView *)adView {
  NSLog(@"adViewWillLeaveApplication");
}

Swift

// Tells the delegate an ad request loaded an ad.
func adViewDidReceiveAd(bannerView: DFPBannerView!) {
  print("adViewDidReceiveAd")
}

// Tells the delegate an ad request failed.
func adView(bannerView: DFPBannerView!,
    didFailToReceiveAdWithError error: GADRequestError!) {
  print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}

// Tells the delegate that a full screen view will be presented in response
// to the user clicking on an ad.
func adViewWillPresentScreen(bannerView: DFPBannerView!) {
  print("adViewWillPresentScreen")
}

// Tells the delegate that the full screen view will be dismissed.
func adViewWillDismissScreen(bannerView: DFPBannerView!) {
  print("adViewWillDismissScreen")
}

// Tells the delegate that the full screen view has been dismissed.
func adViewDidDismissScreen(bannerView: DFPBannerView!) {
  print("adViewDidDismissScreen")
}

// Tells the delegate that a user click will open another app (such as
// the App Store), backgrounding the current app.
func adViewWillLeaveApplication(bannerView: DFPBannerView!) {
  print("adViewWillLeaveApplication")
}

实际应用示例

以下是这些广告事件方法的一些实际应用示例。

在接收到广告之后才将横幅广告添加到视图层次结构中

您可以选择在接收到广告之前暂不将 DFPBannerView 添加到视图层次结构中。为此,您可以监听 adViewDidReceiveAd: 事件。

Objective-C

- (void)adViewDidReceiveAd:(DFPBannerView *)adView {
  [self.view addSubview:adView];
}

Swift

func adViewDidReceiveAd(bannerView: DFPBannerView!) {
  view.addSubview(bannerView)
}

如果父视图中已经有视图,则 addSubview 会自动移除该视图,因此您可以放心地调用此方法。

为横幅广告制作动画

您还可以在系统返回横幅广告后使用 adViewDidReceiveAd 为广告制作动画效果。这段代码显示出如何以淡入效果展示横幅广告。

Objective-C

- (void)adViewDidReceiveAd:(DFPBannerView *)adView {
  adView.alpha = 0;
  [UIView animateWithDuration:1.0 animations:^{
    adView.alpha = 1;
  }];
}

Swift

func adViewDidReceiveAd(bannerView: DFPBannerView!) {
  bannerView.alpha = 0
  UIView.animateWithDuration(1, animations: {
    bannerView.alpha = 1
  })
}

第三方分析

SDK 会自动跟踪点击和展示,但如果您还使用第三方分析解决方案,也可以单独跟踪每个 GADBannerViewDelegate 调用。

暂停和恢复应用

当某次点击引发重叠式广告展示或关闭或者调用外部浏览器时,GADBannerViewDelegate 会将这些事件通知给您。如果您希望知道这些事件是由于广告而发生的,请注册 GADBannerViewDelegate 方法。

但是,如果您要获取所有类型(而不仅仅是由于广告点击而发生)的重叠式广告展示或外部浏览器调用,您的应用最好监听 UIViewControllerUIApplication 上具有同样功效的方法。 下表显示了可与 GADBannerViewDelegate 方法同时调用且具有同样功效的 iOS 方法。

GADBannerViewDelegate 方法 iOS 方法
adViewWillPresentScreen: UIViewController 的 viewWillDisappear:
adViewWillDismissScreen: UIViewController 的 viewWillAppear:
adViewDidDismissScreeen: UIViewController 的 viewDidAppear:
adViewWillLeaveApplication: UIApplicationDelegate 的 applicationDidEnterBackground:

GADInterstitialDelegate 实现

注册插页式广告事件

要注册插页式广告事件,请将 DFPInterstitial 上的 delegate 属性设置为实现 GADInterstitialDelegate 协议的对象。通常,实现插页式广告的类也可以用作委托类,在这种情况下委托属性可设置为 self

Objective-C

#import "DFPInterstitial.h"
#import "GADInterstitialDelegate.h"

@interface ViewController ()<GADInterstitialDelegate>

@property(nonatomic, strong) DFPInterstitial *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.interstitial = [[DFPInterstitial alloc] initWithAdUnitID:"/6499/example/interstitial"];
  self.interstitial.delegate = self;
}

Swift

import UIKit
import GoogleMobileAds

class ViewController: UIViewController, GADInterstitialDelegate {

  var interstitial: DFPInterstitial!

  override func viewDidLoad() {
    super.viewDidLoad()
    interstitial = DFPInterstitial(adUnitID: "/6499/example/interstitial")
    interstitial.delegate = self
  }
}

实现插页式广告事件

GADInterstitialDelegate 中的每个方法都是可选方法,因此您只需实现所需的方法即可。以下示例实现了每个方法并将消息记录到控制台。

Objective-C

// Called when an interstitial ad request succeeded.
- (void)interstitialDidReceiveAd:(DFPInterstitial *)ad {
  NSLog(@"interstitialDidReceiveAd");
}

// Called when an interstitial ad request failed.
- (void)interstitial:(DFPInterstitial *)ad
    didFailToReceiveAdWithError:(GADRequestError *)error {
  NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}

// Called just before presenting an interstitial.
- (void)interstitialWillPresentScreen:(DFPInterstitial *)ad {
  NSLog(@"interstitialWillPresentScreen");
}

// Called before the interstitial is to be animated off the screen.
- (void)interstitialWillDismissScreen:(DFPInterstitial *)ad {
  NSLog(@"interstitialWillDismissScreen");
}

// Called just after dismissing an interstitial and it has animated off the screen.
- (void)interstitialDidDismissScreen:(DFPInterstitial *)ad {
  NSLog(@"interstitialDidDismissScreen");
}

// Called just before the app will background or terminate because the user clicked on an
// ad that will launch another app (such as the App Store).
- (void)interstitialWillLeaveApplication:(DFPInterstitial *)ad {
  NSLog(@"interstitialWillLeaveApplication");
}

Swift

// Called when an interstitial ad request succeeded.
func interstitialDidReceiveAd(ad: DFPInterstitial!) {
  print("interstitialDidReceiveAd")
}

// Called when an interstitial ad request failed.
func interstitial(ad: DFPInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
  print("interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)")
}

// Called just before presenting an interstitial.
func interstitialWillPresentScreen(ad: DFPInterstitial!) {
  print("interstitialWillPresentScreen")
}

// Called before the interstitial is to be animated off the screen.
func interstitialWillDismissScreen(ad: DFPInterstitial!) {
  print("interstitialWillDismissScreen")
}

// Called just after dismissing an interstitial and it has animated off the screen.
func interstitialDidDismissScreen(ad: DFPInterstitial!) {
  print("interstitialDidDismissScreen")
}

// Called just before the application will background or terminate because the user clicked on an
// ad that will launch another app (such as the App Store).
func interstitialWillLeaveApplication(ad: DFPInterstitial!) {
  print("interstitialWillLeaveApplication")
}

实际应用示例

第三方分析

SDK 会自动跟踪点击和展示,但如果您还使用第三方分析解决方案,也可以单独跟踪每个 GADInterstitialDelegate 调用。

常见问题解答

在使用中介功能时,我是否可以获得 GADBannerViewDelegateGADInterstitialDelegate 事件通知?
可以,中介适配器必须发送所有这些事件,并且系统会将这些事件转发给您的应用。
我是否可以使用 interstitialDidReceiveAd 事件展示插页式广告?
这违背了我们的最佳做法建议,并且可能会导致用户体验不佳。不过,您可以在需要展示相应的广告之前进行预加载,并检查 DFPInterstitial 上的 isReady 方法以了解广告是否已准备好进行展示。

发送以下问题的反馈:

此网页
Google Mobile Ads SDK for DoubleClick for Publishers iOS Apps
需要帮助?请访问我们的支持页面