Limesurvey integration

Could we possibly get some information about the following concerning the Limesurvey integration?

  1. The javascript code that has to be entered in the End message
  2. The information that has to be entered in the Hidden question
  3. The code that has to be entered in the micro dialogue command (“show-web [url] …”)

Answer to (1) The javascript code that has to be entered in the End message:

Please note that the javascript code snippets below for MobileCoach apps built from the source code after 30th March 2020 is outdated (relevant commit is here). See the new answer.

The javascript code that needs to be entered is below. There are two cases,
a) If you don’t have to send any information from Limesurvey, please use the following,

<script>
function checkReady() {
if (window.postMessage != undefined && window.postMessage != null && typeof window.postMessage === 'function' && window.postMessage.length === 1) {
  sendResults()
  } else {
  setTimeout(checkReady, 100)
  }
}

function sendResults() {
  window.postMessage('complete');
}

$(document).ready(function() {
  setTimeout(checkReady, 100)
})
</script>

b) Alternatively, if you need to send any information to MobileCoach, i.e set a variable ($result) in MobileCoach with the results of the Limesurvey, please use the code below, (Please make sure that the access setting for the variable $result is set as “Manageable by Service” in the variables tab in MC Designer.

<script>
function checkReady() {
if (window.postMessage != undefined && window.postMessage != null && typeof window.postMessage === 'function' && window.postMessage.length === 1) {
  sendResults()
  } else {
  setTimeout(checkReady, 100)
  }
}

function sendResults() {
  var send_data = {
    'variable':'$result',
    'value': '100'
  };
  window.postMessage(JSON.stringify(send_data));
  window.postMessage('complete');
}

$(document).ready(function() {
  setTimeout(checkReady, 100)
})
</script>

The MobileCoach variable $result will have the value 100 in it after this execution. Also if you want to send an answer of a question in Limesurvey to MobileCoach you can use the following code, (see sendResults method)

<script>
function checkReady() {
if (window.postMessage != undefined && window.postMessage != null && typeof window.postMessage === 'function' && window.postMessage.length === 1) {
  sendResults()
  } else {
  setTimeout(checkReady, 100)
  }
}

function sendResults() {
  var send_data = {
    'variable':'$result',
    'value': '{Q01}'
  };
  window.postMessage(JSON.stringify(send_data));
  window.postMessage('complete');
}

$(document).ready(function() {
  setTimeout(checkReady, 100)
})
</script>

In the above code, {Q01} returns the answer to the question with question code Q01 in Limesurvey. This code is made for a text answer but for different question types the way of sending the answers to MobileCoach may differ.

Answer to (2) The information that has to be entered in the Hidden question

Hidden questions are used in our context to send information in the URL from MobileCoach to Limesurvey.

  1. For most purposes, the question can be of “Short free text” type.
  2. The option “Always hide this question” needs to be set so that this is not asked to the participant.
  3. Go to panel integration in the settings and add this question to the list.
  4. In the survey URL, you can add the information that will be set as an answer to the Hidden question.

Please refer to the following links for more information:

  1. https://manual.limesurvey.org/QS:Hidden
  2. https://manual.limesurvey.org/Panel_integration

Answer to (3) The command that has to be entered in the micro dialogue for the survey to appear in the chat.

show-web [url] [name of the button (spaces allowed)]

As an example, the following command would display in the chat as the image below,

show-web https://dev-cdhi.ethz.ch/limesurvey/index.php/592375?lang=en&particpantCode=$particpantCode&test=0 Fragebogen starten

Hi Prabhu, I used the javascript code a) (no info sent from LimeSurvey), but it doesn’t seem to work. Just wondering if the code you copied here is still the valid end message, or if something has changed meanwhile? Thanks!

Hi Sonja,

I have updated the javascript code that was outdated and added the new answer below.

Answer to (1) The javascript code that has to be entered in the End message:

The javascript code that needs to be entered is below. There are two cases,
a) If you don’t have to send any information from Limesurvey, please use the following,

<script>
function checkReady() {
if (window.ReactNativeWebView.postMessage != undefined && window.ReactNativeWebView.postMessage != null && typeof window.ReactNativeWebView.postMessage === 'function') {
  sendResults();
  } else {
  setTimeout(checkReady, 100);
  }
}

function sendResults() {
  window.ReactNativeWebView.postMessage('complete');
}

setTimeout(checkReady, 100);
</script>

b) Alternatively, if you need to send any information to MobileCoach, i.e set a variable ($result) in MobileCoach with the results of the Limesurvey, please use the code below, (Please make sure that the access setting for the variable $result is set as “Manageable by Service” in the variables tab in MC Designer.)

<script>
function checkReady() {
if (window.ReactNativeWebView.postMessage != undefined && window.ReactNativeWebView.postMessage != null && typeof window.ReactNativeWebView.postMessage === 'function') {
  sendResults();
  } else {
  setTimeout(checkReady, 100);
  }
}

function sendResults() {
  var send_data = {
    'variable':'$result',
    'value': '100'
  };
  window.ReactNativeWebView.postMessage(JSON.stringify(send_data));
  window.ReactNativeWebView.postMessage('complete');
}

setTimeout(checkReady, 100);
</script>

The MobileCoach variable $result will have the value 100 in it after this execution. Also if you want to send an answer of a question in Limesurvey to MobileCoach you can use the following code, (see sendResults method)

<script>
function checkReady() {
if (window.ReactNativeWebView.postMessage != undefined && window.ReactNativeWebView.postMessage != null && typeof window.ReactNativeWebView.postMessage === 'function') {
  sendResults();
  } else {
  setTimeout(checkReady, 100);
  }
}

function sendResults() {
  var send_data = {
    'variable':'$result',
    'value': '{Q01}'
  };
  window.ReactNativeWebView.postMessage(JSON.stringify(send_data));
  window.ReactNativeWebView.postMessage('complete');
}

setTimeout(checkReady, 100);
</script>

In the above code, {Q01} returns the answer to the question with question code Q01 in Limesurvey. This code is made for a text answer but for different question types the way of sending the answers to MobileCoach may differ.

Hi Prabhu, reviving this old topic :slight_smile: thanks very much for the code above, it works well for 1 result from LimeSurvey. Could you please show me how to adapt the code if I need several results returned (from several questions in LimeSurvey)? Thank you!

For multiple MobileCoach variables ($result1, $result2, …) to be set from Limesurvey, you can send as shown below,

<script>
function checkReady() {
if (window.ReactNativeWebView.postMessage != undefined && window.ReactNativeWebView.postMessage != null && typeof window.ReactNativeWebView.postMessage === 'function') {
  sendResults();
  } else {
  setTimeout(checkReady, 100);
  }
}

function sendResults() {
  var send_data = {
    'variable':'$result1',
    'value': '{Q01}'
  };
  window.ReactNativeWebView.postMessage(JSON.stringify(send_data));
  send_data = {
    'variable':'$result2',
    'value': '{Q02}'
  };
  window.ReactNativeWebView.postMessage(JSON.stringify(send_data));
  window.ReactNativeWebView.postMessage('complete');
}

setTimeout(checkReady, 100);
</script>

Please note that window.ReactNativeWebView.postMessage(JSON.stringify(send_data)); should be sent for each MC variable and window.ReactNativeWebView.postMessage('complete'); should be sent only once at the end.