-
-
Save jkudish/2269675 to your computer and use it in GitHub Desktop.
mediawiki google analytics plugin
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 | |
if ( !defined( 'MEDIAWIKI' ) ) { | |
die( 'This file is a MediaWiki extension, it is not a valid entry point' ); | |
} | |
$wgExtensionCredits['other'][] = array( | |
'path' => __FILE__, | |
'name' => 'Google Analytics Integration', | |
'version' => '2.0.2', | |
'author' => 'Tim Laqua', | |
'descriptionmsg' => 'googleanalytics-desc', | |
'url' => 'https://www.mediawiki.org/wiki/Extension:Google_Analytics_Integration', | |
); | |
$wgExtensionMessagesFiles['googleAnalytics'] = dirname(__FILE__) . '/googleAnalytics.i18n.php'; | |
$wgHooks['SkinAfterBottomScripts'][] = 'efGoogleAnalyticsHookText'; | |
$wgHooks['ParserAfterTidy'][] = 'efGoogleAnalyticsASAC'; | |
$wgGoogleAnalyticsAccount = "UA-12265173-3"; | |
$wgGoogleAnalyticsAddASAC = false; | |
$wgGoogleAnalyticsIgnoreSysops = true; | |
$wgGoogleAnalyticsIgnoreBots = true; | |
function efGoogleAnalyticsASAC( &$parser, &$text ) { | |
global $wgOut, $wgGoogleAnalyticsAccount, $wgGoogleAnalyticsAddASAC; | |
if( !empty($wgGoogleAnalyticsAccount) && $wgGoogleAnalyticsAddASAC ) { | |
$wgOut->addScript('<script type="text/javascript">window.google_analytics_uacct = "' . $wgGoogleAnalyticsAccount . '";</script>'); | |
} | |
return true; | |
} | |
function efGoogleAnalyticsHookText( $skin, &$text='' ) { | |
global $wgUser; | |
$text .= efAddGoogleAnalytics(); | |
$text .= "<script> | |
_gaq.push(['_setCustomVar', | |
1, | |
'Username', | |
'".trim( $wgUser->getName() )."', | |
2 | |
]); | |
</script>"; | |
return true; | |
} | |
function efAddGoogleAnalytics() { | |
global $wgGoogleAnalyticsAccount, $wgGoogleAnalyticsIgnoreSysops, $wgGoogleAnalyticsIgnoreBots, $wgUser; | |
if ( $wgUser->isAllowed( 'bot' ) && $wgGoogleAnalyticsIgnoreBots ) { | |
return "\n<!-- Google Analytics tracking is disabled for bots -->"; | |
} | |
if ( $wgUser->isAllowed( 'protect' ) && $wgGoogleAnalyticsIgnoreSysops ) { | |
return "\n<!-- Google Analytics tracking is disabled for users with 'protect' rights (I.E. sysops) -->"; | |
} | |
if ( $wgGoogleAnalyticsAccount === '' ) { | |
return "\n<!-- Set \$wgGoogleAnalyticsAccount to your account # provided by Google Analytics. -->"; | |
} | |
return <<<HTML | |
<script type="text/javascript"> | |
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); | |
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); | |
</script> | |
<script type="text/javascript"> | |
var pageTracker = _gat._getTracker("{$wgGoogleAnalyticsAccount}"); | |
pageTracker._trackPageview(); | |
</script> | |
HTML; | |
} | |
///Alias for efAddGoogleAnalytics - backwards compatibility. | |
function addGoogleAnalytics() { return efAddGoogleAnalytics(); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment