From the category archives:

analytics and metrics

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://','');
}
}
});
}
});

{ 1 comment }

This seems to be something that people always want to do at some point - are people clicking on my outbound links?

How Google recommends external tracking

They currently recommend you tag your links like:

<a href="http://www.example.com" onClick="javascript: pageTracker._trackPageview("/G1/example.com");">

Just as a point of reference - the /G1/example.com is purely symbolic - you can use the artificial pagename as the Goal URL in your analytics, as google shows:

/G1/example_com

or

http://mysite.com/G1/example_com

Kind of tedious, wouldn’t you say? Do you really want to run through your site and specifically tag each link that goes somewhere else? I’ve seen functions attached that basically duplicate this functionality - but if you’re moving the “onclick” into a function, it seems pretty much a moot effort.

So, boom goes the dynamite, and in comes javascript.

Add outbound tracking javascript

This code from Jamie Huskisson accomplishes it simply:


if (document.getElementsByTagName){
var ahrefs = document.getElementsByTagName('a');
for(var i=0;i<ahrefs .length;i++){
if(ahrefs[i].href.indexOf('http://ikeif.net') == -1 &amp;&amp; !ahrefs[i].onclick){
ahrefs[i].onclick = function(){
var track = this.href + "pageTracker._trackPageview('/outgoing/'"+track.substring(7));
}
}
}
}

Nice and succint, as code should be. In this example, it checks to see if we can grab tags - grabs all anchors, and then we check to see if they reference my site and there is no onclick event, and if so - assign the onclick! Technically, you don’t necessarilly need the track.substring(7) - you can call it something else if you like.

Do the same - but with MooTools!
As I’m sure you’ve figured it out, I’m a fan of mootools - one of the many well done javascript frameworks. The above code is just as easilly executed:


window.addEvent('domready',function(){
if ($$('a')){
$$('a').each(function(anchor){
var href = anchor.get('href');
if(href.indexOf('http://ikeif.net') == -1 &amp;&amp; href.indexOf('/') !== 0) {
anchor.addEvent('click', function(e){
var track = this.href +"pageTracker._trackPageview('/outgoing/'" + track.substring(7);
}
}
});
}
});

[Edit: It just so happens that David Walsh hit the same topic!]

{ 0 comments }

As a developer and an enthusiast of metrics and media, it’s handy to help flesh out your numbers and “weed yourself out.” Numbers - especially metrics - can help define success or failure of campaigns.

Why would I filter myself?

Why should you neglect yourself? Ever number counts, right? Well, because as a developer (or author, or editor, or paranoid owner) you can skew your metrics numbers by visiting, testing, reloading, hitting the page again and again - throwing off all your numbers. You need unadulterated materials to work with - so at launch when all the employees are visiting that cool new micro site, you know that those million visits were filtered out, which makes the million other visits a lot more relevant.

How to: filter by IP address

Google makes it incredibly easy - you just need to follow a few basic steps.

  1. Collect the IP addresses you need blocked (i.e. the network you want blocked).
  2. Log in to Google Analytics and select “edit” under profile, in the same row as your site.

    Google Analytics Dashboard Snap-shot

    Google Analytics Dashboard Snap-shot

  3. Go to the section ‘Filters applied to profile’ and select ‘+Add Filter.’
  4. For this example we want to choose ‘Exclude all traffic from an IP address’
  5. Enter the IP address(es) you collected, and in true coders fashion, we are going to ‘escape’ the ‘.’ using a backslash  - like xxx\.xxx\.xx\.xx

How to: filter by domain

Working on a recent project, I noticed that their hits were skyrocketing - due to the massive amounts of hits from my testing (and their testing, and QA). They had no filters set up on their development site!

As a developer, this should be standard practice - or, if you have a metrics person to work with, have them set up the filter for you. You don’t want to “comment out” the analytics code - this can cause you to forget to uncomment it, or worse case, find another developer has deleted the un-used code (and if you don’t have a subversion repository, you could lose whatever custom code was being used). Fortunately, it’s just as easy to filter out your test domains - On step four, you just select ‘Exclude all traffic from a domain’ and enter it in - a la ‘dev.test.com.’

{ 6 comments }