Most solutions that I've found for tracking outbound links with Google Analytics have been to send the link through a local file that would then be able to be tracked by Google, sending a query string as your end-result location. I never really liked that.

The other day, I accidentally found someone else's solution to use jQuery and Google Analytics for outbound link tracking.

The following code snippet is what I ended up with for my own use (so it doesn't add an actual visit/pageview to my own site's ranking):

What's happening here?
$(document).ready(function(){ - when the DOM is ready, do the following...

$('a[href^="http"]:not([href*="'+domain+'"])').click(function(){ - whenever a hyperlink is clicked that does not contain our domain anywhere in the URL, run the following... (this includes any subdomain, further string trickery would be required to keep subdomains listed in the same category)

pageTracker._trackEvent('Outbound', 'click', $(this).attr('href')); -- Run Google's _trackEvent method to register an event. Typically the 2nd parameter passed in is an "action". In my final version, I changed "click" to be $(document.location) instead, so that I can tell where people are clicking these outbound links from.

You can read more about the Google method "_trackEvents" from the Google Documentation on Google Analytics. Oh, and...about the commented out code, that's more of a reminder to myself, if I ever want to track PDF or DOC file clicks on my own domain, but the code would have to be revised a bit for that to work. Also, make sure you change the domain string to whatever you'd like to track (a specific subdomain, or your whole domain).

Trackbacks


Trackback specific URI for this entry
    jQuery and Google Analytics: Take 2
    Awhile back I had reported on my findings of how to use jQuery to automatically tag links on my site (that link to a site that is not my own) for event tracking with Google Analytics. Although there are ways to use the pageView method and parse the dat
    Weblog: mysiteonline™
    Tracked: Dec 09, 12:45

Comments


    #1 Brendon Kozlowski on 08/26/09 at 02:27 PM [Reply]
    Something seems to not be working for me. The _trackPageview code works as expected, but the _trackEvents is not aggregating data. I will need to look in to this further and report back.
    #1.1 Leon Ridge-Cooke on 03/25/10 at 07:50 PM [Reply]
    Hey,

    Did you manage to get this to work in the end?
    #1.1.1 Brendon Kozlowski on 03/25/10 at 09:22 PM [Reply]
    Actually, no. But I do believe I now know how to fix it. I will test it tomorrow and both update this entry and write a new blog post. Thank you for reminding me.

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

BBCode format allowed