Google recently added the ability for "Adsense for Search" results to be diplayed within an iframe on your own website.

Unfortunately, it doesn't seem to work within Drupal, because Google sends info back to your custom search page with a GET request, which includes the search terms as "q=your search terms". Drupal sees the "q" and tries to find a page with that name, returning "page not found" (unless you happen to search for something like 'admin' or 'user'). The result is the same whether or not clean urls are enabled.

Has anyone found a way to get this working within Drupal?

With clean URLs enabled, would it be possible to make Drupal ignore GET variables passed in the URL? (while still leaving them intact for Google's javascript to use)?

Any way to hack drupal to use a something other than "q" for that variable?

Comments

pobster’s picture

Maybe you're going about it the wrong way...? I've been using adsearch for about six months now and it's generated a whopping £1.94 in revenue!!! ;o) Woohoo! Yeah rubbish innit!

http://www.arcadegeek.co.uk

I've just disabled the normal search and stuck the adsearch code in a custom block. Have a play with mine and make me some money!!! ;o)

Pobster

khenney’s picture

The search you're using on arcadegeek.co.uk is just the standard google search, with the results displayed on Google's domain (although you can customize the color scheme and display a logo).

I'm talking about a new option that has just been added in the past 24 hours, which allows you to have the search results displayed in an iframe on your own page (and on your own domain). Rather than just the usual script you'd insert for the search box, you also have a second script that goes on your results page to load the iframe with the search results.

Google's page about this feature is here. News of this is just spreading around now, and it looks like it might be causing some issues for users of some other CMS's as well, so maybe Google will make some changes if enough bloggers start complaining of compatibility issues.

pobster’s picture

Well... Not a 'standard' search, as it's still adsense for search! How else do you think I get revenue for it?! ;o)

I can't see the point of it myself... Who cares if when you search for something the results appear on your site or googles site? ALL the results point to your site, so anything you click on takes you there?

Anyways... I reckon (cos I'm not bothered about trying it to test it) you'll have to create a page with an iframe/ frame on it for the adsense for search to return to. Possibly like this:

function adsense_search_help($section) {
  switch ($section) {
    case 'admin/modules#description': // This description is shown in the listing at admin/modules.
      return t('Enables a page for googles adsense for search.');
  }
}

function adsense_search_perm() {
  return array('access adsense search page');
}

function adsense_search_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array('path' => 'adsense_search',
      'title' => t('Search'),
      'callback' => 'adsense_search',
      'access' => user_access('access adsense search page'),
      'type' => MENU_CALLBACK
    );
  }
  return $items;
}

function adsense_search() {
  $output = "copy your adsense for search results page code here! Don't forget to escape any quotes like \"this\"!";
  return $output;
}

This way Drupal sends the GET to the adsense_page function rather than think it's a page.

Pobster
edit: If you prefer this page to be in your menu obviously change MENU_CALLBACK to MENU_NORMAL_ITEM
edit #2: On the link ensure you leave a trailing slash so the google code creates the callback as ...com/?domains=... rather than ...com?domains=...

itinerantwitness’s picture

Like Khenney, I would also like to add this feature to my site but have run into the same problem with 'q' as a URL variable.

Pobster, the code you post above: are you suggesting pasting it into a node and marking it as PHP code? Or creating a adsense_search.module file and uploading it to the modules directory? Or adding the functions to the adsense.module?

Pasting it into a node as php code doesn't seem to make sense and doesn't work. I also added the functions to the adsense.module but it still wasn't clear to me what to do. I did not try creating a separate adsense_search.module because I didn't understand how it was supposed to work. Creating these functions seems to entail also creating at least one search results node, yes?

I have not programmed modules in the past so please bear with me.

Thanks for your help.

Mark

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Mark
ItinerantWitness.com
http://groups.yahoo.com/groups/ItinerantWitnessTravelUpdates
There is no such thing as an uninteresting place.

pobster’s picture

Well as I specified above, I don't know if it'll work or not as I didn't test it - but the other guy didn't reply so I'm guessing it either must have helped or perhaps he just gave up?!!! :o)

It's a complete module the code above, just create adsense_search.module - stick the code above in it and then enable the module. It'll be pretty evident what you need to do with the adsense code I should think? I've given an example in the code above about how to escape double quotes.

Again *UNTESTED*!!!

Pobster

khenney’s picture

Sorry for my failure to reply, I got distracted with something else and forgot to check back in on this post (anyone know if you can get notifications of forum replies sent via email on this forum? like you can on vbulletin?)

I just tried to set this up as a module (inserting my adsense code, escaping double quotes, etc) and I'm getting a blank page. The site I'm trying to add it to is using the 4.6 version, though, so maybe that's the problem?

pobster’s picture

Yes that is the problem... In 4.6 you'll need to use return theme('page', $output);

You'll need to point an iframe at a static page (empty) which contains your adsense code - this gets around the need to recreate a page of your site as the code is then contained within the iframe.

Pobster

khenney’s picture

I tried replacing:
return $output;

with:
return theme('page', $output);

