So I want to add a view counter for every page on my website, even in the dynamic pages. I tried some plugins like Hit Counter, but those are only for a single page. Is there any piece of code that I can add manually? Maybe connect it with a live dataset and collect the page visits? I don’t really mind if it count unique or any visit.
@rainfelt do you have a way of actually increasing the view count in the database? You are increasing the currentItem.home value by 1, but are you storing the increased count back into the database? If not I’d highly recommend checking out the wix data helpers package because it makes it easy to update the value of one specific field of a database. Specifically the getAndUpdate function would allow you to update and save the new view count in one line.
@amotor yes it stores the value automatically back to my database on every refresh of a page. I’ve tested it both on preview and published mode. When I was on the editor I opened a tab to test it and the number increased by one even in the editor automatically, so if I make any changes on my website, it will not affect the actual number. The downside is that I still can’t make a unique counter for every dynamic page. Let me give you an example. I created a dynamic page, having a total of 50+ items so 50+ unique pages with links are on this website. I’ve put the same code on the dynamic page but the counter shows the same number on any dynamic page. I thought that what I did was dumb because its the same field that I’m using but I cannot figure it out how can this work.
@rainfelt Correct. That’s why I would suggest creating a separate database for the view counts and setting a reference field back to the database that contains the items
@amotor Yes I used the data helpers from wix and found this code to work with. I did create a separate database, setting a reference field back to the database. The thing is that I must find a way for the dynamic pages, for example, generating fields on the dataset based on the link of any page in order to work. Because, if a field works for one page, I must create 50+ fields to connect on every page. I don’t really find anything on google or wix helper.