Slide Visit count using a single SL variable & Javascript | identify each Slide uniquely

Feb 12, 2015

I had a need to track the number of visits on each slide of my 100+ slide course. Rather than creating one variable for each slide to track number of times the learner visited a particular slide (and then take actions based on count), I wanted to use only one SL variable. Seemed impossible at first, but some advanced Javascript to the rescue.

This solution works for me. Its not elegant, and requires manual effort after publishing the story. However, it meets my need to minimize number of SL variables.

There were two main hurdles:

1> SL does not expose system variables so how do you find out unique slide identifier

2> in Javascript trigger, how do you REMEMBER how many times it was called in the past

1>> I cleared first hurdle by taking advantage of the fact that each trigger that executes Javascript is defined as a distinct function in user.js with a unique function name. So by parsing out the name of the js function it is possible to uniquely identify each slide (if there are multiple javascript triggers on a single slide, use this solution only on the first one- typically the one that runs on slide timeline start trigger)

Example: function Script1()

Used following JS snippet to extract the name of the function from within it.

var fName = arguments.callee.toString().match(/function ([^\(]+)/)[1]

2>> The next hurdle was to get around the scoping issue i.e. each call to this javascript fucntion will be treated as if its the first time. Got around this by defining a GLOBAL array a new javascript file, say my.js. Added following in my.js

var slideid_arr = {};
slideid_arr["Script1"]=0;

slideid_arr["Script2"]=0;

NOTE: you need to enter one line above for each slide that meets these two conditions: 1> slide has a javascript trigger in it, 2> you want to track number of visits. As mentioned earlier, this is not pretty, but the good thing is the number of slides will not change too much, so you copy/paste number of lines once and then are all set.

Then, copied my.js in /story_content/ folder of published output. And then modified story.html to add reference to my.js BEFORE existing reference to user.js

Then in the javascript function in each slide added following JS code:

var fName = arguments.callee.toString().match(/function ([^\(]+)/)[1];

var player = GetPlayer();
slideid_arr[fName]++;
player.SetVar("curslidevisit",slideid_arr[fName]);

Note: 'curslidevisit' is the only variable that needs to be created in SL. It will correctly show # of visits in each distinct slide.

Hope this helps you in other ways.

7 Replies
Christie Pollick

Hi, Michael -- Thanks for reaching out, and as it's been over a year since the last activity on this post, perhaps you  might want to reach out to Ron directly via the 'Contact Me' link on his profile page? Otherwise, we'll need to defer to your fellow community member to assist you further, as JS is not something for which we can offer support. 

This discussion is closed. You can start a new discussion or contact Articulate Support.