How to capture a search query used in Google, Yahoo, and Bing then display it on your website?

The following php code will capture search queries at Google,Yahoo, Bing and Ask.com as well as list all referring pages including internal pages of your website.It is also desired that we should get the unique keywords or referral counts with the display.Of course the script will do this for you.


//===========================
// Settings that must be done
//============================

// domain setting,change this to your website

$myurl="yourdomain.com"; // do not put http:// or http://wwww 

//change the dir path for your website,this should be the path
//where all files including this script will be there

$dirpath = "/home/username/public_html/scripts/" ;

//create blank files and make sure that following files exist in the folder
//set as at $dirpath above

//create a blank file named internal_referer.txt in folder set at $dirpath

$internal_referer_file = $dirpath."internal_referer.txt"; 

//create a blank file named all_referer_log.txt in folder set at $dirpath

$all_referers_file = $dirpath."all_referer_log.txt";

//create a blank file named yahoo_keywords.txt in folder set at $dirpath

$yahoo_keywords_file = $dirpath."yahoo_keywords.txt";

//create a blank file named google_keywords.txt in folder set at $dirpath

$google_keywords_file = $dirpath."google_keywords.txt";

//create a blank file named bing_keywords.txt in folder set at $dirpath

$bing_keywords_file = $dirpath."bing_keywords.txt";

//create a blank file named ask_keywords.txt in folder set at $dirpath

$ask_keywords_file = $dirpath."ask_keywords.txt";

//=================================
// End of Settings that must be done
//=================================

//let us begin the job

// take the referer

$thereferer = strtolower($_SERVER['HTTP_REFERER']);

// store own website referrals from internal pages

$file_name =  $internal_referer_file ; 

if ( stristr($thereferer,"http://".$myurl) || stristr($thereferer,"http://www".$myurl))
{
	$file_handle= fopen($file_name,"a");//places file ptr at end of file ,file open for writing a+ both reading writing
	$output_string = $thereferer."\n";
   fwrite($file_handle,$output_string);
   fclose($file_handle);
} 

//store all the outside referers ( intenals with $muyrl excluded) to log file referer_log.txt

$file_name = $all_referers_file ;
if (!(stristr($thereferer,"http://".$myurl) || stristr($thereferer,"http://www".$myurl) ))
{
	$file_handle= fopen($file_name,"a");//places file ptr at end of file ,file open for writing a+ both reading writing
	$output_string = $thereferer."\n";
   fwrite($file_handle,$output_string);
   fclose($file_handle);
} 

// see if referral comes from google

$mygooglekeyword = "";

if (!($thereferer==""))
{
   if (strpos($thereferer,"google"))
   {
      // delete all before q=
      $a = substr($thereferer, strpos($thereferer,"q="));
      // delete q=
      $a = substr($a,2);
      // delete all FROM the next & onwards
      if (strpos($a,"&"))
      {
        $a = substr($a, 0,strpos($a,"&"));
      }
    // we have the results.
    $mygooglekeyword = urldecode($a);
    }
}

// write the google keyword if there to file

$file_name = $google_keywords_file ;
if (!($mygooglekeyword==""))
{
	$file_handle= fopen($file_name,"a");//places file ptr at end of file ,file open for writing a+ both reading writing
	$output_string = $mygooglekeyword."\n";
   if (stristr($output_string,"tp://"))
   {}
   else
   {
   fwrite($file_handle,$output_string);
   }
	fclose($file_handle);
} 

// see if referral comes from yahoo
$myyahookeyword = "";
if (!($thereferer==""))
{
   if (strpos($thereferer,"yahoo"))
   {
      // delete all before p=
      $a = substr($thereferer, strpos($thereferer,"p="));
      // delete p=
      $a = substr($a,2);
      // delete all FROM the next & onwards
      if (strpos($a,"&"))
      {
        $a = substr($a, 0,strpos($a,"&"));
      }
    // we have the results.
    $myyahookeyword = urldecode($a);
    }
}

// write the yahoo keyword if there to file

$file_name = $yahoo_keywords_file ;
if (!($myyahookeyword==""))
{
	$file_handle= fopen($file_name,"a");//places file ptr at end of file ,file open for writing a+ both reading writing
	$output_string = $myyahookeyword."\n";
   if (stristr($output_string,"tp://"))
   {}
   else
   {
   fwrite($file_handle,$output_string);
   }
	fclose($file_handle);
} 

