Track MediaWiki Statistics using Google Analytics

Google Analytics is one of the best free web-statistics services available. It’s also quite easy to use with MediaWiki. To install Google Analytics in you MediaWiki you should put the tracking code, which is something that looks like:

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct="UA-xxxx-x";
urchinTracker();
</script>

in every page, preferably just above the </body> tag. The best way to do so will be to put the tracking code inside the base skin php file. That means that unless you changed the default skin for MediaWiki you need to edit /wiki/skins/MonoBook.php. In this file you will find the </body> tag towards the bottom of the file. Insert the tracking code just above it, save the file, and you’re done, as all pages will now show the script. Google Analytics will start gathering statistics usually in about 24-28 hours.

Update:
If you also want to track external links to files and other websites take a look at
Tracking Mediawiki External Links Statistics Using Google Analytics.

14 thoughts on “Track MediaWiki Statistics using Google Analytics

  1. Lasar

    But what about people who set another skin in their preferences? As far as I can see (not too far, I’m rather new to MediaWiki) they would fall off the grid unless I add the code to every skin I’ve got available.

  2. Guy Post author

    You can easily adjust it to use other skins too. Just look in the skins folder for the main PHP file of the skin, you’ll find there the tag which you should place the code snippet above.

    If you got multiple skins, and allow your users to switch them, you’ll need to repeat this procedure for every skin.

  3. Sancho

    What do you set the “Website URL” and “default page” settings to in the website profile settings for google analytics. I’ve inserted the script, and can see it in the served source, but when I attempt to have google analytics check the tracking status, it doesn’t detect the tracking code.

  4. Guy Post author

    Hi Sancho,

    Please note that it may take up to 48 hours for the code to start working. When you view the HTML source-code of the site (in your browser) can you see the code snippet? Are you sure you copied the correct code snippet from Google? (you are provided by Google with your own unique code snippet).

  5. Lasar

    Adding the code to all templates (MediaWiki comes with a handful of them by default) is the way to go, I guess. It’s just not too elegant, so I was hoping for some sort of mechanism to automatically include it in all templates at once.

    Thanks for the writeup!

  6. AlphaDogWiki

    I have gotten google analytics to work with mediawiki, but I cannot seem to get goal conversion tracking set up and working. How did you guys do yours?

  7. Eddie Ma

    I didn’t even think of doing this until now. I previously used Piwik but it didn’t work correctly — appending the updated Google Analytics code to the skin works correctly too, so this hint is still relevant four years later :D

  8. Guy Post author

    You’ll need to edit your skin. The post has the instructions for the default skin, which it seems you are using too.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>