Search code examples
phpsearch-engine

How to learn from what search engine and with which query a user found my site?


I would like to know which search engine and what particular query led a user to my site. I have partially solved this problem. I can determine from what search engine the user has logged in to the site. But I am not able to find out what query has been used.

This is what I have so far:

$R=$_SERVER['HTTP_REFERER'];
echo "<br />this 1^".$R;

$A=$_SERVER['REMOTE_ADDR'];
$U=$_SERVER['REQUEST_URI'];
$R=urldecode ($R);    

echo "this  IP - ".$A."<br>";
echo "this  - ".$U."<br>";
if (strpos($S, "yandex") != 0) {
  preg_match('"text=(.*?)[^&]*"', $S, $arr);
  echo "this  ".$arr[1];
}
elseif (strpos($S, "google") != 0) {
  preg_match('/q=(.*)&/sei', $S, $arr);
  echo "this  Google ".$arr[1];
}
elseif (strpos($S, "rambler") != 0) {
  preg_match('"query=(.*?)[^&]*"', $S, $arr);
  echo "this  Rambler  ".$arr[1];
}
else {
  echo "this  ".$R;
}
echo "<br />this ^^".$R;
echo "<br />this  ^^".$R;

All $_SERVER ['HTTP_REFERER'] appears to be encoded like this:

this  ^^http://www.google.ru/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CB8QFjAA&url=http%3A%2F%2Fxn--80aag0ab4aqcid.xn--p1ai%2F&ei=YuDxU6L3PKPMyAO-3ID4Aw&usg=AFQjCNHUtFnVLIHqgsuC9skqk8Qje9K3Bg&sig2=12UXCtusUAr5pvAQGxb_Hg           

How can I find out what search query led the user to my site?


Solution

  • You can't get the search query when the search comes from Google. Other search providers will still provide keyword data (at the moment).

    The reason is because Google has now moved to Secure Search (encrypted searches using HTTPS). See Goodbye, Keyword Data: Google Moves Entirely to Secure Search for more information:

    Nearly two years after making one of the biggest changes to secure search that resulted in a steady rise in "(not provided)" data, Google has switched all searches over to encrypted searches using HTTPS. This means no more keyword data will be passed to site owners.

    Encrypted Google searches don't pass the keyword data through to websites, thereby eliminating the ability to track users by their keyword searches.

    ...

    At this point, it seems even when you aren't logged in, using private browsing (or incognito mode) and forcibly type , you are being redirected to the HTTPS version, thereby encrypting your search and no doubt leading to a total removal of keyword data – at least from Google search visitors. Remember, keyword data from other search engines – like Bing, for example – still send keyword data through.

    See also How to get the referer search query from google?