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

I am creating posts for my plugins 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) and store that directly on the page content file.

Echo the tracking 1x1px image anywhere in your template.

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

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

in your page blueprint

    label: Page view count
    type: viewcount

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

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

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

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

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

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

Is an sqlite file created in /site/logs?

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

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 :

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

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');

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

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

Then sort:

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

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 ?

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.

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

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

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.

1 Like