Created
August 30, 2011 14:16
-
-
Save jnv/1180987 to your computer and use it in GitHub Desktop.
Google Analytics for MediaWiki
This file contains hidden or 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 | |
/* | |
* Google Analytics integration for MediaWiki | |
* Based on extension by Tim Laqua: http://www.mediawiki.org/wiki/Extension:Google_Analytics_Integration | |
* | |
* Usage: | |
* - Drop GoogleAnalytics.php to your extensions/ dir | |
* - In SystemConfig.php add: | |
* | |
* require_once( "$IP/extensions/GoogleAnalytics.php" ); | |
* GoogleAnalytics::$account = 'UA-YOUR TRACKING NUMBER'; | |
* - See GoogleAnalytics class' static variables for other tracking options | |
*/ | |
if ( !defined( 'MEDIAWIKI' ) ) | |
{ | |
die( 'This file is a MediaWiki extension, it is not a valid entry point' ); | |
} | |
$wgGoogleAnalytics = new GoogleAnalytics(); | |
$wgHooks['BeforePageDisplay'][] = $wgGoogleAnalytics; | |
class GoogleAnalytics | |
{ | |
/** | |
* Google Analytics Account (UA-XXXX-XXXX) | |
*/ | |
public static $account = ''; | |
/** | |
* Whether to render GA tracking code | |
*/ | |
public static $enable = true; | |
/** | |
* Don't render GA for users with 'protect' rights | |
*/ | |
public static $ignoreSysops = true; | |
/** | |
* Don't render GA for users with 'bot' rights | |
*/ | |
public static $ignoreBots = true; | |
function onBeforePageDisplay( &$out) | |
{ | |
if(!self::$enable || empty(self::$account)) | |
{ | |
$out->addInlineScript('// GoogleAnalytics disabled'); | |
return true; | |
} | |
global $wgUser; | |
$this->user = $wgUser; | |
$out->addInlineScript( $this->generate() ); | |
return true; | |
} | |
function generate() | |
{ | |
if (!$this->user->isAllowed('bot') || !self::$ignoreBots) | |
{ | |
if (!$this->user->isAllowed('protect') || !self::$ignoreBots) | |
{ | |
$account = self::$account; | |
$funcOutput = <<<GASCRIPT | |
var _gaq = _gaq || []; | |
_gaq.push(['_setAccount', '{$account}']); | |
_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); | |
})(); | |
GASCRIPT; | |
} | |
else | |
{ | |
$funcOutput = "// GoogleAnalytics disabled for users with 'protect' rights (I.E. sysops)"; | |
} | |
} | |
else | |
{ | |
$funcOutput = "// GoogleAnalytics disabled for bots"; | |
} | |
return $funcOutput; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment