Search by

arnoson / kirby-stats

arnoson

Simple and privacy friendly web statistics for Kirby CMS.

Package info

github.com/arnoson/kirby-stats

Type:kirby-plugin

pkg:composer/arnoson/kirby-stats

Statistics

Installs: 2 310

Dependents: 0

Suggesters: 0

Stars: 9

Open Issues: 4

1.0.0-beta.9 2025-11-11 14:11 UTC

README

Kirby Stats

Simple and privacy-friendly Kirby plugin for tracking website activity.

Installation

composer require arnoson/kirby-stats

Usage

Add the tracking script to the <head> of your templates:

<?php snippet('kirby-stats'); ?>

The snippet loads the plugin's stats.js and outputs nothing while the enabled option is false. Pass extra script attributes, like a CSP nonce, as attrs:

<?php snippet('kirby-stats', ['attrs' => ['nonce' => $nonce]]); ?>

The script waits for the first click, scroll, key press, mouse move or touch, so most bots aren't counted. Reloads aren't counted either. Client-side navigation through history.pushState() and the browser's back and forward buttons counts as a new page view when the path changes. A new query or hash on the same page doesn't.

To load the script without editing templates, turn on the inject option. The plugin then adds the snippet's output to every HTML page Kirby renders, before </head>, or before <body> when an HTML minifier drops </head>. Pages that already call the snippet keep one script. Use the snippet when you need a nonce or a specific position.

If you bundle your own scripts, you can copy stats.js or the source instead. Without the snippet, the script sends to /kirby-stats on the current host.

Also see the example folder for a full example.

Options

// site/config.php
[
  'arnoson.kirby-stats' => [
    // Enable or disable tracking. This is useful to disable tracking in local
    // development by using Kirby's multi environment config setup:
    // https://getkirby.com/docs/guide/configuration#multi-environment-setup
    'enabled' => true, // default

    // The time interval in which to group the collected data. The default is
    // 'day', which should be sufficient for most use cases. Use 'hour' to
    // collect data more granularly, or 'week' or 'month' if you only need a
    // broader overview.
    'interval' => 'day', // default

    // Where to create the database.
    'database' => kirby()->root('logs') . '/kirby-stats/stats.sqlite', // default

    // When to treat a request as a new visit. In this case, if the same user
    // visits again after more than six hours, we store it as a new visit.
    'sessionDuration' => 60 * 60 * 6, // in seconds, default is 6 hours

    // Add the tracking script to every HTML page without the snippet.
    'inject' => false, // default
  ],
];

Advanced Usage

If you want more control, you can also call the tracking endpoints manually instead of including the provided script. Always call both page and site endpoints for each page visit. Have a look at the provided script for more details.

const path = location.pathname.replace(/^\//, '') // remove leading slash
fetch(`/kirby-stats/page/${path}`, { keepalive: true }) // increase page counters
fetch(`/kirby-stats/site`, { keepalive: true }) // increase total page counters and unique visitors

To record where a visit came from, send the Kirby-Stats-Source header with the first page view: the referring host, a campaign tag like utm_source, or an empty value for a direct visit. Encode it with encodeURIComponent(). Leave the header out for navigation within the site. Don't add the source to the URL, because the browser caches each endpoint URL to recognize visits.

Panel

The Statistics area starts with an overview of visitors, visits, views and views per visit for the selected period, each compared with the period before. While a period is still running, the comparison covers its finished hours or days and the same stretch of the previous period. The tabs choose the metric for the chart and the pages table. Pages, sources, device types, browsers, operating systems and languages are listed below. The area is translated into every Panel language Kirby ships.

Privacy

I'm not a legal expert and cannot provide any legal advice regarding GDPR. However, I can explain what data the plugin collects: For each page view, various counters are incremented within a specified time interval. For instance, after the first user visits your about page, the data might look like this:

Traffic

uuid time interval views visits visitors
site:// day 1 1 1
page://about day 1 1 0

Meta

uuid time interval category key value
site:// day Browser Firefox 1
page://about day OS Windows 1

Metadata such as browser, OS, device type (desktop, mobile, tablet or other) and source is grouped per time interval as well. It's only collected for visits, not for every view. With the hour interval, metadata is stored per day.

The source is the host of the referring page, never its path or query, or a campaign tag from the utm_source or ref query parameter. A visit without a referrer counts as direct. Navigation within the site has no source.

Unique Visitor Tracking

Visitors are tracked on a site-wide level using a technique based on request caching headers. This method, pioneered by Cabin, allows for the accurate measurement of unique visitors without the need for cookies. You can learn more about this approach in their blog post.

That's it. No IP addresses are stored, no cookies are set by this plugin, and no unique requests are stored. All data is grouped into time intervals.

The session duration (the amount of time after which a user is treated as a new visitor again) is calculated based solely on the cache headers (by comparing the value of the If-Modified-Since header with the current time).

Support

This plugin is free and open source. While it's provided at no cost, maintaining and improving it requires ongoing time and effort. If you're using it in a commercial setting or simply wish to support its development, please make a donation of your choice.

Thanks

  • Matomo for making their device detector a standalone package
  • Cabin Analytics for sharing their method on how to measure unique visitors without cookies