Maximize Adsense By Only Showing Ads To Search Engine Visitors Even With WP Super Cache For Your WordPress Blog

Share On Facebook

I recently spent some time working with Adsense on so I thought I’d share with you some of the optimizations I’ve made with Google ads on this blog.

As many of you may have noticed, this blog makes part of its revenues from Adsense ads which can be found directly above and below the content. But depending on how you arrived at this site, you may not have noticed the ads at all.

Because the income I make from Adsense ad clicks is directly related to the conversion rate of the people seeing the ads, it is to my benefit to only show these ads to interested parties.

If too many ad impressions are shown but do not lead to conversions, Google will reduce the payout per click for my entire site. This phenomenon is know as “smart pricing”.

In this case of, the best way for me to maximize conversions is to show Google Adsense ads only to search engine visitors and this is exactly what I do.

Now if you look online, there are many articles already posted on how to show ads only to search engine visitors so why am I covering this topic yet again?

The problem is that none of these tutorials cover how to do this if you cache your website.

WP Super Cache And Other Caching Plugins

Most of the solutions out there rely on php modifications to the WordPress template in order to serve Adsense ads only to search engine visitors.

However, if you run any sort of caching plugins for your wordpress blog, you will run into problems with dynamic code.

The reason lies with the way caching works. In the case of the WP Super Cache plugin, the plugin makes an exact copy of your html code and saves it into a file on your server.

When many visitors hit the exact same article on your site, the static copy is served instead of the regular version. As a result, many of the CPU intensive PHP and database operations are avoided.

In fact, caching is one of the best ways to speed up your website without having to upgrade your server and everyone should be doing it because it’s quick, easy and free.

The problem with caching however is that because a static copy of your site is being served to the end user, dynamic content using php can not be used because the php interpreter is not even being run when a cached copy is served.

In short, many of the php based solutions to serve Adsense ads only to search engine visitors will not work when caching or the WP Super Cache plugin is used.

In addition, many other plugins that rely on dynamic content can not be used with the WP Super Cache plugin unless it is run in half-on mode.

A Javascript Solution

Even though the WP Super Cache plugin serves a static copy to the end user, one of the best ways to still serve dynamic content is through the use of javascript.

Javascript works in such a way that all of the javascript code is run directly on the client’s computer. As a result, you can still serve static html files to the end user and run dynamic code.

It is through javascript that I’m able to serve fully cached files and still run my little script to serve adsense ads only to search engine visitors.

My Javascript Code

Here is my little javascript snippet below which is all contained in a .js file which I’ve named adsense.js.

