﻿String.prototype.startsWith = function (str){
	return this.slice(0, str.length) == str;
};

String.prototype.endsWith = function (str){
	return this.slice(-str.length) == str;
};

/*
Description:
	This is a utility method used for Google Analytics event tracking.
	It takes page navigation in to consideration using a time out ensuring that the request to Google gets executed.

Arguments:
	category:	Required, the name you supply for the group of objects you want to track
	action:		Required, a string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object
	label:		Optional, a string to provide additional dimensions to the event data
	value:		Optional, an integer that you can use to provide numerical data about the user event
	href:		Optional, a string that, when set, will force navigation to the given href
	target:		Optional, a string that will tell the browser which frame to navigate (_top == top.location, else window.location)

Prerequisites:
	The Google Analytics script needs to be included

Usage example:
	<img alt="" src="http://www.google.se/images/srpr/logo3w.png" onclick="trackEvent('TestCategory', 'TestAction'); return false;" />
Result:
	An event will be registered with the category 'TestCategory' and action 'TestAction'

Test your code:
	You can easily check with fiddler or a similar application if a GET request is made to http://www.google-analytics.com/__utm.gif when you expect the tracking to occur
*/
function trackEvent(category, action, label, value, href, target) {
	try {
		var v = parseInt(value);
		_gaq.push(['_trackEvent', category, action, label, isNaN(v) ? 0 : v]);
		if(href) {
			setTimeout(function () {
			//if (confirm("href: " + href + " category: " + category + " action: " + action + " label: " + label + " value: " + value)) {
				switch(target)
				{
					case '_top':
						top.location = href;
						break;

					case '':
						window.location = href;
						break;

					default:
						window.open(href, target);
						break;
				}
			//}
			}, 100);
		}
	}catch (err){alert(err);}
}

/*
Description:
	This is a utility method used for Google Analytics event tracking which can be used on anchor tags.

Arguments:
	link:		Required, the anchor element
	category:	Required, the name you supply for the group of objects you want to track
	action:		Required, a string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object
	label:		Optional, a string to provide additional dimensions to the event data
	value:		Optional, an integer that you can use to provide numerical data about the user event

Prerequisites:
	The Google Analytics script needs to be included

Usage example:
	<a href="http://www.example.com" onclick="trackLink(this, 'TestCategory', 'TestAction'); return false;">Example</a>
Result:
	An event will be registered with the category 'TestCategory' and action 'TestAction'. The user will be transfered to the anchors href, in this case "http://www.example.com"

Test your code:
	You can easily check with fiddler or a similar application if a GET request is made to http://www.google-analytics.com/__utm.gif when you expect the tracking to occur
*/
function trackLink(link, category, action, label, value) {
	trackEvent(category, action, label, value, link.href, link.target);
}
