I’ve developed a quick and easy little wordpress plugin that will add Google Analytics tracking code to any absolute links in a post/page. You can go to the plugin home page at http://wordpress.org/extend/plugins/xooanalytics/. It basically just searches the post content for a regular expression match for a link. If the link contains an absolute URL, then it adds the urchinTracker() function as an onclick event. When a user clicks on that link, the urchinTracker function saves that click as a hit on that link.

The code appends ‘/external/’ to a link and ‘/mailto/’ to an email address, so you can filter all of your hits with the ‘contains /external/’ or ‘contains /mailto/’ section and you’ll only see results to external links from your posts/pages.

Requirements, you must have the Google Analytics code just below the opening <body> tag of your page, rather than at the bottom of the page before the closing </body> tag. Otherwise, your page will throw a JavaScript error.

Here’s a look at the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
Plugin Name: xooAnalytics
Plugin URI: http://xooxia.com/projects/xooanalytics-wordpress-plugin/
Description: This plugin will mark any external links (those with absolute URLs) and email links for tracking purposes with Google Analytics.
Version: 1.2.1
Author: Xooxia
Author URI: http://xooxia.com
*/
/*  Copyright 2007  Xooxia Development  (email : xooxia@gmail.com)
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
 
function xooAnalytics($content){
	// first, add tracking to all absolute links and append /external/ to them
	$content = preg_replace('/<a href="(http|https|ftp)(\:\/\/.*?)">(.*?)&lt;\\/a&gt;/i', '</a><a href="$1$2" onclick="urchinTracker(\'/external/$1$2\')">$4</a>', $content);
	// then, add tracking to all email links and append /mailto/ to them
	$content = preg_replace('/<a href="(mailto\:)(\w+[\w-\.]*\@\w+)((-\w+)|(\w*))(\.[a-z]{2,3})">(.*?)&lt;\\/a&gt;/i', '</a><a href="$1$2$3$4$5$6" onclick="urchinTracker(\'/mailto/$2$3$4$5$6\')">$8</a>', $content);
	return $content;
}
 
add_filter('the_content', 'xooAnalytics', 0);
?&gt;

If you have any comments or enhancements, please let me know!

If you enjoyed this post, make sure you subscribe to my RSS feed!

Bookmark: del.icio.us Reddit Slashdot Digg Facebook Technorati Google StumbleUpon Windows Live Furl Yahoo Ask Newsvine Simpy Backflip Spurl Squidoo

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.67 out of 5)