Unobtrusive Google Analytics

Tracking your visitors around your website is now made easy by Google. Analytics is a very nice piece of software and very easy to use. Just place the google analytics javascript in your code and your done.

If you do not have a google analytics account or you do not know what it is. Check google for more information.

if your site has a lot of navigation items like menu, submenu, breadcrumbs and perhabs an utilitymenu and a lot of links in your navigation point to the same page in different menus. It would be helpfull to see wich menu they have clicked.

The script

window.onload = setLinkBehaviours;

function setLinkBehaviours()
{
	var Links = document.getElementsByTagName( 'A' );
	
	for( var i = 0; i < Links.length; i++ )
	{
		Links[i].onclick = function() {
		
			if( this.title == '' 
				|| this.title == null 
				|| this.title == undefined )
			{
				urchinTracker( this.href );
			}
			else
			{
				urchinTracker( this.title );
			}
				
		}
	}
}

This script will get all the link elements in your document and store them in array

var Links = document.getElementsByTagName( 'A' );

Loop through the array

for( var i = 0; i < Links.length; i++ )
{
	// 
}

When a link element is clicked you have to let analytics know wich menu the user clicked

Links[i].onclick = function() {
		
			if( this.title == '' 
				|| this.title == null 
				|| this.title == undefined )
			{
				urchinTracker( this.href );
			}
			else
			{
				urchinTracker( this.title );
			}
				
		}

Links[i].onclick = function() { } will give the onclick event of the link a function that will first look if it has a title attribute (<a href="link" title="title attribute">Link name</a>).

if( this.title == '' 
	|| this.title == null 
	|| this.title == undefined )

Then it will give the title attribute to google by urchinTracker( this.title ) or if it has no title attribute it will give the link (href) information to google urchinTracker( this.href ). You can specify in the title attribute which menu this links come from so you will see lateron in google analytics which menu they click or even from wich page like this:

// code

<ul id="menu">
	<li><a href="/page/menu/linkinfo">Menu item1</a></li>
</ul>

// code

<ul id="submenu">
	<li><a href="" title="/page/submenu/linkinfo">SubMenu item1</a></li>
</ul>

// code

If you are logged in to google analytics you have to navigate to Content Optimization -> Content Performance -> Content Drilldown . Here you will find the specified navigation tracking.