Skip to content

Instantly share code, notes, and snippets.

@eviltester
Created September 1, 2021 12:15
Show Gist options
  • Save eviltester/ac7ed61c589855c7d35fe56e6718cc77 to your computer and use it in GitHub Desktop.
Save eviltester/ac7ed61c589855c7d35fe56e6718cc77 to your computer and use it in GitHub Desktop.
Calculate average number of views for a YouTube channel's visible videos
const viewCounts = [...document.querySelectorAll('div#metadata-line span:first-child')].map((node) => {viewCount = node.innerText.replace(" views","");valueOfViewCount = viewCount.indexOf("K")==-1 ? parseInt(viewCount,10) : parseFloat(viewCount)*1000; return valueOfViewCount;});
console.log(viewCounts);
const sum = viewCounts.reduce(( previousValue, currentValue ) => previousValue + currentValue);
console.log(`${sum} / ${viewCounts.length}`);
console.log(sum/viewCounts.length)
@eviltester
Copy link
Author

  • on a YouTube Channel page with videos listed
  • scroll down to add more videos to page
  • inspect to open console
  • run script in the console to calculate average number of views for videos displayed
  • console output shows the views in an array, and the calculation so you can double check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment