Articles

AdMob Rewarded Video – Mobile Ads Garage #7

August 13, 2019


[MUSIC PLAYING] Hey, hey, everybody. I’m Andrew Brogdon, and welcome
back to the Mobile Ads Garage. Today, with the help of my
partner, Gary, the graphics guy, we’re going to talk about
rewarded video, a full screen video format from AdMob
that can help you monetize while increasing
engagement with your users. We’ll have screencasts and talk
implementation, but first– well, yeah, yeah, with
Katie– super nice engineer, by the way– that covered reward video,
but only through mediation. But now, AdMob has launched
its own rewarded video demand. That means you can get
rewarded video ads right from Google’s advertisers
with no mediation or third-party SDKs required. Well, yeah. Dude, I had a baby. I was in the room. Do you know what it’s like to
chase a two-foot tall version of me around a house? All right, I’m going to get
back to talking to YouTube now. If you haven’t used it
before, rewarded video is a full screen ad format
that gives users the option to watch a video in exchange
for something, usually an in-game reward. Because users have
the power of choice, they don’t have to see ads
they’re not interested in. And the ads they do see
are presented in full. That dynamic allows publishers
to build monetization into the mechanics
of their games, offering different paths
within an adventure or items that affect game play. And that’s key. We’ve seen publishers who, after
integrating rewarded video, saw an increase of overall user
engagement and long-term value. All right, so that’s
the what and the why. Let’s get to our screencast
and implement rewarded video in Android Studio. OK, so I’ve got a real
basic Android app here. It’s just got a button
to display an ad along with a text view that’s going to
keep a log of events as we go. And I’m just to wired up
to load an ad and show it. So how do I do that? With these steps. So first, I’ll make sure
that the SDK’s initialized. Then I’ll set up the
listener that will keep me wired into the ad lifecycle. And after that, it’s
just loading the ad, showing it, and
making sure I catch the rewards that come out. So step one, initialize the SDK. Normally for these
videos, I start with the SDK already imported. But I wanted to be sure to
mention that Google recently started distributing the
SDK via Maven and not in the SDK manager. So I’m here in my apps
project level gradle file. And you can see this
Google line here. That’s new for Android Studio
3.0, which will, hopefully, be released before this
video makes it to YouTube. If you’re using version
two, though, you could just add the Maven
repo manually like this. Here we go. And the URL’s just
maven.google.com. And this way, studio
will know which repo to use to go get the SDK. So once that’s done, you can go
into the app level gradle file and add the mobile ads SDK
the same way you normally would before. It’s just com.google.android.gms
and then Play Services ads. And always make sure you’re
using the latest version. Right now, it’s 11.4.2. All right, so with
that in place, let’s head into my
activity file and make the call to initialize the SDK. You only need to do this once
in your app per execution. Although, if you do it more
than once, it’s not a big deal. And I’m just going to call
the static method, initialize. And I’m giving it a
context and an app ID. Since this is just a test
app I’m messing around with and not a real
published app, I’m actually using the App ID from
our quickstart guide, which you can use as well if
you’re just playing around with the SDK. So don’t publish with
it, of course, but it’s fun for testing if you’re just
playing around with the SDK and it’s API. All right, so step one, done. Let’s move on to the listener. First, I’m going to come
up here and add to my class the rewarded video ad listener
interface so my activity can serve as the
listener for the ads and get callbacks for
events in the ad life cycle. Then I’m going to use Android
Studio’s implement methods right there to do
some code generation. So it’s going to stub
out these methods. And let’s talk about them. So I’ve got the big seven here. Number one is the
ad loaded callback. This gets fired when an
ad loads successfully. Then I’ve got ad opened. That will get called when an
ad opens and covers the screen. Video started, of course,
for when the playback starts. Ad closed for when
the ad is closed, and you’ve returned
to your app’s UI. Then on rewarded, this is
the reward item callback– we’ll get back to this
toward the end in step five. Ad left application,
this is called when focus leaves your app
to go to the Play Store or destination URL. And then ad failed
to load, which has an error code
to let you know that a load add call failed. OK, so I’m just going to drop
some log statements in here, and I’m going to leave the
reward callback blank for now. Like I said, we’ll come back
to that one in just a second. And I’m just going to
put some text in here, so I can see it
in that text view. OK, now I’m going
to scroll back up and make a member
variable that’s going to hold a reference to
the rewarded video ad singleton. And here, I’m going to
retrieve that reference using a static method in
the mobile ads class. Unlike all the other
formats, rewarded video uses the singleton pattern. So rather than instantiating
individual objects yourself for each ad, you just need to
grab a reference in the SDK and hang onto it. All your calls are going to
go through as one object. And now that I have
that reference, I can set this activity as the
listener for rewarded video ads. And I’m wired up. Cool. So that’s two steps down. Time to load an ad. Video ads are big,
so it’s a good idea to load them well in advance
and make sure they’re ready. I’m going to load mine as soon
as this activity’s created by calling load ad. And I’m going to give it an ad
unit and a new request object. And that’s the same
ad request class used for all the other formats, so
the usual methods for options and targeting and stuff, those
are right there in place. And the ad unit I’m using
here is a test unit. It will always return
test ads, which makes it safe to use while developing. You can find that same
ad unit right here in our rewarded video guide. And you’re welcome to
use it in your apps. If you’re just starting
out, you haven’t registered an ad unit of your own, or
any time you’re debugging and just want to make
sure you get test ads, you’re welcome to use this one. All right. So if I launch my app now,
I should see it in ad loads. And there it is. Cool. All right, on to step
four, showing the ad. First, let me just slip
a little line in here to enable the button
when it loads. I’m going to put that into
the on ad loaded callback, so my button will be lit. Perfect. Now I’m going to go up to the
click listener for the button. And I’m going to disable
it when it’s clicked, because we’ll be showing an ad. And then I’m going to check
the page is loaded method to make sure I’ve got an
ad ready, and then show. Now, technically,
I don’t really need to do the is loaded call
because I know, for a fact, I’ve got an ad. But it’s a good practice to
get in the habit of doing, and so I do recommend it. All right, let’s run it again. And I’ve got an ad loaded. Let’s show it. And there’s my test ad. All right. So one step left, and that’s
to reward the user when they watch a video completely. For that, I’m going to pop down
to the on rewarded callback. So this is fired when a user
watches a video to completion. And it’s how your app knows
to distribute a reward. And it comes with this
little reward item parameter that includes the
amount and type of reward. And those will have the data
that you put into the AdMob UI when you make your ad units. So depending on the
ad unit, you might want to have one that’s
10 coins, for example, and another one that
rewards 37 sheep. Whatever you put in, you’re
going to get out here. They come in the form
of a number and a string description. So I’m just going
to log the reward to show that it
happened, but you can do pretty much
whatever you want with this data in your own app. OK, so let’s run one last time. There’s my ad. So I’ll go ahead and click show. And we just need to
wait for this to finish. Let me quickly say,
by the way, that when they asked me to record
the video for the test ad, I didn’t think about the
fact that people are going to have to look at it,
like, 50 times while testing the rewarded code. So if you get to that 50th
go round and you’re thinking, dear lord, if I have to look
at this guy one more time– don’t feel bad. I completely understand. The good news is
we got the reward. We got our 10 coins, though,
so this implementation’s done. That’s all five
steps– initialize, set up the listener,
load, show, and reward. All right, so that’s rewarded
video in Android Studio. Actually, that’s kind of true. Rewarded ads are a
lot like interstitials in that you load them in
advance and then show them at a pause in the
flow of your app. In fact, a rewarded ad is
really just a video interstitial with an extra callback
to reward the user. So with Android down, let’s see
how rewarded ads work in iOS. For that, we’re going
to head in to Xcode. All right, here’s my iOS app
running on the simulator. I’ve got a button up
top to show an ad, and a log that will show
events as they come in. Now, how am I going
to get this wired up to show a rewarded video ad? Well, here are the
steps, and they’re almost identical to
the ones for Android. First, I’ll initialize
the SDK and set up a delegate that
will get callbacks for the events in
the ad lifecycle. And once that’s done, it’s
just load, show, and reward. So first up,
initialising the SDK. Here’s my AppDelegate. And I’ve already
got the SDK building into this app via CocoaPods,
so I can import it right here. And then I’ll hop down and call
the class method for configure on GADMobileAds. And I need to pass
that my application ID. There we go. Since this is just a sample app
that’s not really registered, I’m using the sample app ID
from our Get Started Guide on the AdMob developer site. If you’re just messing
around with the SDK, testing things out, you can use
this as well in your own test apps. Cool. So that’s step one. Now, let’s get the
delegate going. Here I am in my view controller. And the first thing I’m going
to do is import the SDK. Then I’ll make a
property that’ll store a reference to the
rewarded video singleton. So unlike the other
formats, rewarded video uses the singleton
pattern in iOS. So instead of creating your
own object for each ad, you just grab a reference to
the shared instance, which you’ll see right
here, and then you use that to make all your calls. And that’s all I’m
doing in this step. So next, I’m going to
assign this view controller as the delegate for that
rewarded video singleton. And Xcode’s going to
yell at me, correctly, because my view controller
doesn’t implement the delegate protocol. So I’ll pop up here, and
I’ll add that protocol. There we go. And I’m going to get a
new error, because I don’t implement the required methods. Now, XCode 9 has
this new thing where it will stub out the
protocol for you, but it only does the
required methods. And I want all of
them, so I’m going to do it through the magic
of cutting and pasting. This is like in a
cooking show where they make a roast
turkey that has to go in the oven for three hours. And they put it in,
and then, like, I just happen to have this other
turkey I put in three hours ago. And they pull out
the finished one. So you can get these
method signatures from our reference docs. They also auto-complete if
you start typing them in. Let me go down a list, though,
and I’ll cover each one. So, first we have
video ad did open. This is called when an ad
opens and covers the UI. Then we have ad did close. It’s the opposite–
when an ad closes and you go back to your app. This is ad did receive. This is the ad loaded
callback, basically, so this is where you get an ad. Ad did start playing, that’s
for when video playback begins. Ad will leave
application, that’s when somebody clicks
through on the ad and goes to the app
store or destination URL, YOU get that one. This is did fail to
load, which is called when an ad fails to load. You get an error code
explaining the problem. And finally, did reward
user with reward– and that’s the reward callback. We’ll cover that in step five. Cool. So the view controller
is now set up as the delegate for
rewarded video ads. That’s all the methods. Let’s load an ad. I’m just going to
call the load method. First, I’ll pass it
a GAD request object. And this is the same request
class used for other formats, so the same options and
targeting methods are there. And I’m also going to
give it an ad unit. And since I’m just
messing around here and need to use test ads, I’m
actually using the ad unit from our rewarded video
guide– which you can use too. It’s specially configured
to always return test ads. So you can mess around with
it, load ads, tap on them, click on them, whatever
you’re going to do, and you don’t have
to worry about it. Cool. So if I build this, I
should see an ad get loaded. And there it is. Cool. All right, so I’ve got an ad. Let’s show it. First up, my button
for showing an ad starts off disabled,
so I’m going to need to enable it in the
did receive ad delegate method. This gets called
when an ad loads. And so I’ll just enable that
button, so I can press it. Now, I’m going to scroll
back up to the action method for that button. And first, I’ll disable it. Since we’re going to show, we
want to disable the button. And then I’m going to
check the is ready property and call present. The is ready check here isn’t
really necessary, since I know for a fact that I
just loaded an ad, but it’s a good idea
to call it anyway. It doesn’t take much time. It’s a good thing to
check on each show. All right. So if I build and run this,
let’s see what happens. All right, there’s my ad. And we click the button,
and there’s our test ad. Cool. So, just one step
left, and that’s to reward the user when they
watch a video to completion. And that’s done in a did reward
user with reward delegate method. This method is invoked
whenever a video is watched to completion
and the user needs to be given their award. So I’m just going
to log this info, but your app or game can
do pretty much whatever you want with the reward data. It arrives in a GAD
ad reward object, which has two properties,
amount and type, for the reward. It’s going to be set to whatever
you entered when you created your ad unit in the AdMob UI. In this case, I
think it’s 10 coins, but it’s literally just
a number and a string. It can be almost
anything you want. All right. So now when I run this– get the simulator back up. And I’ll click show ad. And we just need to
watch this to completion, and that reward
callback will be fired. So we’re watching a video
of me in a video of me. I’m basically incepting. There we go. Cool. So if I close this,
there’s my reward callback just before the
I close callback. All right, so that’s
all five steps. Rewarded video ads
are finished for iOS. Job done. And that’s rewarded video. For links to our guides for
Android, iOS, and Unity, plus samples on GitHub,
check the description below. We’ve also got a link down
there for our support forum if you run into any problems. And as always, if you’ve got
a question about this episode or an idea for something
you’d like us to cover, leave a comment below, and Gary
and I will see you next time. [MUSIC PLAYING]

No Comments

Leave a Reply