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.

Suggested Reading


8 Comments On This Post


Comment by PESME-CANSAR Sébastien on June 18th, 2007

Hi, this seems to be nice, I will try it to see if that works on Mediawiki 1.6.x.

Thanks,

Seb.

Comment by Steve on June 25th, 2007

Hi:

I’m not sure what the difference is between using this method and pasting the straight google code into the {skinname}.php file in the skins subdir? I’ve pasted the straight google code into my skinname file in the skins subdir and it is working just fine.

Thanks,
steve

Comment by kinshuksunil on June 25th, 2007

Hi Steve…

I did say in my post that I am not a pro with either Media Wiki nor PHP.. i am not even a beginner… But I think that there is one inherent advantage of this method over including the gA code in the {skinname.php} file and that is:

you would have to include the code in every skin on your wiki (dunno, why you’d want that.. yet) while the method i am describing (which is, in essence, just a workaround and not a solution) will be done only once and would be automatically added in all the skins.

I don’t see any other difference.

Comment by Phanx on July 7th, 2007

Hi… thanks for u great tip. the code is shown on every site, but the gA tool says that it isnt avaible. did u have a hint for me? other sites with gA working fine in my panel.

Comment by kinshuksunil on July 7th, 2007

Hey Phanx…

I encountered the same problem when I tried to host two websites on the same domain… something like:

http://www.firstsite.tld and
http://www.firstsite.tld/secondsite

in this case the trick didnt work for one of the sites.. i hoep you are not trying something similar… other than that… i have no clue

Comment by Brisbane SEO Guy on August 31st, 2007

I was looking for a way of sticking some javascript for a web site stats program in my SEO training Wiki. This post helped me find a way to do it. I tried the easy way first – just stuck the code in the monobook.php file

Worked a treat. Thank you.

Comment by Google T on September 3rd, 2007

This is a better way, its just an extension:

http://www.mediawiki.org/wiki/Extension:Google_Analytics_Integration

Comment by John on November 6th, 2007

I ve been having problems with analytis and the newest MW version 1.11

I tried to put it in monobook.php but google doesnt find the conversion code. So I will try the extension and let you know.

Leave Your Own Comment on This Post


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