0

I am picking up video view count for a handful of videos from my own channel. This is the code I have just showing you 2 as an example:

<?php
    define("YOUTUBE_DATA_API_KEY", 'KEY_IS_IN_HERE');

    function youtube_video_statistics($video_id) {
        $json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $video_id . "&key=". YOUTUBE_DATA_API_KEY );
        $jsonData = json_decode($json);
        $views = $jsonData->items[0]->statistics->viewCount;
        return $views;
    }

    echo '<strong>Track Title 1 - </strong>';
    $num = youtube_video_statistics('VIDEO_ID_HERE');
    echo $formattedNum1;
    $formattedNum1 = number_format($num);
    echo $formattedNum1;

    echo '<strong>Track Title 2</strong>';
    $num = youtube_video_statistics('VIDEO_ID_HERE');
    echo $formattedNum2;
    $formattedNum2 = number_format($num);
    echo $formattedNum2;
?>

The $formattedNum is just to format the numbers with apostrophes given a lot of my videos have views in the thousands.

This works fine but as you can see it is very manual. The track title and video ID is hardcoded in which isn't ideal.

What I want to do is pull the titles with view count in one batch of code that doesn't require me to keep updating it with a new ID once I add a new video etc. The caveat also is I have over 90 videos but it is only really 12 of them that I need to closely monitor the stats for but I guess if the only option to do this is to pull in the title and view count of every video on my channel, that's still better than my current position but I can't see any way to do even this in the docs.

So is there any way with the API to perhaps group the videos I want to monitor together so I can then query that particular group so for example use the same tag on them or add them to a playlist then query against that tag or playlist?

In reply to Benjamin Loison's answer, the code I get is just:

{
    "kind": "youtube#videoListResponse",
    "etag": "YIUPVpqNjppyCWOZfL-19bLb7uk",
    "items": [],
    "pageInfo": {
      "totalResults": 0,
      "resultsPerPage": 0
    }
}
jfar_2020
  • 161
  • 4
  • 23

1 Answers1

0

It seems that as you are using the Youtube Data API v3 endpoint Videos: list which is the way to go. To request a batch of videos instead of a single one you can gather them in the id field with a comma, here is an example: https://www.googleapis.com/youtube/v3/videos?part=statistics&id=dQw4w9WgXcQ,o-YBDTqX_ZU&key=YOUTUBE_DATA_API_KEY

It doesn't seem that we have to adapt maxResults field (default value: 5). However you can join with a comma up to 50 videos only proceed request by request if you have more than 50 videos to proceed.

You can also monitor videos of a playlist by gathering the videos ids with PlaylistItems: list.

If you need you can list (only) your public videos (if you are using the API key) with the following method shared by jambrose:

It consists in using the Channels: list with part=contentDetails and retrieve the uploads playlist which you can retrieve videos from as descriebed above.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
  • Thanks, I'm struggling though. Using your example https://www.googleapis.com/youtube/v3/videos?part=statistics&id=dQw4w9WgXcQ,o-YBDTqX_ZU&key=YOUTUBE_DATA_API_KEY which ID goes in here, the playlist ID? As for me with my playlist ID and API KEY, all I get is in the original question (I edited it to show the code) and using Jambrose's code, I end up with an enormous list of every video title, description and stats (no view count) but not just of my playlist. – jfar_2020 Nov 06 '21 at 12:34
  • @jfar_2020 do you get the "code" you added by retrieving the URL I gave you ? Because on my side I got this correct result: https://pastebin.com/0Tf1g0t1 Please take a look at the documentation "Videos: list" I give you it answers your question (`id` is about videos here not playlist (it's in the endpoint name...)) With jambrose's code you get all your channel videos if you are interested in a different playlist just precise it with the `playlistId` field. Here is a "high-level" way to check playlist id: https://www.youtube.com/playlist?list=PLAYLIST_ID Get the view count from the videos ids – Benjamin Loison Nov 06 '21 at 15:01
  • I'm getting somewhere now, I was getting a little mixed up earlier. I am now pulling in the videos from the specific playlist with view count. Now I just need to work out how to include the video title too then write the code to output it. – jfar_2020 Nov 06 '21 at 15:24
  • Based on your answer I am getting results by comma seperated id's as that seems the easiest way as not having much luck with the playlist solution. So I have viewcount and all description/titles by modifying the URL to part=snippet,statistics but that gives a lot of info, I only want the title so how can I cut this down so it is only title and view count? – jfar_2020 Nov 06 '21 at 15:55
  • I'm not aware of any way to do so (and I don't think there is a way to request only these pieces of information to Google). Just retrieve the URL with `part` interesting you and then from the bunch of data extract what you are really interested in. – Benjamin Loison Nov 06 '21 at 20:13
  • Thanks Benjamin, could you clarify though what you mean by retrieve the URL with part? – jfar_2020 Nov 07 '21 at 12:01
  • "So I have viewcount and all description/titles by modifying the URL to part=snippet,statistics but that gives a lot of info" I was just telling you that it is the right way to do what you are interested in. – Benjamin Loison Nov 07 '21 at 14:38