$SERVER['HTTP_REFERER'] gives you the site that they came from if they clicked a link.
I use the following code to convert that to just: domain.com or subdomain.domain.com but will remove any www.
$url = $_SERVER['HTTP_REFERER'];
$hostname = parse_url($url, PHP_URL_HOST);
// Reduce the hostname to simply subdomain.domain.extension
$array = array_splice(explode('.',$hostname),-3);
if ($array[0] == 'www'){ unset($array[0]); }
$hostname = implode('.', $array);