{"id":2516,"date":"2016-06-14T17:57:27","date_gmt":"2016-06-14T17:57:27","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=2516"},"modified":"2016-06-14T17:57:27","modified_gmt":"2016-06-14T17:57:27","slug":"tracking-external-links","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/tracking-external-links\/","title":{"rendered":"How to Track External Links From Your Website"},"content":{"rendered":"<p>Google Analytics is great, and keeps improving all the time. But one thing it can&#8217;t do natively is track where a visitor goes after leaving your site through an external link.<\/p>\n<p>You can, of course track this yourself, by firing off an AJAX call\u00a0to store data\u00a0when a user clicks an external link.<\/p>\n<p>There&#8217;s an easier way though, one that ties into your existing data in Google Analytics. It leverages &#8220;event tracking&#8221;.<\/p>\n<p>Google Analytics provides a means to track custom events on your site, such as &#8220;user downloaded a PDF&#8221; or &#8220;user clicked a radio button&#8221;.<\/p>\n<p>In our case, we&#8217;re interested in tracking when a user clicks an external link from your website.<\/p>\n<p>Let&#8217;s say, for example, that you have some Amazon affiliate links setup on your site. You&#8217;d like to know which specific pages links your visitors\u00a0are using.<\/p>\n<p>Let&#8217;s start with the code, then I&#8217;ll explain. jQuery example below:<\/p>\n<pre class=\"prettyprint\"><code>$('a[href*=\"amazon\"]').click(function(e) {\r\n\r\n  e.preventDefault(); \/\/ cancel the link event\r\n  var href = $(this).attr('href');\r\n  var label = '';\r\n\r\n  if ($(this).children('img').length) {\r\n    \/\/ use the image alt tag as the event label\r\n    label = $(this).children('img').first().attr('alt');\r\n  }\r\n  else {\r\n    \/\/ if it's a text link, use the link text as the event label\r\n    label = $(this).text();\r\n  }\r\n\r\n  var eventParams = {\r\n    'hitType': 'event',\r\n    'eventCategory': 'Amazon Link',\r\n    'eventAction': 'click',\r\n    'eventLabel': label,\r\n    nonInteraction: true\r\n  }\r\n\r\n  eventParams['hitCallback'] = function() {\r\n    location.href = href;\r\n  }\r\n\r\n  setTimeout(function() {\r\n    \/* I like to add a fallback, just in case the event tracking takes too long, \r\n       or errors for some reason *\/\r\n    location.href = href; \/\/ in case event tracking fails\r\n  }, 2000);\r\n\r\n  ga('send', eventParams);\r\n\r\n});\r\n<\/code><\/pre>\n<p>In the snippet above, we are adding a click event handler to all outbound links to Amazon.com.<\/p>\n<p>The tracking on the individual links is handled automatically by grabbing the alt tag from an image or the text with a text-based link. Given this, you&#8217;ll want to make sure that your links or alt tags contain text that is easily identifiable and unique.<\/p>\n<p>In order to ensure that the event tracking works, you need to &#8220;cancel&#8221; the link event itself. Google Analytics Event tracking provides a callback that you can leverage to navigate to the link\u00a0&#8220;manually&#8221;.<\/p>\n<p>I like to provide a &#8220;just-in-case&#8221; timeout that will be called in the off chance that Google&#8217;s callback doesn&#8217;t fire, or simply takes too long.<\/p>\n<p>That&#8217;s it! You&#8217;ll find these events in the Real-Time or Behavior \/ Events areas of Google Analytics.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visitors will leave your site via external links, we can&#8217;t fight it. But we can gather data on where the user is going and what pages they are exiting from with just a little bit of JavaScript.<\/p>\n","protected":false},"author":1,"featured_media":2520,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[5],"tags":[297,298,133,235],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2516"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=2516"}],"version-history":[{"count":4,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2516\/revisions"}],"predecessor-version":[{"id":2522,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2516\/revisions\/2522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media\/2520"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=2516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=2516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=2516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}