Ok, please forgive the overly dramatic title, but I want to talk about something pretty darn important and I wanted to get your attention. Does your site have an e-commerce feature? Is the main purpose of your site e-commerce? If so - let me ask you a question. I'm sure you can tell me right now (I hope!) how many orders your site gets per day, along with other things like the average size of the order, highest order, etc. But can you tell me how many people visit your site, add a few items to their cart, and just give up never to return? Can you tell me if there is a particular part of your checkout process where people tend to abandon their carts? If you can't - then you could (literally) be losing money every day. Money isn't everything - but I've heard good things about it - so why not go the extra mile to ensure you are losing money due to some stupid reason like a hard to navigate checkout process or even a simple typo that makes it difficult for consumers to complete their order. Luckily we can add some monitoring using a simple feature of ColdFusion MX7: Application.cfc.

For this blog entry I've created a very simple e-commerce application. It is so simple it doesn't even have real forms. It does, however, have a set of files that let you mimic a user moving through the site and the checkout process. Please download the files by clicking the Download link below. Having them in front of you will help you understand this blog entry. You can expand the zip and run as is - no need for a database or anything fancy like that.

The application has the following files:

  • Application.cfc: This is the file that will handle monitoring the check out process. It also initializes the session.
  • index.cfm: This is the first page you should hit when testing the application.
  • addcart.cfm: This page represents a user who has items in her cart.
  • checkout1.cfm: This page represents someone who is on the first step of the checkout process, the address page typically.
  • checkout2.cfm: This page represents someone who is on the second step of the checkout process, the billing information page.
  • checkout3.cfm: This page represents the confirmation page. The user must go past this page to finish the order.
  • done.cfm: This page represents the end of the e-commerce process and a successful order. (Yeah money!)
  • stats.cfm: This is a page that would normally be for administrators only. We will use it for reporting.

Feel free to play with the checkout process now, but I'd ask that you avoid the stats page until we discuss it. The application has an extremely low timeout value, so you can leave it for 30 seconds and your session will abort. I don't auto-reset you in the checkout process, so you can just keep clicking away. Now that you've played with it a bit. Let's dig into the code. First let's take a look at the Application.cfc file:

<cfcomponent output="false">

<cfset this.name = "ecomtest"> <cfset this.applicationTimeout = createTimeSpan(0,2,0,0)> <cfset this.clientManagement = true> <cfset this.clientStorage = "registry"> <cfset this.loginStorage = "session"> <cfset this.sessionManagement = true> <cfset this.sessionTimeout = createTimeSpan(0,0,0,30)> <cfset this.setClientCookies = true> <cfset this.setDomainCookies = false> <cfset this.scriptProtect = false>

<cffunction name="onApplicationStart" returnType="boolean" output="false"> <cfreturn true> </cffunction>

<cffunction name="onApplicationEnd" returnType="void" output="false"> <cfargument name="applicationScope" required="true"> </cffunction>

<cffunction name="onRequestStart" returnType="boolean" output="false"> <cfargument name="thePage" type="string" required="true"> <cfreturn true> </cffunction>

<cffunction name="onRequestEnd" returnType="void" output="false"> <cfargument name="thePage" type="string" required="true"> </cffunction>

<cffunction name="onError" returnType="void" output="true"> <cfargument name="exception" required="true"> <cfargument name="eventname" type="string" required="true"> <cfdump var="#arguments#" label="Exception"> </cffunction>

<cffunction name="onSessionStart" returnType="void" output="false"> <cfset session.status = ""> </cffunction>

<cffunction name="onSessionEnd" returnType="void" output="false"> <cfargument name="sessionScope" type="struct" required="true"> <cfargument name="appScope" type="struct" required="false">

<!--- just in case... ---> <cfif not structKeyExists(arguments.sessionScope, "status")> <cfset arguments.sessionScope.status = ""> </cfif>

<cflog file="ecomtest" text="Session Status: #arguments.sessionScope.status#"> </cffunction>


Let me pick out the parts you want to be concerned with. First off, note the super low session time out:

<cfset this.sessionTimeout = createTimeSpan(0,0,0,30)>

You would obviously not have this on a production system. I did this to make my testing easier. Next lets take a look at the onSessionStart code:

<cffunction name="onSessionStart" returnType="void" output="false"> <cfset session.status = ""> </cffunction>

The session variable, status, is what I'm going to use to note where the user was when her session ended. Speaking of the end of a session...

<cffunction name="onSessionEnd" returnType="void" output="false"> <cfargument name="sessionScope" type="struct" required="true"> <cfargument name="appScope" type="struct" required="false">

