mfsymb mfsymb - 7 months ago 118
PHP Question

YouTube Analytics API php Samples

I want to make this YouTube Analytics Request with the PHP Client Libary

https://www.googleapis.com/youtube/analytics/v1/reports
?ids=channel==CHANNELID
&start-date=STARTDATE
&end-date=ENDDATE
&metrics=views,
estimatedMinutesWatched,
averageViewDuration,
comments,
favoritesAdded,
favoritesRemoved,
likes,
dislikes,
shares,
subscribersGained,
subscribersLost
&dimensions=7DayTotals
&fields=rows
&sort=day


Is this possible?
Are there any PHP code Samples on how the get a YouTube Analytics Report from the API?

Couldn't find one on the Google Developers page .(https://developers.google.com/youtube/analytics/v1/code_samples/)

Thanks

Answer

You can add metrics in the following way rather than calling each metric one by one.

$metrics = 'views,estimatedMinutesWatched,averageViewDuration,comments,favoritesAdded,favoritesRemoved,likes,dislikes,shares,subscribersGained,subscribersLost';

Your result will be in the from as depicted in the documentation. Example:

$optparams = array(
    'dimensions' => '7DayTotals',
    'sort' => 'day',
);

$metrics = 'views,estimatedMinutesWatched,averageViewDuration,comments,favoritesAdded,favoritesRemoved,likes,dislikes,shares,subscribersGained,subscribersLost';

$api_response = $metrics;
$api = $analytics->reports->query($id, $start_date, $end_date, $metric, $optparams);

if (isset($api['rows'])) {
    //Get values in form of multidimensional arrays. 
}