Tracking MediaWiki External Links Statistics using Google Analytics

When you track MediaWiki statistics, you usually track only internal page statistics, but tracking external links which leads out of your site is not some thing you can ignore. Unfortunately we probably can’t put actual tracking code in the pages linked to by our site’s external links. Fortunately we can track the actual clicks on those links that lead out of the site, and it’s quite easy to do when tracking statistics with Google Analytics. If you don’t already use Google Analytics with your MediaWiki site, open a new account in Google Analytics and see my previous post: Track MediaWiki Statistics using Google Analytics.

Now we have two possible ways to add the tracking code to each external link. The first one is to hack the MediaWiki internal parser for wiki code to generate additional code for each link. While this way will probably work the best it’s pretty complicated and not straightforward. The other way, which is the one will follow, is to use a small JavaScript snippet which will will iterate through the links in every page and add an “onClick” attribute to them with the tracking code.

<script>
var links = document.getElementsByTagName("a");

for (var i = 0; i < links.length; i++) {
        if (links[i].className=="external text") {
                addtrackcode(links[i]);
        }
}

function addtrackcode(obj) {
        obj.setAttribute('onClick',"javascript:pageTracker._trackPageview('/outgoing/"+ obj.href.split("://")[1]+"');");
}
</script>

This code snippet should go between you Google Analytics code and the </body> tag, meaning you can add it to /wiki/skins/monobook.php if you haven’t changed the default skin for your MediaWiki. After the code is in place it may take up to 48 hours (usually you don’t have to wait at all) for the external links statistics to show up in the Google Analytics. In the Google Analytics the statistics for clicking on an external link which leads to http://www.google.com will show up as view of the page /outgoing/www.google.com. This script will also track download statistics of files which are linked from the wiki pages (like pdf”s and such).

While this code does the job, it has a drawbacks. It only tracks the external links created by wiki code (e.g. [http://example.com]) as it uses CSS classes to determine what is external link. However this drawback isn’t very important and overall the script does a decent job tracking the external links in MediaWiki using Google Analytics.

UPDATE 2010-12-30: updated the post to reflect an Analytics API change.

10 thoughts on “Tracking MediaWiki External Links Statistics using Google Analytics”

  1. I think it should still work. Anyway, it shouldn’t create any long term effect if you add it, and delete it afterwards, as it’s fully client-side.

  2. It shouldn’t matter how you added the analytics code,a s long as it’s there. Apperantly, since the post was written, there was a slight update to the Analytics’ API, so I update the post to reflect it. Try with the updated code, and see if it works for you.

  3. Guy,

    unfortunenately it doesn’t work yet. Just to be sure this is what i did.

    1. i already have google analytics set up and it works perfectly.
    2. i added your code to monobook.php just before the body tag and i’m using only monobook as a skin.

    i dont see anything appearing on GA although i am certain that there are quite some clicks on external links every day.

    Do i need to set some other things in GA? What am i doing wrong?

    thanks again!

  4. Just to make sure, you’ve put it just before the closing body tag? Where does your analytics code code? Before or after my snippet?

  5. Guy, many many thanks to you! I was a bit stupid and pasted the code before the starting body tag! Now i’ve placed it before the closing tag and it works!

Leave a Reply

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