User:Dantman/Analytics integration

From mediawiki.org

I keep putting these in pasties for people, so they're in a page now. Most of the extensions I see for analytics integration are crap for various reasons, and to be quite frank, inflexible. There's no real good reason to use any extension at all to add analytics to your wiki. A short snippet of code to let you insert whatever analytics code any analytics provider gives you is all you need.

The permissions[edit]

To filter out analytics from bots and other groups I add a new right. Feel free to tweak the permissions as you please, I like to filter administrators out of analytics, you can filter less, or even exclude all users from analytics. That's what the permissions are for.

$wgGroupPermissions['*']['noanalytics'] = false;
$wgGroupPermissions['bot']['noanalytics'] = true;
$wgGroupPermissions['sysop']['noanalytics'] = true;
$wgGroupPermissions['bureaucrat']['noanalytics'] = true;

Basic Google Analytics[edit]

$wgGoogleAnalyticsAccount = "UA-[...]";
$wgHooks['SkinAfterBottomScripts'][] = 'lfGAScript';
function lfGAScript( $sk, &$text='' ) {
	global $wgGoogleAnalyticsAccount;
	if ( $sk->getUser()->isAllowed('noanalytics') ) {
		$text .= "<!-- Google Analytics code omitted -->\n";
		return true;
	}
	$text .= <<<GASCRIPT
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '{$wgGoogleAnalyticsAccount}']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script'); 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>

GASCRIPT;
	return true;
}

Throw in some security[edit]

This is something I don't believe most of the extensions offer, good ole security paranoia of not including 3rd party scripts like analytics on pages like login.

function lfIsTitleSafe( $title ) {
	if ( $title->isSpecial('Userlogin')
	 || $title->isSpecial('Userlogout')
	 || $title->isSpecial('Preferences')
	 || $title->isSpecial('Resetpass') )
		return false;
	return true;
}

// [...]

function lfGAScript( $sk, &$text='' ) {
	global $wgGoogleAnalyticsAccount;
	if ( !lfIsTitleSafe( $sk->getTitle() ) ) {
		return true;
	}
// [...]
	return true;
}

Some extra tracking[edit]

I like to track statistics of logged in users, and skins. That way I can tell if my stats are 'really' visitors, and what skins editors like the most.

var _gaq = _gaq || [];
_gaq.push(['_setAccount', '{$wgGoogleAnalyticsAccount}']);
_gaq.push(['_setCustomVar', 1, 'Loggedin', !!wgUserName, 2]);
_gaq.push(['_setCustomVar', 2, 'Skin', skin, 3]);
_gaq.push(['_trackPageview']);

Other analytics[edit]

Naturally the beauty of something so plain and simple being inside your LocalSettings.php is not only being able to tweak it as you need, but also being able to adapt it to any analytics system you add.

$wgQuantcastAccount = "p-[...]";
$wgHooks['SkinAfterBottomScripts'][] = 'lfQuantScript';
function lfQuantScript( $sk, &$text='' ) {
	global $wgQuantcastAccount;
	if ( !lfIsTitleSafe( $sk->getTitle() ) ) {
		return true;
	}
	if ( $sk->getUser()->isAllowed( 'noanalytics' ) ) {
		$text .= "<!-- Quantcast code omitted -->\n";
		return true;
	}
	$text .= <<<QUANTSCRIPT
<!-- Start Quantcast tag -->
<script>
_qoptions={ qacct:"{$wgQuantcastAccount}" };
(function() {
	var q = document.createElement('script'); q.async = true;
	q.src = 'http://edge.quantserve.com/quant.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(q, s);
})();
</script>
<noscript><img src="http://pixel.quantserve.com/pixel/{$wgQuantcastAccount}.gif" style="display: none;" border="0" height="1" width="1" alt="" /></noscript>
<!-- End Quantcast tag -->
QUANTSCRIPT;
	return true;
}