Integrating Google Analytics in Mediawiki

This post was originally posted on Digimantra

In this post, we will talk about how to hack the Mediawiki Template to include Google Analytics without tracking the sysop user. The better way is, obviously, to use the GoogleAnalytics Extension (which would be discussed in a later post). This hack applies to Mediawiki 1.9.3+ In the mediawiki…

This post was originally posted on Digimantra

Inserting Google Analytics Code in Media Wiki

IMPORTANT: I do not know PHP, or for that matter any server-side scripting language. This method (described here) is a work around to successfully add your Google Analytics code in your mediawiki installation, based on the article: MediawikiEtGoogleAnalytics/En. I am using Mediawiki 1.9.3 and I don’t know if this works on previous/later versions though logically it should.

When I followed the aforementioned article to add Google Analytics (referred to as ‘gA’ henceforth) on the OSS Camp wiki that I am currently administering, I found that the code is successfully added (there was an error for the “if (!$wgUser->isSysop())” check in the addGoogleAnalytics() function which i commented), but it is nowhere near the </BODY> tag as required for gA to work correctly. So we needed some alternative method. I digged in a bit at the Media Wiki website and tried understanding the Media Wiki flow to understand how the output page is being created. I observed this in the “includes/Skin.php” file:

(around line # 691)

/**
* This gets called shortly before the \</body\> tag.
* @return String HTML-wrapped JS code to be put before \</body\>
*/

function bottomScripts() {
global $wgJsMimeType;
return “\n\t\t<script type=\”$wgJsMimeType\”>if (window.runOnloadHook) runOnloadHook();</script>\n” ;
}


I changed this to:

/**
* This gets called shortly before the \</body\> tag.
* @return String HTML-wrapped JS code to be put before \</body\>
*/

function bottomScripts() {
global $wgJsMimeType, $gStr;
$gStr = $this->addGoogleAnalytics();
return “\n\t\t<script type=\”$wgJsMimeType\”>if (window.runOnloadHook) runOnloadHook();</script>\n” . $gStr ;
}

And at the end of the document, just below the last closing brace “}”, I added the addGoogleAnalytics() function. The modified function is shown below:

(color coding this whole block was a pain and i am lazy)

function addGoogleAnalytics() {$printGoogleAnalytics = “<script src=\”http://www.google-analytics.com/urchin.js\” type=\”text/javascript\”>\n”;
$printGoogleAnalytics .= “</script>\n”;
$printGoogleAnalytics .= “<script type=\”text/javascript\”>\n”;
$printGoogleAnalytics .= “_uacct = ” ;
$printGoogleAnalytics .= “\” UA-XXXXXX-X \”" ;
$printGoogleAnalytics .= “;\n”;
$printGoogleAnalytics .= “urchinTracker();\n”;
$printGoogleAnalytics .= “</script>\n”;return $printGoogleAnalytics;
}

Substitute the highlighted XXXXXX-X with your code and et voila! the code is working fine, absolutely fine!

Do leave a comment if you find this useful and I do want to credit the original author (at the aforementioned link) for his work, without which I couldn’t have found the solution.



Warning: Invalid argument supplied for foreach() in /home/kinshuks/public_html/blog/wp-content/themes/PinkMyRide/footer.php on line 3