// see if referral comes from bing
$mybingkeyword = "";
if (!($thereferer==""))
{
   if (strpos($thereferer,"bing.com"))
   {
      // delete all before q=
      $a = substr($thereferer, strpos($thereferer,"q="));
      // delete q=
      $a = substr($a,2);
      // delete all FROM the next & onwards
      if (strpos($a,"&"))
      {
        $a = substr($a, 0,strpos($a,"&"));
      }
    // we have the results.
    $mybingkeyword = urldecode($a);
    }
}

// write the bing keyword if there to file

$file_name = $bing_keywords_file ;
if (!($mybingkeyword==""))
{
	$file_handle= fopen($file_name,"a");//places file ptr at end of file ,file open for writing a+ both reading writing
	$output_string = $mybingkeyword."\n";
   if (stristr($output_string,"tp://"))
   {}
   else
   {
   fwrite($file_handle,$output_string);
   }
	fclose($file_handle);
} 

// see if referral comes from ask.com
$myaskkeyword = "";
if (!($thereferer==""))
{
   if (strpos($thereferer,"ask.com"))
   {
      // delete all before q=
      $a = substr($thereferer, strpos($thereferer,"q="));
      // delete q=
      $a = substr($a,2);
      // delete all FROM the next & onwards
      if (strpos($a,"&"))
      {
        $a = substr($a, 0,strpos($a,"&"));
      }
    // we have the results.
    $myaskkeyword = urldecode($a);
    }
}

// write the ask.com keyword if there to file

$file_name = $ask_keywords_file ;
if (!($myaskkeyword==""))
{
	$file_handle= fopen($file_name,"a");//places file ptr at end of file ,file open for writing a+ both reading writing
	$output_string = $myaskkeyword."\n";
   if (stristr($output_string,"tp://"))
   {}
   else
   {
   fwrite($file_handle,$output_string);
   }
	fclose($file_handle);
}

Copy the above code to a file ‘get-referer.php’ and place it in the folder set at $dirpath in the above code

The script needs to be called every time your website is visited.To do this you should paste the following code in your website’s header or footer.

include "/home/username/public_html/scripts/get-referer.php"

Now that the first part of capturing the referrals is complete,We should have a way to to read the data gathered on a web page.It is also desired that we should get the unique keywords or referral counts with the display.The following script code does exactly this


//===========================
// Settings that must be done
//============================

// domain setting,change this to your website

$myurl="yourdomain.com"; // do not put http:// or http://wwww 

//change the dir path for your website,this should be the path
//where all files including this script will be there

$dirpath = "/home/username/public_html/scripts/" ;

//create blank files and make sure that following files exist in the folder
//set as at $dirpath above

//create a blank file named internal_referer.txt in folder set at $dirpath

$internal_referer_file = $dirpath."internal_referer.txt"; 

//create a blank file named all_referer_log.txt in folder set at $dirpath

$all_referers_file = $dirpath."all_referer_log.txt";

//create a blank file named yahoo_keywords.txt in folder set at $dirpath

$yahoo_keywords_file = $dirpath."yahoo_keywords.txt";

//create a blank file named google_keywords.txt in folder set at $dirpath

$google_keywords_file = $dirpath."google_keywords.txt";

//create a blank file named bing_keywords.txt in folder set at $dirpath

$bing_keywords_file = $dirpath."bing_keywords.txt";

//create a blank file named ask_keywords.txt in folder set at $dirpath

$ask_keywords_file = $dirpath."ask_keywords.txt";

//=================================
// End of Settings that must be done
//=================================

//Let us begin the display
?>
<p align="center"><b><? echo $myurl ;?> User Search Data</b></p>

<?// read the keywords
// google
$file_name = $google_keywords_file ;
	$file_handle= fopen($file_name,"r");//places file ptr at beg of file ,file open for reading
	$file_contents_string = file_get_contents($file_name);
	$pieces = explode("\n", $file_contents_string);//our file has a new line separator

	$unique_array = array_count_values($pieces); // this will give a unique array with counts like ['George'] => 3 ['Clark'] => 2   ['John'] => 4
    arsort($unique_array);
   ?>
   <br/><br/><b><hr>Visitors came from Google searching for : </b><br/><br/>
   <?
	foreach ($unique_array as $key => $value)

   {
      if ($key) // check for a blank key
      {
         ?><li><? echo $key;?> (<? echo $value; ?>)</li><?
      }
	}
	fclose($file_handle); 

