Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Search API sometimes reurns first result sometimes second result compared to web interface

Firstly this is not a duplicate of: Why does a google search through the google api return different results to a search in the web page? Nor any of the other "google search API returning unexpected results" questions that I have been able to find.

I am writing a small javascript program which uses the Google websearch API. I am creating custom links for my where to buy page which are supposed to direct the user to the correct page on our dealers site to buy the product they were last viewing when they clicked "where to buy". I use the javascript method of document.referrer then parse the URL to determine which page on my site the user last visited.

I then create a appropriate query string, restrict the site to the particular dealer, tell google to only return one result then run the query. I then use the URL to rewrite the HTML in that particular div href to the first (and only) search result. I am essentially emulating the functions of the "I'm feeling lucky" button Google used to have, but structuring the query in such a way that I get exactly the result I'm looking for.

OK, so now that you know what I'm doing and why I'm doing it, here is where things get weird: Most of the results I get work perfectly. They return the first search result. Some of them (and its always the same ones) return the second search result.

Here is the code you can copy and paste into the Code Playground yourself to see the issue I am referring too:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>

    <title>Google AJAX Search API Sample</title>
    <script src="http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0" type="text/javascript"></script>
    <script type="text/javascript">
google.load('search', '1');   
var SearchVariables = [
      ["alto","altomusic.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/altomusic-button.png","wp-image-6343"],
      ["acme","www.acmelectronics.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/acme-button.png","wp-image-6344"],
      ["bhphoto","www.bhphotovideo.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/bh-button3.png","wp-image-6345"],
      ["frontend","www.frontendaudio.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/frontend-button.png","wp-image-6346"],
      ["fullcompass","www.fullcompass.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/fullcompass-button3.png","wp-image-6347"],
      ["performance","www.performanceaudio.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/performance-audio-button.png","wp-image-6348"],
      ["proaudio","www.proaudiosuperstore.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/ProAudioSolutions-button.png","wp-image-6349"],
      ["rmc","www.rmcaudiodirect.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/rmc-button1.png","wp-image-6350"],
      ["sweetwater","www.sweetwater.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/sweetwater-button3.png","wp-image-6351"],
      ["vintageking","www.vintageking.com","http://www.earthworksaudio.com/wp-content/uploads/2012/03/vintageking-button3.png","wp-image-6352"]
    ];  
var Search = new Array(SearchVariables.length);
function searchComplete() {


  for(var i = 0; i < Search.length; i++){
    if (Search[i].results && Search[i].results.length > 0) {
       var contentDiv = document.getElementById(Search[i].ed[0]);
       var curURL = Search[i].results[0].unescapedUrl;
       var newImage = ' <a href="'+curURL+'"><img class="alignleft size-full '+Search[i].ed[2]+'" title="'+Search[i].ed[0]+'" src="'+Search[i].ed[1]+'" alt=" " width="235" height="94" /></a>';
       contentDiv.innerHTML = newImage;
    }
  }     
} 
function parseURLString(inString)
{
    var OutString = 'earthworks';
        inString = inString.replace(/-/g,' ');
        inString = inString.replace('zdt','');
    var MicVsPre=['preamps','microphones'];
    var idx = [];
        for(var j=0;j<MicVsPre.length;j++)
        {
          if(inString.search(MicVsPre[j])>=0)
          {
            OutString = OutString+' '+MicVsPre[j];
                  for(var i=inString.length;i>0;i--)
                  {
                    if(inString[i]=='/')
                    {
                      idx.push(i);
                    }
                  }
            OutString = OutString+' '+inString.substring(idx[0],idx[1]+1);
          }
        }
return OutString;
}

function OnLoad() {
      var String_To_Parse = 'http://www.earthworksaudio.com/microphones/qtc-series-2/qtc40/';     
      var query = parseURLString(String_To_Parse);

       var testloc = document.getElementById('vardiv');
       testloc.innerHTML = query;

      for(var i = 0; i < SearchVariables.length; i++)
      {
        Search[i] = new google.search.WebSearch();
        Search[i].setNoHtmlGeneration();
        Search[i].setResultSetSize(1);
        Search[i].setUserDefinedLabel([SearchVariables[i][0],SearchVariables[i][2],SearchVariables[i][3]]);
        Search[i].setSiteRestriction(SearchVariables[i][1]);
        Search[i].setSearchCompleteCallback(this, searchComplete,null);
        Search[i].execute(query);
      }
}
google.setOnLoadCallback(OnLoad);

    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="vardiv">Testing</div>
    <div id='acme'></div>
    <div id='alto'></div>
    <div id='bhphoto'></div>
    <div id='fullcompass'></div>
    <div id='frontend'></div>
    <div id='performance'></div>
    <div id='proaudio'></div>
    <div id='rmc'></div>
    <div id='sweetwater'></div>
    <div id='vintageking'></div>
  </body>
</html>​ 

I have included a sample product page URL instead of actually using the document.referrer method (since obviously you are not navigating my website right now) to demonstrate whats going on. I have also created a extra div tag at the top which shows what the exact search that is being performed.

Notice how the last one on the list (vintage king, or fourth to last in proaudio soltuions) both pull down the URL for the SECOND search result if you do the exact same search yourself. However most of them correctly pull down the FIRST search result (for example: sweetwater, b&h, full compass). This happens with almost every single search I do on those particular sites. I know that this is not related to anything to do with my browser, and my stored settings since I have tried this incognito, on other computers, in different browsers etc. Does anyone have any idea what is going on here? Why cant I always get the first search result from the web based Google search to be the returned URL?

like image 328
dynamphorous Avatar asked Sep 04 '12 16:09

dynamphorous


1 Answers

I had that problem when I was doing something similar, and according to what I found back then (iirc) returned results depend on the language parameters you pass to google and the browser ID string.

Note that this could have changed, and it's a long shot, but it's worth checking.

EDIT:

I just saw you're looking for some kind of proof for the answer, so here you go: http://www.windmeadow.com/node/36 <-- Google gives different search results based on browser. http://productforums.google.com/forum/#!topic/webmasters/7K02-AUCTVM <-- Different search results with different languages

EDIT 2:

It's a know bug in Googles AJAX API: http://code.google.com/p/google-ajax-apis/issues/detail?id=43

like image 189
alexandernst Avatar answered Oct 26 '22 08:10

alexandernst