// ==UserScript==
// @name           Wiki Taglinks
// @namespace      namespace
// @include        http://wiki.ok-labs.com/
// ==/UserScript==

//  (Initial)
//  Javascript to tag file downloads and external links in Google Analytics.
//	To use, place reference to this file should be placed at the bottom of all pages, 
//	  just above the Google Analytics tracking code.
//	All outbound links and links to non-html files
//    should now be automatically tracked.
//
//	(Nov 11) UPDATE: Now accounts for human errors on hyperlinks when developers put 
//    too many slashes in the url.
//
//  (Nov 28) UPDATE: Now can track more than one profile.
//
//  (Dec 6) UPDATE: Now works in all browsers. Tracks code for WIKI.ok-labs.com now.
//
//  (Jan 29, 2008) UPDATE: Found many logic errors. Replanned/Redid logic for reporting.
//    Also merged this file with general GA code for tracking html pages.
// 
//  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//	Created by: 	Colm McBarron, colm.mcbarron@iqcontent.com
//  Modified by Sean Neilan @ metristpartners.com to work in all browsers
//    for wiki.ok-labs.com
//	Last updated: 	January 29, 2007
//	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//

// Track the loaded page in all browsers.
if (typeof window.location != "undefined")
{
	host_name = window.location.hostname;
	path_name = window.location.pathname;
	search_query = window.location.search;
	anchor_hash = window.location.hash;
	
	// Trim /'s at end of url
	//  (Do not do on homepage)
	if (path_name != "/")
	{
		path_name = path_name.replace(/\/$/, "");
	}
}
	
if (typeof document.location != "undefined")
{
	host_name = document.location.hostname;
	path_name = document.location.pathname;
	search_query = document.location.search;
	anchor_hash = window.location.hash;
	
  // Trim /'s at end of url
  //  (Do not do on homepage)
	if (path_name != "/")
	{
		path_name = path_name.replace(/\/$/, "");
	}
}

_uff = false; // reset Google Analytics
_udn = "wiki.ok-labs.com";
// Log click into Wiki profile_uacct = "UA-1818088-2";urchinTracker(path_name + search_query + anchor_hash);
//alert(path_name + search_query + anchor_hash);
_uff = false; // reset Google Analytics_udn = "ok-labs.com";
// Log click into Combined profile_uacct = "UA-1818088-3";urchinTracker("/" + host_name + path_name + search_query + anchor_hash);
//alert("/" + host_name + path_name + search_query + anchor_hash);

// Get the target of a link.
// Works across browsers.
function getEventTarget(event)
{
  var targetElement = null;

  if (typeof event.target != "undefined")
  {
    targetElement = event.target;
  }
  else
  {
    targetElement = event.srcElement;
  }

  while (targetElement.nodeType == 3 && targetElement.parentNode != null)
  {
    targetElement = targetElement.parentNode;
  }

  return targetElement;
}

