user1364743
user1364743

Reputation: 5661

How to count the total number of views from a youtube channel?

I want to get the total amount of views (all videos) from a channel on youtube using the Youtube API in PHP. I didn't found any method to do that. Does anyone have en idea ? Thanks in advance for your help.

Upvotes: 0

Views: 18333

Answers (2)

ljn
ljn

Reputation: 27

This is the code (don't forget to rename yourUserName with your YouTube username):

$xdoc = new DomDocument;
$xdoc->Load('http://gdata.youtube.com/feeds/api/users/yourUserName');
$ytstat = $xdoc->getElementsByTagName('statistics')->item(0);
$total_views = $ytstat->getAttribute(totalUploadViews);
echo $total_views;

Upvotes: 0

Matias Molinas
Matias Molinas

Reputation: 2296

You can use the new YouTube Analytics API

https://developers.google.com/youtube/analytics/v1/available_reports

you can modify the code of the sample application to call the api in the client side:

https://developers.google.com/youtube/analytics/v1/sample-application

and do something like this to get the number of views per day:

var request = gapi.client.youtubeAnalytics.reports.query({
      // Convert dates to YYYY-MM-DD strings for start-date and end-date parameters.
      'start-date': formatDateString(lastWeek),
      'end-date': formatDateString(today),
      // Identify channel for which you're retrieving data.
      ids: 'channel==' + channelId,
      dimensions: 'day',
      metrics: 'views'
    });

Upvotes: 1

Related Questions