Show Adsense To Search Visitors Only – On Blogger
January 31st, 2009 by Stephen Cronin (Please wait) [Shortlink]Want to make money online with Google Adsense? If so, you better make sure you don’t get smart priced! One of my most successful posts covers how to avoid smart pricing by only serving Adsense to search engine visitors. That post was written for the WordPress platform. In this post, I outline how to do the same on Blogger blogs (although there are severe limitations).
Why Only Show Adsense To Search Visitors?
First, why are we doing this? The main reason for showing Adsense to search visitors only, is to try to avoid smart pricing. I won’t go over smart pricing again (go read the above post in detail), but here’s the brief summary:
Smart Pricing is a penalty Google applies to Adsense accounts that don’t convert well for the advertiser and results in you earning only about 10% of what you’d normally earn per click. Search engine visitors provide targeted traffic for the advertiser, which converts well (no problem). Regular readers and social networks visitors provide untargeted traffic, which doesn’t covert well, increasing your chances of being smart priced (problem).
After reading Grizzly’s recent post on making money online anonymously, I learnt that it’s even more complicated than that. Grizzly, who’s a master of making money online, reveals that when he writes a post his CPC (Cost Per Click) drops for a couple of days:
Whenever I post I get a surge of traffic – the surge is un-targeted (my readers) and my CPC drops for several days until readers tail off. As long as my ad impressions stay above the 3000 barrier my CPC doesn’t drop more than 10 cents a click. If my ad impressions fall below the 3000 barrier I see a 30 cent drop in earnings per click.
Grizzly also says that his CPC dropped when he received a surge of traffic from a forum. It follows that the same would happen when you receive a surge of traffic from the social networks (ie Digg, StumbleUpon, etc).
It may be that this phenomenon is separate to smart pricing, or it may be that it’s actually a part of smart pricing and that smart pricing is far more complex than anyone has guessed.
Either way, showing Adsense to search engine visitors only should ensure that Adsense units are only served to targeted traffic, which should protect your CPC and help you make more money.
Serving Adsense Only To Search Visitors on Blogger
I’ve already covered how to display Adsense units only to search visitors for WordPress and I actively use the method on this blog. However, I also have a few Blogger blogs (and Grizzley’s site is on Blogger), so I started wondering how to show Adsense only to search traffic on Blogger.
With WordPress it’s easy. You can use PHP on the server side to detect where the visitor came from, then decide whether to include the Adsense code. If they came from a search engine, the Adsense code is added to the HTML sent to the browser. If they came from somewhere else, then it’s not.
However, with Blogger, it’s not possible to detect where the user came from on the server side. The only option is to use JavaScript, on the client side, to a) detect where the user came from and b) show the Adsense code or not.
Part A – Detecting Search Engine Visitors
Detecting search engine visitors is relatively easy. We can use JavaScript’s document.referrer to work out where the user came from. Converting the code I used for the PHP in JavaScript, we can do something like:
<script type='text/javascript'>
//<![CDATA[
var ref = document.referrer;
var se = new Array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');
var sevisitor = false;
for (var i = 0; i <= se.length-1; i++) {
if (ref.indexOf(se[i])!== -1) {
var expiry = new Date ();
expiry.setTime(expiry.getTime() + 3600000);
document.cookie = "sevisitor=1; expires=" + expiry + "; path=/; domain=scratch99.com";
sevisitor = true;
}
}
//]]>
</script>
Note: In the document.cookie line, you must change “.scratch99.com” to your own domain!
This code detects whether the user arrived from one of a number of search engines and, if so, sets a variable called ‘sevisitor’ to true. It also creates a cookie, so these visitors can be identified if they subsequently navigate to another page (they will no longer appear to have come from a search engine).
We’ll use the variable or cookie later to decide whether we’re going to display Adsense.
So where does this code go? We only need this code once on the page, regardless of how many Adsense units we show, so we’ll put it at the top. The bad news is that it doesn’t work if you try to add it via a Blogger gadget, so we’ll have to add it to the template itself, as follows:
- In Blogger, go to Layout, then choose Edit HTML
- Make a backup of your template by clicking Download Full Template
- Click Expand Widget Templates
- Search for <title><data:blog.pageTitle/></title> (near the top somewhere)
- Place the code above on the line immediately below this.
- Save the template
Your blog should now be setting the variable and the cookie.
Part B – Showing Adsense To Search Visitors
Next we need to detect whether the variable and cookie are set and, if so, show the Adsense unit. To check whether they’re set, we’re going to use the following code:
var results = document.cookie.match ( '(^|;) ?sevisitor=([^;]*)(;|$)' );
if (sevisitor == true || results[2] == 1) {
Put this together with your Adsense code and it should look something like this:
<script type='text/javascript'>
//<![CDATA[
var results = document.cookie.match ( '(^|;) ?sevisitor=([^;]*)(;|$)' );
if (sevisitor == true || results[2] == 1) {
var adsenseString = "<div style=\"float: left;\">\n";
adsenseString += "<script type=\"text/javascript\"><!--\n";
adsenseString += "google_ad_client = \"pub-xxxxxxxxxxxxxxxx\";\n";
adsenseString += "google_ad_host = \"pub-xxxxxxxxxxxxxxxx\";\n";
adsenseString += "google_ad_slot = \"xxxxxxxxxx\";\n";
adsenseString += "google_ad_width = 336;\n";
adsenseString += "google_ad_height = 280;\n";
adsenseString += "//-->\n";
adsenseString += "<\/script>\n";
adsenseString += "<script type=\"text/javascript\"\n";
adsenseString += "src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">\n";
adsenseString += "<\/script>\n";
adsenseString += "<\/div>\n";
document.write(adsenseString);
}
//]]>
</script>
You’ll have to replace the xxxx’s with the appropriate numbers from your Adsense code and you’ll have to change any other details (such as ad width and height if you’re not using a large rectangle).
What this script does is first check whether the variable or the cookie is set and if so, builds a string containing the Adsense code, then writes it. We have to build a string then write it, because there’s an external script called by the Adsense code. If there wasn’t an external script called, we could just strip the <script> tags and put the code in directly.
Where does this code go? The answer is wherever you want it, but it has to be in the template, as follows:
- In Blogger, go to Layout, then choose Edit HTML
- Make a backup of your template by clicking Download Full Template
- Click Expand Widget Templates
- Find where you want to put the ad unit and place your code near this.
- Save the template
Here are some suggestions for where you might like to put the Adsense unit:
- At the top of the post body: Place it on the line after <data:post.body/> (or <p><data:post.body/></p>).
- Before the post title: Place it on the line before <b:includable id=’post’ var=’post’>
- At the top of the sidebar: Place it on the line after <b:section class=’sidebar’ id=’sidebar’ preferred=’yes’>
Remember, according to Google’s Terms Of Service, you can only have 3 Adsense units on a page (plus 3 link units), so make sure you don’t put this in too many places. You don’t want to be banned from Adsense.
Why In The Template?
You might think you could save yourself the trouble of editing the template by using HTML/JavaScript gadgets that can be added via Layout > Page Elements.
Save yourself the time and don’t bother. These gadgets are extremely fussy. CDATA doesn’t work, the code you paste in mysteriously changes, using htmlentities doesn’t help.
And before anyone asks, there is no way this can work with the built in Adsense gadgets. The only way seems to be through the template.
Is It Worth It?
This obviously takes a while to set up and only you can decide whether it’s worth it to you. It really depends on whether you’re likely to get smart priced. I’m not going to bother, as 99 percent of traffic on my Blogger blogs are search engine visitors to start with, so I’m unlikely to get smart priced.
If one of my Blogger blogs morphs into something similar to this blog, with regular readers and social traffic, then I’ll definitely shift all of the Adsense units into the template and show them only to search engine visitors.
I’d highly recommend you do some testing (remembering it takes about a week for smart pricing to be removed) and base your decision on that. Remember, this is all about making money online.
The IFrame Solution
After writing much of this post, I came across an alternative solution from Rhys, which places Adsense within an IFrame. This gets around the problems mentioned above and should allow you to use the HTML/JavaScript widget.
However, although this is much easier, I’m slightly hesitant to recommend it. It seems to me that when the Adsense bot goes looking at the page that the Adsense unit is displayed on, it will crawl the IFrame, rather than the real page, meaning the ads may not be relevant. However, it seems to work for Rhys, so I’ll leave you to make up your own mind.
Final Thoughts
Google Adsense is one of the best ways to make money online, but you have to make sure that you’re not smart priced. Don’t just believe what I’m saying – go and test it out. It’s definitely worth looking into, because it can help you make extra money.
Tags: Adsense, blogger, javascript, make money online, smart pricing

