I’m trying to retrieve a YouTube view count for a specific video on my WordPress site. I’m not a coder but I’ve managed to get it working using this code:
<div id="viewCount" style="display: inline-block;"></div>
<script>
let getviewCount = () => {
fetch(`https://www.googleapis.com/youtube/v3/videos?part=statistics&id=Cemk32wKN_k&key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX`)
.then(response => {
return response.json()
})
.then(data => {
console.log(data);
viewCount.innerHTML = data["items"][0].statistics.viewCount;
})
}
getviewCount();
</script>
The final touch I’d like to add is decimal separators so instead of the number looking like this:
13526897
It looks like this:
13,526,897
Based on some research here I think I need to use a function similar to this:
function numberWithCommas(x) {
return x.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
}
But I don’t know how to code, so combining these two ideas is beyond my ability right now.
If anyone wouldn’t mind showing me how to do it, I’d be extremely grateful!
Thanks