Capture Browser.UserAgent after every screen

Capture Browser.UserAgent after every screen

Summary How to capture the Browser's UserAgent after every screen using javascript
Applies to Askia Design
Written for Survey Designers
Keywords browser; UserAgent; user; agent; Browser.UserAgent; capture; open-ended; open; javascript; script


See attached example QEX.



Askia only captures one instance of the browser's UserAgent inside of the SQL database, meaning anytime you use the "Browser.UserAgent" keyword, it references the UserAgent that was captured in the database (which is the last device to enter into the survey). This Askia keyword does not keep track of which devices/UserAgents partook in the survey itself. Again, it only records the UserAgent of the last device that entered the survey or answered a question.

If you want to keep track of which UserAgent was used to answer which question, you'll need to use the below javascript method to pull in the UserAgent into an open-ended variable after every screen.



Askia uses the "ntext" data type used on the "Open Data" column in the SQL database that's used to store all of the data from an open-ended variable:

Surveys_Data_1_.png  -->  Surveys_Data_2_.png

According to Microsoft:

"Variable-length Unicode data with a maximum string length of 2^30 - 1 (1,073,741,823) bytes. Storage size, in bytes, is two times the string length that is entered."

Note: a single UserAgent string typically contains as many as 115(+-) UTF-8 characters:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36

Please make sure the amount of data you plan on capturing in this variable is below that limitation, else you may run into problems.

Step 1:

Create a punch chapter and a hidden open-ended variable:


Step 2:

Create a "set value" routing that happens after your punch chapter that sets a string value of "NA|NA|NA|NA|NA|NA|" to your hidden open-ended:



Step 3:

Insert the following into your "Internet Settings > Contents > Before content" section:


<input type ="hidden" id="_QOpen" name ="_QOpen" value="!!QOpen.Value!!">

Note: replace "QOpen" with the name of your open-ended variable.


Step 4:

Insert the following javascript onto each page where you'd like to capture the browser's UserAgent string:

Attention: where the script says SetAt(1 please change this from "1" to "2", or "3", or "4", etc. It needs to be a different, sequential number, per screen. 1 references the first "NA" that is punched in the open-ended, 2 references the second "NA", etc. Each screen's script will need to have its own "SetAt" value.


document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("_QOpen").value = '{%:= QOpen.Value.Split("|").SetAt(1,Browser.UserAgent).Join("|") %}';

Note: replace "QOpen" with the name of your open-ended variable.

Note: if you want to also capture the variable's shortcut name, you can use the following script:

document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("_QOpen").value = '{%:= QOpen.Value.Split("|").SetAt(1, "(" + CurrentQuestion.Shortcut + ")" + Browser.UserAgent).Join("|") %}';


Final results:

If everything was done correctly, your results should look like this:


Notice that the UserAgent strings that are captured are being split using the "|" vertical bar.

This is what it looks like if you're capturing the question's shortcut too:



Contact if you have any questions.

Have more questions? Submit a request