Skip to content

Tracking External Links and "In-Network" Links with Google Analytics and jQuery

1 min read

MothersClick is a flourishing site that is growing very quickly. As such, tracking user behavior with Google Analytics is becoming very important as it helps to determine how to adjust the site to better meet the needs of our users. And now, as we prepare to launch our full ParentsClick Network of sites, we need to track what they are doing across our network of sites.

Well thankfully Analytics makes this easier through its ability to track outbound links and cross domain links. But the problem then arises, who is going to update all of those hardcoded links with Javascript code? Is there an easier way?

Sure there has gotta be, jQuery is wonderful and we could use that. So I googled it and found a great post doing something very close to what I was looking for: Rebecca Murphey’s blog post on that topic. While this worked great for outbound links, it didn’t help with cross domain (or in-network links). So I modified her code a bit to check links against an array of our sites and adjust the Analytics code as necessary. The result was this:

<script type="text/javascript">
  // this code adds analytic specific onclick handlers for links to sites in our network (to pass cookies)
  // and external links (to track exit points)
  $(document).ready(function() {
    // strip the host name down, removing subdomains or www
    var host =^(([^\/]+?\.)*)([^\.]{4,})((\.[a-z]{1,4})*)$/, '$3$4');
    var sites = ['', '', '', '', ''];
    $('a').each(function() {
      var $a = $(this);
      var href = $a.attr('href');
      var pass = false;

      try {
        // see if the link is external
        if ( (href.match(/^http/)) && (! href.match(host)) ) {
          $.each(sites, function (i, n) {
            // if link is to one of our sites, pass cookie data
            if (href.match(n)) {
              pass = true;
          if (pass) {
            $ {
              return false;
          else {
            // if external link to some other site
            $ {
              pageTracker._trackPageview('/outgoing/' + href);
      // IE7 throws errors often when dealing with irregular links, such as:
      // <a href="node/10"></a> Empty tags.
      // <a href="">example</a> User:pass syntax.
      catch(error) {
        return false;

Hope that helps someone else, makes tracking all of that stuff considerably easier.


Related Posts

MothersClick Acquired by Lifetime Networks [] and the rest of the ParentsClick Network []have been acquired by Lifetime Entertainment [] . I’ve been working on this site and with Dietrich (CEO & founder) for over 2

news — 5 min read

Leo talks about the new site

At the end of last week’s TWiT episode [], Leo and company talk about the new site []. A clip of that can be listened to below. Also last week on the Lullabot podcast []

code — 1 min read

My first 15 min of fame!

A few weeks back, Leo Laporte [] approached Lullabot [] about developing the new TWiT website []. Well we gladly accepted, I mean, this is Leo! So I was put in charge of developing the new site, and

code — 1 min read