Plausible

From Koha Wiki
Jump to navigation Jump to search

Plausible Analytics is an easy to use, open source, lightweight and privacy-friendly alternative to Google Analytics. They offer both free self-hosted community version and paid hosted service.

With opaccredits

1. Setup the site in Plausible to get the tracking script HTML snippet. Alternatively if the site is already set up, go to Site Settings -> Review Installation.

2. Copy the snippet, which will look like:

<script defer data-domain="library.example.org" src="https://plausible.io/js/script.js"></script>

3. In Koha in the intranet interface search in the global system preferences in Administration for "opaccredits".

4. Add the tracking code in the text field in the raw HTML view.

Now all OPAC pages should be tracked by Plausible.

With OpacUserJS

These instructions mimic the script snippet creation from above within JavaScript and serve as an alternative to the instructions above.

Replace data-domain value with your OPAC's domain name, as configured in the Plausible panel.

Replace plausible.io with your own domain if you're self-hosting and data-api path if you're proxying the API call (useful to evade default ad-blocking of the default script and API paths of the hosted instance).

Minimal

Default minimal script, with default paths, only need to change library domain if using the hosted version.

const plausible = document.createElement("script");
plausible.setAttribute("src", "https://plausible.io/js/script.js");
plausible.setAttribute("defer", "");
plausible.setAttribute("data-domain", "library.example.org");
plausible.setAttribute("data-api", "/api/event");
document.body.appendChild(plausible);

Thorough

Script that tracks outbound links and also some extra props, namely: whether user is logged in OPAC and what biblio id and title they were viewing if applicable. Tracking most popular biblios with the users may be pretty useful for librarians and their cataloging decisions.

const plausible = document.createElement("script");
plausible.setAttribute("src", "https://plausible.io/js/script.outbound-links.pageview-props.js");
plausible.setAttribute("defer", "");
plausible.setAttribute("data-domain", "library.example.org");
plausible.setAttribute("data-api", "/api/event");
plausible.setAttribute("event-is_logged_in", (!!is_logged_in).toString());
{
    const biblioTitleEl = $("h1.title[property=name]");
    const biblioIdEl = $("div#catalogue_detail_biblio");
    if (biblioTitleEl.length === 1 && biblioIdEl.length === 1) {
        const biblioTitle = biblioTitleEl.text();
        const biblioId = +biblioIdEl.attr("data-biblionumber");
        //console.log({ biblioTitle, biblioId });
        if (biblioTitle && biblioId > 0) {
            plausible.setAttribute("event-biblio_title", biblioTitle);
            plausible.setAttribute("event-biblio_id", biblioId);
        }
    }
}
document.body.appendChild(plausible);

With plugin

There's no dedicated plugin as of now.

Weblinks