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.)

(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!

Code, cook and fatherhood