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 = window.location.host.replace(/^(([^\/]+?\.)*)([^\.]{4,})((\.[a-z]{1,4})*)$/, '$3$4');
    var sites = ['parentsclick.com', 'parentsclick.net', 'mothersclick.com', 'fathersclick.com', 'momblognetwork.com'];
    $('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) {
            $a.click(function() {
              pageTracker._link(href);
              return false;
            });
          }
          else {
            // if external link to some other site
            $a.click(function() {
              pageTracker._trackPageview('/outgoing/' + href);
            });
          }
        }
      }
      // IE7 throws errors often when dealing with irregular links, such as:
      // <a href="node/10"></a> Empty tags.
      // <a href="http://user:pass@example.com">example</a> User:pass syntax.
      catch(error) {
        return false;
      }
    });
  });
</script>

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

code

Related Posts

Members Public

The Future of Automotive Mobility will be Powered by Open Source Software

Automotive companies are shifting from bending metal to bending bits. Soon they will be offering software and services to complement their manufactured metal. In order to win in the market, they will need to embrace open-source software to build leverage and enhance their offering.

Members Public

How to Host Your Static Website on Amazon S3: Step-by-Step Guide

1. Login to your Amazon AWS account 2. Create a new hosted zone for your domain using Route 53 3. Copy the NS records for this zone to your domain provider (e.g., GoDaddy) 4. Edit your domain on your domain registrar (e.g., GoDaddy) by clicking Set Nameservers 5.

Members Public

Book Review: Drupal 7 Module Development

Well, it’s been a while my Drupal friends, almost 18 months… dusting off this old Drupal 6 blog and time to start posting again. And what better way to get started again than by talking about Drupal 7, by far the best release of Drupal. And with almost 7