Welcome to the LimeSurvey Community Forum

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

Is Limesurvey able to detect the used device?

  • kasparis
  • kasparis's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months ago #204683 by kasparis
Hi,

I've been wondering if there is an option that Limesurvey is able to determine whether respondents use a smartphone or a computer.
So that the respondents are automatically directed to the "correct" survey, depending on their choice of device.

Thanks a lot
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago #204684 by tpartner
Replied by tpartner on topic Is Limesurvey able to detect the used device?
Are you trying to differentiate between touch-screens vs desktop or simply by screen size?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago #204685 by Joffm
Enter this script in a question of type "long free text" (source code mode)
Code:
<script type="text/javascript" charset="utf-8">
  /**
   * JavaScript Client Detection
   * (C) viazenetti GmbH (Christian Ludwig)
   */
  (function (window) {
    {
      var unknown = '-';
 
      // screen
      var screenSize = '';
      if (screen.width) {
        width = (screen.width) ? screen.width : '';
        height = (screen.height) ? screen.height : '';
        screenSize += '' + width + " x " + height;
      }
var vpw = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); 
var vph = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); 
      // browser
      var nVer = navigator.appVersion;
      var nAgt = navigator.userAgent;
      var browser = navigator.appName;
      var version = '' + parseFloat(navigator.appVersion);
      var majorVersion = parseInt(navigator.appVersion, 10);
      var nameOffset, verOffset, ix;
 
      // Opera
      if ((verOffset = nAgt.indexOf('Opera')) != -1) {
        browser = 'Opera';
        version = nAgt.substring(verOffset + 6);
        if ((verOffset = nAgt.indexOf('Version')) != -1) {
          version = nAgt.substring(verOffset + 8);
        }
      }
      // Opera Next
      if ((verOffset = nAgt.indexOf('OPR')) != -1) {
        browser = 'Opera';
        version = nAgt.substring(verOffset + 4);
      }
      // MSIE
      else if ((verOffset = nAgt.indexOf('MSIE')) != -1) {
        browser = 'Microsoft Internet Explorer';
        version = nAgt.substring(verOffset + 5);
      }
      // Chrome
      else if ((verOffset = nAgt.indexOf('Chrome')) != -1) {
        browser = 'Chrome';
        version = nAgt.substring(verOffset + 7);
      }
      // Safari
      else if ((verOffset = nAgt.indexOf('Safari')) != -1) {
        browser = 'Safari';
        version = nAgt.substring(verOffset + 7);
        if ((verOffset = nAgt.indexOf('Version')) != -1) {
          version = nAgt.substring(verOffset + 8);
        }
      }
      // Firefox
      else if ((verOffset = nAgt.indexOf('Firefox')) != -1) {
        browser = 'Firefox';
        version = nAgt.substring(verOffset + 8);
      }
      // MSIE 11+
      else if (nAgt.indexOf('Trident/') != -1) {
        browser = 'Microsoft Internet Explorer';
        version = nAgt.substring(nAgt.indexOf('rv:') + 3);
      }
      // Other browsers
      else if ((nameOffset = nAgt.lastIndexOf(' ') + 1) < (verOffset = nAgt.lastIndexOf('/'))) {
        browser = nAgt.substring(nameOffset, verOffset);
        version = nAgt.substring(verOffset + 1);
        if (browser.toLowerCase() == browser.toUpperCase()) {
          browser = navigator.appName;
        }
      }
      // trim the version string
      if ((ix = version.indexOf(';')) != -1) version = version.substring(0, ix);
      if ((ix = version.indexOf(' ')) != -1) version = version.substring(0, ix);
      if ((ix = version.indexOf(')')) != -1) version = version.substring(0, ix);
 
      majorVersion = parseInt('' + version, 10);
      if (isNaN(majorVersion)) {
        version = '' + parseFloat(navigator.appVersion);
        majorVersion = parseInt(navigator.appVersion, 10);
      }
 
      // mobile version
      var mobile = /Mobile|mini|Fennec|Android|iP(ad|od|hone)/.test(nVer);
 
      // cookie
      var cookieEnabled = (navigator.cookieEnabled) ? true : false;
 
      if (typeof navigator.cookieEnabled == 'undefined' &amp;&amp; !cookieEnabled) {
        document.cookie = 'testcookie';
        cookieEnabled = (document.cookie.indexOf('testcookie') != -1) ? true : false;
      }
 
      // system
      var os = unknown;
      var clientStrings = [
        { s:'Windows 10', r:/(Windows 10.0|Windows NT 10.0)/ },
        { s:'Windows 8.1', r:/(Windows 8.1|Windows NT 6.3)/ },
        { s:'Windows 8', r:/(Windows 8|Windows NT 6.2)/ },
        { s:'Windows 7', r:/(Windows 7|Windows NT 6.1)/ },
        { s:'Windows Vista', r:/Windows NT 6.0/ },
        { s:'Windows Server 2003', r:/Windows NT 5.2/ },
        { s:'Windows XP', r:/(Windows NT 5.1|Windows XP)/ },
        { s:'Windows 2000', r:/(Windows NT 5.0|Windows 2000)/ },
        { s:'Windows ME', r:/(Win 9x 4.90|Windows ME)/ },
        { s:'Windows 98', r:/(Windows 98|Win98)/ },
        { s:'Windows 95', r:/(Windows 95|Win95|Windows_95)/ },
        { s:'Windows NT 4.0', r:/(Windows NT 4.0|WinNT4.0|WinNT|Windows NT)/ },
        { s:'Windows CE', r:/Windows CE/ },
        { s:'Windows 3.11', r:/Win16/ },
        { s:'Android', r:/Android/ },
        { s:'Open BSD', r:/OpenBSD/ },
        { s:'Sun OS', r:/SunOS/ },
        { s:'Linux', r:/(Linux|X11)/ },
        { s:'iOS', r:/(iPhone|iPad|iPod)/ },
        { s:'Mac OS X', r:/Mac OS X/ },
        { s:'Mac OS', r:/(MacPPC|MacIntel|Mac_PowerPC|Macintosh)/ },
        { s:'QNX', r:/QNX/ },
        { s:'UNIX', r:/UNIX/ },
        { s:'BeOS', r:/BeOS/ },
        { s:'OS/2', r:/OS\/2/ },
        { s:'Search Bot', r:/(nuhk|Googlebot|Yammybot|Openbot|Slurp|MSNBot|Ask Jeeves\/Teoma|ia_archiver)/ }
      ];
      for (var id in clientStrings) {
        var cs = clientStrings[id];
        if (cs.r.test(nAgt)) {
          os = cs.s;
          break;
        }
      }
 
      var osVersion = unknown;
 
      if (/Windows/.test(os)) {
        osVersion = /Windows (.*)/.exec(os)[1];
        os = 'Windows';
      }
 
      switch (os) {
        case 'Mac OS X':
          osVersion = /Mac OS X (10[\.\_\d]+)/.exec(nAgt)[1];
          break;
 
        case 'Android':
          osVersion = /Android ([\.\_\d]+)/.exec(nAgt)[1];
          break;
 
        case 'iOS':
          osVersion = /OS (\d+)_(\d+)_?(\d+)?/.exec(nVer);
          osVersion = osVersion[1] + '.' + osVersion[2] + '.' + (osVersion[3] | 0);
          break;
      }
 
      // flash (you'll need to include swfobject)
      /* script src="//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" */
      var flashVersion = 'no check';
      if (typeof swfobject != 'undefined') {
        var fv = swfobject.getFlashPlayerVersion();
        if (fv.major > 0) {
          flashVersion = fv.major + '.' + fv.minor + ' r' + fv.release;
        }
        else  {
          flashVersion = unknown;
        }
      }
    }
 
    window.jscd = {
      screen: screenSize,
      browser: browser,
      browserVersion: version,
      browserMajorVersion: majorVersion,
      mobile: mobile,
      os: os,
      osVersion: osVersion,
      cookies: cookieEnabled,
      flashVersion: flashVersion,
            vieww : vpw,
            viewh: vph
    };
  }(this));
 
 
$(document).ready(function() {  
 
    // Hide this question (remove double slash below to hide)
    //$('#question{QID}').hide();
 
    // Load the question
    $('#question{QID} textarea').val(
    'OS: ' + jscd.os +' '+ jscd.osVersion + '\n' +
    'Browser: ' + jscd.browser +' '+ jscd.browserMajorVersion +
      ' (' + jscd.browserVersion + ')\n' + 
    'Mobile: ' + jscd.mobile + '\n' +
    'Flash: ' + jscd.flashVersion + '\n' +
    'Cookies: ' + jscd.cookies + '\n' +
    'Viewport width: ' + jscd.vieww + '\n' +
    'Viewport heigth: ' + jscd.viewh + '\n' +
    'Screen Size: ' + jscd.screen + '\n\n' +
    'Full User Agent: ' + navigator.userAgent
    );
    });
</script>

You will get a lot of information


and as you may restrict the output to the needed info you are able to filter or whatever you want to do.

And hide the question by uncommenting this line
//$('#question{QID}').hide();

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • kasparis
  • kasparis's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months ago #204687 by kasparis
Replied by kasparis on topic Is Limesurvey able to detect the used device?
we want our survey to be accessible to both smartphone and computer users. However, we have noticed that our questions (mainly tables) are displayed differently in the smartphone version (e.g. manual word division is no longer correct), so we would like to create two versions in which the respondents are redirected to the correct survey depending on the end device.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago #204689 by tpartner
Replied by tpartner on topic Is Limesurvey able to detect the used device?
What LimeSurvey version are you using?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • kasparis
  • kasparis's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months ago #204690 by kasparis