in the original code, but I'm still getting a blank page, like you'd get with a missing semicolon. I can't find anything obviously wrong, but I'm not very familiar with creating modules in Drupal.

pobster’s picture

Try;

print theme('page', $output);
return;

Look... This isn't straight forward as plonk it into the code and it'll work... I'm not going to walk you through this as I've not bothered to do it myself (I can't see as it's necessary), but it's certainly not as simple as putting your adsense code into the $output variable as I'd first suggested... You need to put your code in a static page and then call it from the module you've created... Are you sure you're up for getting your head around this??????

(a concerned) Pobster

khenney’s picture

Yeah, I'm probably over my head here. I'm sure I could figure it out, it's just a low priority since I'm already working around the issue by using a static page to return the search results. I just wanted to give your solution a try since you went through the trouble to post it here. Thanks anyway.

Valerio3’s picture

My understanding is that the "q" GET variable is provided to Drupal by the Apache rewriting module (thanks to the .htaccess file). The use of this variable clashes with the use of the same variable by the Google search script to contain the query search string.

When Drupal receive the "q" GET variable containing the query search string it try to find that page instead, but without success and reply with a 404 page not found error.

I think there is no solution to this problem, unless we succeed to convince Google to use a different variable name.

In the meantime I have found a workaroud that, at least for me and for my site (http://www.audiocast.it), is quite good

1. with clean urls enabled and using url aliasing, I have entered a "story" with the url alias of "google-result.html", the content of the story is the Google search result script. It is important that this story has the "PHP script" attribute (Plain HTML is not OK).

2. I created a static page version of the previous entered story with the following unix command:

wget -O google-results-static.html http://www.audiocast.it/google-results.html

and put this file on the Drupal installation root directory ("/var/drupal" in my own case)

3. I entered the Goole search box script in a block I put on the right column of my web site. In this script I put "http://www.audiocast.it/google-result-static.html" as the link to the result page. You can check the result going to http://www.audiocast.it (this site is in Italian, sorry!)

This workaround is based to the fact that Drupal is not involved in the result page, Apache will deal directly with the static version of the drupal page without rewriting the page using the q GET variable.

The disadvantage of this method is that the result page is not generated by Drupal, but it is a static copy of a Drupal page that can be regularly updated using the Unix cron facility.

Another issue is that the result page, provided by Google in an iframe, wants to be displayed on an area that is at least 650 pixels wide. I tryed to change the "googleSearchFrameWidth" variable in the Google script, but without success. In my website I was forced to eliminate all blocks on the right column (only on the result search page) to allow the result page to overwrite nicely the right column.

Hope this help

Valerio Di Giampietro

steve22’s picture

Thank you valerio. Your way works like a charm!! But I am also unable to change the width of frame from 650px.

mrb’s picture

Google also accepts the parameter as 'Q=', which Drupal ignores. So if you can catch the URL and change 'q=' to 'Q=', Google and Drupal are both happy. I added a few lines to the .htaccess file in the root of our web server to redirect the browser to a new URL formed by replacing the first 'q' in the old query string with 'Q':

# IF not a url pointing to an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# AND not a url pointing to an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
# The basic RewriteRule only affects the part of the URL before the arguments (ie before the '?'
# then mod_rewrite just adds the ?... from the original request on to the end of the rewritten path
# so we have to get the infrmation from the query string with the following line
RewriteCond %{QUERY_STRING} ^([^q]+)q(.*)$
# NE stops the module from escaping the '%' in the new URL
# R forces a redirect so that the browser starts from new
# L stops any other attempts at rewriting after this
RewriteRule my-search-results-page http://www.mydomain.com/my-search-results-page?%1Q%2 [NE,R,L]

You can see it work at http://www.batgung.com

Kinda klunky, and may break at any time that Google change their mind re case & parameter names, but works for me for now.

MrB

igbonine’s picture

Just subscribing

btilma’s picture

Google seems to accept 'Q' or 'as_q', although I haven't tried too many combinations. After playing around with mod_rewrite quite a bit, I've come up with the following:

Drupal's default rewrite rule looks like this:

  # Rewrite current-style URLs of the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I added a section (above it) in .htaccess that looks like:

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{QUERY_STRING} ^(.*)q=(.*)$
  RewriteRule searchresults searchresults?%1as_q=%2     [C,NE]
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA,NE]

