Views Counter for Dynamic Pages

ATTENTION: THIS IS OUR OLD SITE. TO SEE OUR NEW WEBSITE, CLICK HERE.

This code will show you how to add a page views counter which iterates by one each time a website visitor navigates to and views each dynamic page. You can then use the number of views for sorting your dynamic pages for the most popular using a repeater or you can display it back to your website visitors as an engagement indicator.
First, you need to add a field in your dynamic items collection to hold the number of views. Be sure to set the field to numeric. Review the permissions of the collection to ensure that anyone or members (depending on your audience) can update content for the collection. This is needed as your visitors will effectively be updating the collection by their presence on the page. In the screenshot below, a field called 'Views' is created for this purpose - but you can call it anything you'd like.

Next, at whatever point the items in your dynamic collection are created - you need to initiate this field to start the views counter at zero. If you do not do this, the calculation to add views later will fail as you cannot do arithmetic with missing values. I have a form for entering items into my dynamic collection. The example code below sets the 'views' field to zero and saves it to my collection when the submit button is clicked. It also navigates back to the home page, but this is an optional extra after the save event is successful.
Please login or sign-up (it's free!) to view our code. Refresh the page if already logged in.
Disclaimer: To the best of our knowledge, this code works for its stipulated purpose. If you find an error in our code, or know of a better way to achieve the same result - please contact us! We will help where we can, however please be aware that the code is provided 'as is' and without a promise of support in using or implementing it.
  • Facebook Social Icon
  • MeWe-Fluid-Icon
© CorvidCodeBank