
This article I will discuss how to integrate Google Adsense Search into a website. If you are a Drupal website owner, I have some goodies here as well.
When it comes to offering search facilities on your website you have several choices. One is to code your own which in most cases would take far too much work. If you use a content management system like Joomla! or Drupal, a search engine is built in. But the problem with these is that they take up quite a bit of server resources and do not always generate the best search results.
If you are a Google Adsense publisher, you know about Google Adsense Search. Drupal website owners can exploit this feature and monetize their site. Here is how.
Go to your Google Adsense publisher account and click on the Adsense Setup tab, then Adsense for Search. If you only want to have Google search and display pages on your site (recommended), choose the "Only sites I select" button.
Next, enter in your website so that all pages are selected and Google comes to visit and spider it. Select all the other options and make sure you choose a Custom Channel so you can monitor your website.
Choose the option of the look and feel branding of the search box to be the watermark feature. If you choose any of the others, I believe you will have problems controlling the output styling with Drupal. The most important step is next. Choose the "Open results within my own site" and specify a URL:
Note that I am following the website name with 'search' (very important).
Give a name to your search engine and hit the submit button. On return you will get two pieces of Javascript to enter into your Drupal pages.
You will need the Content Construction Kit (CCK) module for the following steps.
At this point you now have a search results page for Drupal.
Now here is where things get a little challenging. Because you are working with a content management system (CMS) like Drupal, you are likely using a default theme or have one of your own. Integrating Google Adsense for Search into your theme's template is a little tricky and would almost certainly break it.
But there's a cool trick to all of this and it relies upon some PHP code to monkey with the templates. The first thing you need to do is let Drupal know about the Google Search custom content type you created:
Go to your theme's template.php file and insert the code below. Replace "xxxxx" with your Drupal themes name:
function xxxxx_preprocess_node(&$variables) {
/* hook to route to google search theme node */
if ($variables['node']->type == "googlesearch") {
$variables['template_files'][] = 'node-googlesearch';
}
}
function xxxxx_preprocess_page(&$variables) {
/* hook to route to google search theme page */
if ($variables['node']->type == "googlesearch") {
$variables['template_files'][] = 'page-googlesearch';
}
}
These two functions allow your googlesearch content type to be noticed by PHPTemplate on a page and node basis.
Now that we have the templating hooks in place its time to create a custom Drupal search node. Give this a filename of "node-googlesearch.tpl.php" and put it in your theme's directory:
"; "; ";
echo "
echo "
" . $title . "
echo "
echo "
echo $content;
echo "
The code above is pretty straight forward. All it does is wrap the body content of your google search node with a few divs for styling. Google will insert its search results in an iframe of which will be part of the content variable. Remember your Google search content type page above? The code you put in it was the Google search result javascript snippet that does all the dirty work.
Ok now that we got this far, here comes the hard part. Create a file named page-googlesearch.tpl.php and copy and paste this code inside of it:
<?php echo $content; ?>
<?php echo $closure ?>
!doctype>
Lets walk through this code. First notice that this is a page template for the google search content type that we created above. Whenever someone clicks on the Search link the user is taken to this page.
The Google Adsense For Search search box code is inserted here and is modified on one very important line:
You must make this change for Google Search to work as Drupal will conflict with Adsense's ?q parameter. Save this file also in your theme's directory.
Test your search page out by going to your website and clicking on the search link (if you made one on the menu) or specify it on the browser address bar (mysite.com/search). If you did everything correctly above, you should now see Google Adsense mixed in with Google search results on the lower portion of the page and in the upper region a Google search box.