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