Tutorial: How to add username to your stats
This tutorial should give you an overview of how to add a new data to TraceWatch.
If people login to your website to use some services like forum, shopping cart, etc... you might want the usernames to show up in your stats to know who is doing what, this is how you do it:
Creating The Username Entity
Any data must be converted to a "TraceWatch Entity" before it can be usable by TraceWatch so the first thing you should do is define an Entity for Username. Go to Administrate > Entities and fill out the New Entity box. Let type be "String" enter "Username" for "name", enter "with username {value}" for "visitor title", "username" for "data key" and set cleanup cycle to 30 days. Now click "Add Entity".
Giving the username to TraceWatch logger
Now you should give the username to TraceWatch for any request, you do this by making a little modification to the php code you put on your pages. twatchLogRequest() function definition is like this:
function twatchLogRequest( $cookie = false, $websiteId = 1, $logErrors = true, $showErrors = false, $mutedErrors = true, $pageParams = true, $data = array() )
The last argument $data lets you add, remove or override request data. Suppose the username of the visitor is stored in a variable named $username which is undefined (not set) when the user is not logged in. We put that in the $data array with "username" key (this is the very "data key" we specified when we created the Username Entity). the code you put on pages should look like this now:
$data = array();
if( isset( $username ) ) {
$data[ 'username' ] = $username;
}
twatchLogRequest( true, 1, true, false, true, true, $data );
If you are using the javascript code you should write the code dynamically using whatever server side scripting language you're using to add the username to twatchData and send it to tracewatch. e.g. if you're using PHP:
<script type="text/javascript"><!--
//<![CDATA[
twatchData = 'page='+encodeURIComponent( window.location );
twatchData += '&username='+encodeURIComponent( '<?php echo $username; ?>' );
if( typeof document.referrer != 'undefined' && document.referrer != '' ) {
twatchData += '&ref='+encodeURIComponent( document.referrer );
}
if( typeof screen.width != 'undefined' ) {
twatchData += '&resolution='+screen.width+'x'+screen.height;
}
document.write('<scr'+'ipt type="text/javascript" '+
'src="/twatch/remote/js_logger.php?'+twatchData+'">'+
'</scr'+'ipt>');
//]]>
//--></script>
Creating a Counter for Usernames
The Username Entity is now being generated for any request but it's just waisted unless we do something useful with it, like counting it! we should make a List Counter for the Username Entity.
Go to Adminstrate > Counters fill the New Counter box. Set "type" to "list", enter "Usernames" for "name" and select "Username" for entity. In front of "when:" select "Entity Changed" from the drop down list, another drop down shows up, select "Username Changed" from the list and click "Add".
The condition "Username Changed" tells TraceWatch to count only at the beginning of a session or when the username is changed. This way we actually count "sessions with a certain username" not requests.
Now click Add Counter then on the top right corner of the newly created counter click Start.
Creating a Counter View for Usernames in the Stats Page
Now usernames are being counted but the counter's result doesn't show up anywhere unless we make a Counter View for it. What about showing the list of usernames at the bottom of the main stats page? here is how you do it:
Go to Administrate > Stats Pages. At the bottom of the page you find the New List Counter View. enter "Usernames" for "title", select "Usernames" from the drop down list in front of "Counter:" and then click Add Counter View.
Now you should see a list of usernames at the bottom of the main stats page:
Showing Usernames in Latest Visitors page
To show the Username entity for each request in Latest Visitors page you should define a Data Writer for it.
Go to Administrate > Latest Visitors there is a New Data Writer box. select "Username" for "entity" and add "Username Changed" in front of "when:" the same way you did it for the counter and click Add Data Writer.
Now if the user is logged in you should be able to see the username in the list of data when you click on the "Request Data Button" for a request in the latest page:
But most probably you like to show the username more prominently on sessions like where the country and IP address is shown. to do that you should define an item for it:
In Administrate > Latest Visitors in Latest Visitors Page > Secondary Items in the green "New Item" box select Username for entity, enter "Username" for "title", enter "Not Logged In" for "not found", click "Add" and then click "Apply Changes".
Now you should see the username (or "not logged in" when the visitor is not logged in) show up right next to IP on every session. Also know that only entities which have a Data Writer show up as items which means you can't skip the previous step.

