Google Analytics Moo-ized

by keif on October 14, 2008

As both David Walsh and I wrote about Google Analytics Tracking utilizing Mootools to make your outbound link tracking easier, and David did setup a variable for the Google Tracking code *before* we run the code to tag our outbound links.

Upon second review, it’s a little different than the modified code I presented yesterday, but I dig using the href replacement on your outgoing string.

Here’s the updated code:


window.addEvent('load',function(){
// Double-checking in case your drop this site wide
// Do anchors exist on the page?
if ($$('a')){
$$('a').each(function(anchor){
var href = anchor.get('href');
// if it matches my site or is an absolute path it's outgoing
if(href.indexOf('http://ikeif.net') == -1 && href.indexOf('/') !== 0) {
anchor.addEvent('click', function(e){
var track = "pageTracker._trackPageview('/outgoing/'" + href.replace('http://','');
}
}
});
}
});
http://ikeif.net/2008/10/14/google-analytics-mooized/

{ 1 comment… read it below or add one }

1

Gravatar IconLim Chee Aun 10.15.08 at 2:11 am

I’m seeing lots of HTML ampersand codes in the code example :) And uh, I think var anchors = $$('a[href]‘); would be better.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>