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: WIX Ideas
Facebook Community Group: Wix Ideas Community | Facebook
Instagram: Wix Ideas (@wixideas) • Instagram photos and videos
Website: https://www.wixideas.com/
YouTube Channel: https://www.youtube.com/@wixideas