Tuesday, December 2, 2008

How To Display Ads Only To Search Visitors

In the past, I’ve written about only showing Google Adsense to search engine visitors, so as to decrease the chance of being smart priced. In this post, you’ll see how to improve the technique outlined previously, so that you can make more money.

Note: Although this post is about showing Adsense to search engine visitors, it’s really about determining whether a user came from a search engine or not. The technique can be used to show search engine visitors whatever you choose, not just Adsense.

Background On Smart Pricing

By now, most people know that Smart Pricing is a penalty Google applies to Adsense accounts that don’t convert well for the advertiser, resulting in you earning only about 10% of what you’d normally earn per click.

I’m not going to go into more detail than is absolutely necessary in this post, so if you want more information on Smart Pricing, see my post on how smart pricing may cost you money or for the ultimate description, see Grizzly’s Optimization Tips for Adsense.

For now, you just need to know that only certain visitors are going to click Adsense ads and buy something from the advertiser. Who? It’s not your regular readers. It’s not the stream of visitors from StumbleUpon, Digg or Sphinn. It’s the search engine visitors.

Search engine visitors provide targeted traffic for the advertiser, which converts well. The other sources don’t. If the majority of your traffic comes from the social news sites, then you’re in danger of being smart priced. That’s why I chose to show Adsense only to search engine visitors.

The Original “From Search” Function

In my original Shylock Adsense Plugin - Hack To Avoid Smart Pricing post, I provided two approaches: one based on hacking just the Shylock Adsense plugin; another on a more generic solution that can be called from wherever you want (within the Shylock Adsense plugin, the sidebar, single.php, etc).

In this post, I’m using the second approach as it has a wider application. This was originally explained in the Hacking Shylock Adsense AND Sidebars section of the previous post. This worked by adding the following function to functions.php in your theme’s folder (wp-content/themes/yourtheme folder, where yourtheme is the name of your theme):

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

Note: This function is based on the ‘only show search engine’ functionality from the Who Sees Ads plugin.

This function was then called from the Shylock Adsense plugin, a sidebar widget, or anywhere else you may want to, via the following code:

if (function_exists('scratch99_fromasearchengine')) {
if (scratch99_fromasearchengine()) {
INSERT YOUR CODE HERE
}
}

Obviously, INSERT YOUR CODE HERE needs to be replaced with whatever code you want to show the search engine visitors: your Adsense code if it’s in the sidebar (using the ExecPHP widget) or the Shylock Adsense code (see my original post for more details).

Problem With The Original Function

As Zath pointed out in the comments, the original function only works on the first page the visitor lands on. If the visitor subsequently navigates to another page on your site, the ads disappear. This is because the http_referer is no longer the search engine, it’s now the page where they landed on your site.

Of course, that means they no longer have the option to click an ad before leaving the site. I wasn’t worried about this, because I figured not many search engine visitors would go to another page on my site. I was wrong!

Leaving Money On The Table

According to Google Analytics, over the last month, visitors arriving at this site from a search engine read 1.54 pages per visit. That means that every second visitor is clicking another page.

Every second visitor! That means I could be serving up 50% more ads to the sort of visitor who is likely to click ads!

I decided I better change the way I was doing things, so that search engine visitors see ads on every page they visit at my site. Fortunately, this is fairly easy to do, by setting a cookie that identifies search engine visitors.

New “From Search” Function Using Cookie

Here is the solution I’m now using on this blog.

First, the you need to add some code to functions.php in your theme’s folder (wp-content/themes/yourtheme folder, where yourtheme is the name of your theme). The following should be added immediately before the ?> at the bottom of the file:

$ref = $_SERVER['HTTP_REFERER'];
$SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');
foreach ($SE as $source) {
if (strpos($ref,$source)!==false) {
setcookie("sevisitor", 1, time()+3600, "/", ".scratch99.com");
$sevisitor=true;
}
}

function fromasearchengine(){
global $sevisitor;
if ($sevisitor==true || $_COOKIE["sevisitor"]==1) {
return true;
}
return false;
}

Note: In the setcookie line, you must change “.scratch99.com” to your own domain!

The fromasearchengine function can then be called from wherever you want to use it (to check if a visitor is from a search engine or not). This is explained further below.

Notes About The New “From Search” Function

The code that detects whether the user is from a search engine has been moved out of the fromasearchengine function, into the body of functions.php. This means it will only run this code once per page load, rather than every time the function is called, which may be several times per page load (I call it from Shylock Adsense and from the sidebar).

Also, the $sevisitor variable was introduced because the cookie can’t be detected on the first page view. The cookie is set on the first page view, but the $_COOKIE function won’t see it until the next page view. The above code checks whether the $sevisitor variable is set (ie it’s the first page view) or whether the cookie is set (ie it’s a subsequent page view).

I had one problem with setting the cookie via WordPress, but it doesn’t affect the final solution above, so I leave that for a future post.

Calling The New “From Search” Function

To call the new fromasearchengine function from Shylock Adsense, edit the shylock_adsense.php file which comes with the plugin. Look for the following code (on line 325 in version 1.2):

function shylock_adsense_filter($content){
global $id,$user_level;
$output = $content;

and replace it with:

function shylock_adsense_filter($content){
global $id,$user_level;
$output = $content;

if (function_exists('fromasearchengine')) {
if (fromasearchengine()) {

Then go down to about line 364 and look for:

return $output;

and replace it with:

} } return $output;

Note: I’ve just noticed that the Shylock Adsense Plugin has been renamed to the WhyDoWork Adsense Plugin. The name change seems to be the only difference, so the line numbers above are still correct, but the function is called whydowork_adsense_filter instead of shylock_adsense_filter.

Alternatively, to call it from a PHP sidebar widget or somewhere else (such as single.php), you would add the following code in the appropriate place:

if (fromasearchengine()) { ?>
INSERT YOUR CODE HERE

replacing INSERT YOUR CODE HERE with your Adsense code or whatever else you wanted to show the search engine visitor.

Final Thoughts

Using this technique, to set a cookie to identify search engine visitors, then only serving Adsense to such visitors, should help you make more money, while avoiding being smart priced. However, as always, it pays to experiment with Adsense and monitor the affects on your income.

Of course this technique will have other applications as well, allowing you to serve whatever content you like to search engine visitors. I hope some of you find it of use.

No comments:

Post a Comment