I've also tried the following which also doesn't work.
<?php global $piwikuserid;
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$piwikuserid = $current_user->user_login;
} else {
$piwikuserid = "visitor555";
}
echo sprintf("_paq.push(['setUserId', '%s']);", $piwikuserid);
?>