Forum Discussion

BridgetODell's avatar
BridgetODell
Community Member
6 years ago

Export Variable Values via Email Message using JavaScript

Ever wish you had visibility into how users are interacting with the courses you create, such as which slides they’re viewing, what answer they selected in each question of a quiz? Well, you can have visibility into this using Storyline Variables and JavaScript! 

This article explains how to export variable values from a Storyline file into the body of an email message using JavaScript. This can be done either by using pre-defined “To” and “Subject” fields or by using JavaScript to populate the “To” and “Subject” fields with user-specified variable values.

This functionality has many benefits. It provides as much visibility into how the user is interacting with the course as you desire, depending on how much variable programming you build into your course. If you want to just receive an email when a user completes a course, you can use this programming for something that simple. Or, if you’re like me and want a lot more visibility into the inner workings of how a user is interacting with the course, you can program it so that the email message contains information on which slides the user viewed, what answer they selected for each quiz question, their overall quiz score, the values of any entry fields they typed text/numbers into within the course, and much more. Anything with a Storyline variable value associated with it can be exported via email.

 

Instructions:

1.      Build your course, including all variables and triggers to adjust the variables as needed throughout the course. If desired, add a text entry field somewhere in the course where the user can specify the email address where these variable values should be sent. You can do the same thing if you would like the user to specify the subject of the email message via text entry field. If you are using this method, ensure you require the user to populate these fields (prevent them from leaving these fields blank).

2.      On the last slide of the course, or whichever slide you would like this email sent from, add the trigger with the JavaScript code listed below.

Note: This can be placed on whatever object is applicable to your course, such as a submit button, upon timeline start of the slide, etc.

 

Within the body_start section, you can add as many of the player.GetVar(“VariableName”) actions as you need to pull in as many variable values as desired. In addition, you can insert any plain text in quotation marks “ “. Just be sure to add a plus sign (+) between each aspect of this function to ensure it is all added as a text string.

 

Populate Email Message with Pre-Defined Email Address and Subject:

Action: Execute JavaScript

Script:

var email="YourEmailAddress@email.com";

var subject="Subject Goes Here";

var player = GetPlayer();

var body_start= “Any basic text you want to include, just place it in quotation marks " + player.GetVar("YourVariable1Name") + “Any other basic text you want to include " + player.GetVar("YourVariable2Name");

var mailto_link='mailto:'+email+'?subject='+subject+'&body='+body_start;

win=window.open(mailto_link,'emailWin');

When: (Desired Action)

Object: (Desired Object)

 

Populate Email Message with User-Defined Email Address and Subject:

Action: Execute JavaScript

Script:

var player = GetPlayer();

var email= player.GetVar(“UserDefinedEmailVariableName”);

var subject= player.GetVar(“UserDefinedSubjectVariableName”);

var body_start= “Any basic text you want to include, just place it in quotation marks " + player.GetVar("YourVariable1Name") + “Any other basic text you want to include " + player.GetVar("YourVariable2Name");

var mailto_link='mailto:'+email+'?subject='+subject+'&body='+body_start;

win=window.open(mailto_link,'emailWin');

When: (Desired Action)

Object: (Desired Object)

  

3.      Publish the course to HTML to test. Note: You cannot test JavaScript using Preview mode.

 

4.      Test the trigger (click the button, view the slide, etc.) which activates the JavaScript programming within your course.

 

5.      An Email Message opens with the contents of the Variable values and plain text displayed.

 

6.      Click Send to send the Variable values and plain text to the designated user(s).

 

For additional information regarding JavaScript & Storyline 2, see the article:

https://articulate.com/support/article/javascript-best-practices-and-examples-sl2

 

