I am trying to find the total number of views of all videos in a playlist I own in YouTube. I can see that a “total views” number is posted under the playlist name. However this appears to be the number of views generated by entering the playlist first. When I add up the number of views of individual videos I get a substantially larger number because, I assume, most of videos are watched by following a link other than the playlist.
In searching for an answer I see two suggestions
- using the following JS in the YT console
ytInitialData.header.playlistHeaderRenderer.viewCountText.simpleText
But this just gives the same number that is available under the playlist name.
- using the following
var totalViews = 0;
var videos = document.querySelectorAll('ytd-playlist-video-renderer');
for (var i= 0; i < videos.length; i++) {
var viewsElement = videos[i].querySelector('#metadata > span:nth- child(1)');
if (viewsElement !== null) {
var views = viewsElement.textContent.trim().replace(/D/g,'');
totalViews += parseInt(views);
}
}
console.log('Total views of all videos in the playlist: ' + totalViews);
But this gives a total count of 0.