I’ve had an absolute blast at the Infusion User Conference meeting with Infusion clients and seeing the creative ways they’re using Infusion CRM to grow their businesses. Today, day 2 of the conference, I explained how we are using a PHP script along with Google Analytics to capture and track some very valuable information about new leads.
First, I’ll give an overview of what’s happening with each piece of the code and then I’ll show you the entire script.
Conceptual Overview:
As you’re collecting leads information on your website, you always want to know where those leads are coming from. If you’re advertising in a specific place, driving people to a specific landing page, then hardcoding the leadsource in the form is not a bad idea. But many of your leads should come from various sources of internet traffic. This script allows you to pass a value in the url to set the leadsource to a specific value OR the leadsource will be set dynamically by the referring domain. Here’s the code for that section:
if ((!isset($_COOKIE[’LeadSource’]) || ($_COOKIE[’LeadSource’] == “”))) {
if (isset($_REQUEST[’ls’])) {
setcookie(”LeadSource”, $_REQUEST[’ls’], time()+100000000, “/”, $domain);
$source = $_REQUEST[’ls’];
} elseif (isset($_REQUEST[’source’])) {
setcookie(”LeadSource”, $_REQUEST[’source’], time()+100000000, “/”, $domain);
$source = $_REQUEST[’source’];
} else {
$raw_url = parse_url($_SERVER[’HTTP_REFERER’]);
setcookie(”LeadSource”, $raw_url[’host’], time()+10000000, “/”, $domain);
$source = $raw_url[’host’];
}
} else $source = $_COOKIE[’LeadSource’];
This code takes a value from either ‘ls’ or ’source’ in the query string and sets it as the leadsource. If neither of those exist, the referring domain is set as the leadsource. You’ll notice that we store the leadsource in the $source variable as well as in the ‘LeadSource’ cookie. The reason is because cookies are set on the first page of your site that a visitor visits and is only available for reading on the subsequent pages. If someone lands on your site and fills out a form on the first page, the cookie won’t be set yet. So, once you set the leadsource as $source, feed it into a hidden field in your form
We also want to know the exact URL that linked to our site. So, we store another value (referring URL) in a custom field within Infusion CRM.
// SET REFERRING URL
if (!isset($_COOKIE[’Referer’])) {
setcookie(”Referer”, $_SERVER[’HTTP_REFERER’], time()+10000000, “/”, $domain);
$referurl = $_SERVER[’HTTP_REFERER’];
} else { $referurl = $_COOKIE[’Referer’]; }
Next, we read the Google Analytics cookie and chop it up to harvest the data it contains. This cookie contains two pieces of data that we store in custom fields with each lead record. The first is the “type” of referral. The values for this field are (none), organic, cpc, or referral. The other data is the keywords that the visitor used to find your site if they arrived via a search engine search.
// SET COOKIES FROM GOOLGE-ANALYTICS COOKIE
$info = $_COOKIE[’__utmz’];
// Get rid of id stuff
$holder = split(”u”, $info, 2);
$string = “u” . $holder[1];
// Parse String
$ga_vars = split(”\|”, $string);
foreach ($ga_vars as $var) {
list($key,$value) = split(”=”,$var);
if ($key == “utmcmd”) { setcookie(”Type”, $value, time()+100000000, “/”, $domain); $type = $value; }
if ($key == “utmctr”) { setcookie(”Keywords”, $value, time()+100000000, “/”, $domain); $keywords = $value; }
}
We also always store the form URL - in other words, the actual URL that the form is on.
The last section in this code is to store a click history for each visitor. WARNING: this is probably not a good idea for two reasons. We found that this cookie can become too large for the browser to handle and the browser denies access to the site - that’s a bad thing. The other reason is because the click history has to be manually searched and interpreted. It can’t be searched, sorted, or analyzed in mass. So, I WOULD NOT RECOMMEND using this part of the code. We actually turned it off shortly after implementing it.
So, here is the entire PHP code. Any good webmaster can take this code and convert it to the language that your site uses if its not PHP.
//DEFINE COOKIE DOMAIN, ALLOWS SCRIPT TO BE USED ACROSS MULTIPLE DOMAINS
$domain = $_SERVER[’SERVER_NAME’];
$domain = “.” . ltrim($domain,”www.”);
// SET LEADSOURCE
if ((!isset($_COOKIE[’LeadSource’]) || ($_COOKIE[’LeadSource’] == “”))) {
if (isset($_REQUEST[’ls’])) {
setcookie(”LeadSource”, $_REQUEST[’ls’], time()+100000000, “/”, $domain);
$source = $_REQUEST[’ls’];
} elseif (isset($_REQUEST[’source’])) {
setcookie(”LeadSource”, $_REQUEST[’source’], time()+100000000, “/”, $domain);
$source = $_REQUEST[’source’];
} else {
$raw_url = parse_url($_SERVER[’HTTP_REFERER’]);
setcookie(”LeadSource”, $raw_url[’host’], time()+10000000, “/”, $domain);
$source = $raw_url[’host’];
}
} else $source = $_COOKIE[’LeadSource’];
// SET REFERRING URL
if (!isset($_COOKIE[’Referer’])) {
setcookie(”Referer”, $_SERVER[’HTTP_REFERER’], time()+10000000, “/”, $domain);
$referurl = $_SERVER[’HTTP_REFERER’];
} else { $referurl = $_COOKIE[’Referer’]; }
// SET COOKIES FROM GOOLGE-ANALYTICS COOKIE
$info = $_COOKIE[’__utmz’];
// Get rid of id stuff
$holder = split(”u”, $info, 2);
$string = “u” . $holder[1];
// Parse String
$ga_vars = split(”\|”, $string);
foreach ($ga_vars as $var) {
list($key,$value) = split(”=”,$var);
if ($key == “utmcmd”) { setcookie(”Type”, $value, time()+100000000, “/”, $domain); $type = $value; }
if ($key == “utmctr”) { setcookie(”Keywords”, $value, time()+100000000, “/”, $domain); $keywords = $value; }
}
// SET FORM_URL VALUE
$formurl = “http://”.$_SERVER[’HTTP_HOST’].$_SERVER[’REQUEST_URI’];
// SET CLICK HISTORY
$date = date(’j-M-Y’);
if (!isset($_COOKIE[’ClickHistory’])) {
$clickhistory = “Date||”.$date.”\nPath||”.$_SERVER[’HTTP_REFERER’].”\nPath||”.$formurl;
setcookie(”ClickHistory”, $clickhistory, time()+1000000000, “/”, $domain);
} else {
$clickcookie = $_COOKIE[’ClickHistory’];
$lines = explode(’\n’,$clickcookie);
foreach ($lines as $line) {
$items = explode(”||”,$line);
if ($items[0] == “Date”) {
$latestvisit = $items[1];
}
}
if ($latestvisit = $date) {
$clickhistory = $_COOKIE[’ClickHistory’].”\nPath||”.$formurl;
} else {
$clickhistory = $_COOKIE[’ClickHistory’].”Date||”.$date.”\nPath||”.formurl;
}
setcookie(”ClickHistory”, $clickhistory, time()+1000000000, “/”, $domain);
}
?>
The last thing you need to do is put those values in to hidden fields in a form:
<form action="https://crm.infusionsoft.com/AddForms/processForm.jsp" method="post">
<input type="hidden" name="formid" value="22" />
<input type="hidden" name="type" value="CustomFormWeb" />
<input type="hidden" name="Contact0LeadSource" value="<?=$source?>">
<input type="hidden" name="Contact0_LinkType" value="<?=type?>">
<input type="hidden" name="Contact0_Keywords" value="<?=$keywords?>">
<input type="hidden" name="Contact0_FormUrl" value="<?=$formurl?>">
<input type="hidden" name="Contact0_ClickHistory" value="<?=$clickhistory?>">
<input type="hidden" name="Contact0_ReferringURL" value="<?=$referurl?>">
First Name: <input type="text" name="Contact0FirstName" value="" />
Last Name: <input type="text" name="Contact0LastName" value="" />>
Email: <input type="text" name="Contact0Email" value="" />
Phone:<input type="text" name="Contact0Phone1" value="" />
</form>
Hopefully this helps many of you. If you have any questions, feel free to post them here. Thanks.

