Forum Discussion
AndrewRost
13 years agoCommunity Member
Retrieve LMS User Name as Variable
I know you can have a user fill in a text box with their name and then later use that variable for personalization. What we would like to do is programmatically retrieve the user name from the LMS....
Jonathan_Hill
2 years agoSuper Hero
For anyone not using Docebo (which handles complex surnames like De Palma or Van Gogh) , Chat GPT recommended the following amendments to the code:
var player = GetPlayer();
function findLMSAPI(win) {
if (win.hasOwnProperty("GetStudentID")) return win;
else if (win.parent == win) return null;
else return findLMSAPI(win.parent);
}
var lmsAPI = findLMSAPI(this);
var myName = lmsAPI.GetStudentName();
if (myName.charAt(0) === ' ') {
myName = myName.substring(1);
}
var regex = /([^,]+),(.+)/;
var match = myName.match(regex);
var lastName = match[1];
var firstName = match[2];
player.SetVar("firstName", firstName);
player.SetVar("lastName", lastName);