Alexander Conroy

Track Referer and Entry Page with a Cookie

I found this simple script on the internet by Ammon Johns. He was nice enough to share it as long as we give credit. His script was originally to track a referer. It sets a “referrer” cookie which you can pull into any document using the php code <?php echo $_COOKIE[‘referrer’] ?>. I hope that helps someone, it took me forever to figure it out. There is a way to pull the information with javascript too something like “document.cookie.referrer or some such, but i’m no expert in javascript yet.

I wanted to track the entry page as well because I had another script assigning a variable of k_wcid (omniture) to any person who came into our site through google’s PPC (Pay Per Click) Adwords campaign. This way we coudl track what site and keywords they came from with the referrer, and what page they landed on, which would also tell us if it was PPC or not.

 

The following code creates the cookie “URL” which can be called in PHP with <?php echo $_COOKIE[‘URL’] ?>

The funny thing about setting and using these cookies is that you set the field with the value of “var=” (“URL=”, or “referrer=” for example)  and then call them withotu the “=”!

Dont forget to give me some credit too!

// JavaScript Cookie Code
// Coding by Ammon Johns
// www.webmarketingplus.co.uk
// Modified for URL by Alexander Conroy “Geilt”
// www.esotech.org – [email protected]

// You may not modify or use this code except as stated in at
// http://www.webmarketingplus.co.uk/promotions/cookie-tracking.html

var cDomain = self.location.hostname; // cDomain becomes hostname www.vitalonehealth.com
if(cDomain.indexOf(“.”) < cDomain.lastIndexOf(“.”)){ //Checks the number position of the domainname whcih is www. (4) vs the number position of .com which is 4.  if it is less then…
var domainOffset = cDomain.indexOf(“.”)+1  //add 1 to the index number total, so if its 3 then it puts it to 4
cDomain = cDomain.substr(domainOffset); // domain now extracts 4 letters out.
}

if(document.referrer.indexOf(cDomain)==-1 && document.referrer!=”” && document.cookie.indexOf(“referrer=”)==-1){
var expDays = 90;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
var refdate = new Date();
document.cookie = “referrer=” + escape(document.referrer);
}

if(document.cookie.indexOf(“URL=”)==-1){
var expDays = 90;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
document.cookie = “URL=” + escape(document.URL);
}

var allCookies = document.cookie;
var cPos = allCookies.indexOf(“referrer=”);
if(cPos != -1){
var cdstart = cPos + 9;
var cdend = allCookies.indexOf(“;”, cdstart);
if(cdend == -1) cdend = allCookies.length;
var cookieContent = allCookies.substring(cdstart,cdend);
cookieContent = unescape(cookieContent);
var cdatestart = cookieContent.indexOf(“&&&”, 0);
var cdateend = cookieContent.length;
var cRefer = cookieContent.substring(0,cdatestart);
var cDateRef = cookieContent.substring(cdatestart +2,cdateend)
}
else{
var cRefer = “No cookie”;
var cDateRef = “No cookie”;
}