// Attach event listener to any object.
// Works across browsers.
function attachEventListener(target, eventType, functionRef, capture)
{
  if (typeof target.addEventListener != "undefined")
  {
    target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined")
  {
    target.attachEvent("on" + eventType, functionRef);
  }
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return  functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true; 
}

var hrefs = document.getElementsByTagName("a");

for (var l = 0; l < hrefs.length; l++)
{
	// Add a page tracker to all anchors.
	attachEventListener(hrefs[l], "click", trackfiles, false);
}

// Reports all clicks to non-html/external pages.
function trackfiles(array_element)
{
	// If this variable is still false at the end, don't report anything
	// If still true, clicked link is an internal HTML file
	// Email Ken or Avery for use cases document to see exactly what is reported.
	url_report = false;
	
	// Get reference to link that was clicked on.
	var obj = getEventTarget(array_element);
	
  // If clicked object is an image, get link that contains the image
	if (obj.nodeName != "A")
	{
	  obj = obj.parentNode;
	}
	
	// We modify this data and then always
	//   report host_name + path_name + search_uri.
	// If we don't want to report host_name, leave it blank.
	// (Same with other variables.)
	host_name  = "";
	path_name  = "";
	search_uri = "";
	anchor_hash = "";
	
	/*
  For the rest of this document, we report clicks for two different
    Google Analytics accounts with different reporting logic.
  So, we calculate the click for each account separately according to its logic.
  See the use cases document to see how different this logic is.
  */
  // --- Reporting code for Wiki Profile 
  
  // Track stuff now
	if (obj.hostname.match(/ok\-labs\.com/)) // if it's to ok-labs
	{	
		if (obj.hostname.match(/wiki/)) // & if it's a file on wiki.ok-labs.com
		{
      if (obj.pathname.match(/\.(pdf|tar|gz|sha1|md5|txt|zip)$/))
   		{
   			host_name  = ""; // Don't log hostname for internal nonhtml links on wiki account
   			path_name = obj.pathname; // Log pathname for internal nonhtml links on wiki account
   			
   			search_uri = ""; // Don't log search uri for internal nonhtml links on wiki account.
   			anchor_hash = ""; // Don't log hash for internal nonhtml links on wiki account.
   			
   			url_report = true; // report the click
   		}
		}
		else // if it doesn't match wiki, it's a link to ok-labs.com
		{
			host_name  = "/ok-labs/"; // Label hostname as /ok-labs ok-labs.com on wiki account
			path_name  = obj.pathname; // Log pathname for links to ok-labs.com on wiki account
	    search_uri = obj.search; // Log search uri for links to ok-labs.com on wiki account
			anchor_hash = obj.hash; // Log hash for links to ok-labs.com on wiki account
			
			url_report = true; // report the click
		}
	}
	else  // if it's external, report it (means it doesn't match ok-labs.com)
	{
		if (obj.href.match(/mailto/)) // Log and label mailto links on wiki account
	  {	  	
	  	// We aren't really logging a normal link.
	  	//   It's an email address. So, just stick the
	  	//   entire email in host_name & change everything else
	  	//   to blank. This will log the email
	  	host_name = obj.href; // Take the entire mailto link & log it
	  	path_name = "";
	  	search_uri = "";
	  	anchor_hash = "";
	  	
	  	url_report = true; // report url
	  }
	  else
	  {
			host_name = "/offsite/" + obj.hostname; // Log/label hostname for external links on wiki account
			path_name = obj.pathname; // Log pathname for external links on wiki account
			search_uri = obj.search; // Log search uri for external links on wiki account
			anchor_hash = obj.hash; // Log hash for external links on wiki account
			
			url_report = true; // report the click
	  }
	}
  
	if (url_report == true)
	{
		url_report = host_name + path_name + search_uri + anchor_hash;
		
		// Replace all double //'s with single /'s.
	  while (url_report.match(/\/\//) != null)
	  {
	    url_report = url_report.replace(/\/\//g, "/");
	  }
	  
	  // Trim /'s at end of url.
    url_report = url_report.replace(/\/$/, "");
		
		// If there is no beginning slash, add one
	  if (url_report.match(/^\//) == null)
	  {
	  	url_report = "/" + url_report;
	  }
		
		_uff = false; // reset google analytics (GA)
		// line required by GA to report to ok-labs.com & wiki.ok-labs.com
		_udn = "ok-labs.com";
		
		// Submit url_report to wiki profile
		_uacct = "UA-1818088-2";
		urchinTracker(url_report); // Report it again.
		//alert(url_report);
	}
	
 	// --- End reporting code for corporate profile  -----------------------------

 	// --- Begin reporting code for Combined profile -----------------------------
		
	// We modify this data and then always
	//   report host_name + path_name + search_uri.
	// If we don't want to report host_name, leave it blank.
	// (Same with other variables.)
	host_name  = "";
	path_name  = "";
	search_uri = "";
	anchor_hash = "";
	
  // Reset our lil' reporting mechaschism here..
 	url_report = false;

  // Make url to track here.
  //   See use cases document for info on how this all works.
 	if (obj.hostname.match(/\ok\-labs\.com/))
 	{
 		if (obj.pathname.match(/\.(pdf|tar|gz|sha1|md5|txt|zip)$/))
 		{
 			if (obj.hostname.match(/wiki/))
 			{
 				host_name  = "/wiki.ok-labs.com/"; //Log/label hostname for nonhtml wiki links on combined account.
 			}
 			else // It's a nonhtml link on ok-labs.com
 			{
 				host_name = "/www.ok-labs.com/"; // Log/label hostname for nonhtml corp links on combined account
 			}
 			 
			path_name  = obj.pathname; // Log pathname for nonhtml links on combined account.
			search_uri = ""; 					 // Don't log search uri for nonhtml links on combined
																 //   account.
			anchor_hash = ""; // Don't log anchor hash for nonhtml links on combined account.
			
			url_report = true; // report url
 		}
 	}
	else // If offsite (or an email)
	{
		if (obj.href.match(/mailto/)) // Log and label mailto links on combined account
	  {
	  	// We aren't really logging a normal link.
	  	//   It's an email address. So, just stick the
	  	//   entire email in host_name & change everything else
	  	//   to blank. This will log the email
	  	host_name = obj.href; // Take the entire mailto link & log it
	  	path_name = "";
	  	search_uri = "";
	  	anchor_hash = "";
	  	
	  	url_report = true; // report url
	  }
	  else
	  {
			host_name  = "/offsite/" + obj.hostname; // Log/label hostname for external
																							 //   links on combined account.
			path_name  = obj.pathname; 						 	 // Log pathname for external links 
																							 //   on combined account.
			search_uri = obj.search;   							 // Log search uri for external links
																							 //   on combined account.
			
			anchor_hash = obj.hash; // Log hash for wiki links on combined account.
		  
		  url_report = true; // report url
	  }
	}
	
	// Report the click now.
	if (url_report == true)
	{
		url_report = host_name + path_name + search_uri + anchor_hash;
		
		// Replace all double //'s with single /'s.
	  while (url_report.match(/\/\//) != null)
	  {
	    url_report = url_report.replace(/\/\//g, "/");
	  }
	  
	  // If there are any /'s, trim them.
	  if (url_report.match(/\/$/))
	  {
	  	url_report = url_report.replace(/\/$/, "");
	  }
		
		_uff = false; // reset google analytics (GA)
		// line required by GA to report to ok-labs.com & wiki.ok-labs.com
		_udn = "ok-labs.com";
		
		// Submit url_report to combined profile
		_uacct = "UA-1818088-3";
		urchinTracker(url_report); // Report it again.
		//alert(url_report);
	}
	
	// --- End reporting code for Combined profile --------------------------------
}

