Unable to retrieve video metrics using Youtube Analytics API - youtube-api

According to the Youtube Analytics API documentation (https://developers.google.com/youtube/analytics/v1/available_reports), it looks like you should be able to retrieve metrics for specific videos using the "video" dimension. I am able to get all other metrics successfully - either specifying dimensions like "day" and "country" or supplying no dimension at all.
But when I change the dimensions value to "video", I get a 400 error code with the message "The query is not supported. Check the documentation for supported queries." This is a channel report - not a content owner report - but according to the documentation, this should be a valid report request. I've even tried limiting the result set with a number of extra parameters like start-index, max-results, and sort.
WORKS
client.execute(:api_method => "youtubeAnalytics.reports.query",
:parameters => {'ids' => "channel==##USER_ID##",
"start-date" => "2012-01-01", "end-date" => "2012-02-01",
"metrics" => "views"})
client.execute(:api_method => "youtubeAnalytics.reports.query",
:parameters => {'ids' => "channel==##USER_ID##",
"start-date" => "2012-01-01", "end-date" => "2012-02-01",
"metrics" => "views",
"dimensions" => "day"})
DOES NOT WORK - returns 400 error
client.execute(:api_method => "youtubeAnalytics.reports.query",
:parameters => {'ids' => "channel==##USER_ID##",
"start-date" => "2012-01-01", "end-date" => "2012-02-01",
"metrics" => "views",
"dimensions" => "video"})
client.execute(:api_method => "youtubeAnalytics.reports.query",
:parameters => {'ids' => "channel==##USER_ID##",
"start-date" => "2012-01-01", "end-date" => "2012-02-01",
"metrics" => "views",
"dimensions" => "video",
"start-index" => 1,
"max-results" => 5,
"sort" => "views"})
Has anyone been able to make a successful request for a channel report for video level details?

So the following does work:
channel==USER_ID
start-date=YYYY-MM-DD
end-date=YYYY-MM-DD
metrics=views
dimensions=video
max-results=10
sort=-views
The important thing is that you need to sort by descending views if you want to run a dimensions=video report, and you can only retrieve at most 10 results. This is explained in the second table at
https://developers.google.com/youtube/analytics/v1/available_reports#Channel_Reports
The 10 max results mentioned in their docs, ordered by decreasing views, is obviously an artificial limit imposed by the backend source for Analytics data, but that's all the API could support before. Google just updated it so you can get up to 200 video results - https://developers.google.com/youtube/analytics/revision_history
If you're in a scenario where you want to get Analytics data for arbitrary videos in a given account, not just the 10 with the most views, you need to set the dimension to something other than video, and then run a report with a filter= set to each video id in your account that you're interested in. Again, this might change in the future, but as of right now the Analytics API isn't suited for getting a huge dump of data for every single video in an account in a single API call.

Note that a recent change, in Aug 2014, to YouTube API now allows fetching metrics for up to 200 videos per API call.
See https://developers.google.com/youtube/analytics/revision_history for Aug 28, 2014

Related

Image thumbnails for a LiveBroadcast object

I can't seem to associate image thumbnails with a LiveBroadcast object as per the documentation when trying to create a live stream via the API.
For my application, I am using the following reference:
https://developers.google.com/youtube/v3/live/docs/liveBroadcasts#snippet.thumbnails
Here's an example of my payload sent to /liveBroadcasts:
array:3 [
"snippet" => array:5 [
"title" => "Test broadcast"
"description" => "Test description",
"thumbnails" => array:1 [
"default" => array:3 [
"url" => "my-image"
"width" => 120
"height" => 90
]
]
"scheduledStartTime" => "2021-03-01T21:35:37.000000Z"
"scheduledEndTime" => "2021-03-01T21:40:37.000000Z"
]
"contentDetails" => array:2 [
"enableAutoStart" => true
"enableAutoStop" => true
]
"status" => array:1 [
"privacyStatus" => "public"
]
]
The URL to my-image of course is actually a working URL.
Must the images in thumbnails be previously uploaded to YouTube and hosted there instead? What else could I be missing?
Thank you!
According to the official specs of the LiveBroadcasts.insert and LiveBroadcasts.update API endpoints, you're not allowed to set the respective live broadcast resource object's thumbnails property when invoking either of these endpoints:
Neither the request body of the former endpoint, nor the request body of the latter one has specified the property thumbnails among those accepted to be initialized or, respectively, modified.
For to update your live broadcast's thumbnails, you have to use the Thumbnails.set API endpoint instead, invoked appropriately with its request parameter videoId set to the ID of your broadcast.
For PHP (though it's not clear from your post what's your programming language/environment), look for the official Google sample code that is uploading thumbnails to YouTube using Thumbnails.set: upload_thumbnail.php.

Can I get duration of the videos from YouTuve API3.0 even if it's search result?

I know it can get duration if fetch particular video directly.
What if it's search result?
I'm pretty sure that it provides us video's title, unique ID, and published date.
In my code below, any error won't occur unless I put contentDetails into 4th line.
It seems that the error occurs when there are more than two in part section.
If I leave it just as :part => 'snippet',, everything works fine.
However I have to add contentDetails to gain information about duration of the videos on Youtube.
How can I achieve it?
videos_controller.rb
#search_response = client.execute!(
:api_method => youtube.search.list,
:parameters => {
:part => 'snippet, contentDetails',
:q => 'cats',
:maxResults => 20,
:order => 'date',
:pageToken => pageToken
}
)
Error
ActionView::Template::Error (undefined method `prev_page_token' for nil:NilClass):
1:
2: <% if !#search_response.prev_page_token.nil? %>
The problem is that the part parameter of the Search:list request doesn't take contentDetails. The documentation for the part parameter in the Search:list request explains it like this :
The part parameter specifies a comma-separated list of one or more
search resource properties that the API response will include. Set the
parameter value to snippet.
Notice that it says :
one or more search resource properties
But the search resource doesn't contain the contentDetails property. So I think you might be getting a badRequest (400) response.
But you do get the video's duration in contentDetails property of a video resource.
UPDATE
Okay if you have decided to use Videos: list API, so that you can get the contentDetails of all the videos in the search result with a single request, this is one way to do it (I'm assuming that the request gets a successful response) :
# Get the search result like you did.
#search_response = client.execute!(
:api_method => youtube.search.list,
:parameters => {
:part => 'id',
:q => 'cats',
:maxResults => 20,
:order => 'date',
:pageToken => pageToken
}
)
# Extract the ids of only the videos in the search result and make
# a comma separated list. Note if there aren't any videos in the search
# result the ids will contain an empty string.
ids = #search_response.items.select do |item|
# You only want the 'videos'.
item.id.kind == 'youtube#video'
end.map do |video|
# Gets the video's id.
video.id.videoId
end.join(',')
# Now use it to get the list of videos with content details from
# Videos: list.
#videos = client.execute!(
:api_method => youtube.video.list,
:parameters => {
# Whatever you want from a Video resource.
:part => 'snippet, contentDetails',
:id => ids
}
)
Note these videos correspond to the ones you get in a single search (one you do above), and I'm not really sure if the order of the result is the same as the search result. I think you can now use #videos to get the details of the videos instead of using #search_response, but you will have to depend on #search_response to control the search parameters like queries and pagination.

Send google analytics event in ruby

I have the following code for google analytics real time, in ruby:
# Execute the query
sa_visitCount = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
'ids' => "ga:" + saprofileID,
'metrics' => "ga:activeVisitors",
})
Followed with:
send_event('sa_realtimevisitors', current: sa_visitCount.data.rows)
This works great and returns an active visitor count.
Now I am trying to change this to return an event I have created on google analytics. Here is what the event looks like. You find this under Real-Time and Events. I am trying to use the Event Category "Ad" Listed as #2 in the screenshot.
The code I am using to attempt this in ruby is as follows.
phonehub_visitCount = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
'ids' => "ga:" + saprofileID,
'metrics' => "ga:sessionsWithEvent",
})
Followed with:
send_event('phonehub_event', current: phonehub_visitCount.data.rows)
This returns nothing. I'm still using the original GA ID, however I'm trying to tell it to look at the event I created. Is there a way in ruby to make the GA realtime return the results from the event? Ideally the solution I'm looking for is to return real time active visitors on the event I have made.
After further research I found that I had to specify v3 in the api method.
So instead of phonehub_visitCount = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
I had to use
api_method = client.discovered_api('analytics','v3').data.ga.get
Then add
phonehub_visitCount = client.execute(:api_method => api_method, :parameters => {

Google Calendar API v3 - Update Event

I'm using google-api-ruby-client for working with Google Calendar API v3.
The only problem I'm facing is with updating an event twice.
It has been discussed here before (Google Calendar api v3 re-update issue) but without ruby client and no answer.
When I add a new event I get an ID and an ETAG. I use ID to update the event and I get a new ETAG. Now If I try to update 2nd time, it doesn't update and sends 400 error with message "Invalid Value".
I have to send latest ETAG when updating 2nd time but I'm not sure how to send that when working with google-api-ruby-client.
Here's the code:
event = {
'summary' => "Summary",
'location' => "Location",
'description' => "Description",
'start' => {
'dateTime' => #entry.from_date
},
'end' => {
'dateTime' => #entry.to_date
}
}
result = client.execute(:api_method => service.events.update,
:parameters => {'calendarId' => #entry.calendar.gid, 'eventId'=>#entry.gid},
:body => JSON.dump(event),
:headers => {'Content-Type' => 'application/json'})
The docs are really sketchy on this but it seems you have to increment the sequence property each time you do an update. It's a revision control mechanism.
http://www.kanzaki.com/docs/ical/sequence.html

mini fb posting to friend's wall using graph api

Hi I have read all the other post relating to this but I think I am missing something fundamental. I am using mini_fb in my ruby on rails application for handling the facebook api. I have the following code:
current_user.session.post('me', :type => :feed, :params => {:name => "name",
:to => "{\"data\":[{\"name\":\"#{friend.facebook_name}\",\"id\":\"#{friend.facebook_id}\"}]}",
:link => url_of_app,
:description => "desc",
:actions => "{\"name\": \"action name\", \"link\": \"url\"}"})
The above posts to the current user's wall with or without the "to" parameter set. Everything works, except for the "to" parameter. I have read the graph post to wall over and over again and I can't figure out what is wrong with this code of mine. I would really appreciate it if someone could point out my mistake.
I've never used ruby's version, but probably the problem is in the first parameter. You are targeting 'me' feed, while should be targeting your friends feed. Try fetching your friend id and doing something like
current_user.session.post(friend.facebook_id, :type => :feed, :params => ...)
Wow, mini_fb looks so verbose :)
Telémako is right, you need to use your friends id. I give you another alternative for more nice code. Use Koala.
https://github.com/arsduo/koala/wiki/Graph-API
#graph.put_wall_post("explodingdog!", {:name => "i love loving you", :link => "http://www.explodingdog.com/title/ilovelovingyou.html"}, "tmiley")
=> {"id"=>"83901496_520908898070"}
I use it in my projects and works very well.

Resources