Quick Tip: Google Analytics Tracking for external links

 

Here’s a nice snippet I’ve been using to track visitor exit flows on some of the sites I’ve built. You see, google analytics doesn’t automatically log what external link visitors click on to exit your site. You might want to know this if your site is trying to reach a certain goal of funneling visitors to a particular place on another server.

For example, if you’re trying to boost views particular YouTube video by directing traffic there, you want to know how effective your website is at doing that. So here’s what you do. You set up a bit of javascript to log a google analytics “event” every time users try to visit an external link (click). If you use jQuery, you can basically copy and paste this snippet anywhere into your code (after jQuery is loaded):

(function($, window, document){

$(document).on('click', 'a[href^=http]', function(e){

    var $this = $(this)
        ,url = $this.attr('href')
        ,newtab = ($this.attr('target') === '_blank' || e.metaKey || e.ctrlKey)
        ;

    window._gaq = window._gaq || [];
    
    try {

        window._gaq.push(['_trackEvent', 'Outbound Links', e.currentTarget.host, url, 0]);
        
        if (!newtab) {

            e.preventDefault();
            setTimeout(function(){
                document.location = url;
            }, 100);
        }
    } catch (err){}
});

})(jQuery, this, this.document);

This will detect whether or not the user has tried to load it in a new tab or if you’re using target="_blank" and accommodate for those outcomes. It will also work before domReady has fired because we’re using event delegation.

Enjoy :)

Sheep Bounce (like an ideal gas) for Science!

 

After seeing the silly, yet addictive website, cat-bounce.com, I decided to quickly remix the idea - For Science!

Sheep Bounce

Using artwork created by Henry Reich (of MinutePhysics), I created a small web app that allows you to play around with sheep that bounce around the screen. The sheep resemble how atoms might jiggle around in an ideal gas. My hope is that it sparks people’s interest to know more about ideal gases. The silly app could be the beginning of a question about physics, so I’ve placed a button leading to one of Henry’s videos in the side-panel.

Will this work to get people to watch more MinutePhysics? I’m not sure. I’ll have to wait until the analytics build up to make any assessment.

I see this as the beginning of a direction towards interaction as a learning process. I hope to build more highly-interactive web apps (with Henry) to engage people in the learning process. Ideally these interfaces would be much more active in education than Sheep Bounce, and would provide not just a top-down approach, but also a bottom-up. By that I mean: learning by interacting with the interface. More on this later…