<!--- just in case... ---> <cfif not structKeyExists(arguments.sessionScope, "status")> <cfset arguments.sessionScope.status = ""> </cfif>

<cflog file="ecomtest" text="Session Status: #arguments.sessionScope.status#"> </cffunction>

First - a reminder. When ColdFusion fires the onSessionEnd method, you can't use "session.foo" to refer to the session scope. Why? Because it's dead. There is no more session. However - ColdFusion passes in the data from the session scope as a structure. (It also passed in the Application scope.) I wrote a bit of code to ensure my session variable, status, still existed. It should never fire - but I like to be extra careful. Finally I use the cflog command to report the session status. I could have inserted into a database as well, but I wanted to keep this demo simple. The important thing to note is - I record the value so I know exactly what was going on when the user's session ended.

Now let's look at the checkout process. I described the files above. If you took the time to actually do the checkout process, then you know that each page simply has a text description and a button. Therefore I'm not going to show you all the pages, but just two so you can get the idea. Let's look at the very first page, index.cfm: <!--- Record Status ---> <cfset session.status = "No Cart">

<cf_layout title="Intro Page">

<p> This page represents the introduction, or home page, for the simulation. If you end your session here, it means you didn't purchase a darn thing. </p>

<p> <form action="addcart.cfm" method="post"> <input type="submit" value="Add Stuff to Your Cart"> </form> </p>


There is one line you want to care about - the first line. This is where I set the status for the user's session. I'm using a simple string to describe where she is in the e-commerce process. Let's compare this to addcart.cfm:

<!--- Record Status ---> <cfset session.status = "Cart with Items">

<cf_layout title="Added to Cart">

<p> You have now added stuff to your cart. If you leave here, it means you never even tried to check out. </p>

<p> <form action="checkout1.cfm" method="post"> <input type="submit" value="Start Checkout Process"> </form> </p>


Again - the only line here of import is the first line. Note how I've changed the status. Now obviously these files would have a lot more to them. Like - oh - actual form fields. But I think you get the idea of what I'm doing here. So - take a look at stats.cfm. If everything is working fine, you should see a report like so:

Your site has had 22 total sessions.
Of those sessions, 10 (45.45 %) sessions didn't order anything.
Of those sessions, 1 (4.55 %) sessions ordered items, but didn't start the checkout process.
Of those sessions, 1 (4.55 %) stopped at the first step in the check out process (Entering Address).
Of those sessions, 2 (9.09 %) stopped at the second step in the check out process (Entering Billing Information).
Of those sessions, 3 (13.64 %) stopped at the third step in the check out process (Confirmation).
Of those sessions, 3 (13.64 %) finished their order.

This report shows exactly where folks are ending in their process. (In case you are curious - the reason why the lines below the total don't add up to the session total represents the fact that some people don't hit the initial page.) For an e-tailer (I really hate all those "e-" words), the lines you would care most about are the lines involved in the process itself. If you saw a spike in step two for example, you would inspect that form and see if anything stands out as being particularly difficult. You could also email a few users and simply ask them why they gave up. (You don't want to email all of them though, just pick a few select random folks.) In my sample code I just logged the status, but you could store a lot more information if you wanted to. Although a log file probably wouldn't be a good idea - you could store their cart - their IP - anything you can think of.

In case you are curious - what exactly is stats.cfm doing? Honestly I don't want to waste space here. Because I used a log file, I had to do some string parsing and query of queries. Since that really isn't relevant to the discussion, I won't post the code - but you can peruse it via the Download link below.

Here are some other ideas to consider:

  • Keep a count (in the application scope perhaps) of the people who don't finalize the e-commerce process. If the count goes over a threshold, fire off an email to the powers that be to let them know that something may be up.
  • When a user doesn't complete their order, consider storing their cart so they can retrieve it when she returns. I can give you a great example of this. My wife, for the past few days, has been trying to order from a retailer I won't name. As far as she knew - her cart was stored, yet every day she returned to be told her cart had been emptied. This is with a site she had to logon and register for - so there is no reason for this. She was very frustrated and probably will switch to another store.
  • When a user doesn't complete their order, maybe send them a coupon code to encourage them to complete an order.
  • Examine the contents of abandoned carts. Do some products seems to be abandoned most often? Perhaps they have higher taxes, or shipping fees, that discourage users from actually purchasing the items. Maybe you should be more up front with the users about these fees?
  • Can you think of some other good uses here?

Download attached file.