Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Track browser width with Limesurvey?

  • steveleathers
  • steveleathers's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 5 months ago #113032 by steveleathers
Track browser width with Limesurvey? was created by steveleathers
I want to track how many of my survey respondents are viewing completing a certain survey on a phone or tablet vs. a desktop computer. Is there a way to do this with Limesurvey? If so, how?
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 5 months ago #113033 by holch
Replied by holch on topic Track browser width with Limesurvey?
Well, you can read the width and the height of the viewport via Javascript (as long as the browser is able to run Javascript).

Then you write this into a question (e.g. Equation) and you have the data.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • steveleathers
  • steveleathers's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 5 months ago #113035 by steveleathers
Replied by steveleathers on topic Track browser width with Limesurvey?
Interesting. I've added an equation question to a test survey, and the browser shows the browser width in the question, but it doesn't get saved to the database.

I've also found that the width doesn't get passed to other questions using the expression manager.
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 5 months ago #113038 by holch
Replied by holch on topic Track browser width with Limesurvey?
I think it should save the value. But in your case you could also just write it into a normal text question and hide this question.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
More
9 years 5 months ago #113039 by Ben_V
Replied by Ben_V on topic Track browser width with Limesurvey?
You can try with a text question instead equation question type...

I only can give you a code to retrieve the browser details, but it would be something similar... This code works perfectly if you paste it in the source of a long text question type...
Code:
<script type="text/javascript">
var SID = '{SID}';
var GID = '{GID}';
var QID = '{QID}';  
 
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = navigator.appName;
var fullVersion  = ''+parseFloat(navigator.appVersion); 
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;
 
// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
 browserName = "Opera";
 fullVersion = nAgt.substring(verOffset+6);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
 fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome" 
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
 browserName = "Chrome";
 fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
 browserName = "Safari";
 fullVersion = nAgt.substring(verOffset+7);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox" 
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
 browserName = "Firefox";
 fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent 
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
          (verOffset=nAgt.lastIndexOf('/')) ) 
{
 browserName = nAgt.substring(nameOffset,verOffset);
 fullVersion = nAgt.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
  browserName = navigator.appName;
 }
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
   fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
   fullVersion=fullVersion.substring(0,ix);
 
majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
 fullVersion  = ''+parseFloat(navigator.appVersion); 
 majorVersion = parseInt(navigator.appVersion,10);
}
 
$(document).ready(function() {
  $('#answer'+SID+'X'+GID+'X'+QID).val(''
+'Browser name = '+browserName+'<br>'
+'Full version = '+fullVersion+'<br>'
+'Major version = '+majorVersion+'<br>'
+'navigator.appName = '+navigator.appName+'\n'
+'navigator.userAgent = '+navigator.userAgent); 
 
// hide the question 
// $('#question'+QID).hide();
 
});
</script>

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The following user(s) said Thank You: socius
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 5 months ago - 9 years 5 months ago #113041 by holch
Replied by holch on topic Track browser width with Limesurvey?
Works like a charme!

To hide the question you have to delete // before the question hide code (basically last line). I already wanted to complain... ;-)

Thanks for sharing this.

Should be easily adaptable for the viewport.

Here my version (there might be a more elegant way, I am really rusty in JS):
Code:
<script type="text/javascript">
var SID = '{SID}';
var GID = '{GID}';
var QID = '{QID}';  
 
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = navigator.appName;
var fullVersion  = ''+parseFloat(navigator.appVersion); 
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;
 
// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
 browserName = "Opera";
 fullVersion = nAgt.substring(verOffset+6);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
 fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome" 
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
 browserName = "Chrome";
 fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
 browserName = "Safari";
 fullVersion = nAgt.substring(verOffset+7);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox" 
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
 browserName = "Firefox";
 fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent 
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
          (verOffset=nAgt.lastIndexOf('/')) ) 
{
 browserName = nAgt.substring(nameOffset,verOffset);
 fullVersion = nAgt.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
  browserName = navigator.appName;
 }
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
   fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
   fullVersion=fullVersion.substring(0,ix);
 
majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
 fullVersion  = ''+parseFloat(navigator.appVersion); 
 majorVersion = parseInt(navigator.appVersion,10);
}
 
var vpw = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
var vph = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
 
$(document).ready(function() {
  $('#answer'+SID+'X'+GID+'X'+QID).val(''
+'Browser name = '+browserName+'\n'
+'Full version = '+fullVersion+'\n'
+'Major version = '+majorVersion+'\n'
+'navigator.appName = '+navigator.appName+'\n'
+'navigator.userAgent = '+navigator.userAgent+'\n'
+'viewport-width = '+vpw+'\n'
+'viewport-height = '+vph+'\n');
 
// hide the question 
//$('#question'+QID).hide();
 
});
</script>

I got rid of the line breaks in html, because in the text field, they do no good and subsituted them with '\n', which looks better in the text field.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

Last edit: 9 years 5 months ago by holch.
The following user(s) said Thank You: Ben_V
The topic has been locked.
  • tammo
  • tammo's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
6 years 10 months ago #153322 by tammo
Replied by tammo on topic Track browser width with Limesurvey?
I played around with this. Please see the attached .lss file, containing a question with this code.

It is truely a good solution!
Thanks!


Tammo ter Hark at Respondage
For Limesurvey reporting, education and customized themes
respondage.nl
The following user(s) said Thank You: socius
The topic has been locked.
More
6 years 3 months ago #160943 by socius
Replied by socius on topic Track browser width with Limesurvey?
Dear all,

thanks to @Ben_V and @tammo for this great solution! Works perfectly.

I just had some issues with a reimported survey (from tab separated txt file) - and found a solution:
  • In an exported/reimported survey the code does not work anymore.
  • In the process of the txt export the code gets rid of line feeds, so the whole javascript is reduced to one line.
  • The fix is simple - add two semicolons:

Change
Code:
var vpw = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
var vph = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)

to
Code:
var vpw = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var vph = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

That's it.

Thanks again!

Best,
G
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose