psychopy.org | Reference | Downloads | Github

How can I detect participant's OS?

URL of experiment: https://run.pavlovia.org/marchesini.id/comportamientolinguistico_pilot3/html/

Description of the problem: I’ve been searching how to detect the participant’s OS in which he/she is running the experiment but found nothing.

I only found 2 possible codes in JS but I think I can’t just paste it on my online experiment, can I?

First one:

function getOS() {
  var userAgent = window.navigator.userAgent,
      platform = window.navigator.platform,
      macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
      windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
      iosPlatforms = ['iPhone', 'iPad', 'iPod'],
      os = null;

  if (macosPlatforms.indexOf(platform) !== -1) {
    os = 'Mac OS';
  } else if (iosPlatforms.indexOf(platform) !== -1) {
    os = 'iOS';
  } else if (windowsPlatforms.indexOf(platform) !== -1) {
    os = 'Windows';
  } else if (/Android/.test(userAgent)) {
    os = 'Android';
  } else if (!os && /Linux/.test(platform)) {
    os = 'Linux';
  }

  return os;
}

alert(getOS());

Second one:

// This script sets OSName variable as follows:
// "Windows"    for all versions of Windows
// "MacOS"      for all versions of Macintosh OS
// "Linux"      for all versions of Linux
// "UNIX"       for all other UNIX flavors 
// "Unknown OS" indicates failure to detect the OS

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);

Any ideas on how to do it?

Hi @imarchesini, this is fine. In a code component, select the code type as JS (or both, where JS is on the right hand panel), and paste the following to have the OS printed to the browser console (press F12 to see developers tools, and go to the console tab):

console.log(window.navigator.platform) // on my windows machine, outputs win32

For this to be saved in the data file it presumably needs to go into either expInfo, so it will be saved alongside every data line like participant id and date. Something like this (untested):

expInfo['OS'] = window.navigator.platform;

@imarchesini this can certainly be added as a code component (in the begin experiment setting) but I think it’s something we should also add to the Builder-generated scripts.

Done, see https://github.com/psychopy/psychopy/pull/2510

Hi,

I would like to store information about the browser and OS used by subjects. From the conversation above, I gathered this was already implemented in the Builder for detecting the OS, but I couldn’t figure out whether this is automatic or some option must be selected in the Builder.

Regarding the browser used - does anyone have an idea of how to store that info too?

Thanks!

You should be able to detect the browser using this function:

function getBrowserId () {
    var browsers = ["MSIE", "Firefox", "Safari", "Chrome", "Opera"];
    sUsrAg = window.navigator.userAgent,
    nIdx = browsers.length - 1;
    for (nIdx; nIdx > -1 && sUsrAg.indexOf(browsers [nIdx]) === -1; nIdx--);

  return browsers[nIdx];
}

myBrowser = getBrowserId();
console.log(myBrowser);

This was taken from the MDN Website.

1 Like

Thanks!

So, to store both browser and OS used in the data file, I should just put it in the ExpInfo dictionary, right? Since I am currently using the entries in ExpInfo for my diag box, will the browser/OS infos appear in the diag box too? If so, how can I avoid that?

Can anybody help? I added the two info as part of the expInfo dictionary:

var sUsrAg;
var nIdx;
function getBrowserId () {
    var browsers = ["MSIE", "Firefox", "Safari", "Chrome", "Opera"];
    sUsrAg = window.navigator.userAgent,
    nIdx = browsers.length - 1;
    for (nIdx; nIdx > -1 && sUsrAg.indexOf(browsers [nIdx]) === -1; nIdx--);

  return browsers[nIdx];
}

// store info about the experiment session:
let expName = 'exp';  // from the Builder filename that created this script
let expInfo = {'list': ['1','2']};

expInfo['OS'] = window.navigator.platform;
expInfo['browser'] = getBrowserId();

// schedule the experiment:
psychoJS.schedule(psychoJS.gui.DlgFromDict({
  dictionary: expInfo,
  title: 'Please fill out the form below to start'
}));

but now the diagbox shows that information as well. How can I hide the OS and browser information from it?

You could move the new expInfo entries after the call to the dialog box and they will not be included. If the data is not saving, then make explicit calls to save the data e.g.:

psychoJS.experiment.addData("OS", expInfo['OS']);
1 Like

I finally got the chance to test this. If others want to put OS and browser information on the output data files of subjects, this can be done as follows:

// open window:
psychoJS.openWindow({
  fullscr: true,
  color: new util.Color([0, 0, 0]),
  units: 'norm'
});

// the function below detects the browser used. this function
// can be put anywhere at the beginning of the experiment
// but needs to be *before* the flow scheduler get defined
var sUsrAg;
var nIdx;
function getBrowserId () {
    var browsers = ["MSIE", "Firefox", "Safari", "Chrome", "Opera"];
    sUsrAg = window.navigator.userAgent,
    nIdx = browsers.length - 1;
    for (nIdx; nIdx > -1 && sUsrAg.indexOf(browsers [nIdx]) === -1; nIdx--);

  return browsers[nIdx];
}

// store info about the experiment session:
let expName = 'exp';  // from the Builder filename that created this script

// defining flowSchedule
...

// the info can then be inserted in the function updateInfo() below
var frameDur;
function updateInfo() {
  expInfo['date'] = util.MonotonicClock.getDateStr();  // add a simple timestamp
  expInfo['expName'] = expName;
  expInfo['psychopyVersion'] = '3.1.2';
  expInfo['OS'] = window.navigator.platform; // storing OS info
  expInfo['browser'] = getBrowserId(); // storing browser info

  // store frame rate of monitor if we can measure it successfully
  expInfo['frameRate'] = psychoJS.window.getActualFrameRate();
  if (typeof expInfo['frameRate'] !== 'undefined')
    frameDur = 1.0/Math.round(expInfo['frameRate']);
  else
    frameDur = 1.0/60.0; // couldn't get a reliable measure so guess

  // add info from the URL:
  util.addInfoFromUrl(expInfo);
  
  return Scheduler.Event.NEXT;
}