Web Design Archive

How to use "Google AJAX Search API" for your site's search engine

How to use "Google AJAX Search API" for your site's search engine
photo by Onur

Google was offering free search modules for websites but they did not allow to modify the look of the results that much before. With the “Google AJAX Search API”, now we have the ability to modify the look of the results. Even better, you can be more creative with the results queries.

In this post I will show you how to create a tabbed search results for your site. First thing to do is to sign-up and get your Google AJAX API.
There is a very useful “Developer’s Guide” and samples of the codes to play around in the playground.

In this blog, I am using the tabbed version of Google Search and here is how I accomplish. (At the time of writing this post, content of the blog was carried from another domain that’s why Google hasn’t indexed the pages yet. So that search results will be empty for this blog for a while.)
There are 3 pieces of code, 1 for the header section of your HTML, 1 for the search box and 1 for the results area.

Code for the header section;

This piece of code has to be in the head section of your HTML before the “</head>” tag.

<script src="http://www.google.com/jsapi?key=YourAPI" type="text/javascript"></script>
<style type="text/css">
#searchResults .gsc-control { width : 500px; }
.search-control { margin: 20px; }
</style>
<script language='Javascript' type='text/javascript'>
//<![CDATA[
// Load Search and Feed APIs
google.load("search", "1");
function OnLoad() {
var controlRoot = document.getElementById("searchResults");
// Create a search control
var searchControl = new GSearchControl();
searchControl.setLinkTarget(GSearch.LINK_TARGET_SELF);
searchControl.setResultSetSize(GSearch.LARGE_RESULTSET);

var searchFormElement = document.getElementById("searchControl");
var drawOptions = new GdrawOptions();
drawOptions.setSearchFormRoot(searchFormElement);
drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);

// this is where you create your tabbed search results
// for every URL tab, you have to use the search address and the tab label
        // This Blog
      var searcher = new GwebSearch();
      searcher.setSiteRestriction("http://www.lycie.com");
      searcher.setUserDefinedLabel("Lycie");
      searchControl.addSearcher(searcher);

      // google also allow you to search the entire web
      searcher = new GwebSearch();
searcher.setUserDefinedLabel("Web");
      searchControl.addSearcher(searcher);

      // search all other blogs
      searcher = new GblogSearch();
searcher.setUserDefinedLabel("All blogs");
      searchControl.addSearcher(searcher);

// draw it
searchControl.draw(controlRoot, drawOptions);

}
google.setOnLoadCallback(OnLoad, true);
//]]>
</script>

There are 3 important things to do in this code.

  1. Sign up and get your Google AJAX Search API. Put your API key where it says YourAPI in the code.
    <script src="http://www.google.com/jsapi?key=YourAPI" type="text/javascript"></script>
    <style type="text/css">
  2. CSS styling and tab creation are handled by Google. But if you want to use more styling use the CSS section in the code.
    <style type="text/css">
    #searchResults .gsc-control { width : 500px; }
    .search-control { margin: 20px; }
    </style>
  3. Creating the tabs for the search results. Whenever somebody search my blog, search results will be displayed in 3 tabs, Lycie – results from my site, Web – results from the web and All Blogs – Results from all the blogs on the web.

Here is how I do. For every URL tab you want to create use the code below.

var searcher = new GwebSearch();
searcher.setSiteRestriction("http://www.lycie.com");
searcher.setUserDefinedLabel("Lycie");
searchControl.addSearcher(searcher);

There are 2 things to change in this piece of code in order to create new tabs.

searcher.setSiteRestriction("http://www.lycie.com");
searcher.setUserDefinedLabel("Lycie");

This is where you limit your search results. In other word, you tell Google to display results only from this URL. Here I say, limit this tab’s results for this URL “http://www.lycie.com” and use the label “Lycie”

Limiting your results from an URL means, only results from that URL will be displayed in that tab. This is very useful if you have different sections on your site. Common usage for this is Forums and Galleries. If you have a forum on your site, most likely its URL is www.example.com/forums. By using the code below, you can create another tab just for results from your forums.

searcher.setSiteRestriction("http://www.example.com/forums/");
searcher.setUserDefinedLabel("Forums");

Google does not only allow URL restrictions. Google also allows you to search the entire web for videos, blogs, news, images, books and patents. Check the AJAX API’s Playground for more coding tips.

Code for the search box

Put this into your HTML wherever you want to show your search box.

<div id="searchControl">Loading...</div>

Code for the search results

Put this into your HTML wherever you want to show your search results.

<div id="searchResults">Loading...</div>

That’s it. Enjoy your new search engine.

Tracking file downloads and external links with Google Analytics

Tracking file downloads and external links with Google Analytics
photo by Onur

Since, I have become a Qualified Google Analytics Individual, I decided to write a little tip about how to use Google Analytics for some of the link types on your site.

You can see the statistics of your internal links but how about download links and external links to other sites.
Google Analytics can also track these kind of user activities too. But in order to keep track of these data you need to modify your links.

A regular link looks like

<a href="http://www.example.com">Example Site</a>

In order to keep track of any link clicks you need to use a piece of extra code;

onclick="pageTracker._trackPageview('/external/example.com')"

The part ‘/external/example.com’ is the part you need to come up with. These are virtual page names which you need to create for your statistics data. I use self explanatory path names different than actual folder names like “lycie-outgoing-blog” so that I can understand that the link is an external link from the blog section of lycie. Or “lycie-download-pdf” which will tell me a pdf download from lycie.

So the final links will look like;

<a href="http://www.example.com" onclick="pageTracker._trackPageview('/lycie-outgoing-blog/example.com');">Example Site</a>

or

<a href="http://www.lycie.com/imaginary.pdf" onclick="pageTracker._trackPageview('/lycie-download-pdf/imaginary');">Download the imaginary PDF</a>

Now, your links are ready to be tracked. You can see the related links’ data on the Content part of your reports. When you filter your reports with the virtual path names, you can see the most downloaded file or the most visited external link on your report.

parishiltonrecord.com, she probably doesn't know

parishiltonrecord.com, she probably doesn't know

While I was surfing around last night, I found Paris Hilton’s website(it was just a coincidence, I was not looking for something else:)).http://www.parishilton.com – According to twitter, this is Paris Hilton’s official website (https://twitter.com/ParisHilton).

I get excited when I find these kind of celebrities. Even though she is not the rocket scientist, she is a very well known person all around the world. Becuase of the internet you can come very close to these kind of personalities. What was very strange about her site is the website she is sending people as her official album site (parishiltonrecord.com). The site does not work and gives errors.

Most likely, she doesn’t know anything about it and most likely she wouldn’t even care about about this. But unless you are Paris Hilton or at the same level, you shouldn’t do the same mistake.