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!