FYI, my page alias is '/searchresults'. This does exactly what the previous mod_rewrite hack did, but without the redirect (and the previous one didn't work too well for me)

techmag’s picture

Change the param q to as_q
google will recognise it
Check the article and implementation at

http://www.techmag.biz/google_onsite_search_on_drupal

chadm’s picture

Thanks for the tip. I tried it and I no longer get a Page Not Found error, but I'm still having issues. I left the search on my site to be http://www.google.com/custom for now. I tried changing it http://linuxappfinder.com/search_results, but no results are being displayed.

Has anyone else run into this problem? I looked at the source on www.techmag.biz, and even matching that exactly doesn't work. I'm not sure what could be causing the lack of results.

http://linuxappfinder.com

adminfor@inforo.com.ar’s picture

I´ve implemented it, with an addition.
I´ve put the whole code in a node (with the search field large 60 chars), and also I've implemented the first piece of code (only the search) in a block with 20 chars search fields that turns to the same result page.
Take a look here http://www.inforo.com.ar if you want, search something in the google right block.
Thank you all for the "as_q" recipe!!!

PoisedGuru’s picture

By going to the Google Adsense site and generating the code for the search box and the results page on our site I had everything I needed.

I then made a static page that only consisted of the two code snippets from google. Once that was in place I did have to change the q variable, I used as_q as suggested. I also stripped out the Google logo (but have a note saying the search is powered by Google) and changed the results iframe to fit into our theme (550 px wide). I flipped the 'checked' to our site so it defaulted to search our site but I made the 'web' search option available as well.

While it cuts off a bit of the note from Google about using Safesearch and such everything is just fine and it works great. You can see the implementation here:

http://www.xomba.com/google_results_page

Thanks for the info.

PoisedGuru
Xomba.com
"Show and Tell Has Never Been So Rewarding..."

kaimeta’s picture

what is the script you used to call the results page? and was that put into a page or into a file?

Thanks
JCO Group

PoisedGuru’s picture

Here is what the code of the page looks like.

<!-- SiteSearch Google -->
<form method="get" action="http://www.xomba.com/google_results_page/" target="_top">
<table border="0" bgcolor="#ffffff" width="400">
<tr><td nowrap="nowrap" valign="top" align="left" height="32">
<a href="http://www.google.com/"></a>
</td>
<td nowrap="nowrap">
<input type="hidden" name="domains" value="www.xomba.com"></input>
<input type="text" name="as_q" size="31" maxlength="255" value=""></input>
<input type="submit" name="sa" value="Search"></input>
</td></tr>
<tr>
<td>&nbsp;</td>
<td nowrap="nowrap">
<table>
<tr>
<td align="left">
<input type="radio" name="sitesearch" value="www.xomba.com" checked="checked"></input>
<font size="-1" color="#000000">www.xomba.com</font>
</td>
<td align="left">
<input type="radio" name="sitesearch" value=""></input>
<font size="-1" color="#000000">Web</font>
</td>
</tr>
</table>
<input type="hidden" name="client" value="OUR ADSENSE ID"></input>
<input type="hidden" name="forid" value="1"></input>
<input type="hidden" name="ie" value="ISO-8859-1"></input>
<input type="hidden" name="oe" value="ISO-8859-1"></input>
<input type="hidden" name="safe" value="active"></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 -->
<!-- Google Search Result Snippet Begins -->
<div id="googleSearchUnitIframe"></div>

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

You can check it out here:
http://www.xomba.com/google_results_page/

It's just a page with that code in it and the width set to fit our theme.

PoisedGuru
Xomba.com
"Show and Tell Has Never Been So Rewarding..."

jason342’s picture

This is so strange. This feature does not work on my main site as how it is, but yet it works on my TEST site?

How come it works for one and not the other.

I got the same problem as mentioned here for the main site, but no problem for my test site.

And I have a feeling that running this feature on my main site is the reason why my logs have stopped functioning properly. But there's no problem for my test site.

nicholasthompson’s picture

I can confirm this does work PERFECTLY... Thanks a million!

One VERY important note... The FORID in the cof hidden field must be 11... or at least it wont work if its left at 1 which is what google gave me for the custom page they provide. Now my search is on-site :-) Bring on the dollars!

http://www.thingy-ma-jig.co.uk/

jason342’s picture

nicholasThompson, what did you do that worked perfectly?

nicholasthompson’s picture

I used Poised Guru's method for implementing Google Adsense Search on my site. Before I was having the usual issue of google using 'q' for the search phrase and drupal using 'q' for the URL.

marcoBauli’s picture

The "as_q" tip works fine for an on-site search but once someone adds your CSE to their Google Homepage and tries to use it as a Google Gadget and searches it still implements the "q" variable.

Any ideas? thank you very much for any help

adminfor@inforo.com.ar’s picture

You are right, as_q doesn't work in this conditions.
The solution for CSE was already in http://drupal.org/node/78888#comment-150786 Thanks mrb !!!
I've tried it in my test site and worked fine. Now I'm going to put it in production soon.
Have to try first more than one on site-search, the standard and a CSE one. May be duplicating the rewrite rules with two different urls.
Gustavo
http://www.inforo.com.ar

snerky’s picture

1. You change the name of the text box in the code on the search page:

Presently your 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="">

Acknowledgement: this is from : http://www.techmag.biz/google_onsite_search_on_drupal

2. THEN you have to remember to change the url for your Search Results page to take out the "?q=..." stuff and replace it with "/node/..."

e.g. <form id="searchbox_0010862524140715207:wpomv_db_9c" action="http://yoursite.com/node/3">

where /node/3 is the Search results page.

That's it.

jason342’s picture

It's against Google's Terms to modify the code they provide in any shape or form or how insignificant the modification may be.

keto-1’s picture

Is it not possible to just add the adsense for content code into search.module? I don't really want to change the way drupal displays search results, I just want to make a little room for adsense. Anyone have any insight on this?

Mortgage Blog