Forum Moderators: coopster

Message Too Old, No Replies

Trying to track Referrers with PHP

         

dane120

1:33 pm on Aug 6, 2008 (gmt 0)

10+ Year Member



I am currently running the following below code to try and track referring websites and search strings being used to find my site.

I recently discovered filters which can be used within Google Analytics to show refering websites and search strings and some of these are not appearing when we use the code. Is there any reason why this code wouldn't work or could be causing problem when customers are visiting my site?


<?

// Save Campaign and Keywords used to get here

if (strstr($_SERVER["QUERY_STRING"], "CAMPAIGN") ¦¦ strstr($_SERVER["QUERY_STRING"], "KEYWORDS")) {

$_SESSION["CAMPAIGN"] = $_REQUEST["CAMPAIGN"];

$_SESSION["KEYWORDS"] = $_REQUEST["KEYWORDS"];

}

if (!$_SESSION['theURL']) {

$_SESSION['theURL'] = $_SESSION['referringURL'];

}

// Save the referral page that has sent user to here

//if ($_SESSION['theURL'] != "") {

// Check Session hasn't already been recorded

$sessionCheckQuery = mysql_query("SELECT * FROM referer WHERE session = '".$sessionId."'");

// Then record details

if (mysql_num_rows($sessionCheckQuery) == 0) {

$searchEngineReferer = seReferer($_SESSION['theURL']."?".$_SESSION['referringQryStr']);

mysql_query("INSERT INTO referer (url, date_time, session, keyword, campaign, searchStr, website, visitor_ip, user_agent, requested_page) VALUES ('".$_SESSION['theURL']."?".$_SESSION['referringQryStr']."', '".date("Y-m-d H:i:s")."', '".$sessionId."', '".$_SESSION[KEYWORDS]."', '".$_SESSION[CAMPAIGN]."', '".$searchEngineReferer[Query]."', '".$searchEngineReferer[Se]."', '".$_SERVER['REMOTE_ADDR']."', '".$_SERVER[HTTP_USER_AGENT]."', '".$_SERVER[PHP_SELF]."')");

}

//}

?>

eelixduppy

5:12 am on Aug 7, 2008 (gmt 0)



So this code only works some times? From what I can see there seems to be an issue with the method you are grabbing the data from the referrers because it will only work if "CMPAIGN" and "KEYWORDS" are part of the URI query-string. There is obviously more to this code, too, as I don't know what the function seReferer is doing. If this script doesn't work only some of the time then it could still be many different issues. Most likely, though, there is something going wrong with the database in one way or another. Maybe if the search term contains certain characters the query breaks; you aren't, after all, escaping any of the data you are placing in your mysql queries.