CODEGATOR

.NET info worth sinking your teeth into!
Welcome to CODEGATOR Sign in | Join | Help
in Search

Martin Cook

Yet another C# developer with a blog.

Embedding adsense units inside CS 2007 blog posts

I was poking around today trying to figure out a way to add Google AdSense units to my blog posts. Even though I don't try to make money with CODEGATOR I still need to pay the hosting fees from GoDaddy, and every little bit of $$$ helps in that regard, so I decided to embed a couple of ad units into each blog post in the hope that someone might take pity on me and actually click on one of my advertisements every now and then. Cool Anyway, here is what I went through today to get this to work.

Since CODEGATOR is built on Community Server 2007 I naturally began my quest by checking out the forums on http://www.communityserver.org. The first thing I noticed was the InlineAdModule module that is part of the CSModule developer contest. After downloading the zipfile and installing it on my sandbox I quickly figured out that it doesn't work. Sad  I even went back to the forums several times to make sure I wasn't overlooking anything, but no, it doesn't work.

Next I found a post in the forums entitled 'Embedding AdSense unit within blog post' that seemed very promising. It started out with more info about the InlineAdModule but then switched to a technique wherein the Google AdSense HTML was posted directly into one or more master pages. That approach probably works but I just didn't want to bother with it. The biggest reason was that it makes modifying my advertisements difficult since I would have to manually edit master pages any time I wanted to make a change. Yuck!

As I was pondering my situation I hit upon another, slightly different technique - one that wouldn't require me to write code, and would still be fairly flexible at runtime. Here is what I did:

 

Step 1: My blogs all use the lean & green theme, so I started by modifying the Themes\Blogs\leanandgreen\post.aspx file. I added the following lines to the very top of the page:

<%@ Register TagPrefix="CSUserControl" TagName="AdTop" Src="Common/Ad-Top.ascx" %>
<%@ Register TagPrefix="CSUserControl" TagName="AdBottom" Src="Common/Ad-Bottom.ascx" %>

I added those two lines right under the one that looks like this:

<%@ Import namespace="CommunityServer.Blogs.Components"%>

 

Step 2: In the same file I then added the following lines:

<CSControl:AdPart runat = "Server" contentname="StandardBlogTop" ContentCssClass="CommonContentPartBorderOff" ContentHoverCssClass="CommonContentPartBorderOn">
     <DefaultContentTemplate>
         <CSUserControl:AdTop runat="server" />
     </DefaultContentTemplate>
 </CSControl:AdPart>

I added this section right after the line that looks like this:

<asp:Content ContentPlaceHolderID="bcr" runat="Server">

 

Step 3: Once more in the same file I added the following lines:

 <CSControl:AdPart ID="AdPart1" runat="Server" ContentName="StandardBlogBottom" ContentCssClass="CommonContentPartBorderOff" ContentHoverCssClass="CommonContentPartBorderOn">
     <DefaultContentTemplate>
         <CSUserControl:AdBottom ID="AdBottom1" runat="server" />
     </DefaultContentTemplate>
 </CSControl:AdPart>
 

Now, I had to play around with this section a little to get it to look right. I ended up putting it right before the section that produces the comments area for the post. The section right before where I added my text looked like this:

<CSBlog:PostAttachmentData runat="server" LinkTo="Attachment" Tag="Div" Property="FileName"><LeaderTemplate><span class="em">Attachment:</span> </LeaderTemplate></CSBlog:PostAttachmentData> 
        </div>                           
    </div>

[Here is where I added the text shown above]

Now, you might want to put the second AdSense section somewhere else, or your theme file might by slightly different than mine, so you may need to play with the positioning a little to get things right.

 

Step 4: I saved the modified post.aspx file then I copied these two files:

  • Themes\leanandgreen\Common\ad-bottom.ascx
  • Themes\leanandgreen\Common\ad-top.ascx

To a new directory that I created at: Themes\Blogs\leanandgreen\Common

 

