Capture the Browser UserAgent data after every screen
Summary | How to capture the Browser's UserAgent after every screen using JavaScript |
Applies to | AskiaDesign |
Written for | Survey Designers |
Keywords | browser; UserAgent; user; agent; Browser.UserAgent; capture; open-ended; open; javascript; script |
Update:
As of 5.5.3, it's preferred to use the newest "{All Questions}" starting point and set this via routing.
After/During > "{All Questions}" > Set Value to "HID_BrowserAgent" variable > HID_BrowserAgent + "|" + Browser.UserAgent
See attached example QEX.
Why?
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.
Disclaimer
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:
-->
According to Microsoft: https://docs.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql
"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.
<script>
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("_QOpen").value = '{%:= QOpen.Value.Split("|").SetAt(1,Browser.UserAgent).Join("|") %}';
});
</script>
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:
<script>
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("_QOpen").value = '{%:= QOpen.Value.Split("|").SetAt(1, "(" + CurrentQuestion.Shortcut + ")" + Browser.UserAgent).Join("|") %}';
});
</script>
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: