<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Paul Hayes &#187; analytics</title>
	<atom:link href="http://www.paulrhayes.com/tag/analytics/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paulrhayes.com</link>
	<description>The web musings and experiments of.</description>
	<lastBuildDate>Sun, 06 Nov 2011 11:31:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Track outbound links using Google Analytics</title>
		<link>http://www.paulrhayes.com/2009-03/track-outbound-links-using-google-analytics/</link>
		<comments>http://www.paulrhayes.com/2009-03/track-outbound-links-using-google-analytics/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 00:06:20 +0000</pubDate>
		<dc:creator>Paul Hayes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[analytics]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.fofronline.com/?p=14</guid>
		<description><![CDATA[Combine CSS3 selectors and analytics event tracking to report on outbound links.]]></description>
			<content:encoded><![CDATA[<p>“Track everything”, lest vital visitor trends fall through the cracks — that’s my newly endorsed web analytics doctrine. As a precursor to the quantitative ‘what’ and the qualitative ‘why’ we need that cold hard data before analysis can begin; Google Analytics is the popular harvester of choice and out of the box it grabs a lot. Visits, Pageviews, Screen resolution, et al — <acronym title="Google Analytics">GA</acronym> seemingly has all your conventional data needs covered. But one significant trend is lacking — how visitors leave your site, specifically through outbound links on a page, data that inevitably leads to a what and an avenue for investigating the why. For instance, “<em>Which partner sites are attracting the highest click throughs?</em>” or more generally “<em>Why do visitors leave my site?</em>”.<br />
<span id="more-14"></span></p>
<p>GA gives the ability to <a href="http://code.google.com/apis/analytics/docs/eventTrackerGuide.html">create your own events</a> with a category, action, label and numerical value using the syntax:</p>
<pre class="thin">_trackEvent(category, action, optional_label, optional_value)</pre>
<p>Hence, on an outbound link click, by calling this JavaScript method you can trigger a tracked event in GA. An obtrusive onclick attribute on every outbound link is both cumbersome to implement and difficult to manage, it also goes against the best practices of progressive enhancement and unobtrusiveness.</p>
<p>The solution is to attach a click event listener to each of the outbound links on the page, and the question becomes how to do that. CSS3 comes with a couple of handy <a href="http://www.w3.org/TR/css3-selectors/">new selectors</a> that we can use in combination with Prototype or jQuery to root out the correct links. The appropriate selectors:</p>
<blockquote><p>
E[foo^=“bar”]  	an E element whose “foo” attribute value begins exactly with the string “bar“<br />
E[foo*=“bar”] 	an E element whose “foo” attribute value contains the substring “bar“<br />
E:not(s)  	an E element that does not match simple selector s
</p></blockquote>
<p>The magic outbound link selector then becomes one of the following, depending on your needs:</p>
<pre class='prettyprint'>
/* Any link that does not contain yourdomain.com */
a:not(a[href*="yourdomain.com"])

/* Any link that does not start with yourdomain.com */
a:not(a[href^="yourdomain.com"])

/* Any link that does not start with yourdomain.com or www.yourdomain.com */
a:not(a[href^="yourdomain.com"]):not(a[href^="www.yourdomain.com"])

/* Any link that starts with http - e.g. any non relative links */
a[href^="http"]

/* Catch all - any link that starts with http but doesn't link to your domain */
a[href^="http"]:not(a[href*="yourdomain.com"])
</pre>
<p>With an array of all the outbound links at hand, adding a click listener is simple. But we do need to set up the category, action and label. I have opted to create an arbitrary “Outbound Link” category that uses the link’s text (with HTML tags stripped out) as the action and the url as the label:</p>
<pre class='prettyprint'>
Event.observe(outboundLink, 'click', function() {
        // category, action, label
        pageTracker._trackEvent('Outbound Link', outboundLink.innerHTML.replace(/(&lt;([^&gt;]+)&gt;)/ig,&quot;&quot;), outboundLink.href);
}
</pre>
<h3>The complete code</h3>
<p>Using Prototype version 1.6 the final code might look like this:</p>
<p><strong>Update</strong>: As pointed out in the comments, hard coding a domain into your code isn’t the best idea, <code>window.location.hostname</code> is a good alternative. This may not always work if you do not want to exclude subdomains.</p>
<pre class="prettyprint">
var domainName = &quot;domainname.com&quot;;
// Select all outbound links
$$('a[href^=&quot;http&quot;]:not(a[href*=&quot;'+domainName+'&quot;])').each(function(outboundLink) {
        // Add listener to each of the links
        Event.observe(outboundLink, 'click', function() {
        // category, action, label
        pageTracker._trackEvent('Outbound Link', outboundLink.innerHTML.replace(/(&lt;([^&gt;]+)&gt;)/ig,&quot;&quot;), outboundLink.href);
        }
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.paulrhayes.com/2009-03/track-outbound-links-using-google-analytics/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

