Skip to content

Commit

Permalink
Don't override $text within MatomoAnalyticsHooks::matomoScript (mirah…
Browse files Browse the repository at this point in the history
…eze#93)

This should prevent duplicates.
  • Loading branch information
paladox authored Jan 29, 2023
1 parent 36d0a2c commit 733a588
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## ChangeLog for MatomoAnalytics

### 1.1.1 (29-01-2023)
* Don't override $text within MatomoAnalyticsHooks::matomoScript

### 1.1.0 (29-01-2023)
* Ensure matomo script isn't added more than once
* Change error to warning for "could not find db in matomo table" logging
Expand Down
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"Universal Omega"
],
"url": "https://github.com/miraheze/MatomoAnalytics",
"version": "1.1.0",
"version": "1.1.1",
"descriptionmsg": "matomoanalytics-desc",
"license-name": "GPL-3.0-or-later",
"type": "specialpage",
Expand Down
89 changes: 43 additions & 46 deletions includes/MatomoAnalyticsHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ public static function wikiRename( $dbw, $old, $new ) {
* @param string &$text Output text.
* @return bool
*/
public static function matomoScript( $skin, &$text = '' ) {
public static function matomoScript( $skin, &$text ) {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'matomoanalytics' );

static $alreadyDone = false;

// Check if JS tracking is disabled or if the script has already been added and bow out early
if ( $alreadyDone || $config->get( 'MatomoAnalyticsDisableJS' ) ) {
// Check if JS tracking is disabled and bow out early
if ( $config->get( 'MatomoAnalyticsDisableJS' ) === true ) {
return true;
}

Expand All @@ -47,49 +45,48 @@ public static function matomoScript( $skin, &$text = '' ) {

$permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
if ( $permissionManager->userHasRight( $user, 'noanalytics' ) ) {
$text .= '<!-- MatomoAnalytics: User right noanalytics is assigned. -->';
} else {
$id = strval( $mAId );
$globalId = (string)$config->get( 'MatomoAnalyticsGlobalID' );
$globalIdInt = (int)$globalId;
$serverurl = $config->get( 'MatomoAnalyticsServerURL' );
$title = $skin->getRelevantTitle();

$jstitle = Xml::encodeJsVar( $title->getPrefixedText() );
$dbname = Xml::encodeJsVar( $config->get( 'DBname' ) );
$urltitle = $title->getPrefixedURL();
$userType = $user->isRegistered() ? 'User' : 'Anonymous';
$cookieDisable = (int)$config->get( 'MatomoAnalyticsDisableCookie' );
$forceGetRequest = (int)$config->get( 'MatomoAnalyticsForceGetRequest' );
$text .= <<<SCRIPT
<script>
var _paq = window._paq = window._paq || [];
if ( {$cookieDisable} ) {
_paq.push(['disableCookies']);
}
if ( {$forceGetRequest} ) {
_paq.push(['setRequestMethod', 'GET']);
}
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u = "{$serverurl}";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setDocumentTitle', {$dbname} + " - " + {$jstitle}]);
_paq.push(['setSiteId', {$id}]);
_paq.push(['setCustomVariable', 1, 'userType', "{$userType}", "visit"]);
if ( {$globalIdInt} ) {
_paq.push(['addTracker', u + 'matomo.php', {$globalId}]);
}
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="{$serverurl}matomo.php?idsite={$id}&amp;rec=1&amp;action_name={$urltitle}" style="border:0;" alt="" /></p></noscript>
SCRIPT;
$text = '<!-- MatomoAnalytics: User right noanalytics is assigned. -->';
return true;
}

$alreadyDone = true;
$id = strval( $mAId );
$globalId = (string)$config->get( 'MatomoAnalyticsGlobalID' );
$globalIdInt = (int)$globalId;
$serverurl = $config->get( 'MatomoAnalyticsServerURL' );
$title = $skin->getRelevantTitle();

$jstitle = Xml::encodeJsVar( $title->getPrefixedText() );
$dbname = Xml::encodeJsVar( $config->get( 'DBname' ) );
$urltitle = $title->getPrefixedURL();
$userType = $user->isRegistered() ? 'User' : 'Anonymous';
$cookieDisable = (int)$config->get( 'MatomoAnalyticsDisableCookie' );
$forceGetRequest = (int)$config->get( 'MatomoAnalyticsForceGetRequest' );
$text = <<<SCRIPT
<script>
var _paq = window._paq = window._paq || [];
if ( {$cookieDisable} ) {
_paq.push(['disableCookies']);
}
if ( {$forceGetRequest} ) {
_paq.push(['setRequestMethod', 'GET']);
}
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u = "{$serverurl}";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setDocumentTitle', {$dbname} + " - " + {$jstitle}]);
_paq.push(['setSiteId', {$id}]);
_paq.push(['setCustomVariable', 1, 'userType', "{$userType}", "visit"]);
if ( {$globalIdInt} ) {
_paq.push(['addTracker', u + 'matomo.php', {$globalId}]);
}
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="{$serverurl}matomo.php?idsite={$id}&amp;rec=1&amp;action_name={$urltitle}" style="border:0;" alt="" /></p></noscript>
SCRIPT;

return true;
}
Expand Down

0 comments on commit 733a588

Please sign in to comment.