Welcome to "Learning the YouTube APIs in 10 Minutes."
I'm your host, Jeremy Walker.
I'm a YouTube developer advocate.
So let's get started.
I'm first going to talk to you about the Analytics API
itself, and then I'm going to talk to you about defining a
report and taking that information and testing your
report so you can basically get an idea of if
it's working or not.
And then from there, we'll take that information and
quickly move it into some code.
Now, the first three sections, doesn't matter what language
you're using.
The last section, I will use Java, but you can see from the
end of it when you actually go through it that you can apply
this to any language you want to go with.
So, the YouTube Analytics API--
if you're used to YouTube, you know that there's a bunch of
data, and we provide an Analytics window where you can
get a lot of that in a dashboard.
But you may want to mash up those reports in different
ways, or combine it with other data, or even automate
processes to handle that data.
And we let you do that through our API.
It's a HTTP RESTful API.
So you can write the URIs yourself, or we provide nine
libraries, so you can pick the library
that fits your language.
We also have a lot of examples so you don't have to write
code from scratch.
You can just grab ours and tinker with it a little bit to
fit your needs.
So let's get started and define your report.
So there's two types of reports you can get.
You have a channel and a content owner report.
Today we're going to cover channel report, which is
basically just getting information just for a
specific channel.
For partners who have multiple channels, they can do a
content owner report, where they can get information on
one of their channels or all of their channels and kind of
bundle it together.
I will be giving a talk on that at I/O that will be
recorded, so you'll be able to watch that as well if you're
interested.
But again, this presentation here actually applies to both,
so I would definitely recommend going through this
and then jumping to that.
So when you start with a report, after you've decided
on whether it's going to be a channel report or a content
owner report, now you have to decide OK, what
information do I need?
And that's where the big three come into play.
So there's metrics, which are the most important one.
Those are going to be any sort of activity or user activity
that we record--
so views, likes, uniques, subscribers added, loss, all
that great stuff.
So that's kind of the metric you're looking for-- well,
metric, of course.
And then we kind of bundle those together in what are
called dimensions.
So when you get views, you have to get a dimension on it
so you can decide on, you want it by days, or you want it by
week or month, or maybe you want it by country.
So we give you a bunch of those options because you can
kind of bundle it how you want.
Now if you don't want all that information and you want to
filter it a little bit, we have filters, so you can--
for instance, if you want views and you take them by
country, but maybe you're only interested in Japan, then you
can set a filter to only get Japan.
And after you come up with the metrics and dimensions and
possible filter you want, then you're going to want to look
at our page that's labeled Available Reports.
And I can show that to you right here.
So if you just search for the YouTube Analytics API, this
will be what comes up.
And under reports, because you have to choose one, you can
choose Channel Reports which is what I'm doing here.
And you can see available reports.
So what this is is it's a bunch of information about
what metrics and dimensions work with each other because
you can't just throw them all together and
hope it will work.
There's only certain reports that will
work with each other.
So you have to kind of go through this and make sure
it's working.
So in this case, I'm actually going to look at
just getting the views.
Now, you can see here, for metrics, I
combined a bunch of these.
I could combine views, comments, favorites added, a
bunch of stuff.
And then I can do it on a dimension by day, seven-day
totals, or a month.
I'm actually going to show you an example--
a month--
but you can combine those two, and on the right, you can
filter, either by country or by video, which is a video ID,
and then from there by country if you choose to do so.
So again, you kind of put your report together, get a rough
idea with the big three, and then you look at the available
reports for either the content owner or channel-- in our
case, channel--
and you decide what you want to do from there.
So now that you've kind of got the report you want, or
possibly reports you want, the next step is to
go to the API Explorer.
Now this is a really, really powerful tool that's available
on our website that basically lets you run reports without
writing any code, including authorization.
Because with the Analytics API, you do have to
authorize--
this is really important.
Because then you get the report working and in great
shape before you go to the code.
I see a lot of people who start writing code and then
they say they're having problems, when it's just that
they never had a proper report to begin with.
So always start with this.
That's a best practice.
And how this works is, again, you just search YouTube
Analytics API, and this will come up.
And if you go the Reference link, you'll see we have some
great information.
Again, there's a RESTful API, so if you want don't want to
use our libraries and you want to just construct the URLs
yourself, you can do that.
We list all the parameters.
And then we even provide some examples in Python,
JavaScript, in Java and Ruby, and then we have some more
libraries beyond that.
But at the bottom of the page is this API Explorer, is what
we call it.
And so this is where you can try out your reports before
you dive into code.
So you can see in the right here, there's
this Authorize Request.
I just hit that I want to authorize that.
And then now it's authorized, so now I can run requests as
if I've gone through the whole OAuth 2 process, which
actually is done on the background.
So let's get started.
So you can see at the bottom here, we have Metrics,
Dimensions, Filters, which were the big
three I'm talking about.
You can see above that Start Date and End Date.
It's pretty self-explanatory.
But at the top, you can see IDs.
And this is what you actually use to specify which channel
report you want.
So do you want a channel report, or a
content owner report?
If you're doing a content owner, you'd just type in
contentowner== and then your content owner ID.
But in this case, we're doing a channel report, so you'd
just do channel== and then you'll do your channel ID.
Now, you're probably not going to know your channel ID.
Most people don't.
It's this long string with a bunch of numbers and letters.
So that, actually, you have to get from the Data API.
So we have another API that's part of the YouTube API family
which is called the Data API.
And this lets you do most everything
you can do on YouTube.
And one of those things is to get
information on your own channel.
So we're going to actually do that.
And if you just search for the YouTube Data API, it'll be the
first thing that comes up.
It's also in the same area as the Analytics API.
But here you see V3 comes up.
And if you expand the reference, you'll
see there's a channel.
And I will actually want the list function.
Again, this lists out.
This looks pretty probably almost just like what you just
saw in the Analytics API page.
But you can see we list all the parameters so you can make
the URL yourself.
And then at the bottom, we have the API Explorer again,
so this is great.
This will actually give me my channel ID, so I'm just going
to authorize this really quickly.
And then I'm just going to put the ID--
I could put "snippet" in if I want more information.
And what's nice is you can just set mine equal to "true."
So this will get the channel associated with my account,
which is right here, which is the perfect one.
And here is the ID.
So I'm just going to grab this and drop it here.
Now I'm pretty much set to go, so I can start setting dates.
I'm actually going to run a monthly report, so I'm going
to run it for October and November.
So I just set my times here.
My metric, I just want "views." Dimensions, I'm going
to do "month." And then I'm not going to do any filter at
this point.
But I hit execute, and then you can see it constructs the
actual URL that goes out, and a little bit more information,
and then actually shows you the response.
You can see I got 1,100 views in November and
about 2,000 in October.
So this is kind of a great tool.
You can kind of mess around a lot with it until you get your
report just right.
And if you wanted to see filter, let's say I wanted to
see country.
We'll go with Brazil, see what that pulls up.
And OK, I have one in three views in Brazil.
So that's just an example of what you can do.
If I wanted to, I could also drop a video in here and grab
the video ID and put that in there, or whatever it may be.
So anyway, I have my report.
I'm all set.
Everything's looking good.
The nice thing now is when I jump into coding, a lot of
these parameters here map directly to the client
library, so you pretty much know where to plug and play.
So let's jump into that.
So I've told you a little bit about the API.
We've defined a report.
We've actually run it.
Everything looks good.
Now we just add it to our code.
So in this case, I'm going to use Java.
If you look here, I'm actually constructing a HTTP transport
object in a JSON factory.
These are just to handle the requests and responses.
And you'll see here is a credential.
I don't have all the coding here.
Again, this is an example that's online, so
you can go grab it.
There's a larger function that takes care of the
credentials for you.
I'm not going to go over that, but it takes care of the whole
OAuth 2 process, so you can have a look at that.
But the gist of this page is that, hey, I'm setting up a
YouTube object and a YouTube Analytics object.
And the YouTube object maps to the Data API because I need to
get my channel ID, right?
The other one is YouTube Analytics, which is where I'm
going to run my report.
So next thing I need to do is first get the channel ID.
And you can see I just call in the YouTube object Channels
List, and then I set mine, which if you look at the API
Explorer, it's pretty much exactly what I set.
So again, you don't actually need snippet.
I was just showing that so you could see it was my channel.
So I just set ID there, because I just
really need the ID.
I execute it, and then I would get a list back of channels.
And the first one would be my default one.
So now that I have that, I just run a report--
analytics.reports.query--
and then I just drop in the same information--
channel== and then that channel ID, and then I do the
dates, and then set the dimensions.
And then if I had any filters, I could set
them there as well.
And then I execute that.
That'll give me a result table object that will have all the
information that I can run through with, my own function.
So let's see what that looks like.
So this is all that stuff you just saw.
And I'll actually clear this out, and I'll rerun it.
So this is actually going out, and I've
already set up the OAuth.
So you can see it pulled from my default channel, and you
can see the numbers are the same.
So that gives you an idea how to get started really quick.
I've gone over the Analytics API, kind of what it's used
for, to match up reports, automated things, and combine
maybe external data with your data.
I've gone over defining a report, testing it out with
the API Explorer-- which is very, very important.
Make sure you do that.
That's a big, big best practice.
And then you port that over to your code.
I happened to show Java, but again, you could have done
this in Ruby, Objective C, whatever you want.
So now you should be set to go and have some fun with the
Analytics API.
So if you want to learn more, search YouTube Analytics API.
You can follow my team on google.com/+YouTubeDev.
And then you can follow me as well--
google.com/+JeremyJWalker.
Thank you for your time.