Capturing Session IDs


If the web application you are monitoring has it’s own session id and you would like to capture it with WebTuna so that you can tie WebTuna data to other data such as log files then you can have the webtuna.js capture the session id by overriding the JavaScript function with your own function to get it from the application and return it to WebTuna.

The JavaScript function needs to be called wt.session and needs to be declared somewhere in the page so that it exists at the point where the onload handler is called.

If this function is not present then WebTuna will create its own ‘sessionId’ which is based on the timestamp when the user first connects. This is set in a cookie and is useful to track unique visitors to the site.

Example of how wt.session can be used

This example code will capture the session ID so it can be captured by WebTuna.

<script type="text/javascript">
wt.session = function ()
{
	return mySessionId;
};
</script>

Example for JSESSIONID used by Java applications

This example code will capture the session ID so it can be captured by WebTuna.

<script type="text/javascript">
wt.session = function (){
    var jsId = document.cookie.match(/JSESSIONID=[^;]+/);
    if(jsId != null) {
        if (jsId instanceof Array)
            jsId = jsId[0].substring(11);
        else
            jsId = jsId.substring(11);
    } else {
          jsId = '';
    }
    return jsId;
};
</script>

Example for PHPSESSID used by PHP applications

This example code will capture the session ID so it can be captured by WebTuna.

<script type="text/javascript">
wt.session = function (){
    var phpSessionId = document.cookie.match(/PHPSESSID=[A-Za-z0-9]+;/i);
    if(phpSessionId == null)
        return '';
  
    if(typeof(phpSessionId) == 'undefined')
        return '';
  
    if(phpSessionId.length <= 0)
        return '';
  
    phpSessionId = phpSessionId[0];
  
    var end = phpSessionId.lastIndexOf(';');
  
    if(end == -1) end = phpSessionId.length;
  
    return phpSessionId.substring(10, end);
}
</script>

NOTE – This code must come after the line which calls webtuna.js