ted serbinski – entrepreneur & web architect
  • thoughts
  • about
  • contact



Popular content

  • Blog Redesign and Drupal "Spring Theme" Released
  • Top Tier Gasoline
  • My first 15 min of fame!
  • The US Government on Open Source
  • Preventing Drupal from Handling 404s for Performance
  • MothersClick Wins Niche Social Networking Category in Web 2.0 Awards
  • Fedora: Image Manipulation Servlet
  • Getting Things Done: David Allen at Google
  • Rebuilding a BMW intake: S52 to M50 intake manifold conversion, day 2
  • Live from London
more

Recent comments

  • This is a very nice tutorial
    2 days 23 hours ago
  • Yes that would wor
    6 weeks 17 hours ago
  • I’m Thanks
    6 weeks 17 hours ago
  • I’m Thanks
    6 weeks 17 hours ago
  • trial New to your blog.
    7 weeks 3 days ago
more

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:

  1. <script type="text/javascript">
  2.   // this code adds analytic specific onclick handlers for links to sites in our network (to pass cookies)
  3.   // and external links (to track exit points)
  4.   $(document).ready(function() {
  5.     // strip the host name down, removing subdomains or www
  6.     var host = window.location.host.replace(/^((<sup>\/]+?\.)*)([</sup>\.]{4,})((\.[a-z]{1,4})*)$/, '$3$4');
  7.     var sites = ['parentsclick.com', 'parentsclick.net', 'mothersclick.com', 'fathersclick.com', 'momblognetwork.com'];
  8.     $('a').each(function() {
  9.       var $a = $(this);
  10.       var href = $a.attr('href');
  11.       var pass = false;
  12.  
  13.       try {
  14.         // see if the link is external
  15.         if ( (href.match(/^http/)) && (! href.match(host)) ) {
  16.           $.each(sites, function (i, n) {
  17.             // if link is to one of our sites, pass cookie data
  18.             if (href.match(n)) {
  19.               pass = true;
  20.             }
  21.           });
  22.           if (pass) {
  23.             $a.click(function() {
  24.               pageTracker._link(href);
  25.               return false;
  26.             });
  27.           }
  28.           else {
  29.             // if external link to some other site
  30.             $a.click(function() {
  31.               pageTracker._trackPageview('/outgoing/' + href);
  32.             });
  33.           }
  34.         }
  35.       }
  36.       // IE7 throws errors often when dealing with irregular links, such as:
  37.       // <a href="node/10"></a> Empty tags.
  38.       // <a href="http://user:pass@example.com">example</a> User:pass syntax.      
  39.       catch(error) {
  40.         return false;
  41.       }      
  42.     });
  43.   });    
  44. </script>

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

posted 1 Jun 2008
  • drupal
  • MothersClick

6 comments

#1
sun wrote 3 years 50 weeks ago

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

#2
Schoonzie wrote 3 years 50 weeks 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!

#3
Loukad wrote 3 years 50 weeks ago

Exactly what i was looking for!!

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

Thanks for sharing!

#4
Visitor wrote 3 years 50 weeks 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

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

I hope I did it right.

#5
ted wrote 3 years 50 weeks 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.

#6
Rebecca Murphey wrote 3 years 49 weeks 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.
  • 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>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options


Code examples and downloadable zip files of code are licensed under a Creative Commons License.
All other content, unless where noted, ©2012 Theodore Serbinski. All Rights Reserved.