Infusion CRM and Analytics

Note: This post was archived from the Infusionsoft Blog. More details here.

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:

// 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'];

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.

<?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:

First Name: Last Name: Email: Phone:

Hopefully this helps many of you. If you have any questions, feel free to post them here. Thanks.

About Tyler Garns

Tyler Garns is best known for his work as the Director and VP of Marketing at Infusionsoft, where he led the marketing efforts that produced massive results between 2007 and 2012. But he’s also been the “go-to” Infusionsoft guy for many of the top marketers and Infusionsoft users out there. His combination of technical skill, Infusionsoft expertise, and marketing experience make him one of the most reliable sources of business breakthroughs for Infusionsoft customers.

27 thoughts on “Infusion CRM and Analytics”

  1. 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!

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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

  7. Tyler, i’m getting an error when posting the new code you published on march 10.

    Parse error: syntax error, unexpected ‘=’ in /home/layeredw/public_html/progresshomebuyers/join_investor_buyers_list.php on line 6

    i’m not familiar with php enough to know whats going on here.

    Any thoughts?

    Alan

  8. Hi Tyler,

    2 questions:

    1. Do I need to set any information in the //DEFINE COOKIE DOMAIN, ALLOWS SCRIPT TO BE USED ACROSS MULTIPLE DOMAINS area of the php?

    2. If I do not want to use the click tracking (since you recommend against it), should I delete that section from the code, or is it disabled by default?

    Thanks a lot!

  9. Ben,

    I would delete the click tracking part of the code.

    Regarding the cookie domains: By default, you can’t read cookies from another domain other than the one you’re on. So, what I recommend doing is passing the info in the query string when you send people from domain to domain. Then, read it on the next domain, cookie it and you’re on your way.

  10. Leads are not tracking, it looks like the hidden field in your form might be incorrect. Can you confirm that the correct name for the lead source field is:

    Contact0LeadSource

    Because it looks like it should be
    Contact0_LeadSource

    or possibly

    Contact0_Leadsource

    Thank you.

  11. Jack,

    A field that starts with an underscore is a custom field. Since Lead Source is not a custom field, it should not start with an underscore. I think when I originally wrote this post the form field names were not case sensitive, but they are now. I’m pretty sure your problem will be solved if you use Contact0Leadsource as the field name. Let me know if that doesn’t work.

    Thanks.

  12. Jer,

    You actually what to have this code run on EVERY page of your site. Then, no matter which page a person enters on, you’ll still capture the relevant info.

  13. Hi Tyler!

    I really enjoyed your presentations at the conference. Thanks so much for posting this, it’s great to have the details now that I’m finally getting back to working on this.

    So I don’t have a webmaster – its just me for everything – and I’m using Squarespace on your referral. So “Squarespace does not support the uploading and running of server side code. However, you can embed client side scripts that utilize Javascript within your pages and posts.”

    Any resources/suggestions on how I can get this to work with Squarespace?

  14. Can this be installed in the infusionsoft shopping cart. I have a lot of customers going to that url directly?

  15. Tyler,

    I love this and I've got it working great, with one small issue. The 'keywords' part doesn't seem to function as I would expect. Right now the keywords that I use to 'find' my landing page with my form on it, don't show up in the keywords field.

    However if I use the same browser session to again submit the form, it puts the keywords from my last submission into the keywords field.

    It seems like the analytics cookie isn't being set before the script it being run? Possibly?

    I tried moving around the 'include' statement that I used, but to no avail. I also tried running the analytics code right after the opening <body> tag, and your script after, still this had no affect.

    Any advice?

    Thanks,

    – Bob

  16. Tyler,

    I love this and I've got it working great, with one small issue. The 'keywords' part doesn't seem to function as I would expect. Right now the keywords that I use to 'find' my landing page with my form on it, don't show up in the keywords field.

    However if I use the same browser session to again submit the form, it puts the keywords from my last submission into the keywords field.

    It seems like the analytics cookie isn't being set before the script it being run? Possibly?

    I tried moving around the 'include' statement that I used, but to no avail. I also tried running the analytics code right after the opening <body> tag, and your script after, still this had no affect.

    Any advice?

    Thanks,

    – Bob

  17. So if we take your advice and NOT include the Google cookie part, is it safe to add the URL Value and Click History script you list below the Google Cookie code? …..Then which hidden field do you remove?…. Also on the IS Webform builder side, which optional fields should we add? Which should just be a custom field? And on the custom fields, does that data just get dumped into the contact notes?

    Thanks! I loved your presentation too.

    Ry

  18. Pingback: Infusionsoft Lead Source Tracking « Tyler Garns

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top