Replied by kasparis on topic Is Limesurvey able to detect the used device?
Version 3.22.17+200525
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago #204693 by tpartner
Replied by tpartner on topic Is Limesurvey able to detect the used device?
LimeSurvey does not display differently on smartphones, it uses media queries to adjust the layout of the array tables based on window width. It breaks the tables into a block layout below 760 pixels window width.

Using the window width to direct to different surveys is risky as respondents may be using tablets which can change orientation, therefore changing the window width above or below 760px.

Having said that, if you want to proceed with detecting window width, add this script to the source of a short-text or numeric-input question:

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Load the initial window width
    $('input:text', thisQuestion).val($(window).width()).trigger('keyup');
 
    // Dynamic resizing
    $(window).on('resize', function(e) {
      $('input:text', thisQuestion).val($(window).width()).trigger('keyup');
    });
    });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...7142.lss
File Size:26 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • kasparis
  • kasparis's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months ago #204733 by kasparis
Replied by kasparis on topic Is Limesurvey able to detect the used device?
Thank you for all your answers!
We do not want to differentiate between screen size. We want to direct the respondents to the Desktop Version (more tables and matrix questions) or the Smartphone Version (drop down instead of matrix questions if possible).

What my supervisor wants me to do is find out if Limesurvey is able to detect the end device via meta data (and Joffm gave a solution how I can get this information). However, I now still wonder how the actual steps are to direct respondents to the correct survey.
I made two surveys - one for the desktop version, one for the smartphone version.


I also thought of asking the respondents "Go to Dekstop Version" "Go to Smartphone Version" and then redirect them to the fitting survey. But I also have no clue how to implement this.

Thank you so much!
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago #204741 by Joffm
Well, you saw my answer?
Unfortunately in a different thread, but same name.


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago #204744 by DenisChenu
Replied by DenisChenu on topic Is Limesurvey able to detect the used device?

kasparis wrote: We want to direct the respondents to the Desktop Version (more tables and matrix questions) or the Smartphone Version (drop down instead of matrix questions if possible).

Use some javascript and responsive web design for this

Else : gitlab.com/SondagesPro/QuestionSettingsType/findUserAgentInfo

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • kasparis
  • kasparis's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months ago #204766 by kasparis
Replied by kasparis on topic Is Limesurvey able to detect the used device?
Hi, yes I did and it worked. Now I am trying to direct respondents to the correct survey with the information mobile= true or false

I think it's rather complicated and not as easy as I tried here:

if (mobile=true) {
window.open("https://XXX");
} else {
window.open("https://YYY");
}

but maybe it makes it clearer what I am trying to do.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago #204782 by Joffm
Why do you want to use two surveys?
Display the questions by relevance equation.
That's what we do usually.

If you only load the question (let's call it QMobile) with the information "true" or "false" for mobile
// Load the question
$('#question{QID} textarea').val(
jscd.mobile
);


you may display the questions for mobile with the relevance equation QMobile=="true" and the other with QMobile=="false"

If you really want to redirect to two different surveys you may use the end-url
Start the survey and set the end-url to something like
Code:
{if(QMobile=="true","https://mySurvey.de/index.php/111111","https://mySurvey.de/index.php/222222")}

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose