Preferred Language:

Listing 4.14 - AdRotatorTrack.aspx

Illustrates how you can track advertisement clicks when using the AdRotator control.

Listing 4.14 - AdRotatorTrack.aspx (C#)
Copy

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    protected void AdRotator1_AdCreated(object sender, AdCreatedEventArgs e)
    {
        // Update Impressions
        srcAds.InsertParameters["AdId"].DefaultValue = e.AdProperties["Id"].ToString();
        srcAds.Insert();
        
        // Change NavigateUrl to redirect page
        e.NavigateUrl = "~/AdHandler.ashx?id=" + e.AdProperties["Id"].ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>AdRotator Track</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <asp:AdRotator
        id="AdRotator1"
        DataSourceID="srcAds"
        OnAdCreated="AdRotator1_AdCreated" 
        Runat="server" />
    
    
    <asp:SqlDataSource
        id="srcAds"
        ConnectionString="Server=.\SQLExpress;Integrated Security=True;
            AttachDbFileName=|DataDirectory|AdListDB.mdf;User Instance=True"
        SelectCommand="SELECT Id, ImageUrl, Width, Height, NavigateUrl, AlternateText, Keyword, Impressions
            FROM AdList"
        InsertCommand="INSERT AdStats (AdId, EntryDate, Type) VALUES (@AdId, GetDate(), 0)"    
        Runat="server">
        <InsertParameters>
        <asp:Parameter Name="AdId" Type="int32" />
        </InsertParameters>
     </asp:SqlDataSource>   

    
    </div>
    </form>
</body>
</html>

The page above uses the following Generic Handler to track clicks.

Listing 4.15 - AdHandler.ashx (C#)
Copy

<%@ WebHandler Language="C#" Class="AdHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

public class AdHandler : IHttpHandler {

    const string conString = @"Server=.\SQLExpress;Integrated Security=True;
            AttachDbFileName=|DataDirectory|AdListDB.mdf;User Instance=True";
    
    public void ProcessRequest (HttpContext context) 
    {
        int AdId = Int32.Parse(context.Request["Id"]);

        SqlConnection con = new SqlConnection(conString);
        string navigateUrl = String.Empty;
        using (con)
        {
            con.Open();
            UpdateTransferStats(AdId, con);
            navigateUrl = GetNavigateUrl(AdId, con);
        }

        if (!String.IsNullOrEmpty(navigateUrl))
            context.Response.Redirect(navigateUrl);
    }

    void UpdateTransferStats(int advertisementId, SqlConnection con)
    {
        string cmdText = "INSERT AdStats (AdId, EntryDate, Type) VALUES " +
            "(@AdId, GetDate(), 1)";
        SqlCommand cmd = new SqlCommand(cmdText, con);
        cmd.Parameters.AddWithValue("@AdId", advertisementId);
        cmd.ExecuteNonQuery();
    }

    string GetNavigateUrl(int advertisementId, SqlConnection con)
    {
        string cmdText = "SELECT NavigateUrl FROM AdList WHERE Id=@AdId";
        SqlCommand cmd = new SqlCommand(cmdText, con);
        cmd.Parameters.AddWithValue("@AdId", advertisementId);
        return cmd.ExecuteScalar().ToString();
    }
    
    
    public bool IsReusable 
    {
        get 
        {
            return false;
        }
    }

}

You can use the following page to view the statistics on your advertisements.

Listing 4.16 - AdRotatorStats.aspx (C#)
Copy

<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
    <style type="text/css">
        .grid td,.grid th
        {
            border-bottom:solid 1px black;
            padding:5px;
        }
    </style>
    <title>AdRotator Statistics</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h1>Advertisement Statistics</h1>
    Impressions represent the number of times an advertisement has been viewed. 
    Transfers represent the number of times an advertisement has been clicked.

    <h2>Impressions</h2>

    <asp:GridView
        id="grdImpressions"
        DataSourceID="srcImpressions"
        AutoGenerateColumns="false"
        GridLines="None"
        CssClass="grid"
        Runat="server">
        <Columns>
        <asp:BoundField 
            DataField="AdId"
            HeaderText="Advertisement Id" />
        <asp:BoundField
            DataField="Impressions"
            HeaderText="Impressions" />        
        </Columns>
    </asp:GridView>
    
    <asp:SqlDataSource
        id="srcImpressions"
        ConnectionString="Server=.\SQLExpress;Integrated Security=True;
            AttachDbFileName=|DataDirectory|AdListDB.mdf;User Instance=True"
        SelectCommand="SELECT AdId,Count(*) As Impressions 
            FROM AdStats 
            WHERE Type=0
            GROUP BY AdId
            ORDER BY Impressions DESC"    
        Runat="server" />

    <h2>Transfers</h2>

    <asp:GridView
        id="grdTransfers"
        DataSourceID="srcTransfers"
        AutoGenerateColumns="false"
        GridLines="None"
        CssClass="grid"
        Runat="server">
        <Columns>
        <asp:BoundField 
            DataField="AdId"
            HeaderText="Advertisement Id" />
        <asp:BoundField
            DataField="Transfers"
            HeaderText="Transfers" />        
        </Columns>
    </asp:GridView>
    
    <asp:SqlDataSource
        id="srcTransfers"
        ConnectionString="Server=.\SQLExpress;Integrated Security=True;
            AttachDbFileName=|DataDirectory|AdListDB.mdf;User Instance=True"
        SelectCommand="SELECT AdId,Count(*) As Transfers 
            FROM AdStats 
            WHERE Type=1
            GROUP BY AdId
            ORDER BY Transfers DESC"    
        Runat="server" />
    
    </div>
    </form>
</body>
</html>