Friday 26 September 2014

How to know where your visitors go when they leave your website?

How can I see which specific pages/URLs people visit after leaving my site? In other words, I can see the percentage of people that EXIT on a certain page, but I want to be able to see which links on an exit page they follow (i.e. what percent of the visitors to a certain page of our site click on each outbound link on our page)? Or are they just leaving our site without necessarily visiting an outside site we've linked to?

Short Answer: You add this code to your link so it looks like:

<a href="http://www.example.com/" onClick="javascript: pageTracker._trackPageview('/example');">Co name or link info</a>

Will show up in Google Analytics as a page view.

Detailed Answer: (From Google Support) 


You can customize your Google Analytics tracking code to find out when users click outbound links, or links that take users to a website other than your own.
This article gives you an example of how to set up outbound link tracking. This is a two-step process, and you need to follow both steps complete the process.
You must have Google Analytics account and the web tracking code set up before you can track outbound links. You should have a basic knowledge of HTML and JavaScript or work with a developer to complete the set up.

Step 1: Set up an Event to track outbound links

Event tracking is a way you can track user interactions that aren’t automatically collected by the Google Analytics tracking code snippet, including clicks to outbound links. Learn more about Event tracking.
You can copy and paste the example below into your own pages to set up Event tracking for outbound links. We recommend you put this script in your page headers, but not within the basic Google Analytics tracking code snippet.
When you set up an Event, you must define values for the Event components. The Event components define how the data appears in your reports. In this example, the CategoryAction, and Label are defined (in bold). You can use these values, or change them and define your own values. Learn more about Event components or refer to our Developer Guides for more technical information on the Event tracking.
The changes you need to make to your web pages depend on which tracking code you’re using. See if you have Classic Analytics (ga.js) or Universal Analytics (analytics.js).
This example uses Event tracking for Universal Analytics. If you’re using Classic Analytics, refer to our Developer Guides for more information on how to track outbound links with Events using the ga.js JavaScript library.
<script>
/**
* Function that tracks a click on an outbound link in Google Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label.
*/
var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
     function () {
     document.location = url;
     }
   });
}
</script>

Step 2: Add the onclick attribute to your outbound links

After you have Event tracking set up (Step 1), you must also add (or modify) the onclick attribute to your links. This is how data from a specific link gets sent to Google Analytics.
Use this example as a model for your own links:
<a href="http://www.example.com" onclick=”trackOutboundLink(‘http://www.example.com’); return false;">Check out example.com</a>

Additional resources (for developers)

This example includes the hitCallback field, which tells Google Analytics when the user interaction is complete., and uses the trackOutboundLink() as the JavaScript function. This makes sure that you collect the interaction data before the user leaves your site.
For more information on how this works, refer to the hitCallback reference in our Developer Guides.

This tutorial describes how to track outgoing links using the NEW Google Universal Analytics.js code, commonly called Analytics.js or UA. If you are using the OLD ga.js code click here.
This guide describes how to track outgoing links using Google Universal Analytics or commonly known as Analytics.js - the NEW (since late 2013) tracking that Google provides it's webmasters.
If the tracking code you use on your website starts with
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function()
... then you are using the NEW Analytics.js code and you can continue reading below.
If however your tracking code starts with
var _gaq=_gaq||[];
... then you are using the OLD Google Analytics code, and you should refer to the other guide: Track outbound links with Google Analytics (ga.js)
Since Google introduced the Asynchronous Tracking method, one of the most common questions is: "how do I track outgoing links"? The solution is quite simple, one has to track outgoing links as events (found in Google Analytics under Behavior - Events). The problem however is that it does not always work for everyone, the reason being that events are only recorded once a link is clicked. If that link takes you away from a page (such as an outgoing link in the same window), that tracking event often does not have time to register with the analytics server before the new page starts to load and the tracking request cancelled.
In order to ensure that tracking is done properly, we either have to ensure that the target window is a new window (eg: _blank), or delay the opening of the link by about half a second, giving your browser enough time to register the event and load the tracking url.
The best method of "auto-tracking" outgoing links is to automatically detect outbound links with JavaScript when they are clicked, and automatically track that event. That tracking event should first check to see whether that link is destined to open in a new window (target="_blank"), and:
  • If yes, register the track, and open the link in the new window
  • If no, register the track and delay opening the link by half a second, then proceed to open that link.
This method is by far the most robust, and simply means you need to include an external JavaScript file on your pages.
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)});
If you are wanting to track links manually (ie: in the code), an outbound link on your website should look something like this:
<a href="http://outgoinglink.com"
   onclick="ga('send','event','Outgoing Links','outgoinglink.com')" target="_blank">Link Text</a>
What this will do (when clicked) is track an event called "outgoing_links" as "outgoinglink.com". This means that in your Google Analytics account, which has an "Event Tracking" section, you now get a category called "Outgoing Links" containing an action (and total recorded) of outgoing links. Please note the target="_blank" as this ensures your web browser is kept open and the event is able to register.
Using this new method, you can theoretically track anything on your website, including downloads, videos, etc. You just need to assign an "onclick" event with your own category and "description" (action), such as:
<a href="/myfiles/mypdf.pdf"
 onclick="ga('send','event','downloads','/myfiles/mypdf.pdf')" target="_blank">Link Text</a>