title: Track all outbound links in Google Analytics
body: <p>I've been using a script to track outbound links for a couple of months now. <strong><em>The script WORKS</em></strong>, but in the report generated by Google Analytics many URLs are having a trailing &quot;:80&quot; (default port number) at their end. Read on for more details.</p>
  <p><em>It's maybe important to mention that the website tracking these outbound links has a tremendous amount of outbound traffic (multiply your fantasy by ∞).</em></p>
  <h2>The script's purpose</h2>
  <p><em>It tracks ALL outbound links and tag them as &quot;Outbound Links&quot; in Google Analytics.</em></p>
  <p>The script is heavily commented and has a few instances of console.log() to help debugging (these are kept commented out).</p>
  <p>&quot;Outbound Links&quot; show on GA alright, under:</p>
  <p><strong>Content &gt; Events &gt; Top Events &gt; &quot;Outbound Links&quot; [click on it] &gt; [report showing all urls clicked]</strong></p>
  <h2>The problem</h2>
  <p>Under the &quot;Outbound Links&quot; report, where I get all the links that were clicked, I get &quot;:80&quot; at the end of at least 2/3 of all links reported (probably more). GA treats <a href="http://example.com" rel="noreferrer">http://example.com</a> and <a href="http://example.com:80" rel="noreferrer">http://example.com:80</a> as different links, separating them in the report. <em>That's of course not desired.</em></p>
  <p><strong><em>Worth mentioning:</em></strong></p>
  <p>Links that end with &quot;:80&quot; always have more hits than their equivalent without &quot;:80&quot;, anything from 40% to 60% more hits.</p>
  <h2>The wanted solution</h2>
  <ul>
  <li><em><strong>Merge the links that end with &quot;:80&quot; with those without it, OR</strong></em></li>
  <li><em><strong>Avoid appending &quot;:80&quot; to links, if possible.</strong></em></li>
  <li>Bonus: Understand why we get links ending with &quot;:80&quot; at all.</li>
  </ul>
  <h2>The script</h2>
  <pre><code>// Outbound Link Tracking with Google Analytics
  // Requires jQuery 1.7 or higher (use .live if using a lower version)
  $(function() {
      $(&quot;a&quot;).on('click',function(e){
          var url = $(this).attr(&quot;href&quot;);
          // Console logs shows the domain name of the link being clicked and the current window
          // console.log('e.currentTarget.host: ' + e.currentTarget.host);
          // console.log('window.location.host: ' + window.location.host);
          // If the domains names are different, it assumes it is an external link
          // Be careful with this if you use subdomains
          if (e.currentTarget.host != window.location.host) {
              // console.log('external link click');
              // Outbound link! Fires the Google tracker code.
              _gat._getTrackerByName()._trackEvent(&quot;Outbound Links&quot;, e.currentTarget.host, url, 0);
              // Checks to see if the ctrl or command key is held down
              // which could indicate the link is being opened in a new tab
              if (e.metaKey || e.ctrlKey) {
                  // console.log('ctrl or meta key pressed');
                  var newtab = true;
              }
              // If it is not a new tab, we need to delay the loading
              // of the new link for a just a second in order to give the
              // Google track event time to fully fire
              if (!newtab) {
                  // console.log('default prevented');
                  e.preventDefault();
                  // console.log('loading link after brief timeout');
                  setTimeout('document.location = &quot;' + url + '&quot;', 100);
              }
          }
          /*
          else {
              console.log('internal link click');
          }
          */
      });
  });
  </code></pre>