🚩MORE TUTORIALS https://www.wixideas.com/tutorials
📺Please Subscribe, Like, & Share 👉🏽👉🏽 YouTube
This tutorial will show you how to add view counter to your WIX dynamic page. This will enable anyone to view and their view will be recorded.
Code
//VIEW EVENT FUNCTION 🚀
const viewsEvent = function () {
$w("#dynamicDataset").onReady(() => {
const itemObj = $w("#dynamicDataset").getCurrentItem();
let totalViews = itemObj.views + 1;
$w("#dynamicDataset").setFieldValue("views", totalViews)
$w('#dynamicDataset').save();
$w("#viewCounter").text = formatView(String($w("#dynamicDataset").getCurrentItem().views))
});
}
//RUN THE VIEW WHEN PAGE LOADS⌛
$w.onReady(function () {
setTimeout(viewsEvent, 100);
});
//FORMAT THE VIEWS PER ZERO🚀
function formatView(view) {
if (view > 999 && view < 1000000) {
return `${(view/1000).toFixed(0)}K`;
} else if (view > 1000000) {
return (view / 1000000).toFixed(0) + 'M';
} else if (view < 900) {
return view;
}
}
Thanks🙂
🚩MORE TUTORIALS https://www.wixideas.com/tutorials
📺Check out http://bit.ly/3a07EEF
Facebook Page: https://www.facebook.com/wixideas
Facebook Community Group: https://www.facebook.com/groups/wixideas
Instagram: https://www.instagram.com/wixideas/
Website: https://www.wixideas.com/
YouTube Channel: https://www.youtube.com/@wixideas
Thank you @Walter Odibi this helped a ton!!!
@Walter Odibi Could you explain what's the point of having a setTimeout on this feature?
setTimeout(viewsEvent,100);
Also, I believe there might be a problem because your code will execute 2 times on each rendering, therefore increasing the number of views twice.
It will insert once In backend rendering and once in frontend rendering: read the following articles for more details:
https://support.wix.com/en/article/velo-about-the-page-rendering-process