Code to Display Targeted Advertisements on an Asp.Net Web Form
Use the following code to create a targeting profile, create a user profile, run the content selector object, and display targeted advertisements on an ASP.NET Web form. The code should be used within a Commerce Server project. For more information about creating a Commerce Server project, see Creating a Commerce Project.
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime.Profiles" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime.Caching" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime.Targeting" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime.Pipelines" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime.Diagnostics" %>
<HTML>
<HEAD>
<script language="C#" >
public void ShowAds_OnClick(Object sender, EventArgs args)
{
CommerceContext csContext = CommerceContext.Current;
//
// Create a targeting profile.
//
csContext.TargetingSystem.TargetingContextProfile["PageGroup"].Value = "Sports";
//
// Create a user profile
//
Guid guid = Guid.NewGuid();
Profile userProfile = csContext.ProfileSystem.CreateProfile
(
guid.ToString(),
"UserObject"
);
//
// Get a ContentSelector and set its properties
//
ContentSelector cso = csContext.TargetingSystem.SelectionContexts["advertising"].GetSelector();
cso.Profiles.Add("targetingContext", csContext.TargetingSystem.TargetingContextProfile);
cso.Profiles.Add("user", userProfile);
cso.TraceMode = true;
cso.ItemsRequested = 2;
cso.Size = "Half";
//
// Get some content
//
StringCollection content = cso.GetContent();
//
// Bind the results to a repeater
//
if (content.Count > 0)
{
repeaterShowAds.DataSource = content;
repeaterShowAds.DataBind();
}
//
// Dump out Trace Messages
//
if (cso.TraceMessages != null)
{
csContext.DebugContext.Trace("Targeting Sample", "Trace Messages");
int index = 0;
foreach (StringCollection messages in cso.TraceMessages)
{
csContext.DebugContext.Trace("Targeting Sample", "Content item row index=" + index++);
foreach (string message in messages)
{
csContext.DebugContext.Trace("Targeting Sample", message);
}
}
if (index == 0)
{
csContext.DebugContext.Trace("Targeting Sample", "It looks like there were no ads on schedule");
}
}
else
{
csContext.DebugContext.Trace("Targeting Sample", "ContentSelection tracing is unavailable.");
}
csContext.DebugContext.Trace("Targeting Sample", "Dumping out contents of selectionContext...");
ContentSelectionContext selectionContext = csContext.TargetingSystem.SelectionContexts["advertising"];
foreach (string key in selectionContext)
{
csContext.DebugContext.Trace("Targeting Sample", "key=" + key + " value=" + selectionContext[key].ToString());
}
}
</script>
</HEAD>
<body>
<form ID="Targeting" method="post" >
<h3 id="H31" align="left" >
Display an advertisement targeting the UserObject and TargetingContext
profiles.</h3>
<H4 class="dtH4">You should create an ad with a Target Group associated with the <B>Sports</B>
page group before running this example. </H4>
<P class="dtH4">
<asp:Button id="ButtonShowAds" Text="Show Sports Ads" OnClick="ShowAds_OnClick" /></P>
<P class="dtH4">
<asp:Repeater id="repeaterShowAds" >
<HeaderTemplate>
<table border="1">
<tr>
<td><b>Sports Ads</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Container.DataItem %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</TABLE>
</FooterTemplate>
</asp:Repeater></P>
</form>
</body>
</HTML>
See Also
ContentSelectionContext Class (BCL)
Copyright © 2005 Microsoft Corporation.
All rights reserved.