Jump to content

Extension:Plausible

From mediawiki.org
MediaWiki extensions manual
Plausible
Release status: stable
Implementation User activity
Description Adds GDPR compliant Website Analytics
Author(s) Octfxtalk
Latest version 1.4.0 (2023-12-14)
MediaWiki >= 1.39
PHP >=8.0
Composer octfx/plausible
License GNU General Public License 2.0 or later
Download
https://github.com/octfx/mediawiki-extension-Plausible/blob/master/README.md

  • $wgPlausibleDomain
  • $wgPlausibleDomainKey
  • $wgPlausibleHonorDNT
  • $wgPlausibleTrackOutboundLinks
  • $wgPlausibleTrackLoggedIn
  • $wgPlausibleEnableCustomEvents
  • $wgPlausibleIgnoredTitles
  • $wgPlausibleEnableOptOutTag
  • $wgPlausibleTrack404
  • $wgPlausibleTrackSearchInput
  • $wgPlausibleTrackEditButtonClicks
  • $wgPlausibleTrackCitizenSearchLinks
  • $wgPlausibleTrackCitizenMenuLinks

The Plausible extension allows to use Plausible Analytics which is a simple, lightweight (< 1 KB), open-source and privacy-friendly alternative to Google Analytics. It does not use cookies and is fully compliant with General Data Protection Regulation (GDPR), California Consumer Privacy Act (CCPA) and Privacy and Electronic Communications (EC Directive) Regulations 2003 (PECR).

See Plausible Analytics on GitHub.

Installation

  • Download, extract and place the file(s) in a directory called Plausible in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'Plausible' );
    $wgPlausibleDomain = "https://plausible.io";
    $wgPlausibleDomainKey = "mywiki.example.com"; // change to your site address
    
  • Configure as required.
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Configuration

Key Description Example Default
$wgPlausibleDomain Domain hosting Plausible instance (required) https://plausible.io null
$wgPlausibleDomainKey Domain Key (i.e. site address) registered with the Plausible instance (required) mywiki.example.com null
$wgPlausibleHonorDNT Honor the Do Not Track header and disable tracking false true
$wgPlausibleTrackOutboundLinks Enable Tracking of outbound link clicks true false
$wgPlausibleTrackFileDownloads Enable Tracking of link clicks that lead to files, sending a File Download event. See the official docs. true false
$wgPlausibleTrackFileDownloadExtensions List of additional file extensions to track. See the official docs. ['js', 'py'] []
$wgPlausibleTrackLoggedIn Enable Tracking for logged in users true false
$wgPlausibleEnableCustomEvents Enable to add the global window.plausible function needed for custom event tracking true false
$wgPlausibleIgnoredTitles List of page titles that should not be tracked. Examples. ['/Page1', '/Special:*', ] []
$wgPlausibleEnableOptOutTag Enables or disables the ‎<plausible-opt-out /> tag that allows users to opt-out from being tracked false true
$wgPlausibleApiKey Api key used when Extension:PageViewInfo is active

Included tracking scripts

The following tracking modules can be activated by setting the provided configuration key in LocalSettings.php to true.

Key Description EventName
$wgPlausibleTrack404 Sends a '404' event for unknown titles 404
$wgPlausibleTrackSearchInput Send inputs to #searchInput to plausible as a custom event named 'SearchInput' SearchInput
$wgPlausibleTrackEditButtonClicks Track clicks to #ca-edit a as a custom event named 'EditButtonClick' EditButtonClick
$wgPlausibleTrackCitizenSearchLinks Only for skin Citizen. Track clicks to search result links found in #typeahead-suggestions. Event is named 'CitizenSearchLinkClick' CitizenSearchLinkClick
$wgPlausibleTrackCitizenMenuLinks Only for skin Citizen. Track clicks to links in the sidebar menu. Event is named 'CitizenMenuLinkClick' CitizenMenuLinkClick

Tracking custom events

https://github.com/plausible/docs/blob/master/docs/custom-event-goals.md

