Tracking User Activity in a Web Application, Part II

If you want to track user activity in a Web application, you may want to determine who the user is. Of course this raises all kinds of privacy issues, but let’s ignore those for now. In general, trying to determine a user with server-side logic will not work except in very tightly controlled, and usually artificial, environments. By far the most common approach is to ask the user to identify himself by typing a user name into a text box. Then you can store this information in a persistent cookie on the client machine. This approach assumes that the user has both JavaScript and cookies available and enabled. With a cookie on the client, you now have a way to ID the user. I ran across an interesting alternative to bluntly asking the user for his ID. In a pure Microsoft intranet network environment, you can use a very surprising technique to ID a user. There is a WScript.Network object which has a UserName property — and it’s actually callable from inside a Web page. The code would look something like:

<script language="JavaScript">

function getUser()


   var wn = new ActiveXObject("WScript.Network");

   var username = wn.UserName;

   return username;





This trick doesn’t seem to be well-known because it uses a WScript object and these are normally used strictly in a shell environment. Unfortunately, most browsers these days have ActiveX disabled because of security concerns. Another alternative technique for ID’ing a user is to assign a random ID, and then storing that ID in a cookie. For example:


function randomUserID()


  var chars = "abcdefghijklmnopqrstuvwxyz";


  var slength = 5;

  var result = "x";

  for (var i = 0; i < slength; ++i)


    var rn = Math.floor(Math.random() * chars.length);

    result += chars.substring(rn, rn+1);


  return result;



This JavaScript function creates a random ID. Each ID is 6 characters (lower case ‘a’ through ‘z’) in total length and starts with an arbitrary lower case ‘x’.

This entry was posted in Machine Learning, Software Test Automation. Bookmark the permalink.