How to know what keywords visitors Googled to reach your site?

Here is a simple php script that will detect if your visitor is coming from Google and then log the keyword used to reach your site to a file. The script logs the visitors ip address along with the keyword and exact date and time to a file in the form x.x.x.x googled for “key word here” on Month Date, Year, at Hours:minutes am/pm. To use the script, just paste the following code into your php pages , and set the variable $log_file appropriately.
Here is the code :


The log file name,
this is where the key words will be stored,
make sure to keep a difficult-to-guess name
for privacy reasons

 Set your timezone (just to make sure you can analyze the data better)
 Some useful time zone values :
 for a complete list, refer to

if (strpos($_SERVER['HTTP_REFERER'],'http://google') OR strpos($_SERVER['HTTP_REFERER'],'')) {
$data=$_SERVER['REMOTE_ADDR']." googled for "".$query["q"]."" on ".date("F j, Y,")." at ".date("g:i a")."\r\n\r\n";


Code Explained

We first check to see if our visitor has been referred to our site by google by checking the predefined variable $_SERVER[“HTTP_REFERRER”] for occurrences of the string or http://google using the strpos() function. Then we parse the referrer url using parse_url() function. Then we go on to parse the “query” component of the array returned by parse_url() using the parse_str() function which parses the strings as if it were the query string passed via a URL and sets variables in the current scope. Once the string is parsed into the array $query , we can access our keyword term at $query[“q”] .Finally we open our log file (set in $log_file ) in the append mode (a+) and append the visitors IP address, the keyword and date and time to the file.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2020 All rights reserved | Powered by WordPress
Back to top
Theme by