Happy Programming!

  • JohnFritts's avatar
    JohnFritts
    Community Member

    Hi Starla and All, I have learned enough about JavaScript on my own and through the help of others that I have arrived at an acceptable solution. The goal was create quizzes where the results are sent to a mgmt. reviewer for analysis without the quiz taker seeing the results. I have used an Execute JS Trigger, extracted SL variable values and encrypt the quiz results with each answer in the email. If answered wrong, the text is Qn = Monday, if answer correct, Qn = Any day of the week but Monday.  My sample quiz and results email are attached.

    • StarlaWest's avatar
      StarlaWest
      Community Member

      Thank you, John! I'll dig in and take a look at what you've so graciously shared.

  • I know this is an older thread - but for anyone picking it up, it is worth pointing out that the 'mailto' link within a JavaScript send email function only works if the learner has an email client installed on their device.

    There are companies (like mine) that have a strict "One copy is enough" policy and don't have email clients installed on ANY devices. We read and send email from our cloud server and do not 'synch' email with any devices.

    The reasons for this are:

    • Data security and privacy (emails are held only in one place and not on mobile devices)
    • Single source content control - we never suffer from the problem "I haven't got your email my device hasn't synched yet"
    • Environmental concern - we attempt to minimise our data storage and volume of data transmission over the internet (this will become a far more serious problem as the true carbon footprint of massive data centres and internet traffic becomes fully understood).

    I don't know if that was the problem David referred to above - but it could be. 

  • JohnFritts's avatar
    JohnFritts
    Community Member

    Is there a way to trigger the email using JS and pull in some variable values as you showed here but to use a preconfigured email template file in Outlook AND send it invisible to the quiz taker to a defined quiz reviewer on submission of the quiz results.

    • BridgetODell's avatar
      BridgetODell
      Community Member

      Hi John,

      Unfortunately I don't think this is possible (at least not per my knowledge and testing).

      The email message that is sent has a very basic formatting (only contains any plain text that was a part of the JavaScript and any variable values that are configured to be a part of it) and pre-configured email templates cannot be used. In addition, to my knowledge there is no way to send an email without the learner clicking "send" within the email message that appears. 

      If you're looking for sending results to something without the learner's knowledge you may need to use an LCS and xAPI or API or look into exporting data to a spreadsheet like Google Spreadsheets - I know there are several articles on how to do this on this site.

      Hope that helps!

  • StarlaWest's avatar
    StarlaWest
    Community Member

    Thank you, Bridget! Do you by chance have a sample email you can share with us? I'd love to see what these emails look like. Thanks a ton!

  • Hi Bridget O'Dell,

    Thanks so much for sharing these instructions. The JavaScript trigger isn't working for me for some reason. Is it possible that my organization is blocking the email? 

    Thank you,

    David

  • NatHampson's avatar
    NatHampson
    Community Member

    Hi all,

    Can anybody help with a SL block in RISE problem related to this javascript? My desired outcome is for an automated email to be sent to me to confirm a learner's RISE course completion. I am not receiving any emails via this SL block in RISE.

    I have created a SL file that contains a slide with name and date variables, added the suggested Javascript for an automated email, and added the SL block into a RISE course. 

    Thanks,

    Natalie

  • Hi Natalie

    I guess the first question is: "Is the code working in Storyline?" There are a couple of reasons it might not be.

    A small precaution you might want to take is to URL encode the subject and email body line:

    var subject = encodeURIComponent('Subject Line Here'); // URL encode the subject

    var emailBody = encodeURIComponent('Your email content goes here.'); // URL encode the body of the email

    This removes the risk that special characters in the subject line are misinterpreted as control characters within the mailto: statement:

    window.open('mailto:' + emailAddress + '?subject=' + subject + '&body=' + emailBody);

    The other limitations using this method are:

    • The method relies on the user having a default email client set up that handles mailto: links (as per my earlier post in this thread this might not always be the case).
    • Some browsers or security settings may block mailto: operations, especially if pop-ups are disabled.
    • The amount of text that can be sent via a mailto: link might be limited, depending on the email client.

    If it works OK in Storyline then it should work in RISE - but there is another whole layer of complexity introduced and your problem might be related to security policies set up on the hosting platform.

    You might want to consider using an alternative approach sending the email through an intermediary service - I posted an article on this some time ago:

    Using an Intermediary service to send email from Storyline - Building Better Courses Discussions - E-Learning Heroes (articulate.com)

    Hope this helps...