Forum Discussion
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. Is there a way to set a variable with data from the LMS when the course is loaded instead of having the user enter their name?
Thx
215 Replies
- AngelaKimCommunity Member
Hm, they're the ones asking me if it's possible to do this in Storyline :( Thanks for your input.
- AngelaKimCommunity Member
Thank you, Mr. Matthew Bibby!!! That would be fantastic. Let me see what they say and get back to you!
- SteveFlowersCommunity Member
Hi Angela -
Depends on how / where it's displayed. When the course launches, if the info is contained in the query string ?value=name, then it's pretty easy to grab.
If the info you want to grab appears in a parent window or frame consistently, it can be scraped with some creative javascript. Try this. You can retrieve your name from the title attribute of the user image in the upper right of the elearning heroes site by adding this to the developer's console:
console.log(document.getElementsByClassName("user__img")[0].getAttribute("title"));
Right-click the window and choose inspect (in Chrome) then click the console tab. Paste that line in there. The same concept applies to anything that appears in a page. You can scrape that data from a displayed page easily. Same concept applies to pages that aren't displayed. You can grab stuff from a page return and chisel little bits of data if the page address is reliable and the "address" of the information within the DOM is predictable.
Anything that appears in a page structure can be grabbed from Storyline as long as the user has access to the page. - CuentaOnceCommunity Member
- LucioCommunity Member
If ever the "Works in all" portion doesn't work as is, try preceding it with the following script:
var player = GetPlayer();
function findLMSAPI(win) {
// look in this window
if (win.hasOwnProperty("GetStudentID")) return win;// all done if no parent
else if (win.parent == win) return null;// climb up to parent window & look there
else return findLMSAPI(win.parent);
}var lmsAPI = findLMSAPI(this);
… and the rest following "...GetPlayer() …"
- philMekCommunity Member
Hello everybody,
I read every single message in this thread, and in some others.
But couldn't find what I am looking for :
My client LMS (Talentsoft) in SCORM 2004, can display cmi.learner_name as a variable.
I tried many many many syntaxes to grab this data, but I guess the only working one wasn't in my scope !
Can anymone help me ? what is the syntaxt to grab this value ?
thank you for the help
Philippe
- philMekCommunity Member
Hello again,
Matthew Bibby gave me the answer. It works perfectly in Talensoft LMS.
Be aware that I only needed the firstname.
Here is his code to close my question.
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 name = lmsAPI.SCORM2004_GetStudentName();
var nameArray = name.split(',');
var firstName = nameArray[1];
// var lastName = nameArray[0];
// var fullName = firstName + ' ' + lastName;
var player = GetPlayer();
player.SetVar("Name",firstName);
Philippe
- BrianBland-ClarCommunity Member
I have unfortunately been unable to extract any information using many permutations of the code above from the LMS to my Articulate file. I am testing this with LearnDash.
If anyone has a .story file that works published as SCORM 2004 I would love to see it!
Thanks
- Anjumprawez-b02Community Member
- Jonathan_HillSuper Hero
Hey all! I've been using variations of this code for a while now to add my learner's name to in-course instructions and certificates and it's been very useful. Thanks all!
However, I've found that platforms such as Litmos, Docebo, and Cornerstone, tend to add an unwanted space before the first name.
In the past I've worked around this by reducing the left margin on the text box that displays the variable, as I'm not really a coder. But recently I've been 'chatting' with ChatGPT to analyze and refine code.
During a recent chat, ChatGPT provided this updated code. It pulls the user name from the LMS, removes any spaces, and splits it into First Name and Last Name.And it works!
var player = GetPlayer();
function findLMSAPI(win) {
if (win.hasOwnProperty("GetStudentID")) return win;
else if (win.parent == win) return null;
else return findLMSAPI(win.parent);
}
function removeSpaces(name) {
return name.replace(/\s/g, '');
}
var lmsAPI = findLMSAPI(this);
var myName = lmsAPI.GetStudentName();
if (myName.charAt(0) === ' ') {
myName = myName.substring(1);
}
var array = myName.split(',');
var firstName = removeSpaces(array[1]);
var lastName = removeSpaces(array[0]);
player.SetVar("FirstName", firstName);
player.SetVar("LastName", lastName);- OwenHoltSuper Hero
But what if the last name is complex? de Jesus, Von Trapp, Van Gogh??????
- Jonathan_HillSuper Hero
That was my concern too - but on my LMS it appears this code parses the name from only the first break. So Brian De Palma is split into Brian and De Palma. As always with Javascript, results may vary but this worked for me on Docebo.
Related Content
- 3 years ago
- 9 months ago
- 9 months ago