Friday 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:


OLD BLOGGER

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>

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

<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);}
</script>


NEW BLOGGER

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>

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

<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);}
</script>

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.
Done!


Questions
  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.

No comments:

Post a Comment