Specific Ads on Specific Pages

rated by 0 users
This post has 12 Replies | 3 Followers

Top 75 Contributor
Posts 301
Points 4,410
andrevs Posted: Thu, Mar 1 2007 3:21 PM

Hi, I've added an AdRotator to my Home page by editing Skin-Ads.ascx and Ads.Xml

These Ads are currently displaying on all pages of my CS. 

Is it possible to have AdRotators displaying specific ads on specific pages.(It's all 'custom ads' not Google Ads)

I would for example have certain advertisers who would like their add only to display on the forum default page or only at the top of a specific forum;

Anyone with ideas on how this can be done? 

Top 75 Contributor
Posts 301
Points 4,410
andrevs replied on Sat, Mar 3 2007 1:20 AM
anyone?
  • | Post Points: 20
Not Ranked
Posts 31
Points 515
aylwyn replied on Sun, Mar 4 2007 9:47 PM

Hey

I'm reckoning that if you've added an AdRotator to the page you know a bit about code...

 I guess that you could write some code that fires when the control is initialized to dynamically assign varying 'ads.xml' files according to the page's url.

Thus, if the page's url is 'x' then assign the AdRotator to use 'Ads.Group1.xml'
Else if the page's url is 'y' then assign the AdRotator to use 'Ads.Group2.xml'

If you don't want a page to show adds at all, or if you want a default group of adds to display it's easy to incorporate that into your logic. So go through the list of pages you want to customize, set the xml accordingly or tell it to disable the AdRotator. If it gets through this list then you don't want customization of ads on the current page so use your default ad group.

Does that make sense? Is it helpful?

Paul.

Top 75 Contributor
Posts 301
Points 4,410
andrevs replied on Mon, Mar 5 2007 9:13 AM

Thanks Paul

I understand what you are saying, but I am not sure how one would say "If current Page = X then..."

Any idea on how to do this?

I thought it might be possible to edit the specifc default pages (like forum or blog default) and add the adds to that page....that would however not solve getting specific add to dislpay on specific forums;

Maybe there is a way of doing this by using AdParts?

 Thanks for your help;

  • | Post Points: 20
Not Ranked
Posts 31
Points 515
aylwyn replied on Tue, Mar 6 2007 5:33 AM

Hi again

I actually implemented something like this for a client the day after I left my last on this thread...!

Okay - First of all I'm a VB guy so considering there's no code written in c# in the control it's okay to change it to VB. Also I'm a believer in code-behind and it's quicker and easier to use a code-behind file in which to write the code. The easiest way to do this is to create a NEW control (with a different name - say 'Skin-Ads2.ascx') and copy and paste the mark-up from the old .ascx file to the new one (that's everything appearing after the <%@ Control Language="C#"> declaration. Now simply delete the existing Skin-Ads.ascx and rename Skin-Ads2.ascx to Skin-Ads.ascx. We'll also want to be able to turn off the <div> element with the rotator in, so let's set it to run at the server and give it an id.

You're Skin-Ads.ascx  file shoud look like this:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Skin-Ads.ascx.vb" Inherits="Themes_default_Skins_SkinAds" %>
<span id="StandardTop" runat="server" visible="false">

<div id="AdLayer" align="center" style="padding-top:5px;padding-bottom:5px;" runat="server">
     <asp:AdRotator ID="AdRotator1" runat="server" />
</
div>
</
span>
<
span id="Square" runat="server" visible="false">
</
span>
<
span id="Skyscraper" runat="server" visible="false">
</
span>
<
span id="StandardBottom" runat="server" visible="false">
</
span>
<
span id="GoogleAdsense" runat="server" visible="false">
</
span>
<
span id="Inline" runat="server" visible="false">
</span>

Now you need to go to the code-behind file and add a proc for the Page_Init event in which to put your logic. Here's how I would implement your needs:

Partial Class Themes_default_Skins_SkinAds
Inherits System.Web.UI.UserControl

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

'Create a placeholder for the current page's url
Dim CurrentPage As String = Page.Request.Url.ToString.ToLower
'CurrentPage will look like:
'"http://www.yourdomain.com/pathtocommunityserver/blogs/default.aspx"
'You'll need to implement your own logic for finding the
'last part of the url to make it easier to deal with. I'd take
'out everything up to and inc the root of cs, i.e. the above
'would become "blogs/default.aspx"

'Use constants to define xml definition files
Const DefaultAds As String = "pathtoxml\AdRotator_AdsGroup1.xml"
Const BlogAds As String = "pathtoxml\AdRotator_AdsGroup2.xml"
Const ForumAds As String = "pathtoxml\AdRotator_AdsGroup3.xml"
Const ForumThreadSpecificAds As String = "pathtoxml\AdRotator_ComputerHardware.xml"
Const PhotoGalleryAds As String = "pathtoxml\AdRotator_PhotoGallery.xml"
Const DownloadsAds As String = "pathtoxml\AdRotator_Downloads.xml"

' --------------- Match individual pages --------------
' Do individual pages first so that they supersede matched by area

'Use generics to hold your add groups / page definitions
Dim CurrentPage2AdGroups As New Generic.Dictionary(Of String, String)

With CurrentPage2AdGroups
'Add individual pages here
.Add("blogs/default.aspx", BlogAds)
.Add(
"blogs/1781611/992829.aspx", BlogAds)
.Add(
"forums/default.aspx", ForumAds)
.Add(
"forums/177782928/default.aspx", ForumThreadSpecificAds)
End With

'Define a variable to hold our desired xml file location
Dim AdDefinitionXML As String = vbNullString

'Check match against individual pages
If CurrentPage2AdGroups.ContainsKey(CurrentPage) Then
AdDefinitionXML = CurrentPage2AdGroups(CurrentPage)
End If


'If a AdDefinitionXML is not null then we have a value and can set our
'AdRotator and exit the sub

If Not String.IsNullOrEmpty(AdDefinitionXML) Then
SetAdRotatorFile(AdDefinitionXML)
Exit Sub
End If

'Define a list of pages we don't want ads to show on
Dim NoAddPages As New Generic.List(Of String)
With NoAddPages
.Add(
"forums/282829/default.aspx")
.Add(
"downloads/989/1.aspx")
End With

If NoAddPages.Contains(CurrentPage) Then
DisableAdRotator()
Exit Sub
End If


' --------------- Match pages by area --------------
'If there are no matches for a specific page then look for
'matches by area

If CurrentPage.Contains("photos/") Then
SetAdRotatorFile(PhotoGalleryAds)
Exit Sub
ElseIf CurrentPage.Contains("downloads/") Then
SetAdRotatorFile(DownloadsAds)
Exit Sub
End If

'Are there areas we want NO ADS?
If CurrentPage.Contains("photos/") Then
DisableAdRotator()
Exit Sub
ElseIf CurrentPage.Contains("downloads/") Then
DisableAdRotator()
Exit Sub
End If


' ------------- Default Ads ---------------
'If we've gotten here then there are no pages or areas where
'we have defined either ads or no ads, so let's use the default set

SetAdRotatorFile(DefaultAds)

End Sub

Sub DisableAdRotator()
AdRotator1.Enabled =
False
'Don't think both the following are needed but do it anyway
AdLayer.Visible = False
AdLayer.Style.Clear() 'Get rid of the padding
End Sub

Sub SetAdRotatorFile(ByVal location As String)
AdRotator1.AdvertisementFile = location
End Sub

End
Class

 That should just about do it.

Just post back if you have any queries.

Paul.

  • | Post Points: 50
Top 10 Contributor
Posts 3,323
Points 73,040
MVPs
daveburke replied on Thu, Mar 8 2007 11:53 PM

Paul,

Thanks for posting this.  Apologies for it not showing up right away. It got tagged for moderation, but I released it. :-)

