Tips and Tricks
Tracking Visitor Clicks, Outbound Links, and Non-HTML Files
As discussed at the beginning of this Short Cut,
urchinTracker()
creates pageviews. Because
urchinTracker()
is standard JavaScript, it can be
added to any HTML event handler and thus executed whenever a visitor
performs an action. Therefore, almost any visitor action can be captured
as pageviews within Google Analytics.
The simple implementation for tracking visitor actions, or clicks,
involves adding the urchinTracker()
function to an
HTML tag. For example, to track a visitor click on an image, just add
urchinTracker()
to the onClick
event of that element:
<img src="/image.jpg" onClick="javascript:urchinTracker('/image.jpg');" />
When a visitor clicks on the above image, a pageview will be created for /image.jpg. This exact method can also be used to track non-HTML files:
<a href="/schedule.pdf" onClick="javascript:urchinTracker ('/files/pdf/schedule.pdf');" />PDF</a>
Tip
When creating pageviews for non-HTML files, try to use a
consistent naming convention. This will make it easier to identify
them in the reporting interface. For example, you may want to create a
virtual directory structure using
urchinTracker()
.
In the previous code example, I added /files/
to the value passed to urchinTracker()
. This makes
it easy to identify the non-HTML files in the reports.
Outbound links are tracked in the same manner:
<a href="http://www.lunametrics.com" onClick="javascript:urchinTracker('/outbound/'+this.href);" />www.lunametrics.com</a> ...
Get Google Analytics now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.