This documentation is archived and is not being maintained.

Tracking Ad Responses for the AdRotator Control

The AdRotator control does not inherently provide a way to gather statistics about which ads users have clicked, how many times an ad has been clicked, and so on. Ads displayed by the AdRotator control are effectively nothing more than hyperlinked images, with no special features for gathering statistics.

However, because you have control over the URL to which the ad points, you can add tracking logic to your application for the ads. A typical way to do this is to point all ads to a tracking page that first gathers the statistics you want, and then jumps to the ad's destination page.

Redirecting Ads to a Tracking Page

When you redirect to a tracking page, not only must you jump to the redirection page, but you must also be sure that the redirection page has the right information for gathering statistics. An easy way to do this is to include the necessary information in a query string.

Note   Query strings are appended to the URL of the redirection page and can be seen (and possibly manipulated) by users. If it is important that users not see this information (for example, for security reasons), you should encode the query string or use an alternative way to pass information about the ad to the redirection page. For information about persisting data between pages, see Web Forms State Management.

To redirect ads to a tracking page

  • In the NavigateURL property of an individual ad, specify a URL that includes:
    • The name of your redirection page
    • Information that allows you to identify the ad
    • The page to which the ad ultimately points

    The following example shows an entry for a single ad from an XML ad list file. In this example, the redirection page is AdRedirector.aspx. The ad identifier and ultimate target page are specified as a query string.

    Note   Because the ampersand (&) is a reserved character in XML, the query string includes the entity reference
    <AlternateText>Microsoft Widgets</AlternateText>

Gathering Statistics and Jumping to the Destination Page

The tracking page you specify performs two functions: it gathers statistics about the ads that users have clicked, then it redirects again to the ad's ultimate destination page. This generally means that the tracking page has no UI of its own.

How you gather statistics depends on your application requirements. As a minimum, you will probably count the number of times an ad has been clicked and store this information in a database.

To gather statistics and jump to the destination page

  1. In the Page_Load handler of the tracking page, extract information about the ad that has been clicked and the destination page.
  2. Store or otherwise process the ad information, as required by your application.
  3. Call the Response.Redirect method to jump to the destination page.

    The following example shows a simple Page_Load handler that counts the number of times an ad has been clicked and stores the count in the Application state object, using the name of the ad as the key. (In your applications, you are likelier to store this information in a database.) After updating the count, the code jumps to the destination page.

    ' Visual Basic
    Private Sub Page_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
       Dim source As String = Request.QueryString("ad")
       Dim redirect As String = Request.QueryString("target")
       Dim adcounter As Integer
       If Application(source) Is Nothing Then
          adcounter = 0
          adcounter = CType(Application(source), Integer)
       End If
       adcounter += 1
       Application(source) = adcounter
    End Sub
    // C#
    private void Page_Load(object sender, System.EventArgs e)
       String source = Request.QueryString["ad"];
       String target = Request.QueryString["target"];
       int adcounter;
       if(Application[source] == null) 
          adcounter = 0;
          adcounter = (int) Application[source];
       adcounter += 1;
       Application[source] = adcounter;

See Also

Adding AdRotator Controls to a Web Forms Page | Creating an Ad List for the Ad Rotator Web Server Control | Selecting Ads in an AdRotator Web Server Control at Run Time