Hack 53 Remember Your Customers

figs/moderate.gif figs/hack53.gif

Track your site visitors, regardless of whether they made a purchase with PayPal.

As your eCommerce site becomes more advanced, you might want to begin tracking visitors as they move through your site. For example, you could create a membership system, encouraging users to register and then log in during each subsequent visit. Once acknowledged, your users might have access to special insider deals or premium content. Or, you could address your customers by name on your site's pages.

However, there's a downside. Designing, building, and maintaining a membership database for customers can be a lot of work, and some customers might balk at being asked for a username and password each time they visit. Using the techniques in this hack, you can identify your users by name and offer buyers-only content in minutes?no login required.

5.10.1 Tracking Buyers with Cookies

A popular way to remember your visitors is by using cookies. Cookies are small chunks of information that a user's browser remembers on behalf of your web site. They are handed back to your web site (if it asks) on a subsequent visit. By setting, then reading back, personal information for a visitor, your web site can remember your customers.

This hack sets a cookie when your buyer has returned to your site after making a payment to you with PayPal. Your site will look for this information whenever someone visits and, if found, use it to personalize the site by using the buyer's name and granting access to customer-only content.

You can implement this hack with any web scripting technology; the example code uses ASP with VBScript.

5.10.2 The Return Page

The return page is a page on your site that is activated after a payment has been made, when the buyer clicks the "Click here to continue" link on the You Made A Payment page. Set the return variable in your Buy Now button to the URL you want to use.

Use the return page to create cookies that record the user's name and the fact that the user is a buyer. You should also set the cookies' expiration times; if you don't set the cookies to expire in a set amount of time (such as about an hour, as in the following code), the settings will be lost at the end of the session (such as when your customer closes the browser).

Here's a simple ASP implementation of this:

<% 

'Set cookie expiration

'If this is a completed payment, set "paid" to "yes"

If Request.Form("payment_status") = "Completed" Then

  Response.Cookies("paid") = "Yes"

  'Set the expiration time of the cookie

  Response.Cookies("paid").Expires = Now( ) + 0.042 'About 1 hour

End

Response.Cookies("user") = Request.Form("first_name")

'Set the expiration time of the cookie

Response.Cookies("user").Expires = Now( ) + 0.042 'About 1 hour

%>

The user is identified by the first name provided by PayPal via the first_name variable. The paid cookie remembers that this user is a paying customer; user stores the buyer's name.

In addition to the cookie-handling code in this example, you'll want to have links to other portions of your site, such as your home page.

5.10.3 Cookies at Work

You can use the cookies you created on the other pages of your site. For example, you can greet your customer by name:

Welcome<br><%= Request.Cookies("user")%><br>

Or you can reward your loyal customers with inside information:

<% 

If Request.Cookies("paid") = "Yes" Then

'They have paid, show secret text

%>

We'll be having a <b>big sale</b> on all our exclusive monkey toys this

Thursday! (Preferred customers only.)

<%

End If 

%>

This code shows the secret text only to people who have completed a purchase using PayPal.

5.10.4 Hacking the Hack

PayPal provides more information to your return page than just the payment status and the buyer's name. For example, you can also get the name of the item purchased. Try this addition to your return page to record the item name:

<% 

'Set cookie expiration

'If this is a completed payment, set "paid" to "yes"

If Request.Form("payment_status") = "Completed" Then

  Response.Cookies("paid") = "Yes"

  'Set the expiration time of the cookie

  Response.Cookies("paid").Expires = Now( ) + 0.042 'About 1 hour

  Response.Cookies("item_name") = Request.Form("item_name")

  Response.Cookies("item_name").Expires = Now( ) + 0.042 'About 1 hour

End

Response.Cookies("user") = Request.Form("first_name")

'Set the expiration time of the cookie

Response.Cookies("user").Expires = Now( ) + 0.042 'About 1 hour

%>

Then, use the item name in your content pages:

Welcome<br><%= Request.Cookies("user")%><br>

<% 

If Request.Cookies("paid") = "Yes" Then

%>

Thank you for your recent purchase of 

<%= Request.Cookies("item_name")%>.

<%

End If 

%>

You will need to modify this code for shopping cart applications, because there will likely be more than one item name.


Also, remembering your customer for an hour might not be as long as you would like. Try setting the value to a year:

Response.Cookies("paid").Expires = Now( ) + 365 'About a year

5.10.5 See Also

The "HTML and Hyperlink Variables" section in the PayPal Buy Now Buttons Manual offers important information about using the return and rm parameters.