Wednesday, April 25, 2007

Parsing AdSense HTML For Posts

I am going to teach you to add AdSense code to your posts. Since it can be an overwhelming process, I'm going to break it down into several posts.

This post will teach you how to make the code compatible. To use AdSense code inside a post it is necessary to "escape" the AdSense code. The reason: If you put the code into your template just as it is written when you display the source code for the page, the Google data is invisible...that does not make Google happy and it is a TOS violation of AdSense...and they will pull your account for that!

Adsense does not allow you to alter their code other than "minor" edits. what we are going to do is considered a "minor" edit.

I contacted AdSense with the following question:

Subject: Wrapping Adsense code in blog post
Date: Wed, 18 Apr 2007 09:12:15 -0700

I would like to put the adsense ads inside of a blog post. To make it look EXACTLY like you give it to me (when viewed as a Page source code) it is necessary for a minor alteration. For Blogger to parse the Adsense Code in XML template, the Javascript has to be escaped, using html entities...It looks EXACTLY like you gave it out when viewed by Page source, so is this permissable?

Adsense answered my email with this:

Hi Bonnie,

We do permit minor edits to the AdSense code to place inside the blog posts. Please keep in mind publisher may not alter any portion of the code or manually change the layout of the ads. For instance, publishers may not alter the ad height variable to mask the Ads by Google moniker.

Thanks for your cooperation.


The Google AdSense Team

So here is how we are going to parse the HTML to make it compatible and when viewed by using the Page Source code, it will look exactly as given to you by Adsense.

This is an example of a piece of Adsense code as given out by Adsense:

<script type="text/javascript"><!--
google_ad_client = "pub-XXXXXXXXXXXXXXXX";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
<script type="text/javascript"

You are going to change the (<) to (&lt;) and the (>) to (&gt;) and the quote marks ( " ) to (&quot;) They will all be HTML entities. The code will then look like this:

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = "pub-XXXXXXXXXXXXXXXX";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as&quot;;
google_ad_type = "text_image";
google_ad_channel = "";
&lt;script type=&quot;text/javascript&quot;

The next post will show you how to place this in your template!


  1. António Balbino Caldeira said...
    Dear Bonnie

    Sorry to intervene on this post but I couldn't find your e-mail.

    I read your post about the Blogger NavBar. I fear that if I follow your suggestion, maybe Blogger delets my blog. I think that's too risky.

    But I wonder if I can at least remove the search button from it, since I do not keep archives and do not want any reader to find older posts.

    Please, help!
    Bonnie Calhoun said...
    Hello Antonio,

    Blogger will not delete your blog for removing the Blogger NavBar. That is NOT at TOS violation, because when you click the "next blog" button, it will sometimes take you to pornographic blogs.

    Blogger can not force you to give access to evil places like that.

    If you notice ALL of my blogs have the Blogger Navbar removed, and I am an "official" Google designated Blog*Star. Which means that I am given permission by Google to dispense advice about Blogger.
    BrielEvan said...
    Hello Bonnie,

    I also put adsense code inside post body and it really helps. My adsense revenue are increasing. My friend ask me if this breaks google rules or not. I am glad that I found the answer here :). I write it here


    Free ebooks said...
    this post help me too much. i want to do something more .
    i want to limit no. of posts in label. like i want to limit no. of posts to 5 when anyone click on any label and to see more he can click on older posts . do you have any solution??
    Apple deals said...
    Really...really nice. I am so much helped with the post. I was looking some post like this for a long.

    Thanks a lot.
    Taylor said...
    Great article and easy to follow although I have one problem with the solution.

    Sure, the ad is placed where you want it to and how you want it. Then again, won't it be placed more than 3 times since it's done this way? More than 3 times as you know, breaks adsense TOS.

    Obviously it doesn't actually show up 3 times in the page, but won't adsense bots think you're posting it more than 3 times on your blog?

    -Taylor from
    Bonnie Calhoun said...
    LOL...the bots know how many to alow on a page!
    Maggie said...
    Hi, my name is Maggie and i am very new to the Blogger and i am not sure exactly how to paste the adsense code in to blogger blog,do i have to look into my HTML file and paste it they because i am not very sure about it,hope you can help,greatly appreciate.
    Bonnie Calhoun said...
    Hi Maggie, if you are really new to Blogger I wouldn't start messing with your code yet. Spend a little time learning more about HTML and CSS before you attempt this.

    Use the adsense widget that they give you as an 'add a page element' in the meantime!
    Maggie said...
    hello Bonnie,
    I guess is too late for me to alter my adsense code because when i went in to my Html file and searching for replacing my code(Absense code)again and is gone...and do you mind to look at my blog and i can give you my blog name.
    Bonnie Calhoun said...
    Don't worry about that the program fixed itself. To add Adsense go to any of your "add a page element" links. Click there. When the list opens up, click the adsense widget, and fill in the information. That widget, once it is placed on your Page elements page can be dragged and dropped whereever you want it to be.
    Biceps brachii said...

    Your article helped me to add AdSense code between the 'post end' and 'comments'.
    Thanks a lot!

    Greetings from Macedonia!
    Birdie said...
    Hi Bonnie - I'm so lost! I checked the help section & FAQs before coming to you in desperation. I want to use ad sense so I added it as a page element in my side bar and clicked save (my adblock is turned off on the blog) and it "says" that an adsense form will come up and I can fill it out. Problem is nothing comes up at all. Nothing. If I'm looking under Template there is no adsense tab either. It's as if that whole thing is missing somehow. I even went directly to adsense and tried to register for it there but when I put in my blog address it says I can't use anything to do with Google & that I have to have a site that allows me access to HTML. That made absolutely zero sense to me! Can you offer me any help?? Thanks!
    Bonnie Calhoun said...
    What is the address of your blog?

    Do you have a Google account?

    And lastly...if you can re-do what you did to get that message....copy the whole message here so that I can read it!
    Jimmy Zulhilmi said...
    I have a very important question here. Do I need to do similar thing for Adsense for Search, Adsense for Feeds, and Adsense for Domains?
    willy said...
    post ini membuat saya tenang
    saya memparse kode adsense dan saya tidak tahu apakah itu aman atau tidak
    Ajit said...
    Dear Bonnie,

    I did everything that you mentioned and it worked out fine. But I had an issue. The first three posts shows the Ads. But thereafter, there is a huge gap with no content on the other posts. These should be the places where my Ads should appear. I have no idea as to what I should do. Plz do help me out. Have a wonderful day!

    Bonnie Calhoun said...
    Adsense won't let you use an inline ad more than three times on a page. Go to settings and limit your posts to 3 on a page.
    Ike said...
    I have started parsing my adsense code. Before I paste it on the template of my blog, how do I make it appear on the left?
    Bonnie Calhoun said...
    I'm not understanding you. Why do you want to make it appear on the left?

    Left of what?
    ikert24kfan said...
    It's okay, I know how to do it and I am grateful to your post for making it simple. However I encountered a problem with my adsense account because when I tried to get the code, it was empty and I had to retrieve by clicking the inactive status which is making me nervous I believe something happened. Can you help me with this problem? You can send your advice at or Hoping for your quick and kind response to this problem.
    Stanly Johnson said...
    i know an easy way to change the whole adsense code. go to this page and paste your code there. it really work and have save lots of time..

Post a Comment