Ad Events

This guide explains how to use GADBannerViewDelegate and GADInterstitialDelegate to listen for ad events for banner and interstitial ads. These events allow you to track lifecycle events such as when an ad is loaded, an ad causes the user to leave the application.

Prerequisites

Helpful primers

You may want to read how to add interstitials to your project as motivation for using GADInterstitialDelegate to listen for interstitial ad events.

GADBannerViewDelegate implementation

Registering for banner events

To register for banner ad events, set the delegate property on DFPBannerView to an object that implements the GADBannerViewDelegate protocol. Generally, the class that implements banner ads will also act as the delegate class, in which case the delegate property can be set to self.

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADBannerViewDelegate {

  var bannerView: DFPBannerView!

  override func viewDidLoad() {
    super.viewDidLoad()
    bannerView = DFPBannerView(adSize: kGADAdSizeBanner)
    bannerView.delegate = 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;
}

Implementing banner events

Each of the methods in GADBannerViewDelegate is marked as optional, so you only need to implement the methods you want. This example implements each method and logs a message to the console.

Swift

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

// Tells the delegate an ad request failed.
func adView(_ bannerView: GADBannerView,
    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: GADBannerView) {
  print("adViewWillPresentScreen")
}

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

// Tells the delegate that the full screen view has been dismissed.
func adViewDidDismissScreen(_ bannerView: GADBannerView) {
  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: GADBannerView) {
  print("adViewWillLeaveApplication")
}

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");
}

Use cases

Here are some example use cases for these ad event methods.

Adding a banner to the view hierarchy once an ad is received

You may choose to hold off on adding a DFPBannerView to the view hierarchy until an ad is received. You can do this by listening for the adViewDidReceiveAd: event.

Swift

func adViewDidReceiveAd(_ bannerView: GADBannerView) {
  view.addSubview(bannerView)
}

Objective-C

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

The addSubview method automatically removes a view from its parent if it already has one, so it's safe to make this call every time.

Animating a banner ad

You can also use the adViewDidReceiveAd event to animate a banner ad once it's returned. This snippet shows how to fade in a banner ad.

Swift

func adViewDidReceiveAd(_ bannerView: GADBannerView) {
  bannerView.alpha = 0
  UIView.animate(withDuration: 1, animations: {
    bannerView.alpha = 1
  })
}

Objective-C

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

Third party analytics

The SDK automatically tracks clicks and impressions, but if you're also using a third-party analytics solution, you can also track each of the GADBannerViewDelegate calls separately.

Pausing and resuming the application

GADBannerViewDelegate has events to notify you when an click causes an overlay to be presented or dismissed, or invokes an external browser. If you want to know that these events happened because of ads, then register for these GADBannerViewDelegate methods.

But to catch all types of overlay presentations or external browser invocations, not just ones that come from ad clicks, your app is better off listening for the equivalent methods on UIViewController or UIApplication. Here is a table showing the equivalent iOS methods that are invoked at the same time as GADBannerViewDelegate methods.

GADBannerViewDelegate method iOS method
adViewWillPresentScreen: UIViewController's viewWillDisappear:
adViewWillDismissScreen: UIViewController's viewWillAppear:
adViewDidDismissScreeen: UIViewController's viewDidAppear:
adViewWillLeaveApplication: UIApplicationDelegate's applicationDidEnterBackground:

GADInterstitialDelegate implementation

Registering for interstitial events

To register for interstitial ad events, set the delegate property on DFPInterstitial to an object that implements the GADInterstitialDelegate protocol. Generally, the class that implements interstitial ads will also act as the delegate class, in which case the delegate property can be set to self.

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADInterstitialDelegate {

  var interstitial: DFPInterstitial!

  override func viewDidLoad() {
    super.viewDidLoad()
    interstitial = DFPInterstitial(adUnitID: "/6499/example/interstitial")
    interstitial.delegate = 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;
}

Implementing interstitial events

Each of the methods in GADInterstitialDelegate are marked as optional, so you only need to implement the methods you want. This example implements each method and logs a message to the console.

Swift

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

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

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

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

// Called just after dismissing an interstitial and it has animated off the screen.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
  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: GADInterstitial) {
  print("interstitialWillLeaveApplication")
}

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");
}

Use cases

Third party analytics

The SDK automatically tracks clicks and impressions, but if you're also using a third-party analytics solution, you can also track each of the GADInterstitialDelegate calls separately.

FAQ

Will I get GADBannerViewDelegate and GADInterstitialDelegate event notifications when using mediation?
Yes, mediation adapters are required to send all of these events, and these events are forwarded to your application.
Can I use the interstitialDidReceiveAd event to show the interstitial?
This goes against our best practices recommendation and can cause a poor user experience. Instead, pre-load the ad before you need to show it, and check the isReady method on DFPInterstitial to find out if it is ready to be shown.

Send feedback about...

SDK for DFP Users on iOS
Need help? Visit our support page.