Last active
December 14, 2015 10:49
-
-
Save ninegene/5075279 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
Web Analytics MediaWiki extension | |
==Analytics.php== | |
Add tracking JavaScript scripts to all Wiki Pages. It has option not to track logged-in users. | |
==Installation== | |
Save [https://raw.github.com/genealo/mediawiki-extensions/master/Analytics.php Analytics.php] as <mediawiki_installed_dir>/extensions/Analytics.php. | |
Add the following in LocalSettings.php: | |
require_once($IP.'/extensions/Analytics.php'); | |
$wgEnableAnalyticsToTrackLoggedInWikiUsers = true; | |
$wgGoogleAnalyticsEnabled = true; | |
$wgGoogleAnalyticsAccount = 'UA-12345678-1'; | |
$wgGoogleAnalyticsBaseDomain = 'example.com'; // Set base domain to allow tracking of sub domains | |
$wgClickyAnalyticsEnabled = true; | |
$wgClickyAnalyticsSiteID = '123456789'; | |
$wgWoopraAnalyticsEnabled = true; | |
$wgWoopraAnalyticsDomain = 'example.com'; | |
$wgWoopraAnalyticsIdleTimeoutMillis = 300000; | |
**/ | |
if (!defined('MEDIAWIKI')) { | |
die('This file is a MediaWiki Analytics extension, it is not a valid entry point'); | |
} | |
// Default Values | |
$wgEnableAnalyticsToTrackLoggedInWikiUsers = true; | |
$wgGoogleAnalyticsEnabled = false; | |
$wgGoogleAnalyticsAccount = ''; | |
$wgGoogleAnalyticsBaseDomain = ''; | |
$wgClickyAnalyticsEnabled = false; | |
$wgClickyAnalyticsSiteID = ''; | |
$wgWoopraAnalyticsEnabled = false; | |
$wgWoopraAnalyticsDomain = ''; | |
$wgWoopraAnalyticsIdleTimeoutMillis = 300000; // 5 mins | |
$wgHooks['ParserAfterTidy'][] = 'efAnalytics'; | |
function getGoogleAnalyticsScript($wgGoogleAnalyticsAccount, $wgGoogleAnalyticsBaseDomain) { | |
$htmlText = <<<ANALYTICS | |
<!-- Start of Google Analytics --> | |
<script type="text/javascript"> | |
var _gaq = _gaq || []; | |
_gaq.push(['_setAccount', '{$wgGoogleAnalyticsAccount}']); | |
_gaq.push(['_setDomainName', '{$wgGoogleAnalyticsBaseDomain}']); | |
_gaq.push(['_trackPageview']); | |
(function() { | |
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; | |
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; | |
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); | |
})(); | |
</script> | |
<!-- End of Google Analytics --> | |
ANALYTICS; | |
return $htmlText; | |
} | |
function getClickyAnalytics($wgClickyAnalyticsSiteID) { | |
$htmlText = <<<ANALYTICS | |
<!-- Start of Clicky Analytics --> | |
<script type="text/javascript"> | |
var clicky_site_ids = clicky_site_ids || []; | |
clicky_site_ids.push({$wgClickyAnalyticsSiteID}); | |
(function() { | |
var s = document.createElement('script'); | |
s.type = 'text/javascript'; | |
s.async = true; | |
s.src = '//static.getclicky.com/js'; | |
( document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0] ).appendChild( s ); | |
})(); | |
</script> | |
<noscript><p><img alt="Clicky" width="1" height="1" src="//in.getclicky.com/{$wgClickyAnalyticsSiteID}ns.gif" /></p></noscript> | |
<!-- End of Clicky Analytics --> | |
ANALYTICS; | |
return $htmlText; | |
} | |
function getWoopraAnalytics($wgWoopraAnalyticsDomain, $wgWoopraAnalyticsIdleTimeoutMillis) { | |
$htmlText = <<<ANALYTICS | |
<!-- Start of Woopra Analytics --> | |
<script type="text/javascript"> | |
function woopraReady(tracker) { | |
tracker.setDomain('{$wgWoopraAnalyticsDomain}'); | |
tracker.setIdleTimeout({$wgWoopraAnalyticsIdleTimeoutMillis}); | |
tracker.trackPageview({type:'pageview',url:window.location.pathname+window.location.search,title:document.title}); | |
return false; | |
} | |
(function() { | |
var wsc = document.createElement('script'); | |
wsc.src = document.location.protocol+'//static.woopra.com/js/woopra.js'; | |
wsc.type = 'text/javascript'; | |
wsc.async = true; | |
var ssc = document.getElementsByTagName('script')[0]; | |
ssc.parentNode.insertBefore(wsc, ssc); | |
})(); | |
</script> | |
<!-- End of Woopra Analytics --> | |
ANALYTICS; | |
return $htmlText; | |
} | |
function efAnalytics() { | |
global $wgOut; | |
global $wgUser; | |
global $wgEnableAnalyticsToTrackLoggedInWikiUsers; | |
global $wgGoogleAnalyticsEnabled; | |
global $wgGoogleAnalyticsAccount; | |
global $wgGoogleAnalyticsBaseDomain; | |
global $wgClickyAnalyticsEnabled; | |
global $wgClickyAnalyticsSiteID; | |
global $wgWoopraAnalyticsEnabled; | |
global $wgWoopraAnalyticsDomain; | |
global $wgWoopraAnalyticsIdleTimeoutMillis; | |
if ($wgUser->isAnon() || $wgEnableAnalyticsToTrackLoggedInWikiUsers) { | |
$htmlText = ''; | |
if ($wgGoogleAnalyticsEnabled && $wgGoogleAnalyticsAccount != '') { | |
$htmlText .= getGoogleAnalyticsScript($wgGoogleAnalyticsAccount, $wgGoogleAnalyticsBaseDomain); | |
} | |
if ($wgClickyAnalyticsEnabled && $wgClickyAnalyticsSiteID != '') { | |
$htmlText .= getClickyAnalytics($wgClickyAnalyticsSiteID); | |
} | |
if ($wgWoopraAnalyticsEnabled && $wgWoopraAnalyticsDomain != '') { | |
$htmlText .= getWoopraAnalytics($wgWoopraAnalyticsDomain, $wgWoopraAnalyticsIdleTimeoutMillis); | |
} | |
$wgOut->addScript($htmlText); | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment