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.
Thanks for this tip.
does this still work? I would love to try it but don’t want to ruin my precious installation…
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.
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!
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.
Thanks Guy, i’ve changed the code to the updated code provided above. hope it works!
Thanks Guy, i’ve changed the code to the updated code provided above. hope it works!
Rutger
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!
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, 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!