Regards,
Dave


 

  • | Post Points: 5
Top 75 Contributor
Posts 301
Points 4,410
andrevs replied on Fri, Mar 9 2007 12:33 PM

Thank you so much;

And thank you for all the comments to the code....I really appreciate it;

Great work!

  • | Post Points: 5
Top 75 Contributor
Posts 301
Points 4,410
andrevs replied on Sat, Mar 10 2007 7:25 AM

Paul, the code works great, thank you again;

Can you just help me with one part of it?

I added this to find the last part of the current page's url:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

'Create a placeholder for the current page's url

Dim CurrentPage As String = Page.Request.Url.ToString.ToLower

Dim x As Integer

x = Len(CurrentPage)

CurrentPage = Microsoft.VisualBasic.Right(CurrentPage, x - 30)

My path to CS has 30 characters

This works just fine for pages like "forums/default.aspx"

But as soon as I try a specific forum or thread like..."forums/16/ShowForum.aspx"....it doesn't work;

I did also add it to...

With CurrentPage2AdGroups

.Add("forums/16/ShowForum.aspx", SpecificAds)

End With

I must be missing something silly, or would you recommend using a different method to determine the

current page?

Regards

Andre

 

 

  • | Post Points: 20
Not Ranked
Posts 31
Points 515
aylwyn replied on Sat, Mar 10 2007 9:45 AM

