Last updated September 21, 2011. Created by Sam308 on June 24, 2008.
Edited by develCuy. Log in to edit this page.

How to Set up Google AdSense for Search without using a custom module

Google AdSense for Search offer your users site search or web search while earning revenue from ads relevant to their search terms. Incorporating Google AdSense for Search into a Drupal website without using a custom module can be accomplished if you utilize the following method.

Prerequisites:
(1) Any Drupal CMS website (not version dependent).
(2) You need to have a Google AdSense account.
(3) You need to Set up an AdSense for Search Ad unit.

After you set up your Google AdSense for Search Ad unit, Google will present you with the following 2 code blocks for you to insert into your website. You will need to make a couple of changes to the Original Google AdSense for Search Code in order to get the site search to work properly. In the following example, the node page 35 will be used as the Search Results page.

Original Google AdSense for Search Code
The Google Partner account number (partner-pub-xxxxxxxxxxxxxxxxxxx:g2l95q-v9ce) has been xxxx out in the example below. Remember to use your own assigned Google Partner account number and Search Form code.

Search Box Code
Paste this code in the page where you'd like your search box to appear.

<form action="http://yourwebsite.com/?q=node/35" id="cse-search-box">
  <div>
    <input type="hidden" name="cx" value="partner-pub-xxxxxxxxxxxxxxxxxxx:g2l95q-v9ce" />
    <input type="hidden" name="cof" value="FORID:11" />
    <input type="hidden" name="ie" value="ISO-8859-1" />
    <input type="text" name="q" size="15" />
    <input type="submit" name="sa" value="Search" />
  </div>
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>

Search Results Code
Paste this code on http://yourwebsite.com/?q=node/35, where you would like your search results to appear.

<div id="cse-search-results"></div>
<script type="text/javascript">
  var googleSearchIframeName = "cse-search-results";
  var googleSearchFormName = "cse-search-box";
  var googleSearchFrameWidth = 795;
  var googleSearchDomain = "www.google.com";
  var googleSearchPath = "/cse";
</script>
<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>

Below are the necessary changes to you will need to make to the Search Box Code in order to make the Google AdSense for Search function properly within a Drupal Website.

Changes to the Search Box Code:
If your site is using the default Drupal ?q=node/23 page URL format, then the "q" in the site's url will cause conflicts with Google's AdSense for Search Search Box Form. Google's search form input box statement also utilizes a "q" in the name="q" parameter. To eliminate this "q" conflict, one needs to replace name="q" with name="as_q" in the Search Box Code.

Making the following two changes will result in a properly functioning Google site search.

(1) Remove the ?q= in the form action statement: from: http://yourwebsite.com/?q=node/35 to: http://yourwebsite.com/node/35.
(2) Replace name="q" with name="as_q" in the input statement.

Modified Search Box Code
Paste this code in the page where you'd like your search box to appear.

<form action="http://yourwebsite.com/node/35" id="cse-search-box">
  <div>
    <input type="hidden" name="cx" value="partner-pub-xxxxxxxxxxxxxxxxxxx:g2l95q-v9ce" />
    <input type="hidden" name="cof" value="FORID:11" />
    <input type="hidden" name="ie" value="ISO-8859-1" />
    <input type="text" name="as_q" size="15" />
    <input type="submit" name="sa" value="Search" />
  </div>
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>

Adding Back the "Google Custom Search" Watermark Image
By making the name="as_q" replacement within the search form's input box statement, the Search form will function properly. However, this particular code change causes the "Google Custom Search" Watermark Image not to appear within the search form's input box upon page loading as it would if the original Search Box Code functioned properly in the first place.

To recreate the exact look of the original Google input box which includes the "Google Custom Search" Watermark Image, use the following code substitution for the input box statement:

Replace the following input box statement (still using the name="as_q" substitution for the name="q"):

<input type="text" name="as_q" size="15" />

with the following input box statement:

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

This input box statement will simulate the exact look of the original Google input box by placing a "Google Custom Search" image inside the text box. The image will disappear when the user places focus on the input box as it would in the original unaltered Google input box.

Google AdSense program policies
AdSense for search results pages may show only a single ad link unit in addition to the ads Google serves with the search results. No other ads may be displayed on your search results page.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

For some reason when I try to use an url of the kind http://yourwebsite.com/node/35, I get a 404 error.

Using Drupal 5.10.

Helio Diamant
Editor - PocketPCFreak.com

to make the Google Custom Search image reappear, you can simply add id="q" to the text input.

i've found this tip in a comment of #stark here :

http://teqsnacks.com/2007/02/28/website-google-search-results-within-you...

Yes. It worked for me (Drupal 6.17)

Just replace the line
<input type="text" name="q" size="21" />
with the line
<input type="text" id="q" name="as_q" size="21" />

In this way Google AdSense for Search works in Drupal, and also you can see correctly the watermark gif in the search box.

I dont know if Google offers an official solution but I use something like this:

$_GET[q] = ($_GET['cx']) ? "websearch" : $_GET[q] ;

http://www.cafewebmaster.com/fix-google-adsense-search-and-drupal-conflict

Modify the search box code to:

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

The Google logo image will appear again.

--------------------
Sudheer Satyanarayana
Tech Chorus

It worked like a charm!

--
Dear friend, I pray that you may enjoy good health and that all may go well with you, even as your soul is getting along well (3 John 1:2)

There are many ways to use AdSense to achieve your goals.Some people are using AdSense to earn a great deal of money in amounts that you would not have thought was possible unless you robbed a bank.
http://www.technologiez.net

very simple - Block -> add block - paste the search code in body area and save. make appear on content top

I have done like below:-
copied search-theme-form.tpl.php from Modules->Search Folder to Themes-danland folder
Opened search-theme-form.tpl.php from danland folder
edited below lines:

?>

<?php
print $search_form;
?>

New code:

?>
<div id="search" class="container-inline">
<div class="form-item" id="edit-search-theme-form-1-wrapper">
-------  pasted  Adsense Code from google here -----------
</div>
<input type="hidden" name="form_token" id="edit-search-theme-form-form-token"  value="<?php print drupal_get_token('search_theme_form'); ?>" />
<input type="hidden" name="form_id" id="edit-search-theme-form" value="search_theme_form"  />
  </div>

You can check it at vision4life.in
I am not an expert, but I got this idea from another comment , how to add image in search. I forgot that comment link. sorry.

You can add your adsense code to the custom google search bar as follows.See the following tutorial you can easily embed the adsense with in the search bar.see the guide http://youtu.be/yz2tOjKWAw8 here.

Hi,

It is a nice thing that adsense has the alternative "as_q" parameter. I have a similar problem with a javascript which submits a search using the same "q=" parameter. I contacted them and tried to get them to create an alternate input parameter such as adsense's "as_q", however, they didn't want to. I explained that any drupal website would have this issue, and they still did not agree to anything.

Is there a way for drupal to use something instead of the "q=" parameter, or for me to isolate the javascript so that it sends the value for "q" without conflicting with drupal?

Thanks very much in advance