Skip to content

Instantly share code, notes, and snippets.

@andoodle
Created March 31, 2012 23:27
Show Gist options
  • Save andoodle/2269674 to your computer and use it in GitHub Desktop.
Save andoodle/2269674 to your computer and use it in GitHub Desktop.
mediawiki google analytics plugin
<?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='' ) {
$text .= efAddGoogleAnalytics();
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(); }
global $wgUser; // get the global user which holds info about the current user ?>
<script>
_gaq.push(['_setCustomVar',
1,
'Username',
'<?php trim( $wgUser->getName() )?>', // you're using php with javascript, get the name from the user global - trim is used to sanitize any special/weird characaters from the name
2
]);
</script>
<?php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment