ted serbinski – a blog about drupal, macs, productivity, health, and bmws

a blog about drupal, macs, productivity, health, and bmws

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

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.

6 comments

 
sun (not verified) wrote 18 weeks 11 hours ago

Wow! Looks pretty much like a Domain module integration patch for Google Analytics module! :)

 
Schoonzie (not verified) wrote 17 weeks 6 days ago

This is going to come in very handy. I’ve been messing around with jQuery a lot lately and this is a great addition to the tool box. Thanks!

 
Loukad (not verified) wrote 17 weeks 4 days ago

Exactly what i was looking for!!

Tell me, do I simply add it to the header of my site ?

Thanks for sharing!

 
Visitor (not verified) wrote 17 weeks 4 days ago

Actually I made some research (duh!) I downloaded jquery.js (latest version 1.2.6) and upload it in my root folder. Then I added

<script type="text/javascript" src="http://www.food-intolerance.com/jquery.js"></script>         
<script type="text/javascript">                                        
   // your code here                                
</script>                                 

I hope I did it right.

 
ted wrote 17 weeks 4 days ago

Well you need to customize the code for your own site, but this is just an example of how it is being used on our sites. You can put it in the header yes but you need to have jQuery included and your Google Analytics code as well.

 
Rebecca Murphey (not verified) wrote 17 weeks 2 days ago

Glad to hear this was useful for you — just wanted to let you know that I recently wrote a related plugin that lets you do some other link tracking that you might find useful too.

Add your comment

The content of this field is kept private and will not be shown publicly.
  • You can use Textile markup to format text.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <img> <pre>

More information about formatting options

Subscribe to updates

don't worry, spam free!

Recent comments