HOWTO: Add Google AdSense to topics within InstantForum.NET

Expand / Collapse
 
     

Add Google AdSense to topics within InstantForum.NET


This article details the steps necessary to automatically add Google AdSense ads within InstantForum.NET topics. Although you can easily display Google AdSense through the wrapper functionality without any code changes adding your AdSense advertisements in-line within posts will increase your AdSense visibility and hopefully click-throughs.

You could easily extend this example to append the Google AdSense code to each post or use the topic keywords to determine the ads displayed. We've kept things simple in this example and are showing ads based on our AdSense ClientID. We do intend to improve support in this area with future versions to provide several methods for serving your ads with no code changes.

Add Google AdSense Ads Inline


Using the code in this example will allow you to add AdSense Ads automatically as the second post within any topic within InstantForum.NET. This is shown below...



Code Changes (Post.vb)


This code can be applied to either the End User License or Developer License. You'll only need to modify a single code-behind file.

  1. Open up InstantASP.InstantForum.UI\Pages\Post.vb within Visual Studio.NET
  2. Locate the "InsertPost" method around line 660. We'll need to add a call to our new methods here.
  3. Below the InsertPost method add the following code. You can also find this code in the attached file below.

    InsertAdSensePost - This method actually adds the post to the InstantForum.NET database.

    Private Function InsertAdSensePost(ByVal intTopicID As Int32) As Int32

        ' get adsense post
        Dim AdSensePost As InstantASP.InstantForum.Components.Topic = GetGoogleAdSensePost(intTopicID)

        ' add the post as a reply to our topic
        Dim Topic As InstantASP.InstantForum.Components.Topic = _
       InstantASP.InstantForum.Business.Posts.InsertPost( _
       CType(ForumID.Value, Int32), _
       AdSensePost.TopicID, _
       AdSensePost.ParentID, _
       AdSensePost.UserID, _
       ctlPostControl.MessageIcon, ctlPostControl.Subject, _
       "", AdSensePost.Message, "", False, CurrentContext)

        If Not Topic Is Nothing Then
            Return Topic.PostID
        Else
            Return 0
        End If

    End Function

    GetGoogleAdSensePost - This method simply returns a InstantASP.InstantForum.Components.Topic object representing the Google AdSense post we wish to add/ You should modify the ClientID and AdSense client side code below to suite your requirements.

    Private Function GetGoogleAdSensePost(ByVal intTopicID As Int32) As InstantASP.InstantForum.Components.Topic

        ' IMPORTANT: Your AdSense Client ID
        Dim strAdSenseClientID As String = "pub-XXXXXXXXXXXXXX"

        '  ----------
        ' The actual Adsense Code that will be added to our post
        ' You can modify this to suite your requirements
        ' ----------
        Dim sb As New System.Text.StringBuilder
        sb.Append("<script type=""text/javascript""> ")
        sb.Append("google_ad_client = """ + strAdSenseClientID + """; ")
        sb.Append("google_alternate_color = ""D9DAD5""; ")
        sb.Append("google_ad_width = 468; google_ad_height = 60; ")
        sb.Append("google_ad_format = ""234x60_as""; ")
        sb.Append("google_ad_type = ""text""; ")
        sb.Append("google_ad_channel =""""; ")
        sb.Append("google_color_border = ""676B56""; ")
        sb.Append("google_color_bg = ""D9DAD5""; ")
        sb.Append("google_color_link = ""800000""; ")
        sb.Append("google_color_url = ""800000""; ")
        sb.Append("goggle_color_text = ""333333""; ")
        sb.Append("</script>")
        sb.Append("<script type=""text/javascript"" src=""http://pagead2.googlesyndication.com/pagead/show_ads.js""></script></div>")

        ' create an instance of our anonymous user, you'll use this user for the adsense post
        Dim User As New InstantASP.InstantForum.Components.AnonymousUser

        Dim AdSensePost As New InstantASP.InstantForum.Components.Topic
        AdSensePost.TopicID = intTopicID
        AdSensePost.ParentID = intTopicID
        AdSensePost.UserID = User.UserID
        AdSensePost.Username = User.Username
        AdSensePost.Message = sb.ToString()

        ' return our topic object
        Return AdSensePost

    End Function

  4. Once you've added the two methods above simply add a call to InsertAdSensePost from our existing InsertPost method...

    Private Function InsertPost() As Int32

        ' add post to database, we indicate this is not a poll below
        ' if successfull a topic object is returned with new post
        Dim Topic As InstantASP.InstantForum.Components.Topic = _
        InstantASP.InstantForum.Business.Posts.InsertPost(CType(ForumID.Value, Int32), _
        CType(TopicID.Value, Int32), CType(ParentID.Value, Int32), _
        MyBase.CurrentContext.CurrentUser.UserID, _
        ctlPostControl.MessageIcon, ctlPostControl.Subject, _
        ctlPostControl.Description, ctlPostControl.Message, _
        ctlPostControl.AttachmentGUID, False, CurrentContext)

        ' was post insert ok
        If Not Topic Is Nothing Then

            ' do we have a post
            If Topic.PostID > 0 Then

                ' call our method to add a adsense post
                InsertAdSensePost(Topic.PostID)

    - we've removed the rest of the code for readability.


  5. REbuild your solution within Visual Sutdio.NET and visit your forum installation. Add a new test topic and you should see your Google AdSense ad displayed below the first post in your topic.
That's It!

Making these changes to Post.vb will ensure a Google AdSense ad always appears as the second post within any topic within InstantForum.NET. I hope this helps generate a little extra income from your community.

If you have any questions don't hesitate to leave some comments below or contact us.


Attachments


GoogleAdSenseMethods.zip GoogleAdSenseMethods.zip (1.09 KB, 153 views)

User Comments

Click to subscribe to comments RSS feed...

No Member Photo


Great idea. I may look at adding something like this. However, I would like to filter out those posts so that only non-members see them.
Marked helpful 1 time based on 1 vote.
Helpful? YesYes NoNo

Add Your Comments


Name: *
Email Address:
Web Address:
Verification Code:
*
 

Details
Product: InstantForum.NET
Version: 4.1
Type: HOWTO
Level: Beginner
Rated 5 stars based on 1 vote.
Article has been viewed 8,038 times.
Last Modified:9th June 2008
Last Modified By: Ryan Healey
Options

Home | Products | Purchase | Support | Company | Contact Us
Privacy Statement | © 1999-2007 InstantASP Limited. All Rights Reserved.