How to Show Adsense Ads Only to Search Engine Visitors

It’s a good practice to show Adsense ads only to search engine visitors. This implies that the regular readers who visit your site from a browser bookmark, RSS feed or by manually typing your blog’s address, never see any advertisements.

 

Udegbunam Chukwudi, one of the regular visitors of this blog asked the following question through a comment:

Do you know of any trick for showing ads to search engine visitors only while using cache plug-ins?

There should not be any issue with cache plugins, because all the cache plugins generate a static html file of your pages. To speed up the load time, the static files are shown whenever some one requests to view your posts.

Here are the codes to show advertisements to only search traffic in WordPress:

1. Open the functions.php file of your WordPress theme and paste the following code:

function scratch99_fromasearchengine(){
$ref = $_SERVER['HTTP_REFERER'];
$SE = array(‘images.google.’, ‘web.info.com’, ‘search.’, ‘del.icio.us/search’, ‘soso.com’, ‘/search/’, ‘.yahoo.’, ‘.ask.com/’, ‘.google.’);
foreach ($SE as $source) {
if (strpos($ref,$source)!==false) return true;
}
return false;
}

In the above code, the following array of sites are considered as search engines

$SE = array(‘images.google.’, ‘web.info.com’, ‘search.’, ‘del.icio.us/search’, ‘soso.com’, ‘/search/’, ‘.yahoo.’, ‘.ask.com/’, ‘.google.’);

You can add more sites in the above array by enclosing each site within single quotes and separating two sources by a comma (for example ‘abc.com/search’,'search.xyz.com’)

2. Open the corresponding file of your WordPress theme where you would want to show the ads. If you want to show the ads on single post pages, open the single.php file and if you want to show the ads on the home page, open the index.php file and so on.

For the sake of this tutorial, let’s assume that you want to show the ads on the single post pages, just below the article title. Find the following code:

<?php the_content(); ?>

3. Paste the following code just before the above code:

<?php

if (function_exists(‘scratch99_fromasearchengine’)) {

if (scratch99_fromasearchengine()) { ?>

//Insert Addsense codes here

<?php } } ?>

4. The above code detects the referring source of the visitor as defined in the functions.php file (refer step 1). If a visitor comes from any of the above sources, the function is executed and the ads are shown. If the visitor comes from an RSS feed or by manually typing your blog’s address, the function is not called and thus the ads are not shown.

You can innovate this function for a variety of other purposes. For example:

Show an Email subscription box to search engine visitors and a “Subscribe to RSS Feed” block  to all other visitors.

Showing an email subscription box to search engine visitors is a good idea as most of them do not know about the RSS technology. Use the following code along with the function as described in step 1:

<?php
if (function_exists(‘scratch99_fromasearchengine’)) {
if (scratch99_fromasearchengine()) { ?>
//Insert code for Email subscription
<?php } } ?>
<?php if (!scratch99_fromasearchengine()) { ?>
//Insert code for RSS readers
<?php } ?>

The above code uses a simple IF – Else check and displays different blocks of HTML code to visitors coming from different sources. (also read: How to show Adsense ads only on longer posts).

For Blogs using the Blogger platform, check out this guide. Also read: Adsense Split testing guide for WordPress and Blogger blogs

Email this article

Written by on Sunday, February 28th, 2010

  1. Reader Comments

  2. ArpitNext

    too geeky. :|

    For WordPress users, I’ve a cool trick: Install plug-in “Who sees Ads”. It is capable of detecting source and displaying content accordingly. There are many other “Advanced” plug-ins too. :) This is the real beauty of WordPress – a complete ecosystem!

    March 1st, 2010
    • Amit Banerjee

      @Arpit: Yes, I know of Who sees Ads but there is one concern withthe plugin. The ad blocks are treated as widgets and you cannot control the ad units any more.

      For example: I want to show ads to search visitors as well as hide the ads from blog authors. In that case I can use a function as below:’

      if (!user_logged_in()&&search_visitor()){
      //display code
      }

      Who sees ads works great but I think for complete control, you have to master the code yourself. Thanks for your thoughts.

      March 1st, 2010
  3. Popular Web Geek

    The problem is that the ads will be shown only to landing page of search engine visitor and disappear on the next post he/she reads …. but analytics show that good clicks come from the 2nd or 3rd read post by a search visitor.

    March 1st, 2010
    • Amit Banerjee

      @Popular Web geek: Very true, but do you know of any trick to show the ads even if the visitor goes to the second or third page? Thanks for your comment.

      March 1st, 2010
  4. Soumen Halder

    Ya Arpit and Who sees ads has another big problem, you can define only one administrator from whom the ads are hidden i.e in a multi-admin/author blog like ours its impossible to hide ads from all. In cases like the snippet provided by Amit proves helpful.

    March 2nd, 2010
  5. Udegbunam Chukwudi

    @Amit: What a coincidence! ;) I was about asking the same question you just asked Popular Web Geek.

    Thanks for the tutorial. I really appreciate the effort ;)

    March 6th, 2010
    • Amit Banerjee

      @Udegbunam: Yes, I am looking for a workaround. But I don’t think I will find one … still trying. Thanks for the comment.

      March 6th, 2010
  6. Piseth

    I implement the code into my blog but i cannot execute to have it and showed the error message instead.

    May 1st, 2010
    • Amit Banerjee

      @Piseth: The codes given here are accurate and working. I will advise you to copy the codes to notepad first and then place them in your theme’s source files. Best of luck and do tell us if the problem persists !

      May 1st, 2010
  7. Schalk

    Hi Amit, thanks for the code.

    I think the problem that some are having is that WordPress automatically converts to fancy quotes like ‘ ’ instead of single quotes like ‘ ‘.

    June 7th, 2010
    • Amit Banerjee

      @Schalk: Yes, but if you use the code on the source of your WordPress theme, the quotations wont be an issue. Thanks for the comment

      June 7th, 2010
  8. Debajyoti Das

    Did you find any update on the Q. asked by Popular Web Geek.

    Can I face a penalty if I disable “facebook comments” or any widget section of my WP site for search visitors…

    PS: You can add .bing. to your code.

    March 3rd, 2011
    • Amit Banerjee

      No you wont face any penalty or anything like that if you tweak your code and retuen different widgets or sections to different visitors.

      But of course, dont it to the main content and navigational elements. That will result to “cloaking”

      March 3rd, 2011
  9. MiBe

    “Do you know of any trick for showing ads to search engine visitors only while using cache plug-ins?”

    The plugin “Search Engine Visitor Ads” (http://wpplugins.com/plugin/861/search-engine-visitor-ads/) can also display ads for search engine visitors and works also with caching-plugins…

    July 15th, 2011
  10. www.gasoof.com

    hemmm good new for me, i already tried split test for blogspot from book that i buy on the store book.

    December 19th, 2011

Add Your Comment

All Comments are Moderated.

YOUR COMMENT

About This Site

Ampercent is a technology blog on computer tutorials, software guides, how to tricks and web tools. The blog is updated daily and written by two computer science students from India. Read More »

Tip Us »

Have a great tip which you want to share with fellow readers? Send in your ideas to tips@ampercent.com