# Kirby3 Pageview Counter - Track Page view count and last visited timestamp

**URL:** <https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582>\
**Category:** Plugins\
**Tags:** v3\
**Created:** [October 1, 2021, 6:50pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582 "2021-10-01T18:50:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [October 1, 2021, 6:50pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/1 "2021-10-01T18:50:50Z")

</div>

> I am creating posts for [my plugins](https://getkirby.com/plugins/bnomei) to make it easier to find them using the forum search and not just the docs search.

By default this plugin will just write count and timestamp per page into an **sqlite file** since that is the fastest and best suited solution for concurrent requests. But you can also use **fields** (see [readme](https://github.com/bnomei/kirby3-pageviewcounter#page-fields-alternative)) and store that directly on the page content file.

> **[GitHub - bnomei/kirby3-pageviewcounter: Track Page view count and last visited timestamp](https://github.com/bnomei/kirby3-pageviewcounter)**
>
> Track Page view count and last visited timestamp

Echo the tracking 1x1px image anywhere in your template.

```php
<?php echo $page->counterImage(); ?>

```

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [November 29, 2021, 2:39pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/2 "2021-11-29T14:39:29Z")

</div>

the plugin now includes two optional fields to view the data from the sqlite db.

> **[Release v1.3.0 · bnomei/kirby3-pageviewcounter](https://github.com/bnomei/kirby3-pageviewcounter/releases/tag/v1.3.0)**

**in your page blueprint**

```auto
fields:
  counter:
    label: Page view count
    type: viewcount

  lastvisited:
    label: Page last visited
    type: lastvisited
    # format: 'DD-MM-YYYY'

```

---

<div class="post-metadata">

**Author:** ![claraloustau](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/claraloustau/32/12322_2.png) [@claraloustau](https://forum.getkirby.com/u/claraloustau)\
**Post date:** [August 29, 2022, 6:07pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/3 "2022-08-29T18:07:31Z")

</div>

Hello,  
I’m trying to use your plugin for display the number of views for each articles in a blog, and for sorting them by “popularity”.  
But the `<?php echo $page->counterImage(); ?>` doesn’t display anything,  
does anyone use this plugin ? Or have the same problem ?

Thank you

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 29, 2022, 6:15pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/4 "2022-08-29T18:15:18Z")

</div>

> [@claraloustau](#):
>
> doesn’t display anything,

Well, it doesn’t display anything visible. You mean, there is nothing in the source code?

---

<div class="post-metadata">

**Author:** ![claraloustau](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/claraloustau/32/12322_2.png) [@claraloustau](https://forum.getkirby.com/u/claraloustau)\
**Post date:** [August 29, 2022, 6:19pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/5 "2022-08-29T18:19:09Z")

</div>

In my panel my Page View Acount is still ‘0’ even if I reload the page  
Or maybe I cannot use it in localhost ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 29, 2022, 6:23pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/6 "2022-08-29T18:23:58Z")

</div>

Have you logged out before opening the page? By default, visits of logged-in users are not counted.

---

<div class="post-metadata">

**Author:** ![claraloustau](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/claraloustau/32/12322_2.png) [@claraloustau](https://forum.getkirby.com/u/claraloustau)\
**Post date:** [August 29, 2022, 6:29pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/7 "2022-08-29T18:29:36Z")

</div>

log out from the panel ? I tried, it doesn’t change anything

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 29, 2022, 6:33pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/8 "2022-08-29T18:33:18Z")

</div>

Is an sqlite file created in /site/logs?

---

<div class="post-metadata">

**Author:** ![claraloustau](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/claraloustau/32/12322_2.png) [@claraloustau](https://forum.getkirby.com/u/claraloustau)\
**Post date:** [August 29, 2022, 6:38pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/9 "2022-08-29T18:38:21Z")

</div>

Yes : pageviewcounter.sqlite  
but I can’t open it I guess I need to download an SQLITE extension in my VScode

---

<div class="post-metadata">

**Author:** ![claraloustau](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/claraloustau/32/12322_2.png) [@claraloustau](https://forum.getkirby.com/u/claraloustau)\
**Post date:** [August 30, 2022, 9:07am UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/10 "2022-08-30T09:07:44Z")

</div>

I reload in differents browsers, and in private tab and it’s finaly working, thank you!  
I was also looking for a way to display the counter in front-end, the solution is :

```auto
echo \Bnomei\PageViewCounter::singleton()->count($page->id());

```

---

<div class="post-metadata">

**Author:** ![claraloustau](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/claraloustau/32/12322_2.png) [@claraloustau](https://forum.getkirby.com/u/claraloustau)\
**Post date:** [August 30, 2022, 1:22pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/11 "2022-08-30T13:22:11Z")

</div>

Is there a way to make a sortBy() on the count($id) for a blog ?  
Something like :  
`$mostclicks = $page->children->sortBy(\Bnomei\PageViewCounter::singleton()->count($id), 'desc');`

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 30, 2022, 3:48pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/12 "2022-08-30T15:48:51Z")

</div>

Indirectly. You can create a custom page method that returns this value, then sort by this method.

```auto
Kirby::plugin('my/pagesMethods', [
    'pageMethods' => [
        'getCounter' => function () {
            return \Bnomei\PageViewCounter::singleton()->count($this->id());
        }
    ]
]);

```

Then sort:

```auto
$sorted = $page->children->sortBy('getCounter', 'desc');

```

> **[Page methods](https://getkirby.com/docs/reference/plugins/extensions/page-methods)**

---

<div class="post-metadata">

**Author:** ![claraloustau](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/claraloustau/32/12322_2.png) [@claraloustau](https://forum.getkirby.com/u/claraloustau)\
**Post date:** [August 31, 2022, 10:44am UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/13 "2022-08-31T10:44:51Z")

</div>

Thank you @texnixe !

It’s working with `Page Methods` and not `Pages Methods` I don’t really understand why, is it because the PageViewCounter in accessible from my blog page (and children) and not from all other pages ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 31, 2022, 12:01pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/14 "2022-08-31T12:01:47Z")

</div>

That’s my mistake, sorry for that. Should have been page method, of course. Hit the wrong page in the docs and then copied blindly from the wrong exmaple.

---

<div class="post-metadata">

**Author:** ![glumanda](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/glumanda/32/5258_2.png) [@glumanda](https://forum.getkirby.com/u/glumanda)\
**Post date:** [September 10, 2023, 5:36am UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/15 "2023-09-10T05:36:44Z")

</div>

@bnomei Thank you very much, I very like the simple approach and plugin! So, to my understanding, a page view is what used to be called “impression” back in the days, aka if I navigate to the page, then somewhere else and then to that page again it will add another page view? I still remember back then we also had something like “unique visitors”, which just tracked the visitors.

Also do you need to add any GDPR notice the user has to confirm? I’d prefer not to, as kirby comes with privacy by default too.

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [September 10, 2023, 4:44pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/16 "2023-09-10T16:44:47Z")

</div>

it will record each visit to a page using **Pixel tracking** and like you said even records repeated visits by the same visitor. i wanted it to be as simple as possible with the use-case for recording visits not browsing behaviour.

i can not give you any legal advice on GDPR but since the plugin is no storing any user data, nor is it persisting any user data within the session itself i would assume you are actually not doing any “tracking” but just “counting”.  
some related reading: [Compliant Website Tracking Guide: How To Track Users Legally - Termly](https://termly.io/resources/articles/website-tracking/)

it will be by default ignore visits by panel users and thus check for a user. this might create a session cookie. you can prevent that by setting the config value to `false` and live with recording panel users as well.  
[https://github.com/search?q=repo%3Abnomei%2Fkirby3-pageviewcounter%20ignore-panel-users&type=code](https://github.com/search?q=repo%3Abnomei%2Fkirby3-pageviewcounter%20ignore-panel-users&type=code)

---

<div class="post-metadata">

**Author:** ![hvsrmusic](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/hvsrmusic/32/2722_2.png) [@hvsrmusic](https://forum.getkirby.com/u/hvsrmusic)\
**Post date:** [January 27, 2025, 9:27pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/17 "2025-01-27T21:27:06Z")

</div>

I developed a similar plugin for my own porpouse, logging each visit in a sqlite file. But after a few months, the main table has more than 500K records… I should consolidate each month’ visit on a separate table… Is your plugin doing something like that? In other words: how do you prevent the risk of having too much records in the db?

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [January 28, 2025, 4:56pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/18 "2025-01-28T16:56:53Z")

</div>

my plugin intentionally does not track history as events (like retrour plugin does) but only a single integer and timestamp per page. pretty much like olden days cgi open tracker but with better bot detection.

---

<div class="post-metadata">

**Author:** ![hvsrmusic](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/hvsrmusic/32/2722_2.png) [@hvsrmusic](https://forum.getkirby.com/u/hvsrmusic)\
**Post date:** [January 30, 2025, 9:43pm UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/19 "2025-01-30T21:43:05Z")

</div>

It could be enough for me. But - if I get 100 pageviews each day for every single page of my site, and my site has 10 pages, at the end of the year will I have 100_10_365 records in a single table?

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [February 3, 2025, 8:59am UTC](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582/20 "2025-02-03T08:59:59Z")

</div>

1. one for each page containing an timestamp of last access and the count. it’s not tracking events.

[Next page](https://forum.getkirby.com/t/kirby3-pageview-counter-track-page-view-count-and-last-visited-timestamp/23582.md?page=2)