// yahoo
$file_name = $yahoo_keywords_file ;
	$file_handle= fopen($file_name,"r");//places file ptr at beg of file ,file open for reading
	$file_contents_string = file_get_contents($file_name);
	$pieces = explode("\n", $file_contents_string);//our file has a new line separator

	$unique_array = array_count_values($pieces); // this will give a unique array with counts like ['George'] => 3 ['Clark'] => 2   ['John'] => 4
    arsort($unique_array);
   ?>
   <br/><br/><hr><b>Visitors came from Yahoo searching for : </b><br/><br/>
   <?
	foreach ($unique_array as $key => $value)

   {
      if ($key) // check for a blank key
      {
         ?><li><? echo $key;?> (<? echo $value; ?>)</li><?
      }
	}
	fclose($file_handle); 

// bing
$file_name = $bing_keywords_file ;
	$file_handle= fopen($file_name,"r");//places file ptr at beg of file ,file open for reading
	$file_contents_string = file_get_contents($file_name);
	$pieces = explode("\n", $file_contents_string);//our file has a new line separator

	$unique_array = array_count_values($pieces); // this will give a unique array with counts like ['George'] => 3 ['Clark'] => 2   ['John'] => 4
    arsort($unique_array);
   ?>
   <br/><br/><hr><b>Visitors came from Bing searching for</b><br/><br/>

   <?
	foreach ($unique_array as $key => $value)
   {
      if ($key) // check for a blank key
      {
         ?><li><? echo $key;?> (<? echo $value; ?>)</li><?
      }
	}
	fclose($file_handle); 

// ask.com
$file_name = $ask_keywords_file ;
	$file_handle= fopen($file_name,"r");//places file ptr at beg of file ,file open for reading
	$file_contents_string = file_get_contents($file_name);
	$pieces = explode("\n", $file_contents_string);//our file has a new line separator

    $unique_array = array_count_values($pieces); // this will give a unique array with counts like ['George'] => 3 ['Clark'] => 2   ['John'] => 4
    arsort($unique_array);
   ?>
   <br/><br/><hr><b>Visitors came from Ask.com searching for : </b><br/><br/>
   <?
	foreach ($unique_array as $key => $value)

   {
      if ($key) // check for a blank key
      {
         ?><li><? echo $key;?> (<? echo $value; ?>)</li><?
      }
	}
	fclose($file_handle);    

// All referers
$file_name = $all_referers_file ;
	$file_handle= fopen($file_name,"r");//places file ptr at beg of file ,file open for reading
	$file_contents_string = file_get_contents($file_name);
	$pieces = explode("\n", $file_contents_string);//our file has a new line separator

    $unique_array = array_count_values($pieces); // this will give a unique array with counts like ['George'] => 3 ['Clark'] => 2   ['John'] => 4
    arsort($unique_array);
   ?>
   <br/><br/><hr><b>All Referers : </b><br/><br/>

   <?
	foreach ($unique_array as $key => $value)

   {
      if ($key) // check for a blank key
      {
         ?><li><? echo $key;?> (<? echo $value; ?>)</li><?
      }
	}
	fclose($file_handle);   

// All Internal Visits

$file_name = $internal_referer_file ;
	$file_handle= fopen($file_name,"r");//places file ptr at beg of file ,file open for reading
	$file_contents_string = file_get_contents($file_name);
	$pieces = explode("\n", $file_contents_string);//our file has a new line separator

    $unique_array = array_count_values($pieces); // this will give a unique array with counts like ['George'] => 3 ['Clark'] => 2   ['John'] => 4
    arsort($unique_array);

   ?>
   <br/><br/><hr><b>All Internal page Visits : </b><br/><br/>

   <?
	foreach ($unique_array as $key => $value)

   {
      if ($key) // check for a blank key
      {
         ?><li><? echo $key;?> (<? echo $value; ?>)</li><?
      }
	}
	fclose($file_handle);  

?>
<br/><br/><br/>

Copy the above code to a file ‘show-data.php’ in the folder set at $dirpath.

To see the referral data use the url to the show-data.php file for example

http://yourdomain.com/scripts/show-data.php



That is all.

If you liked this article please do leave a reply and share it with friends.

Thanks.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.