Skip to content

Commit

Permalink
Track 404 Not Found errors for page that don't exist
Browse files Browse the repository at this point in the history
Add code to set the page title to include information about the
URL and referrer for pages that do not exist. Based on
recommendations at https://matomo.org/faq/how-to/faq_60/

Also fix some minor phpdoc syntax.

Bug: DaSchTour#46
  • Loading branch information
samwilson committed Jan 10, 2024
1 parent b74fca9 commit aaec6d2
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace MediaWiki\Extension\Matomo;

use RequestContext;
use Skin;
use Title;
use Xml;

class Hooks {
Expand All @@ -19,7 +21,7 @@ class Hooks {
/**
* Initialize the Matomo hook
*
* @param string $skin
* @param Skin $skin
* @param string &$text
* @return bool
*/
Expand Down Expand Up @@ -77,7 +79,7 @@ public static function onSpecialSearchSetupEngine( $search, $profile, $engine )

/**
* Add Matomo script
* @param string $title
* @param Title $title
* @return string
*/
public static function addMatomo( $title ) {
Expand Down Expand Up @@ -185,6 +187,15 @@ public static function addMatomo( $title ) {

}

$track404s = '';
if ( !$title->exists() ) {
$track404s = "_paq.push(["
. "'setDocumentTitle',"
. "'404/URL = ' + encodeURIComponent(document.location.pathname+document.location.search)"
. " + '/From = ' + encodeURIComponent(document.referrer)"
. "]);";
}

// Prevent XSS
$finalActionName = Xml::encodeJsVar( $finalActionName );

Expand All @@ -205,6 +216,7 @@ public static function addMatomo( $title ) {
<!-- Matomo -->
<script type="text/javascript">
var _paq = _paq || [];{$disableCookiesStr}{$customJs}
{$track404s}
_paq.push(["{$trackingType}"{$jsTrackingSearch}]);
_paq.push(["enableLinkTracking"]);
Expand Down

0 comments on commit aaec6d2

Please sign in to comment.