Step 5: I then navigated into: Control Panel, Administration, System Tools, Manage/Create Articles and created two articles named: StandardBlogBottom & StandardBlogTop. Into each file I pasted the Google AdSense HTML that I obtained from Google's site. One of the nice things about this approach is that I didn't need to replace any characters in the HTML or otherwise reformat anything. 

Note that in order for this step to work you need to put the CS editor into "HTML" mode, which is done by clicking the button that looks like this:

If you don't put the editor into "HTML" mode it will try to convert what you paste from clipboard into HTML, which wont be pretty. Once the editor is in "HTML" mode you can paste the AdSense HTML without problems.

 

Step 6: Of course, you have to have advertisements turned on in order for this to work on your site. In order to do that, navigate to: Control Panel, Administration, System Tools, Manage Ads and check the boxes entitled: "Enable Ads" & "Enable Inline Ad Control".

 

Step 7: Enjoy your newly monitized blogging platform. Smile

 

One final note, the advertisements at the top and bottom of this page are the actual ones that I added using this technique. The whole thing took me about 30 minutes to figure out, complete, debug, and finalize. I really think this is a good way to tackle this problem because you can change your ads at any time without having to FTP into your website and modify master pages directly.

I hope this tip helps.

 

 

Technorati Tags:

 

Comments

 

SJ MAC said:

Thanks for this, its a great tutorial on how to embed adsense in blogs. Its the sort of documentation that is lacking on the CS website, thank you. I have one problem though, I followed all your steps and its worked (I think) but instead of an ad showing up I just get the adsense code. This is because CS blocks all html in blogs, its a real pain in the *** that I've been trying to fix but I cant seem to get round the problem. You obviously did, have you got a tutorial on how you did that? or can you point me in the direction of where I can find out how to overcome this problem?

Thanks,

SJ MAC

July 30, 2007 8:47 AM
 

SJ MAC said:

Sorry, problem fixed! I entered the adsense code into each article when I created the articles in the control panel. It didnt work for some reason but deleted the code and re-entered it when I was on the actual blog, the same way you'd enter the code in the other site-wide adplace holders.

Thanks, again. Great article!

July 30, 2007 8:58 AM
 

Martin said:

Thanks.

I'm glad it worked for you.

July 30, 2007 9:26 AM
 

vanne said:

Great tutorial. Keep it up! Feel free to drop by at www.edisonlim.com

July 31, 2007 7:42 PM
 

Allie said:

I've been struggling with this and I'm eager to give a try.  I haven't been able to locate this information anywhere specific to godaddy blogs. I'll let you know about my success. Thanks for the clarity.

August 16, 2007 1:23 PM
 

Kate said:

Thanks for this! Worked wonderfully!

August 22, 2007 11:27 PM
 

JetSetterJay said:

You sir are an absolute genius! Thanks - worked perfectly with your perfect instructions!

November 3, 2007 1:01 AM
 

JetSetterJay said:

Very odd - I have changed the theme on my page and changed the files as mentioned above. But after the changes it comes up saying that the blog I had previously created cannot be found? I have checked over and over to make sure that I changed the files correctly and I have. Any ideas?

November 27, 2007 1:33 AM
 

Martin said:

JetSetterJay, anything you did under the "Themes\Blogs\leanandgreen\" folder will need to also be done for your new theme. Just follow this tutorial again and substitute the "Themes\Blogs\your theme name here\" folder for the "Themes\Blogs\leanandgreen\" folder. I hope that makes sense.

November 30, 2007 8:21 PM
 

lowster said:

I know this is an old post, but thanks so much!!

June 11, 2008 7:29 PM
 

Martin said:

Hello lowster,

You are most welcome. I'm glad to be of assistance. Thank you for reading my little blog.

June 12, 2008 5:32 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Martin

I work as a software engineer specializing in designing and building object-oriented business solutions for Windows platforms using C#. I have been programming professionally for roughly 20 years.

This Blog

Syndication

Terms of Service | Privacy Statement