Counting Lost Visitors

One statistic that is useful to know is the number of people who start to load your page but leave before the page finishes loading. This tells you how many visitors that you have lost through your page loading too slowly and may provide the incentive to improve the speed at which your page loads.

The trick to this relates to counting hits. A hit is counted for each file that is downloaded from your server. What we need to do is to count the hits on either the html file itself or the first image file that is downloaded and also count the hits on the last image file downloaded. The difference between the first count and the second count is the number of visitors who left before your page finished downloading.

If you have access to your server logs then you have access to a list of all of the hits on each of the files on your site and you can easily get a count of the number of people who started downloading your page by counting the hits on the html file. For more info on how to read your server log to obtain this info read Analyse Your Web Server Log.

The problem is in determining which is the last file on your page to finish loading so that we can count that. We solve this by creating a special image that will always be the last thing downloaded on this page. Copying this image and giving it a unique name will give us a small image (42 bytes) that we can use without adding significantly to the download time for the page. The other thing that we do is to place a small piece of javascript code into the page to reference the image file (and thus download it) after everything else on the page has already downloaded. The code that we use to do this is as follows (where finimage.gif is the name you gave the image to load last on this page):

 function finished() {
if document.images() {img1 = new Image(); img1.src = "finimage.gif";}

We execute this script by adding onload="finished();" to the body tag of our page. The onload code executes after everything else on the page has loaded so the image gets loaded after everything else. The image is not actually displayed on the page so the appearance of your page is not affected by the inclusion of this code and the load time is only slightly affected by the extra 150 or so bytes of code and image.

There are two problems with this method of counting lost visitors. The first is that the count of visitors not lost relys on Javascript so that any visitor with Javascript turned off or using a browser that doesn't support Javascript will be counted as lost.

We can partly solve this by only counting visitors (lost or not) whose browsers support Javascript. We do this by using a second image to count those who start downloading the page instead of the html file itself. To do this we add the following code at the start of the first Javascript code in the head section of our page (where startimage.gif is the name of the copy of our 42 byte image that we are going to use to count total hits):

 if document.images() {img0 = new Image(); img0.src = "startimage.gif";}

Because this is such a small image referenced so early in the code it should almost always be the first file to finish downloading. Now both counts rely on Javascript so the difference between the two counts id the number of lost visitors using browsers that support Javascript.

The second problem is if you don't have access to your server logs. What we need to do is to find a way to use an ordinary counter to achieve the same result.

To do this we need is a counter that can be added to our page as if it were an ordinary image. There are a number of service providers on the web who provide counters that run via their servers. Where the service provider provides a counter that normally works as an ordinary image link then two of their counters may be suitable to use for determining lost visitors. To use such counters you will first need to sign up for their service to obtain two counters which you then link into your page in place of startimage.gif and finimage.gif in the above code. Not all such counters will work for this though so you will need to test your page several times after setting things up to ensure that the counters actually count when used this way.

One counter that doesn't work with this code is the one supplied by Bravenet. While their counter is set to call a Javascript function to perform the counting for visitors whose browsers support Javascript, an image link is provided to count those whose browsers are not able to run the Javascript. I attempted substituting the information from the image link into the above code to see whether the Bravenet counters could be used to determine lost visitors but when used this way the counters do not count at all.

If you do find a different counter to try please let me know the result so I can place the information on this page to assist others in determining which counters can and cannot be used for counting lost visitors.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow