How to capture MTurk workers’ IDs into your Qualtrics survey

When posting a Qualtrics survey link on MTurk, here’s how to capture a worker ID as embedded data:

First, create a HIT (Human Intelligence Task) normally, selecting the Survey Link template type. Continue setting up your hit normally until you arrive at the Design Layout tab, then click the Source button and replace the entire contents with the following code.  (Note that about halfway through the code snippet is placeholder for a link to your survey. You’ll need to replace https://YOUR_QUALTRICS_SURVEY_URL_GOES_HERE with your survey URL.)

<!-- For help on using this template, see the blog post: http://mechanicalturk.typepad.com/blog/2014/04/editing-the-survey-link-project-template-in-the-ui.html --><!-- Bootstrap v3.0.3 -->
<link href="https://s3.amazonaws.com/mturk-public/bs30/css/bootstrap.min.css" rel="stylesheet" />
<section class="container" id="SurveyLink" style="margin-bottom:15px; padding: 10px 10px; font-family: Verdana, Geneva, sans-serif; color:#333333; font-size:0.9em;">
<div class="row col-xs-12 col-md-12"><!-- Instructions -->
<div class="panel panel-primary">
<div class="panel-heading"><strong>Instructions</strong></div>

<div class="panel-body">
<p>We are conducting an academic survey about social networks. We need to understand your opinion about social networks. Select the link below to complete the survey. At the end of the survey, you will receive a code to paste into the box below to receive credit for taking our survey.</p>

<p><strong>Make sure to leave this window open as you complete the survey. </strong> When you are finished, you will return to this page to paste the code into the box.</p>

<p style="font-size: 0.9em; font-style:italic; background-color:#eeeeee; padding:10px;"><strong>Template note for Requesters </strong>- To verify that Workers actually complete your survey, require each Worker to enter a <span style="text-decoration:underline;">unique</span> survey completion code to your HIT. Consult with your survey service provider on how to generate this code at the end of your survey.</p>
</div>
</div>
<!-- End Instruction -->

<table>
 <tbody>
 <tr>
 <td><strong>Survey link:</strong></td>
 <td><div id="myelementLink" style="display: inline; font-family: Verdana;"><tt>URL not shown because there is an error with Javascript on your computer. To perform this HIT, you must have Javascript and cookies enabled on your browser.</tt></div>
<script type="text/javascript" language="JavaScript"><!--
var assignment_id_field = document.getElementById('myelementLink');
var paramstr = window.location.search.substring(1);
var parampairs = paramstr.split("&");
var mturkworkerID = "";
for (i in parampairs) {
 var pair = parampairs[i].split("=");
 if (pair[0] == "workerId")
 mturkworkerID = pair[1];
}
if (mturkworkerID == "" ) {
 assignment_id_field.innerHTML = '<tt>The link will appear here only if you accept this HIT.</tt>';
} else {
 assignment_id_field.innerHTML = '<a target="_blank" href="https://YOUR_QUALTRICS_SURVEY_URL_GOES_HERE?MID=' + mturkworkerID + '"><h1><span style="color: rgb(255, 0, 0);"><span style="font-family: Courier New;"><b>Click here to begin taking the survey!</b></span></span></h1></a>';
}

// --></script>

<div class="link" id="linkDiv">&nbsp;</div></td>
 </tr>
 <tr>
 <td><strong>Provide the survey code here:</strong></td>
 <td style="vertical-align: middle;"><input class="form-control" id="surveycode" name="surveycode" placeholder="e.g. 123456" type="text" /></td>
 </tr>
 </tbody>
</table>
</div>
</section>
<!-- close container section -->
<style type="text/css">td {
 font-size:1.0em;
 padding:5px 5px;
}
</style>

(I will provide a rubber duck debug explanation in this paragraph, but skip it if you are not interested in the technical details.)

  • Everything before line 22 is the untouched template provided by MTurk. A div is inserted with an ID of myelementLink . The text within the div is set as an error message that will remain only if the javascript below fails to execute.
  • Within the javascript, starting on line 24, grab a reference to the div with an ID myelementLink and save it to a variable titled assignment_id_field .
  • Lines 25-32 examine the URL of the page; if it contains a parameter equal to workerId, the mturkworkerID  variable is set to the workerID  value.
  • Line 33 is a conditional statement that looks to see if the mturkworkerID  has been set. If it has not, the text of the myelementLink div should be updated (alerting a user that this URL will be revealed once the HIT is accepted).
  • The survey link is formed in line 36, where you should have a valid workerID . “https://YOUR_QUALTRICS_SURVEY_URL_GOES_HERE&MID=’ + mturkworkerID + ‘”  is appended to the Survey URL. This will set the mturkworkerID value to MID embedded data field within the survey.
  • Everything after line 36 is the remainder of the untouched MTurk template.

Once you have pasted the snippet, click the Source button again. You should see the following text when previewing the layout:

URL not shown because there is an error with Javascript on your computer. To perform this HIT, you must have Javascript and cookies enabled on your browser.  (This is normal behavior and the error can be ignored.)

Within Qualtrics you’ll need to add two pieces of embedded data to your survey within the Survey Flow, as shown below:

embedded-data
Qualtrics Embedded Data

The MID will be set to the MTurker worker ID. You then must set a random number between 1000 and 9999 as the code that the MTurker provides once s/he completes the survey. Both of these data fields will be saved in the Qualtrics result set.

Within this demo video you can see me completing  a HIT with this setup:

Easy, right?

Oh, and lastly –  I’d be remiss not to give credit to Eyal Peer , whose post on this subject provided the code snippet. Thanks, Eyal!