Website Google Search results within your Drupal site

I've played around with how to integrate Google Search results for the Drupal installations within the Drupal site, and finally found the right posts on how to make that happen in Drupal.

This is an extremely useful feature for two reasons :

  • Builtin Drupal search is very heavy on database and query resources and this allows giving up the builtin search completely.
  • Using Google's Adsense for Search you can monetize the search results. Those usually yield very high CTR since they're keyword specific.

-

From http://drupal.org/node/78997#comment-145784

Go through the google setup and create a page on your site using the full html filter and insert the code they give you. Your code may differ to mine dependant on options you chose.

<!-- Google Search Result Snippet Begins -->
<div id="googleSearchUnitIframe"></div>
<script type="text/javascript">
   var googleSearchIframeName = 'googleSearchUnitIframe';
   var googleSearchFrameWidth = 650;
   var googleSearchFrameHeight = 1300;
   var googleSearchFrameborder = 0 ;
</script>
<script type="text/javascript"
         src="http://www.google.com/afsonline/show_afs_search.js">
</script>
<!-- Google Search Result Snippet Ends -->

Put that newly created pages URL in the field where they ask you what page the results should be shown on.

Next, you need to replace the search box on your site with the one they provide you with. How you do this depends on the theme you are using. I'm using Fancy that uses the PHPTemplate engine. The code I need to replace is here:

<td id="menu">
<?php if ($search_box) { ?><form action="<?php print $search_url ?>" method="post">
        <div id="search">
          <input class="form-text" type="text" size="15" value="" name="edit[keys]" alt="<?php print $search_description ?>" />
          <input class="form-submit" type="submit" value="<?php print $search_button_text ?>" />
        </div>
      </form><?php } ?>

This is located in the page.tpl.php file in the theme directory.

Or you could turn off the search box within your settings forcing the newly added one to appear.

This would need a bit of tidying up but this is the code they ask you to insert dependent on the options you chose, again yours may differ but the idea is the same:

Note line 2. That's the URL of the search result page.

<!-- SiteSearch Google -->
<form method="get" action="http://www.sitename.com/googlesearch.htm" target="_top">
<table border="0" bgcolor="#ffffff">
<tr><td nowrap="nowrap" valign="top" align="left" height="32">
</td>
<td nowrap="nowrap">
<input type="hidden" name="domains" value="www.sitename.com"></input>
<input type="text" name="q" size="20" maxlength="255" value=""></input>
<input type="submit" name="sa" value="Google Search"></input>
</td></tr>
<tr>
<td>&nbsp;</td>
<td nowrap="nowrap">
<table>
<tr>
<td>
<input type="radio" name="sitesearch" value="" checked="checked"></input>
<font size="-1" color="#000000">Web</font>
</td>
<td>
<input type="radio" name="sitesearch" value="www.sitename.com"></input>
<font size="-1" color="#000000">www.sitename.com</font>
</td>
</tr>
</table>
<input type="hidden" name="client" value="pub-5006751382868565"></input>
<input type="hidden" name="forid" value="1"></input>
<input type="hidden" name="ie" value="UTF-8"></input>
<input type="hidden" name="oe" value="UTF-8"></input>
<input type="hidden" name="safe" value="active"></input>
<input type="hidden" name="flav" value="0000"></input>
<input type="hidden" name="sig" value="oxG6OMSGsV1OFRVy"></input>
<input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:11"></input>
<input type="hidden" name="hl" value="en"></input>
</td></tr></table>
</form>
<!-- SiteSearch Google -->

 

But there's still a problem, detailed and fixed by http://www.techmag.biz/google_onsite_search_on_drupal :

Google Adsense search has introduced new on site search. Where the search results are shown in the same page. This breaks on sites running drupal cms.Both Drupal and google search uses q as the URL parameter. Drupal thinks the parameter q as the url to be shown to the user and hence shows page not found.To solve this we can use another parameter which will be considered by google as search parameter
Presently you text box code for google search will look like

<input type="text" name="q" size="20" maxlength="255" value="">

Replace it by the following

<input type="text" name="as_q" size="20" maxlength="255" value="">

Google considers 'as_q' as parameter too.That will solve your problem. Try search on this page to believe

  • j

    Results won't show up in the page.

  • http://www.divxtitles.com Mick

    Very useful code, thanks.

  • http://www.diseasesarchive.com Mali

    Yeah I have same problem with results, any tips?

  • http://www.verticity.info Verticity

    thnx for sharing such nice info

  • http://kezi.com/jobbank LEF

    This fixed getting the results page, but the results don't post. What happens if you are searching views?

  • http://industriousone.com/ Starkos

    Thanks for this tip, saved the day. One additional point though: if you set the field name to "as_q" you lose the "Google Custom Search" background image in the search box, which is required by their TOS. You can get it back by adding id="q" to the textbox element, so the full tag looks like:

    <input type="text" id="q" name="as_q" size="22" />

  • chris

    Awesome. the "as_q" hint has saved me. I haven't had much luck with the Drupal CSE module (can't disable ads, results open up in iframe), and thought this would be the way to go. Thank you for posting this!

  • https://www.google.com/accounts/o8/id?id=AItOawmXpbHa5Xzyf3Amk6ah6XawsWs3UNXq4CE Anna

    Another thanks for the as_q tip!