# Live Page View Counter - Static/Dynamic Pages

**URL:** <https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [April 8, 2022, 2:16pm UTC](https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222 "2022-04-08T14:16:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rainfelt](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@rainfelt](https://forum.wixstudio.com/u/rainfelt)\
**Post date:** [April 8, 2022, 2:16pm UTC](https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222/1 "2022-04-08T14:16:56Z")

</div>

Hello everyone!

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.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![amotor](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@amotor](https://forum.wixstudio.com/u/amotor)\
**Post date:** [April 8, 2022, 2:24pm UTC](https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222/2 "2022-04-08T14:24:52Z")

</div>

you could create a page views database and then use the wix database helpers to just increase a count every time the page is loaded

---

<div class="post-metadata">

**Author:** ![rainfelt](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@rainfelt](https://forum.wixstudio.com/u/rainfelt)\
**Post date:** [April 8, 2022, 2:40pm UTC](https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222/3 "2022-04-08T14:40:33Z")

</div>

Thanks for helping out. I created a database as told, put the code below and works like a charm.

```javascript
import wixLocation from 'wix-location';

$w.onReady(function () {

    $w("#dataset1").onReady( () => {

        setTimeout(laggedEvents,1000);

function laggedEvents() {

 const currentItem = $w("#dataset1").getCurrentItem();
 let viewCount = currentItem.home + 1;
    $w("#dataset1").setFieldValue("home", viewCount);
    $w('#dataset1').save();
}
});
});

```

---

<div class="post-metadata">

**Author:** ![amotor](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@amotor](https://forum.wixstudio.com/u/amotor)\
**Post date:** [April 8, 2022, 3:00pm UTC](https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222/4 "2022-04-08T15:00:44Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![rainfelt](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@rainfelt](https://forum.wixstudio.com/u/rainfelt)\
**Post date:** [April 8, 2022, 3:06pm UTC](https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222/5 "2022-04-08T15:06:30Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![rainfelt](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@rainfelt](https://forum.wixstudio.com/u/rainfelt)\
**Post date:** [April 8, 2022, 3:09pm UTC](https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222/6 "2022-04-08T15:09:56Z")

</div>

Also, the permissions of the dataset must be put to _Anyone_ in order to work with this piece of code.

---

<div class="post-metadata">

**Author:** ![amotor](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@amotor](https://forum.wixstudio.com/u/amotor)\
**Post date:** [April 8, 2022, 3:10pm UTC](https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222/7 "2022-04-08T15:10:40Z")

</div>

@rainfelt what happens when you just console.log(currentItem)?  
Are you using the data helpers package from wix?

---

<div class="post-metadata">

**Author:** ![amotor](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@amotor](https://forum.wixstudio.com/u/amotor)\
**Post date:** [April 8, 2022, 3:12pm UTC](https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222/8 "2022-04-08T15:12:34Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![rainfelt](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@rainfelt](https://forum.wixstudio.com/u/rainfelt)\
**Post date:** [April 8, 2022, 3:19pm UTC](https://forum.wixstudio.com/t/live-page-view-counter-static-dynamic-pages/2222/9 "2022-04-08T15:19:48Z")

</div>

@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.
