12 March, 2010

Insert Adsense Block in the Middle of the post-Blogger

 How to place Adsense in the middle of Blogger posts.
The solution to this problem is quite simple: you only need to move the post body around the advertising code, just with a bunch of JavaScript and a couple of DIVs (divisions).
Works For both Old blogger and New Blogger.

1. In your Blogger template:


Find the tag <$BlogItemBody$> and replace it with following code:

<div id="prv<$BlogItemNumber$>"></div>

<div style="clear:both;margin:10px 0">
    <!-- Your AdSense code -->

<div id="fst<$BlogItemNumber$>">

<script type="text/javascript">
var obj0=document.getElementById("prv<$BlogItemNumber$>");
var obj1=document.getElementById("fst<$BlogItemNumber$>");
var s=obj1.innerHTML;
var r=s.search(/\x3C!-- adsense --\x3E/igm);
if(r>0) {obj0.innerHTML=s.substr(0,r);obj1.innerHTML=s.substr(r+16);}


Find the tag <data:post.body/> (you must check Expand Widget Templates in Blogger console) and replace it with following code:

<div expr:id='"aim1" + data:post.id'></div>

<div style="clear:both; margin:10px 0">
    <!-- Your AdSense code here -->

<div expr:id='"aim2" + data:post.id'>

<script type="text/javascript">
var obj0=document.getElementById("aim1<data:post.id/>");
var obj1=document.getElementById("aim2<data:post.id/>");
var s=obj1.innerHTML;
var r=s.search(/\x3C!-- adsense --\x3E/igm);
if(r>0) {obj0.innerHTML=s.substr(0,r);obj1.innerHTML=s.substr(r+16);}

Note that you must replace <!-- Your AdSense code --> with block code provided by Google.

2. In every blog post:

Put a special comment
<!-- adsense -->
in the exact point you want to appear AdSense advertising.

  1. Does this method violate Google ToS?

    No. Answering a direct support request Google stated this method doesn't violate any of their program policies.
  2. What if I forget to put in a post the <!-- adsense --> comment?

    Nothing. The advertising block will appear in the traditional position you defined in your template before implementing this modification.
  3. What happens in Blogger home page and archive pages where there several posts with <!-- adsense --> comment appears together?

    Exactly the same thing happening before this modification: Google automatically inject in your posts up to 3 AdSense blocks. So just the first 3 posts (in a top-down order) in your home page or archive pages will have ads.


  1. it gives me a blank space above my every post

  2. Anonymous that space is for ads....there is nothing wrong with that.

    Try and let me know if it was successful for you

  3. Hi I have the same space problem.. ads never show up... ever?

    Please contact me at curiousread [at] gmail.com



Spam/advertising comments will NEVER BE TOLERATED and will be deleted immediately!

I advise you to also "subscribe to the comment feed" and get email updates when I respond to your question.
Thanks for reading,