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.



That is soooo freakin’ cool!
I’ve been trying to figure out how to do all that stuff for ages.
This one post is going to make/save me thousands a month!
Thanks Tyler, you’re a legend!
Comment by Dan Bradbury — March 7, 2008 @ 11:08 am
Thanks Dan. We’ll be spending a lot more time sharing things we learn with our clients and releasing content that helps you get the most out of our software.
Comment by Tyler Garns — March 7, 2008 @ 12:57 pm
Hi Tyler,
This is awesome stuff, just getting ready to test it. Was working on this exact issue for a client of mine that uses infusion. Two issues however…
’>
should be…
“>
(notice the double quotes)
As well, when copied directly from above, the double and single quotes were knackered. It didnt take me very long to make the changes however.
Otherwise, on to testing!!!!
Thx for the hard work Tyler.
Comment by Paul — March 8, 2008 @ 8:48 pm
Wow, didnt come thru correctly… we will see if this works better…the line i was referring to was…
…’>…
should be
…”>…
I hope it works this time…And if it doesnt…then its the 4th line down on your ‘hidden fields in a form’ code, dealing with the Contact0LeadSource, those should be double quotes in there, not single quotes, see other hidden fields for example.
Cheers.
Paul
Comment by Paul — March 8, 2008 @ 8:52 pm
Hey Tyler, I also had to change the formid…there is 22 in your code, however mine kept coming up with a CRM error when I tested it…checked other pages I have the code on originally, and the formid is 4…unknown to me what that signifies…can you elaborate on that? Anyhow, when I changed it to 4, the form seemed to work, going to check my contacts now…
As well, for Contact0_LinkType, should be $type, not type…
I guess its going to take some more testing on my part…every source ends up being google, even if i use different search engines.
Comment by Paul — March 8, 2008 @ 9:55 pm
Paul,
You’re right, the single quote should be changed to a double quote. I’ll change that right now. In addition, I’ve uploaded a file with all the code and some additional documentation to help people implement it. You can download that code here.
Also, if you have not done so already, you’ll need to create the appropriate custom fields in your application in order to handle and store the data that we’re passing into the form. When you create those fields, make sure you change the form code to reflect the exact spelling of the hidden field names. They are case sensitive.
Lastly, remember to change the form ID of the form so that it contains an actual form id from your application.
Good Luck.
Tyler
Comment by Tyler Garns — March 10, 2008 @ 7:08 am