xooAnalytics Wordpress Plugin
Projects November 27th. 2007, 9:37amI’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)(\:\/\/.*?)">(.*?)<\\/a>/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})">(.*?)<\\/a>/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); ?> |
If you have any comments or enhancements, please let me know!

(3 votes, average: 4.67 out of 5)
- 
November 29th, 2007
Where do we put the google code though?
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-3116396-6";
urchinTracker();
</script>
I added it to wp-content/themes/default/header.php, opened up the blog in my browser and did a view source and the google code is there right after the tag but google analytics claims that tracking isn’t installed
Any ideas?
[reply this comment]
Xooxia reply on November 29, 2007:
ancpl,
The code should go just after the opening body tag of the page. When checking installation in your Google Analytics account, it could take a few minutes before showing that the code is installed correctly. While taking a look at your blog, you have the code automatically included via the Ultimate Google Analytics plugin. Because the tracking function (urchinTracker()) will be used within the content of the page, you need to include the JavaScript file before the page content. Therefore, adding the analytics code just below the <body> tag will insure that when the page calls the urchinTracker() function, it will be available.
November 29th, 2007
[…] xooAnalytics will add Google Analytics tracking code to any absolute links in a post/page.It search an absolute URL on any link in any post/page, then it will add the urchin Tracking() functionality as onclick event. When somebody click the link, the tracker will save it as ‘a hit’ for that link. this plugin is authored by xooxia […]
November 29th, 2007
[…] links. The next version will have support for tracking downloadable files. Take a look at the xooAnalytics wordpress plugin here. addthis_url = […]
January 17th, 2008
[…] Visit […]
January 22nd, 2008
[…] Plugin Homepage […]
February 21st, 2008
[…] xooAnalytics 1.4.2 » Xooxia (url) This plugin will mark any external links (those with absolute URLs) and email links for tracking purposes with Google Analytics. Added support to exclude your internal domain name under the Options menu. […]