When running marketing campaigns, it's great to know where your visitors and customers are coming from, so that you can track conversion based on source, and determine ROI for various advertising link campaigns that you're running.
This code will capture the visitor's referring URL when they first enter your site. This could be a query string from a major search engine, it could be the URL of a website linking to yours, and you could even force it to be something specific. For example, if you want to track how many visitors are clicking on your email signature, you can use the URL www.yourdomain.com?source=email.
Then in any form or conversion point on your site, have a hidden field where you echo the $_SESSION['source'] variable. Then when you get the lead information, you can see where that lead came from.
<?php
//set the source of this visitor
if (!
isset($_SESSION['source'])) {
if((isset($_GET['source']))&&
($_GET['source']!=
")) { // ?source= in querystring, such as email sig
$_SESSION['source'] =
$_GET['source'];
} else {
if($_SERVER['HTTP_REFERER']) { // referred from another web page
$_SESSION['source'] =
$_SERVER['HTTP_REFERER'];
} else {
$_SESSION['source'] =
'direct';
// typed yourdomain.com directly into browser
}
}
}
?>