Get username using Javascript

Ron Linder April 3, 2018

Is there a way to get the username via javascript code? Or a way to store the User Name macro into a JS variable?

2 answers

1 accepted

1 vote
Answer accepted
Ron Linder April 3, 2018

OK managed to do it via ajs.params.remoteUser, but I get the ID of the user not the name...

 

OK solved, its ajs.params.userDisplayName

Susan S. May 23, 2018

I am trying to do something similar for an analytics profile. How did you code the parameter?

Like joe percival likes this
Ron Linder May 24, 2018

Here is a sample:

<body>
<script type="text/javascript">
AJS.toInit(function(){ 
alert("ID: " + AJS.params.remoteUser.toUpperCase() + " | UserName: " + AJS.params.userDisplayName + " | PageID: " + AJS.params.pageId); 
}); 
</script>
</body>

Place this into an HTML macro...

Susan S. May 24, 2018

So I was able to use that variable to get this:

var remoteUser = AuthenticatedUserThreadLocal.getUsername();
_paq.push(['setUserId', 'xxx']);
_paq.push(["setCustomVariable", 1, "username", remoteUser, "visit"]);

However, I am at a loss a since I need to acquire username in DN form for the UserID.

Ron Linder May 24, 2018

It looks like you are trying to write variables rather than read variables...that I haven't tried yet.

Like joe percival likes this
Susan S. June 3, 2018

That method only works in older versions since the newer versions disable AJS.

var user = AJS.Meta.get("remote-user");
var userdn = AJS.params.userDisplayName;
var userremote = AJS.Meta.get("remote-user");

Like # people like this
0 votes
Christopher Siebert December 21, 2018

Here is what I use:

var userFullname = $("#user-menu-link").attr("title");

Your mileage may vary depending on how your Confluence is configured.

Suggest an answer

Log in or Sign up to answer