As an AdSense and Blogger user, I think that using ads in Blogger could be more efficient, although it's very easy to use.
Blogger fully supports AdSense in its mechanism, however I think that we could have a little more power over the places we want to allocate them, for example in a middle of a post, right above the header; at the end of the post, etc, etc.
You can do the basics with Blogger AdSense support, you can place an ad at every X posts, customize colors, pick a format, but for my needs I think it was missing something.
So I decided to implement a solution in javascript, basically you generate all the ads in your Google AdSense Manager, and with the help of this simple script you can place your ads right where you want to.
This solution will be a little more complicated to implement, because you have to change your blogger script, so
please, first of all:
MAKE A BACKUP !
Change your blogger script with caution, and it's never too much to remember,
backup's a friend :)
So as I already said, we'll make a backup of our current script and activate a checkbox called: "
Expand Widget Templates", this will allow us to see a more detailed script so that we can update it to our needs. Here's a picture that illustrates what I'm talking:
This is a rather simple script, I'll implement two different solutions to our ads:
- 1st - you'll be able to print what ad you want;
- 2nd - you'll be able to print all the ads in a alternate way, for example you have 2 ads and 6 posts per page, you'll be able to have a different ad, each post;
Of course if you only have 1 ad, the options above will do exactly the same, since it's the same that will be printed.
I'll give you a more detailed example about the 2nd solution.
I have a blog in which I use 2 types of ads:
As you know each ad has a limit, for example you can have a maximum of 3 link and ad units per page (at the moment I'm writing this snippet), in my blog I use a system of 5 posts per page, so in each post I have a different type of ad.
The 1st post has an ad unit, the 2nd post a link unit, and so on, and so on, this will simple allow me to go alternating the ads, but this is just my case, I made the decision of using this ad technique, you could have yours.
This is just when you're listing your blog contents. When you're in a post, you can use alternating ads too, for example you can have an ad below the post title and/or another at the end.
The other technique is to use a specific ad, you can use a specific type of ad at a position, other ad type at other position.
As you can easily guess, you can put ads wherever you want to, in the post list, in a post, in the sidebar.
But enough (and maybe confusing :)) talking!
When you generate a script, Google will provide you something like this:
<script type="text/javascript"><!--
google_ad_client = "pub-1234567890123456";
/* our ad comment */
google_ad_slot = "9876543210";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
Please take a special attention to these javascript variables:
- google_ad_client;
- google_ad_slot;
- google_ad_width;
- google_ad_height;
- and our ad comment (text between /* */), although the comment's not strictly necessary;
We'll use these
values in this script, this will instruct Google with our account information and ad type. I'll explain further, how to use these variables.
But where do you put this script? You have to edit your template and activate the checkbox "
Expand Widget Templates", here's the
picture again:
You can place this code right before you
</head> tag, or in other position, but please rest assure that the code's inside your
<head> tag.
Here's our javascript class:
<script type="text/javascript">
//<![CDATA[
/**
* A support class to Adsense in Blogger
*
* NOTE: the author of this script takes no responsibility, whatsoever,
* for what you do with this script.
*
* Use, this script, at your own risk.
* You take sole responsibility for its use.
*
* author: pedrocorreia.net
**/
MyAdSenseBlogger = function(){
//_settings
var _settings = {
_post_count: 1, //post count for AlternateAd
_ad_count: 0 //number of ads, this field's filled automatically
}
//our ads
var _my_ads = {
ad1 : { //1st ad
client: "pub-1234567890123456",
comment: "this is my ad unit for blogspot",
slot: "1234567890",
width: "468",
height: "60"
},
ad2 : { //2nd ad
client: "pub-6543210987654321",
comment: "this is my link unit for blogger",
slot: "0123456789",
width: "468",
height: "15"
}
//other ads ....
} //end _my_ads
/**
* Private method
*
* Number of ads
* @return Int
*/
var _Length = function(){
var count=0;
for (var i in _my_ads){count++;}
return count;
} //end _Length
/**
* Private method
*
* Get a specific Ad
*
* @return Object Ad
**/
var _GetSpecificAd = function(pos) {
var count=1;
for (var ad in _my_ads){
if(pos==count) {return ad;}
count++;
}
return;
} //end _GetSpecificAd
//number of ads
_settings._ad_count = _Length();
/**
* Private method
*
* Method that will build compose the Ad javascript
*
* @param String Client Ad
* @param String Comment Ad
* @param String Slot Ad
* @param String Slot Width
* @param String Slot Height
* @return Object
**/
var _BuildAd = function(ad_client, ad_comment, ad_slot, ad_width, ad_height){
return {
ad:[
"<script type=\"text\/javascript\"><!--",
"google_ad_client = \""+ ad_client + "\";",
"/* " + ad_comment + " */",
"google_ad_slot = \"" + ad_slot + "\";",
"google_ad_width = " + ad_width +";",
"google_ad_height = " + ad_height + ";",
"\/\/-->",
"<\/script>",
"<script type=\"text\/javascript\" src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\"><\/script>"
]
}
} //end _BuildAd
/**
* Output the Ad
*
* @param Object Our ad
**/
this.ShowAd = function(ad){
var adsense = _my_ads[ad] || null;
if (adsense){
var my_ad = _BuildAd(adsense.client, adsense.comment, adsense.slot, adsense.width, adsense.height);
for (var i=0, count=my_ad.ad.length;i<count;i++){
document.writeln(my_ad.ad[i]);
}
}
} //end ShowAd
/**
* Cycle through our ads and print each one
*
* @param Object Our Ad
**/
this.AlternateAd = function(){
this.ShowAd(_GetSpecificAd(_settings._post_count));
_settings._post_count++;
//check if we reached our maximum ads
if(_settings._post_count>_settings._ad_count){
_settings._post_count=1; //reset counter
}
} //end AlternateAd
} //end MyAdSenseBloggerAlternate
//create our object
var myAdsense = new MyAdSenseBlogger();
//end script MyAdSenseBlogger
//]]>
</script>
Please take a special attention to this section:
//our ads
var _my_ads = {
ad1 : { //1st ad
client: "pub-1234567890123456",
comment: "this is my ad unit for blogspot",
slot: "1234567890",
width: "468",
height: "60"
},
ad2 : { //2nd ad
client: "pub-6543210987654321",
comment: "this is my link unit for blogger",
slot: "0123456789",
width: "468",
height: "15"
}
//other ads ....
} //end _my_ads
NOTE: You have to change it to suit your needs! I just made up this values, here you'll have to put your codes, and you can add or subtract ads.
However in this example I have 2 ads:
Each ad has 5 properties:
- client;
- comment;
- slot;
- width;
- height;
Well, this just remembers us of our AdSense code provided by Google:
google_ad_client = "pub-1234567890123456";
/* this is my ad unit for blogspot */
google_ad_slot = "9876543210";
google_ad_width = 468;
google_ad_height = 60;
So for example we can add a 3rd ad, just take a special attention to the syntax.
Please take also a note to your ads name, in here I used "ad1", "ad2", but this is just a name, you can give it any other name, just as long as it's valid one.
As an advice use short names, without spaces or punctuation. This is important for our function ShowAd, because in here we can specificy what ad we want to print.
So now that you configured successfully your ads, though, they'll still not appearing ...
Now we'll have to print them, and well, this is just up to your decision, you
call the shots in here!
To print your ads, you can simple type:
<script type="text/javascript">
myAdsense.ShowAd("ad1");
</script>
Just make sure that "ad1" exists in our object "_my_ads", so, for example, if you have a ad named "adlink1", you have to call myAdsense.ShowAd("adlink1"); and so on.
Here's my blog example:
<div class='post-body entry-content'>
<b:if cond='data:blog.pageType == "item"'>
<script type='text/javascript'>
myAdsense.AlternateAd();
</script>
<p> </p>
<p><data:post.body/></p>
<p> </p>
<script type='text/javascript'>
myAdsense.AlternateAd();
</script>
<b:else/>
<script type='text/javascript'>
myAdsense.AlternateAd();
</script>
<p> </p>
<p><data:post.body/></p>
</b:if>
</div>
In this example I just use the function AlternateAd, but of course you can use ShowAd too or even
mixing them!
When listing the blog posts, we'll have each ad post alternating, when we're inside/ viewing a specific post it'll print an ad below post title and place an ad at post ending.
So for example if you want to insert an ad in the middle of your post you can easily call the javascript function.
<script type="text/javascript">
myAdsense.ShowAd("ad1");
</script>
If you want to add ads to the sidebar, just add a "HTML/JavaScript Add Gadget" and place the javascript, just like the following example:
Just a few notes:
- please backup always your blogger script before making any changes!
- take a good strategy about ads, in this example I explained mine; of course this is just an exemple, follow your needs and directions;
- I red a lot of comments about using this technique, because of Google AdSense TOS; some were saying that this will take your account to be closed, since TOS don't allow you to change your AdSense code; and many others were saying that this is completely safe to use, because you don't change anything, ou AdSense code will be printed exactly like you received, only in a different manner, so this leads me to a final note: if you use this script, please bare in mind, that you're using at your own risk, I cannot take any responsibility about what you do with this code;
If you have any doubt or found any error, please drop me an email