Thursday, December 15, 2011

Tracking customer with cookies/ Session and Application/ Global.asa in E-commerce applications (1/3)

Before developing an eCommerce application I would like to describe how to keep track of users visiting your E-commerce web application Using Application and Session objects. The ability to track customers and personalize content is important because you can use it to increase sales. To take a simple example, you might want to display different advertisements to different customers depending on their interests. If you have recorded the fact that a certain customer likes looking at pages in your Web site related to fishing rods, you can automatically show this customer more advertisements related to fishing rods.

In this article you will discuss

  • How to add cookies to customers' browsers so that you can automatically identify customers whenever they return to your Web site.
  • How to use Session and Application variables to store persistent information.
  • How to use the Global.asa file to detect when customers first arrive at your Web site and when they leave.

Tracking Customers with Cookies

We can use a cookie to store information on a customer’s computer when the customer visits our Web site. We can then use this information to identify the customer once again whenever the customer returns to our Web site. Without cookies, the interaction between Web servers and browsers is stateless. You cannot identify the same user of your Web site as the user moves from page to page. The stateless nature of Web server and browser interaction creates a number of problems for Web site developers. For example, imagine you have created a special area of your Web site that contains content, which only registered members can view. Without using cookies, it is difficult to track whether a particular user is a registered member. If the user logs in on one page, it is difficult to detect whether it is the same user on another page.

Types of cookies

There are two types of cookies:

Session cookies: Session cookies are stored in memory. They last on a customer's computer only while the customer is visiting your Web site.

Persistent Cookies: A persistent cookie, on the other hand, can last many months or even years. Persistent cookies are stored in a text file on the customer's computer. This text file is called the Cookie file on Windows computers and the Magic Cookie file on Macintosh computers.

You should never assume that a customer has cookies enabled on their browser. For example, a perfectly legitimate use of cookies is to automatically log in a user at your Web site. If you do this, however, you should include a way for users who do not have cookies enabled to log in.

Adding a Cookie to a Customer's Browser

You can add a cookie to a customer's browser by using the Cookies collection of the Response object. For example, imagine that you want to add a cookie named customer Name that contains a customer name. To add this cookie, you would use the following statement:

Response.Cookies( "customerName" ) = "Binay Tiwari"

This statement adds a cookie named "customerName"  that has the value "Binay Tiwari". The cookie that is created is a session cookie. It last only while the customer is visiting your Web site.

To create a persistent cookie, you must include the date when the cookie will expire. You do this by using the Expires attribute of the Cookies collection. For example, the following two statements create a cookie that will last until July 4, 2012:

 Response.Cookies( "customerName" ) = "Binay  Tiwari"

 Response.Cookies( "customerName" ).Expires =  "July 4, 2012"

When creating cookies, you must create the cookie before any  content is sent to the browser. Otherwise you will receive the following error:
Header Error

 “The HTTP headers are already written to the client browser.  Any HTTP header modifications must be made before writing page content.”

If you want to get around this limitation, you can buffer your ASP page. When you buffer an ASP page, the page is not sent immediately to  a browser. It is retained in memory until the whole page is processed. To buffer an ASP page, include the following statement at the top of the page:

<% Response.Buffer = TRUE %>

According to the original cookie specification a single computer can hold a maximum of 300 cookies from all Web sites. Furthermore, a single Web site cannot add more than 20 cookies to a customer's computer.  Finally, an individual cookie can hold no more than 4KB of data. This limit applies to a combination of the size of the cookie's name and the size of the data contained in the cookie.

Reading Cookies from a Customer's Browser

You can read a cookie you have placed on a customer’s computer by using the Cookies collection of the Request object. For example, to retrieve a cookie named username and assign it to a local variable named username, you would use the following statement:

username = Request.Cookies( "username" )

You can display all the cookies that have been added by your Web site by iterating through the contents of the Cookies collection.

  <HTML><HEAD><TITLE>All  Cookies</TITLE></HEAD>
   FOR EACH cookie IN  Request.Cookies
   Response.Write  cookie& "=" &Request.Cookies( cookie )&  "<BR>"

No comments:

Post a Comment