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

Sincerely,

Sean
The Google AdSense Team

So here is how we are going to parse the HTML to make it compatable 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>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

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 = "";
//--&gt;
&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&gt;
&lt;/script&gt;

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

No comments:

Post a Comment