var ref = document.referrer;
var sevisitor;
var SE = new Array('', 'search.', '', '', '.yahoo.','/url','/search','.google.','.ask.','.baidu.','.bing.','.aol.','.answers.');
for ( var source in SE){
var match = ref.indexOf(SE[source]);
if (match !=-1) {

function adsenseTop(){
if (sevisitor){
var large = '<script type="text/javascript"><!-- \n\
google_ad_client = ""; \n\
/* 336x280 Content Top, created 7/24/09 */ \n\
google_ad_slot = ""; \n\
google_ad_width = 336; \n\
google_ad_height = 280; \n\
//--> \n\
</script> \n\
<script type="text/javascript" src=""> \n\
</script> \n\

Then in my single.php file under my WordPress theme, I add the following lines where I want to display my Adsense ad.

<script type="text/javascript" src="adsense.js"></script>
<script type="text/javascript">adsenseTop();</script>

To all experienced web programmers, this script probably comes across as a big joke. However, many bloggers are not programmers so I will explain what is going on.

In a nutshell, the script detects the referring website and stores this value into the variable ref. It then checks to see if the referring URL matches any of the search engines.

Naturally,I check for all of the popular ones including even Baidu. If the referring URL matches, then I set the sevisitor variable. Based on the sevisitor variable, I then use javascript to output the Google Adsense code.

Smart Pricing

Ever since I started implementing this code, click payouts have increased steadily. Showing adsense ads only to those searching for relevant information makes sense.

You spare your regular readers from having to see the ads and you capitalize on those who are in search of answers. It’s a win win situation.

Ready To Get Serious About Starting An Online Business?

If you are really considering starting your own online business, then you have to check out my free mini course on How To Create A Niche Online Store In 5 Easy Steps.

In this 6 day mini course, I reveal the steps that my wife and I took to earn 100 thousand dollars in the span of just a year. Best of all, it's absolutely free!

Share On Facebook

Similar Posts

Have you read these?

41 thoughts on “Maximize Adsense By Only Showing Ads To Search Engine Visitors Even With WP Super Cache For Your WordPress Blog”

  1. Solid concept thanks for sharing, your tutorial will make it easy to implement. I’ve found it difficult to see solid CTR from blog adsense ads, primarily b/c many visitors are repeat coming from either a feedburner subscription or a RSS reader.

    Gonna go give this a try.

  2. Great tip! I see it works too, as I just searched your post and came in through Google to see the difference. I think I’m definitely going to try this for my site for a bit to see how it does. Thanks for the great how to!

  3. This seems like a useful technique to customize content for searchers verses other browsers. I assume this is also how some sites recognize stumbleupon visitors and other social media specific visitors.

    Anyway, I can’t seem to get mine to properly call the javascript I have. I’ve followed your instructions to make my adsense.js and called it with the two lines in my single.php theme file but can’t get it to actually do anything. I have a else method to write content to the page even for direct visits but don’t seem to get any results. Perhaps you can elaborate on how you made the call from the single.php file and is your adsense.js file listed in its entirety? I’ll have to look for ways to test this since I can’t tell what isn’t working, it simply doesn’t output anything even though my script code works if I add it directly at the html of a page.

    Thanks for any more tips you can offer.

  4. This is useful I will try to get it on to my website. thanks

  5. I followed to create the adsense.js but can’t see any result using the 2 lines code in single post. Can you show me how to implement this code? Thanks

  6. Bill says:


    I am just learning about this “G” function and I want to know which blog folder to FTP the “adsense.js” file?

    I guess I create adsense.js using Notepad and name it as such. Yes?

    Then where in my blog do I put this file? In the related theme folder?

    Another question. I am currently using the Plug-in “Whydowork Adsense” and consequently the Adsense Ad HTML code is not shown during the editing of the page.

    Should I Deactivate the Plugin and copy the Adsense generated code directly into the actual page?

    I guess that I need to do this procedure for each page separately? Please advise.

    Thank you,


  7. I am definitely going to apply this trick when I will have decent traffic from Google search 😀

  8. Great and useful article/post!
    Good job!
    All The Best!

  9. Hmmm, interesting concept, I’ll give it a go. What I don’t understand is how your adsense revenues increased after implementing this, since before, you were showing it to all visitors, so both returning and search referred, now you are showing it only to search referred, so I would think that would mean you get less revenue. It is a better user experience though for returning visitors, so I’ll give it a go as well.

  10. Dear, please pay attention to this part:

    for ( var source in SE){
    var match = ref.indexOf(SE[source]);
    if (match !=-1) {

    Some might found out that your script isn’t working, that’s because the for condition, isn’t properly closed.

    I think this will solve your problem:

    for ( var source in SE){
    var match = ref.indexOf(SE[source]);
    if (match !=-1) {

    1. @nichive
      Thanks for catching that. I’ve updated the post!

  11. Jenny Kinsolan says:

    Hi Steve,

    Thanks for this code, it is working really well for me. I do have one question though if you don’t mind helping me with? How can i get my ads to show after the first page view, because at the moment they only show for the first page and then dissappear on a second page view.

    I saw some other sites using cookies in their code, but unfortunately yours was the only one that worked with my caching plugin.

    Have you added a setcookie to your function code? As I see that your ads do display on the second page view.

    Thanks hun!


    1. Hi Jen,

      Yes, the script I run on my site is different than the one posted on the article in that I use cookies. I tried to email you the code but the email address you entered in your comment did not work


  12. Jenny Kinsolan says:

    I left you my personal email, could you please try sending it to that.



  13. I haven’t experienced smart pricing yet though I feel it’s only a matter of time. This little trick however gives me a chance of avoiding it though I hope it’s not against G’s TOS?

  14. I use the W3 cache but thats just to speed up the load time for my site or so I thought. I hadnt thought of using it the way you are suggesting it above.

  15. This is easier to follow now that you have shown us the code.

    1. This plugin didn’t exist when I wrote the article, though adding the javascript is quick and easy.

  16. Hi Steve,

    I would also like to try the code you have with the cookies enabled so ads are shown when users go to another page on my site. If you can email it to me that would be great.

    Very nice site btw!

  17. Hi.

    This is good.

    I just stumbled on this article while seeking other information. I had noticed that my price per click had gone way down, but I did not know the cause and effect. You may have just helped me bring my income back to a decent level. Sometimes I just don’t think about the obvious.

    Thanks lots,


  18. Price per click will go down if your ads are relevant (headline, copy leading to a relevant landing page) and your quality score is high. Google rewards you for doing this.

  19. James Briggs says:

    Could this be edited to set a cookie? Make it so the ads are shown to that same visitor for a couple of hours or so?

    1. Hey James
      Absolutely. In fact, I use cookies in my implementation on my blog. Feel free to check out the javascript and use the code if you like from my page.

  20. This is new to me, I don’t use or set cookies on my users’ PCs. My understanding was that once the use clicked on an affiliate link on my web page, the cookie would be set by the link. Therefore I am confused as to how or why you are setting a cookie? Why would you do this and apologies in advance if I am missing something very simple here.

  21. The only reason to set a cookie is to ensure that adsense ads are displayed as the search user traverses different pages on your site. This has nothing to do with affiliate tracking.

  22. Hello, your code is very useful, however I don’t know how to use it, because I dont understand the part in which the ad appears:


    How and from where should I change it? And how to set the code when I have more than 1 ad in the single or index page?


    1. Hi Luishi
      Feel free to go through the source html of my webpage, grab the adsense.js file and use it for your own purposes.

  23. Thanks a million for this. The implementation of this is sure to improve my bottom line and user experience.

  24. I read your article, I am not getting Google traffic yet but I am not good with wordpress and more. Can you help me set this for my site. I will email you my passwords when you reply. Please help me get this done. Please.

  25. Great and useful article/post!
    Good job!
    All The Best!

Leave a Reply

Your email address will not be published. Required fields are marked *