//
// Capture First Touch Campaign Script version 0.7 <10/23/09>
// Copyright 2009 POP
// Run this code *AFTER* the ga.js include and getTracker call but *BEFORE* call the _TrackPageview on the page.
// Please leave this header text intact if using this code.
// 

// First check if the first touch custom var already exists.
// If it doesn't then populate it, if it does then do nothing.
// We'll always use custvar #1 for this.
if (pageTracker._getVisitorCustomVar(1) == undefined ) {

// helper function "borrowed" from the urchin.js file.
function _uGC(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;
}

// Get the __utmz cookie value. This is the cookie that 
// stores all campaign information. 
var z = _uGC(document.cookie, '__utmz=', ';'); 

var source  = _uGC(z, 'utmcsr=', '|'); 
var medium  = _uGC(z, 'utmcmd=', '|'); 
var term    = _uGC(z, 'utmctr=', '|'); 
var content = _uGC(z, 'utmcct=', '|'); 
var campaign = _uGC(z, 'utmccn=', '|'); 
var gclid   = _uGC(z, 'utmgclid=', '|'); 
 
// The gclid is ONLY present when auto tagging has been enabled. 
if (gclid !="-") { 
      source = 'google'; 
      medium = 'cpc'; 
}

// Store the data we want in a temp variable
// I'm using just using source, medium, campaign and term for now
var first_touch_data = source + '|' + medium + '|' + campaign + '|' + term;

// store the first touch data in custvar #1 (visitor level tracking)
pageTracker._setCustomVar(
      1,                // This custom var is set to slot #1
      "first_touch",    // The name of the custom variable
      first_touch_data, // The value of the custom variable 
      1                 // Sets the scope to visitor-level
 );

} else {
    // Code to get first touch if you want to do something with it.
    first_touch_data = pageTracker._getVisitorCustomVar(1);
}