Guy Rutenberg

Keeping track of what I do

Tracking MediaWiki External Links Statistics using Google Analytics

with 10 comments

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.

Share and Enjoy:
  • del.icio.us
  • StumbleUpon
  • Digg
  • Facebook
  • Mixx
  • Google Bookmarks
  • Simpy

Written by Guy

July 16th, 2007 at 7:06 pm

10 Responses to 'Tracking MediaWiki External Links Statistics using Google Analytics'

Subscribe to comments with RSS or TrackBack to 'Tracking MediaWiki External Links Statistics using Google Analytics'.

  1. Thanks for this tip.

    Isles Tech

    2 Jun 08 at 10:46

  2. does this still work? I would love to try it but don’t want to ruin my precious installation…

    Klusopedia

    29 Dec 10 at 16:47

  3. 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.

    Guy

    29 Dec 10 at 19:26

  4. thanks for your reply, i’ve added it yesterday but so far nothing has showed in my analytics. Do i need to have analytics installed in the way you explained here http://www.guyrutenberg.com/2007/07/13/track-mediawiki-statistics-using-google-analytics/ or should it also work when i’ve added google analytics by using the analytics extension on mediawiki? thanks!

    Klusopedia

    30 Dec 10 at 10:54

  5. 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.

    Guy

    30 Dec 10 at 17:03

  6. Thanks Guy, i’ve changed the code to the updated code provided above. hope it works!

    Klusopedia

    30 Dec 10 at 21:22

  7. Thanks Guy, i’ve changed the code to the updated code provided above. hope it works!

    Rutger

    Klusopedia

    30 Dec 10 at 21:22

  8. 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!

    Klusopedia

    3 Jan 11 at 11:10

  9. 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?

    Guy

    4 Jan 11 at 00:03

  10. 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!

    Klusopedia

    5 Jan 11 at 13:05

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">