Okay.

First of all (and I didn't make this clear in my code) I was explicity importing the current page's url into lowercase so as to avoid having to match against case as well as, well, path. I suspect that's all the problem is. So when you create your individual pages in the "CurrentPage2AdGroups" put them all in lowercase.

Better still put the following after the "With CurrentPage2AdGroups... End With" block:

dim KVP as Generic.KeyValuePair
for each KVP in CurrentPage2AdGroups
     KVP.value = KVP.value.ToLower
next

And also after the "With NoAddPages.... End With" add:

dim NoAddPage as string
for each NoAddPage in NoAddPages
     NoAddPage = NoAddPage.ToLower
next

This will ensure that all your strings are converted to lowercase for your comparisons.

Basically make sure all your comparisons are against lowercase strings. And a caveat - I created the above code by hand directly in the browser - I think it's fine but I didn't fire up Visual Studio to write it so it may not be perfect - but you get the gist right?

Also maybe an easier way of removing the stuff you don't want in the page's url, assuming that the url is http://www.mydomain.com/cs/forums/default.aspx:

Dim CurrentPage As String = Page.Request.Url.ToString.ToLower.Replace("http://www.mydomain.com/cs/", vbNullString)

Don't forget we want to remove the "/" before "forums/default.aspx".

Tell me how you got on.

Paul.

  • | Post Points: 20
Top 75 Contributor
Posts 301
Points 4,410
andrevs replied on Sun, Mar 11 2007 6:38 AM

Paul, it makes 100% sense;

So I changed the page in "CurrnetPage2AdGroups" to all lowercase...but it still doesn't work;

Surely it can't have anything to do with the number in the url..like the '11' in...

"forums/11/showforum.aspx"

I also tried it by changing the code to your suggestion on removing the stuff from the url...still no luck;

forums/default still works fine

Any idea what I am doing wrong?

  • | Post Points: 20
Not Ranked
Posts 31
Points 515
aylwyn replied on Sun, Mar 11 2007 6:58 AM

Hmmm.... Strange it works sometimes and not others.

Do you have Visual Studio? If you did then you could set a breakpoint on the bits that compare the pages against our selection criteria and see what's happening. You can download a free version (Visual Studio Express Web Developer) from Microsoft.

Try it and see. If it doesn't work then send me back a copy of the files and I'll see try to see what's going on. Not quite sure how much time I have though as I've an awful lot on. But I'll try .Smile

Paul.

  • | Post Points: 20
Top 75 Contributor
Posts 301
Points 4,410
andrevs replied on Tue, Mar 13 2007 2:39 PM

Paul thank you for taking a look at those files and ALL your help...it works great now;

I'll post the code (as you requested me to do) and the rest of this message for anyone else interested;

It turned out that the if you go to an specific forum the browser will display something like... http://www.mydomain/forums/11/ShowForum.aspx the but internally the url is changed to http://www.mydomain/forums/showforum.aspx?forumid=11

So Paul created a label, which can be disabled, to display the actual internal url;

This is the code for the Skin-Ads.ascx.vb

Partial Class Themes_default_Skins_SkinAds

Inherits System.Web.UI.UserControl

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

'Create a placeholder for the current page's url

Dim CurrentPage As String = Page.Request.Url.ToString.ToLower.Replace("http://www.mydomain.com/", vbNullString)

'This can be disabled after you've got the correct internal url

Dim ShowPageUrl As Boolean = False

If ShowPageUrl Then

Label1.Text = CurrentPage

Else

ShowPageLayer.Disabled = True

End If

 

'Use constants to define xml definition files

Const DefaultAds As String = "AdRotator_AdsGroup1.xml"

Const BlogAds As String = "AdRotator_AdsGroup2.xml"

' --------------- Match individual pages --------------

' Do individual pages first so that they supersede matched by area

'Use generics to hold your add groups / page definitions

Dim CurrentPage2AdGroups As New Generic.Dictionary(Of String, String)

With CurrentPage2AdGroups

'Add individual pages here

.Add("forums/default.aspx", BlogAds)

.Add("forums/showforum.aspx?forumid=11", BlogAds)

End With

'Check match against individual pages

Dim KVP As Generic.KeyValuePair(Of String, String)For Each KVP In CurrentPage2AdGroups

KVP.Value.ToLower()

If CurrentPage.Contains(KVP.Key) Then

SetAdRotatorFile(BlogAds)

Exit Sub

End If

Next

'Define a list of pages we don't want ads to show on

Dim NoAddPages As New Generic.List(Of String)

With NoAddPages

End With

Dim NoAddPage As String

For Each NoAddPage In NoAddPages

NoAddPage = NoAddPage.ToLower

Next

If NoAddPages.Contains(CurrentPage) Then

DisableAdRotator()

Exit Sub

End If

 

' --------------- Match pages by area --------------

'If there are no matches for a specific page then look for

'matches by area

If CurrentPage.Contains("forums/") Then

SetAdRotatorFile(DefaultAds)

Exit Sub

ElseIf CurrentPage.Contains("downloads/") Then

SetAdRotatorFile(DefaultAds)

Exit Sub

End If

'Are there areas we want NO ADS?

If CurrentPage.Contains("photos/") Then

DisableAdRotator()

Exit Sub

ElseIf CurrentPage.Contains("downloads/") Then

DisableAdRotator()

Exit Sub

End If

 

' ------------- Default Ads ---------------

'If we've gotten here then there are no pages or areas where

'we have defined either ads or no ads, so let's use the default set

SetAdRotatorFile(DefaultAds)

End Sub

Sub DisableAdRotator()

AdRotator1.Enabled = False

'Don't think both the following are needed but do it anyway

AdLayer.Visible = False

AdLayer.Style.Clear() 'Get rid of the padding

End Sub

Sub SetAdRotatorFile(ByVal location As String)

AdRotator1.AdvertisementFile = location

End Sub

End Class

 

Also add the label to Skin-Ads.ascx

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Skin-Ads.ascx.vb" Inherits="Themes_default_Skins_SkinAds" %>

<span id="StandardTop" runat="server" visible="false">

<div id="AdLayer" align="center" style="padding-top:5px;padding-bottom:5px;" runat="server">

<div id="ShowPageLayer" runat="Server"><asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /></div>

<asp:AdRotator ID="AdRotator1" runat="server" />

</div>

</span>

<span id="Square" runat="server" visible="false">

</span>

<span id="Skyscraper" runat="server" visible="false">

</span>

<span id="StandardBottom" runat="server" visible="false">

</span>

<span id="GoogleAdsense" runat="server" visible="false">

</span>

<span id="Inline" runat="server" visible="false">

</span>

It's a great functionality to have and I hope others find this usefull;

Thank you Paul!

  • | Post Points: 20
Top 500 Contributor
Posts 59
Points 880
rfritts replied on Wed, Mar 14 2007 4:24 PM

Why go through the trouble of lowercasing and comparing/replacing/etc...

Isn't that what regular expressions are for?
Page.Request.Url.ToString.ToLower.Replace("http://www.mydomain.com/", vbNullString)

could be done...
RegEx.Replace(Page.Request.Url.ToString(), "https?://[^/\\]*\.?mydomain\.com/?[\s$]", string.Empty, RegexOptions.IgnoreCase And RegexOptions.SingleLine)

Ya... I'm not a big fan of vbNullString either... try string.Empty instead...

  • | Post Points: 5