ANDREW BROGDON: Hey, everybody.
I'm Andrew Brogdon and welcome back to the Mobile Ads Garage.
Today, with the help of Gary the graphics guy,
I'm going to cover the ad lifecycle.
I'm going to show you what events occur
in the life of an ad, how to use listeners and delegates to tap
into them, and then I'll have a real world example
that puts it all together.
If you're not already familiar with ad events,
they are a fundamental piece of the mobile ads SDK.
They can tell you what's going on with your ads,
like when they're done loading, when they've opened, and so on.
Every format has them, so if you're using the SDK at all,
this is an important thing to know about.
So here are the basic events in the life of an AdMob ad.
Now, some formats have some extra stuff.
Reward a Video has a reward event, for example.
But these are the big five.
Let's drill down.
When you make an ad request, the ad
either loads or fails to load.
So those are the first two events.
If the ad does load successfully, you move on.
First up is presenting, also called opening, on Android.
This means that the ad has caused something
to appear on screen that covers your interface.
When an interstitial or rewarded video ad gets displayed,
for example, this event happens.
It's a way for the SDK to say, heads up,
I just covered your UI with something.
The opposite of opening, of course,
is closing, or dismissing, the ad.
And last, we have leaving the app.
When the user taps on an ad, and the SDK
opens a browser or another app in response,
you get this event.
And it's actually the best one, because it
means the user and the ad were a good match for each other,
and you get credit for a clickthrough.
All right.
So those are the big five events,
but how do you tap into them?
For that, the SDK offers listeners on Android
and delegates on iOS.
There's actually a few different kinds.
There you go.
It's a scary big list, but the good news
is, you don't have to memorize it.
The ad object will tell you.
If you're on iOS, just look for a delegate property.
And on Android, look for a set listener method.
You find that, and you know which type
of listener or delegate goes with the object.
For example, here's the set listener method for an ad view.
I can see from the signature that it takes an AdListener
object.
And for iOS, it's the same deal.
Just look for a delegate property on the ad object.
There you go, GADBannerViewDelegate.
Once you've got that, make your own implementation
and add methods for the events you're interested in.
Loading, for example, on Android,
you can override the onAdLoaded method, and, on iOS,
you implement DidReceiveAd.
When an ad fails to load, there's
onAdFailedToLoad, and DidFailToReceiveAdWithError.
These include an error code that describes the problem.
For an ad opening, there's onAdOpened and
willPresentScreen.
And for dismissing or closing, you
have onAdClosed on Android, and two methods
on iOS, willDismissScreen, and didDismissScreen.
So you can get notified before and after the event.
And finally, if a clickthrough opens
a browser or external app, there is willLeaveApplication on iOS,
and onAdLeft application on Android.
One last little detail, and it's actually
a difference between the platforms-- on Android,
leaving the app also counts as opening the ad
if it's not already open.
So if your user taps on a banner, for example,
you'll get onAdOpened, then onAdLeftApplication,
and, when the user comes back, you'll see onAdClose get fired.
So that's a little extra info you get from the Android SDK.
All right, so we've covered the events
and how to find the right listener
or delegate and use it.
Let's put it all together with an example.
Say you've got an Android app that plays music videos
and have monetized it with a Native Express ad.
There's the interface.
There's a bunch of ways that ad events can help me here.
But first, I need to get wired up.
Here are the steps.
First, I'm going to set an ad listener for my Native Express
ad.
Now I'm going to add individual methods for the events I
care about, and then rock out.
So step one, setting the listener.
Native Express Ad view objects accept the ad listener class.
So I can call set ad listener and my activities onCreate,
and give it an instance, just like I would with an onClick
listener for a button.
And that's it, listener set.
Now I can add individual methods for the events I care about.
Let's say my ad view starts out hidden,
and I want to show it once the ad is loaded.
All I need to do is override onAdLoaded and put
in the code to make it visible.
So I get something like this.
How about when the ad fails to load?
Maybe I'd like to try again, just in case.
All I have to do is add another method
to my listener for onAdFailedToLoad.
It's good to put some kind of flag or count
in place, though, to make sure you don't keep
trying to load an ad over and over again, if there's
no internet connection.
Otherwise, the SDK will keep dutifully
trying to load forever.
Now this app is all about playing music
videos, which is great, because Gary rocks like no other.
Ironically, that's actually true.
But if my user taps on an ad and leaves the app
to go to a browser, I probably want to stop video playback.
I can do that by adding a listener method override
for on ad left application, and then some code
to stop playback.
That way, when a user taps on an ad, the video gets
paused, just as they leave for a browser or the Play Store.
And onAdClose tells me the user has come back
from leaving the app, so it's time
to crank the party back up.
All right, so that's our real-world example.
I've got a Native Express ad that's
shown when it's done loading, and an app
that smart enough to pause playback
when there's a click through.
This is the power that ad events give you,
and they're a great tool to have in your belt.
I used Android for this video, but if you
code in Swift or Objective C, we've got an example for you
up on GitHub.
I'll also sneak in our Android example
for anybody who's interested.
I've got guides that I can point you to for both platforms.
And as always, you're welcome to bring your technical questions
to our support forum.
And if you've got a question about this video,
or an idea for something we should cover,
leave a comment below, and Gary and I will see you next time.