Display Ads Only To Search Engine Visitors On Blogger Blog

I have previously written about displaying ads only to search engine traffic by using ad injection plugin for WordPress. But many people have asked me, if this is possible for blogger or not? The answer is yes, its possible.

In WordPress its as easy as clicking few buttons to setup all this ‘Display ads to search engine visitor’ stuff. But in blogger, you don’t have plugins, neither you can use php code. So the only way you can set up this ‘Ads to organic traffic’ thing is by using JavaScript.

In this post, I’ll show you how to display ads only to search engine visitors on your Blogger blog using some JavaScript. For this you’ll need to edit the blogger template.

There are two parts of code which you’ll need –

  1. Code to detect search engine visitor and set up ad units (Main Code)
  2. Code to Display Ad Units

Main Code

[javascript]

<script type=’text/javascript’>
//<![CDATA[

// codefap-sevisitor-blogger

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it’s in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + “=” +escape( value ) +
( ( expires ) ? “;expires=” + expires_date.toGMTString() : “” ) +
( ( path ) ? “;path=” + path : “” ) +
( ( domain ) ? “;domain=” + domain : “” ) +
( ( secure ) ? “;secure” : “” );
}

function Get_Cookie( check_name ) {
// first we’ll split this cookie up into name/value pairs
// note: document.cookie only returns name=value, not the other components
var a_all_cookies = document.cookie.split( ‘;’ );
var a_temp_cookie = ”;
var cookie_name = ”;
var cookie_value = ”;
var b_cookie_found = false; // set boolean t/f default f

for ( i = 0; i < a_all_cookies.length; i++ )
{
// now we’ll split apart each name=value pair
a_temp_cookie = a_all_cookies[i].split( ‘=’ );
// and trim left/right whitespace while we’re at it
cookie_name = a_temp_cookie[0].replace(/^s+|s+$/g, ”);

// if the extracted name matches passed check_name
if ( cookie_name == check_name )
{
b_cookie_found = true;
// we need to handle case where cookie has no value but exists (no = sign, that is):
if ( a_temp_cookie.length > 1 )
{
cookie_value = unescape( a_temp_cookie[1].replace(/^s+|s+$/g, ”) );
}
// note that in cases where cookie is initialized but no value, null is returned
return cookie_value;
break;
}
a_temp_cookie = null;
cookie_name = ”;
}
if ( !b_cookie_found )
{
return null;
}
}

var ref = document.referrer;
var sevisitor;
var SE = new Array(‘web.info.com’, ‘search.’, ‘del.icio.us/search’, ‘soso.com’, ‘.yahoo.’,’/url’,’/search’,’.google.’,’.ask.’,’.baidu.’,’.bing.’,’.aol.’,’.answers.’);
//document.write(‘<!–‘+ref+’–>’);
for ( var source in SE){
var match = ref.indexOf(SE[source]);
if (match !=-1) {
Set_Cookie(“sevisitor”, 1, “”, “/”, “”,””);
sevisitor=true;
}
}

function adunit1(){
if (Get_Cookie(‘sevisitor’) || sevisitor){
var large = ‘<script type=”text/javascript”><!– n
google_ad_client = “ca-pub-xxxxxxxxxxxxxxxx”; n
/* Sidebar Top Box */ n
google_ad_slot = “xxxxxxxxxx”; n
google_ad_width = 336; n
google_ad_height = 280; n
//–> n
</script> n
<script type=”text/javascript” src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”> n
</script> n
‘;
document.write(large);
}
}

//]]>
</script>

[/javascript]

The code given above will help you to detect a search engine visitor to your Blog. Also at the end of the code you can find a function called as adunit1.

Copy the code given above into a notepad file. And make appropriate changes to the adunit1 function at the end of the code. 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).

Where To Add ‘Main Code’ ?

  1. Go to Blogger Dashboard > Template > Edit HTML
  2. Find <title><data:blog.pageTitle/></title>
  3. And just below it copy paste the Main Code
  4. Save Template

Check the screenshot given below. The main code (which is highlighted) is directly below <title><data:blog.pageTitle/></title>. This is how it should look like when you add the code to your Blogger template.

Adsense Main Code Blogger

Code to Display Ad

[javascript]

<script type=”text/javascript”>adunit1();</script>

[/javascript]

This is the code you’ll need to display the adunit1 which you added in main code. So basically all ad unit codes go inside the main code in the form of a JavaScript function. And wherever you need to display that particular ad unit, you’ll just add a JavaScript function call, which will help to display that ad unit.

Where to Add ‘Code To Display Ad’ ?

You can add this code in your template itself or you can even use it inside a Blogger Gadget. To use it inside Blogger Gadget follow the steps given below –

  1. Go to Blogger Dashboard > Layout
  2. Click on Add a Gadget button depending on where you would like to display the Ad.
  3. Now choose HTML/JavaScript from Gadgets list.
  4. Simple insert the code to display ad in content box
  5. Save the Gadget.

Check if it works

Find your website by using Google Search and visit your site from the search results. If you have followed everything properly then you must see your Ad when you visit the site from a search engine. Good luck.

If you have any problems, feel free to comment below. I’ll try to help.

Adding Multiple Ad Units

You can add multiple ad units by duplicating the adunit1 function in the main code and naming the new function as adunit2. Then you can call the new function by adunit2 function call in a new Blogger Gadget.

Note: Adding more than 3 ad units on single page is against Google AdSense Policies, so don’t do that.

Posts based on Organic Traffic –

  1. Increase AdSense CPC By Displaying Ads Only To Organic Traffic
  2. Using Ad Injection To Show Ads Only To Search Engine Visitors (WordPress)
  3. Display Ads Only To Search Engine Visitors On Blogger Blog
  • Thank You very much Omkar.. It is helping… Thankx again.

  • tejas

    if we using this that its allowd on adsense policy ?????

    • It isn’t against the Adsense Policy. Don’t worry.

  • pooja kumari

    how to increse my cpc…?