Jaa


How to Display Targeted Advertisements

You can target advertisements through page group or user profiles. This topic demonstrates how to target users based on a page group.

To display targeted advertisements

  1. Create a TargetingContextProfile. Specify the PageGroup ad that you want.

  2. Create a ContentSelector object and set its properties.

  3. Set the ad Size.

  4. Use GetContent to retrieve ad content.

  5. Bind the results to a repeater.

  6. Dump out trace messages.

Example

The following code creates a targeting profile, a user profile, runs the ContentSelector object, and displays targeted advertisements on an ASP.NET Web form.

TargetedAd.aspx

<%@ Page Language="C#" Trace="false" AutoEventWireup="true" CodeFile="TargetedAd.aspx.cs" Inherits="TargetedAd" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head >
    <title>Code to Display Targeted Advertisements</title>
</head>
<body>
    <form id="Targeting" method="post" >
        <h3 id="H31" >
            Display an advertisement targeting the UserObject and TargetingContext. 
            profiles.</h3>
        <h4 class="dtH4">You must create an ad within a "<b>Sports</b>" page group
            before running this example.&nbsp;Otherwise no ads will be returned.
            You can also change the page group within the code to work with other page groups.</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>

TargetedAd.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Profiles;
using Microsoft.CommerceServer.Runtime.Caching;
using Microsoft.CommerceServer.Runtime.Targeting;
using Microsoft.CommerceServer.Runtime.Pipelines;
using Microsoft.CommerceServer.Runtime.Diagnostics;

public partial class TargetedAd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void ShowAds_OnClick(Object sender, EventArgs args)
    {
        CommerceContext csContext = CommerceContext.Current;
        //
        // Create a targeting profile. Specify the ad page group that you want here.
        //
        csContext.TargetingSystem.TargetingContextProfile["PageGroup"].Value = "Boots";

        //
        // Create a ContentSelector object and set its properties.
        //
        ContentSelector cso = CommerceContext.Current.TargetingSystem.SelectionContexts["advertising"].GetSelector();
        cso.Profiles.Add("targetingContext", csContext.TargetingSystem.TargetingContextProfile);
        
        cso.TraceMode = false;   //to view debug information.
        cso.ItemsRequested = 1;

        //Set the ad size.
        //Available tags are stored in the Content Sizes reference table.
        cso.Size = "Banner";   
        
        //
        // 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)
        {
            DebugMessage("<table>", "");
            DebugMessage("Targeting Sample", "Trace Messages");
            int index = 0;
            foreach (StringCollection messages in cso.TraceMessages)
            {
                DebugMessage("Targeting Sample", "Content item row index=" + index++);
                foreach (string message in messages)
                {
                    DebugMessage("Targeting Sample", message);
                }
            }
            if (index == 0)
            {
                DebugMessage("Targeting Sample", "It looks like there were no ads on schedule.");
            }
        }
        else
        {
            DebugMessage("Targeting Sample", "ContentSelection tracing is unavailable.");
        }
        DebugMessage("Targeting Sample", "Dumping out contents of selectionContext...");
        ContentSelectionContext selectionContext = csContext.TargetingSystem.SelectionContexts["advertising"];
        foreach (string key in selectionContext)
        {
            DebugMessage("Targeting Sample", "key=" + key + " value=" + selectionContext[key].ToString());
        }
        DebugMessage("</table>", "");
    }

    void DebugMessage(string Category, string Message)
    {
        //csContext.DebugContext.Trace("Targeting Sample", "Trace Messages");
        Response.Write("<tr><td>" + Category + "</td><td>" + Message + " </td></tr>");
    }
}

See Also

Other Resources

What Are Page Groups?

Marketing Run-time Scenarios