If you want to track custom event goals like button clicks or form completions, you have to trigger these custom events from your website using JavaScript.

Scripts need to be placed in MediaWiki:<Your Skin>.js e.g. MediaWiki:Citizen.js.

Example: Tracking edit button clicks on SkinCitizen.

if (typeof window.plausible === 'undefined') {
    return;
}

document.querySelector('#ca-edit a').addEventListener('click', function (event) {
    plausible('Editbtn Clicked');
});

Ignoring pages

https://github.com/plausible/docs/blob/master/docs/excluding-pages.md#common-use-cases-and-examples

By default, Plausible Analytics tracks every page you install the snippet on. If you don't want Plausible to track specific pages, do not include the snippet on those pages.

Common use cases and examples

$wgPlausibleIgnoredTitles input Prevents tracking on pages with a URL path of:
/blog4 /blog4 and exactly /blog4 with nothing before or after it, so not /blog45 nor /blog4/new nor /blog
/rule/* /rule/<anything>, with <anything> being any set of characters (length >=0), but not a forward slash - for example, both /rule/1 as well as /rule/general-rule-14, but not /rule/4/details nor /rules
/how-to-* /how-to-<anything> - for example, /how-to-play or /how-to-succeed, but not how-to-/blog
/*/admin /<anything>/admin - for example, /sites/admin, but not /sites/admin/page-2 nor /sites/2/admin nor /admin
/*/priv/* /<anything>/priv/<anything> - for example, /admin/priv/sites, but not /priv nor /priv/page nor /admin/priv
/rule/*/* /rule/<anything>/<anything> - for example, /rule/4/new/ or /rule/10/edit, but not /rule nor /rule/10/new/save
/wp/** /wp<anything, even slashes> - for example, /wp/assets/subdirectory/another/image.png or /wp/admin, and everything in between, but not /page/wp

Server side tracking

Some events can be sent serverside without having to rely on the included plausible client script.

The following custom events can be activated:

$wgPlausibleServerSideTracking = [
    'pageview' => false,
    'page404' => false,
    'pageedit' => true,
    'pagedelete' => true,
    'pageundelete' => true,
    'pagemove' => true,
    'userregister' => true,
    'userlogin' => true,
    'userlogout' =>  true,
    'fileupload' => true,
    'filedelete' => true,
    'fileundelete' => true,
];

Lua Integration

With Extension:PageViewInfo active, plausible exposes the following functions:

mw.ext.plausible.topPages()

Returns the top pages and the views for the last day. The table is ordered by the number of page views, and can be iterated by using ipairs.  

Example:

local result = mw.ext.plausible.topPages()

> {
  {
    page = "Foo",
    views = 100
  },
  {
    page = "Bar",
    views = 80
  },
  { [...] }
}

Alternatively this function can be called with the number of days to calculate the views over, e.g. mw.ext.plausible.topPages( 30 )

This is expensive as no caching is employed.

mw.ext.plausible.pageData( titles, days )

Returns the page views for the given titles over the last N days.  

Example:

local result = mw.ext.plausible.pageData( { "Foo", "Bar" }, 5 )

> {
  ["Foo"] = {
    ["2023-08-04"] = 10,
    ["2023-08-05"] = 1,
    ["2023-08-06"] = 4,
    ["2023-08-07"] = 7,
    ["2023-08-08"] = 1,
    ["2023-08-09"] = 4,
  },
  ["Bar"] = {
    ["2023-08-04"] = 100,
    ["2023-08-05"] = 14,
    ["2023-08-06"] = 54,
    ["2023-08-07"] = 7,
    ["2023-08-08"] = 31,
    ["2023-08-09"] = 1,
  },
}

mw.ext.plausible.siteData( days )

Returns the site views for the given last N days.  

Example:

local result = mw.ext.plausible.siteData( 5 )

> {
  ["2023-08-04"] = 10,
  ["2023-08-05"] = 1,
  ["2023-08-06"] = 4,
  ["2023-08-07"] = 7,
  ["2023-08-08"] = 1,
  ["2023-08-09"] = 4,
}

See also