// www.epikone.com/blog/2007/10/29/integrating-google-analytics-with-a-crm/
// Get the __utmz cookie value. This is the cookies that 
// stores all campaign information. 
// 
//_uacct = "UA-294116-1";
//urchinTracker();
//var isTrackerInitialized = true;


var z = _uGCC(document.cookie, '__utmz=', ';'); 
// 
// The cookie has a number of name-value pairs. 
// Each identifies an aspect of the campaign. 
// 
// utmcsr  = campaign source 
// utmcmd  = campaign medium 
// utmctr  = campaign term (keyword) 
// utmcct  = campaign content (used for A/B testing) 
// utmccn  = campaign name 
// utmgclid = unique identifier used when AdWords auto tagging is enabled 
// 
// This is very basic code. It separates the campaign-tracking cookie 
// and populates a variable with each piece of campaign info. 
// 
var source  = _uGCC(z, 'utmcsr=', '|'); 
var medium  = _uGCC(z, 'utmcmd=', '|'); 
var term    = _uGCC(z, 'utmctr=', '|'); 
var content = _uGCC(z, 'utmcct=', '|'); 
var campaign = _uGCC(z, 'utmccn=', '|'); 
var gclid   = _uGCC(z, 'utmgclid=', '|'); 
// 
// The gclid is ONLY present when auto tagging has been enabled. 
// All other variables, except the term variable, will be '(not set)'. 
// Because the gclid is only present for Google AdWords we can 
// populate some other variables that would normally 
// be left blank. 
// 
if (gclid !="-") { 
      source = 'google'; 
      medium = 'cpc'; 
} 
// Data from the custom segmentation cookie can also be passed 
// back to your server via a hidden form field 
var csegment = _uGCC(document.cookie, '__utmv=', ';'); 
if (csegment != '-') { 
      var csegmentex = /[1-9]*?\.(.*)/;
      csegment    = csegment.match(csegmentex); 
      csegment    = csegment[1]; 
} else { 
      csegment = ''; 
} 
function populateHiddenFields() { 
      $("source").value  = source; 
      $("medium").value  = medium; 
      $("term").value    = term; 
      $("content_source").value = content; 
      $("campaign").value = campaign; 
      $("segment").value = csegment; 
      return true; 
} 

// Original _uGC function copied here and renamed
function _uGCC(l,n,s) {
 if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
 var i,i2,i3,c="-";
 i=l.indexOf(n);
 i3=n.indexOf("=")+1;
 if (i > -1) {
  i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
  c=l.substring((i+i3),i2);
 }
 return c;
}

