Track Users with there Basic Auth Username

I have a Site where i have to track Users with there Username given in a Apache Login Form,
generated from a .htaccess/htpasswd combination.

I have a PHP driven Application so i used this way to solve my Problem:

fill a Variable with the Infos from the Login Credentials:

$loginuser = $_SERVER['REMOTE_USER'] ;

Use this Data in a Custom Variable of the Tracking Code:

var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 2);
var userName = "<?php echo $loginuser; ?>";
piwikTracker.setCustomVariable(1, "Username", userName, scope = "visit");

Regards Oliver