Skip to content

Instantly share code, notes, and snippets.

@ianthekid
Last active August 29, 2015 14:27
Show Gist options
  • Save ianthekid/ea4d5dc507b7ad62be83 to your computer and use it in GitHub Desktop.
Save ianthekid/ea4d5dc507b7ad62be83 to your computer and use it in GitHub Desktop.
<?php
/****************************
*
* Lead Source Tracking
*
****************************
* @Author: Ian Ray
* @Created: 17-May 2015
* @Updated: 10-July 2015
* @Description: Created for use with Gravity Forms and Salesforce Lead Source field
*
* USAGE:
* 0. Check Session activity. Recreate new session if over 30 mins since last activity
* 1. Saves $_SESSION['leadSource'] and $_SESSION['refer']
* 2. Gravity Form: Hidden field {SESSION:leadSource} and {SESSION:refer}
* 3. Salesforce Web-to-Lead mapping
* 4. Thank You page (with $_REQUEST['eid']) destroys session variables
*
*
* LOGIC:
* IF: Referer data is provided by originating server
* THEN: Process data to determine Lead Source, and assign Session Vars
* ELSE: Assume it is Web Direct
*
*
* note from ian: This is not 100% valid of an assumption to make,
* if you want to analyze referral traffic sources, use Google Analytics not SalesForce.
*
**************************/
// Last request was more than 30 minutes ago
if (isset($_SESSION['activity']) && (time() - $_SESSION['activity'] > 1800)) {
session_unset();
session_destroy();
}
if(session_id() == '')
session_start();
//Always tracking last activity of Session
$_SESSION['activity'] = time();
if( !isset($_SESSION['leadSource']) && isset($_SERVER['HTTP_REFERER']) ) :
function searchArray($search, $array) {
foreach($array as $key => $value) {
if (strpos($search,$value))
return $key;
}
return false;
}
$social = array('twitter.com','t.co','facebook.com','fb.com','linkedin.com','flickr.com','vimeo.com','xing.com','instagram.com','foursquare.com','fb.me','plus.google.com','youtube.com','youtu.be','reddit.com','tumblr.com');
$search = array('ask','bing','msn.com','yahoo','google');
$_SESSION['orig'] = parse_url($_SERVER['HTTP_REFERER']);
switch(true) {
case ( isset($_REQUEST['gclid']) || isset($_REQUEST['kw']) ) :
$leadSource = "Search - Paid";
break;
case (strpos($_SESSION['orig']['host'], str_replace("http://",get_bloginfo('url'))) != false) :
$leadSource = "Web Direct";
break;
case ( searchArray($_SESSION['orig']['host'], $search) != false ) :
$leadSource = "Search - Organic";
break;
case ( in_array($_SESSION['orig']['host'], $social) ) :
$leadSource = "Social";
break;
default :
$leadSource = "Unknown";
break;
}
if($leadSource == "Unknown" && (strpos($_SESSION['orig']['host'], '.') != false) )
$leadSource = "Referring Website";
$_SESSION['leadSource'] = $leadSource;
$_SESSION['refer'] = $_SERVER['HTTP_REFERER'];
endif;
if( !isset($_SESSION['refer']) ) :
$_SESSION['leadSource'] = "Web Direct";
$_SESSION['refer'] = "Not Provided";
endif;
//Clear Tracking Variables on thank you pages
if(isset($_REQUEST['eid']))
session_destroy();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment