Outbound Link Tracking Using Google Analytics

Earlier this year when we launched the OER collection and this blog we decided to utilize Google Analytics.  We did this with the intention of gaining a sense of which types of OER our patrons seem to be most interested in.  Google Analytics offers a simple chunk of javascript that is to be inserted into each webpage of interest.  We did this and so far it has yielded data about which of our pages are being viewed the most, how long users stay on each page, and it gives us an idea of where, geographically our users are located.  Despite doing all of these things it does not give us data about which of our outbound links are being used.  Regrettably, we have been collecting data about how our pages are being used, but none of it is particularly useful to us.  Ideally, we would like to know things like whether or not users are interested in OpenStax books, WikiBooks, open courseware, or any other type or brand of OER, but this is functionality that is not offered by using only the basic Google Analytics code.

 

Global

 The most basic Google Analytics configuration is able to yield interesting data such as this:

Between July 22, 2014 and August 22, 2014 the OER collection has received 679 pageviews over 235 sessions that took place in 22 countries.

 

Domestic

 Although, this is interesting to know, it does not tell us much that we can use to better serve OER.

Earlier this week, we were reminded of how nice it would be to collect this data so I began looking for potential solutions.  Embarassingly,  it did not take much searching before I found what seemed to be a perfect solution!  What I found was that I had completely misunderstood the capabilities of Google Analytics.  In fact, I learned that the basic javascript code that Google provides simply enables Analytics on your page and provides only its most basic functionality.  The piece of javascript below was written by Ralph Slooten.  He has written a wonderful, detailed entry on using Google Analytics to track outbound links where more information on this subject can be found.  All that I did to implement the outbound link tracking capabilities of Google Analytics was insert the following javascript directly after the basic Analytics snippit on each page that I wanted to track.

<script>
function _gaLt(event){
    var el = event.srcElement || event.target;

    /* Loop up the tree through parent elements if clicked element is not a link (eg: an image inside a link) */
    while(el && (typeof el.tagName == 'undefined' || el.tagName.toLowerCase() != 'a' || !el.href))
        el = el.parentNode;

    if(el && el.href){
        if(el.href.indexOf(location.host) == -1){ /* external link */
            ga("send", "event", "Outgoing Links", el.href, document.location.pathname + document.location.search);
            /* if target not set then delay opening of window by 0.5s to allow tracking */
            if(!el.target || el.target.match(/^_(self|parent|top)$/i)){
                setTimeout(function(){
                    document.location.href = el.href;
                }.bind(el),500);
                /* Prevent standard click */
                event.preventDefault ? event.preventDefault() : event.returnValue = !1;
            }
        }

    }
}

/* Attach the event to all clicks in the document after page has loaded */
var w = window;
w.addEventListener ? w.addEventListener("load",function(){document.body.addEventListener("click",_gaLt,!1)},!1)
  : w.attachEvent && w.attachEvent("onload",function(){document.body.attachEvent("onclick",_gaLt)});
</script>

Because our OER collection is built in the form of a LibGuide, I added the standard Google Analytics tracking code and the snippet above to a box that contains the license information for our pages.  LibGuides allows the duplication of boxes via linking meaning that adding these scripts to the HTML of only one box cascaded the changes to all other pages already using the license box.  This made for a quick implementation and with a few test clicks, we instantly saw results in our Analytics data.  Because I only implemented these changes this morning, not much data has been collected; however,  we should soon have a useful histogram that shows which links on our pages receive the most clicks.  Not only does this suit our needs very well, but I should think that it would be easy for most LibGuides users to implement.

License Box

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>