0

enter image description hereI'm trying to code a survey for my capstone project.

I coded a vignette of a candidate debate, and the hypothetical candidates are asked three questions about their stance on abortion, healthcare and immigration. I assigned five possible candidate responses to each question, ranging in stances 1 (very liberal) to 5 (very conservative). I coded it so their responses to each question are randomized.

Unfortunately, I can't see the average of the values that survey respondents were randomly presented with. For example, if the mean value of Candidate A for one randomized survey was 3.5, I would like to see that. I used JavaScript to construct the code, and I need to embed the data detailing the mean value of the randomized candidate. I put an embedded data function in my survey workflow, but it's still not working.

When I look at the embed data columns in my Data and Analytics tab, they're empty.

This is my code and survey workflow:

Qualtrics.SurveyEngine.addOnReady(function() {

    // === Candidate texts with numeric values embedded ===
    const stances = {
        abortion: [
            "5|I support a woman's right to an abortion in all cases. In addition to supporting a woman's right to attain an abortion in instances of rape, incest, or threat to the mother's life, I also support elective abortions in all cases — whether this is because she is married and does not want any more children, cannot afford more children, does not want a child with the fetus's father, or simply does not want a child.",
            "4|I support all abortions through the second trimester, especially in the case of traumatic abortion conditions such as women that became pregnant as a result of rape or incest, serious threats to the health of the mother, or possible birth defects in the child. I am also sympathetic to most elective abortion cases, such as the mother not being able to afford any more children, not wanting to marry the child's father, or simply wanting the abortion for any reason.",
            "3|I support abortion in instances of rape, incest, chances of birth defects in the fetus, or serious threats to the life of the mother. I also support elective abortions — whether this be because the mother cannot afford the child, already has other children to care for, does not want to sustain a relationship with the fetus's father, or simply wants an abortion for whatever reason — up until twelve weeks gestation.",
            "2|I support elective abortions -- whether the mother seeks an abortion because she cannot afford the child, care for another child, does not want a relationship with the child's father, or simply wants an abortion for any reason -- until six weeks gestation. I also support abortion in traumatic cases, such as serious risk of birth defects, threats to the life of the mother, or pregnancies that resulted as a product of rape or incest.",
            "1|I do not support elective abortions in any case, regardless of if the mother seeks an abortion because she cannot afford a child, cannot care for another child, does not want a relationship with the child's father, or wants an abortion for any reason. I only support abortion in traumatic instances, such as in cases of rape or incest, threats to the life of the mother, or serious risks of birth defects."
        ],
        healthcare: [
            "5|I support a single-payer (when the government is the sole payer) health care system. I support protecting the Affordable Care Act as the current solution, but ultimately I think we should pursue a single payer model.",
            "4|I believe in extending the Affordable Care Act and instituting reforms to make it more affordable, such as enhancing ACA prescription drug coverage and allowing the federal government to negotiate prescription drug prices.",
            "3|I support an extension of the Affordable Care Act, but I do not want the Affordable Care Act to be expanded in scope. While I support public health care, I also want to protect the private health care plans that many labor unions fought hard to obtain.",
            "2|While I support extending the Affordable Care Act for another year while we imagine a new solution, ultimately I think a government voucher system, in which people can use vouchers to purchase private insurance, is a better plan.",
            "1|I want to repeal the Affordable Care Act as quickly as possible, and allow the free markets to influence the health care industry rather than mandating that employers and governments provide insurance."
        ],
        immigration: [
            "5|I support amnesty programs for undocumented immigrants, family reunification measures, ten-year residence permits for refugees, and a path to citizenship for undocumented immigrants who have worked in the U.S. for five years with no criminal record.",
            "4|I believe in some amnesty programs for undocumented immigrants who have resided in the U.S. for over a decade without any criminal record. I also believe in five-year residency programs for refugees seeking asylum, but I don't believe the U.S. should significantly increase the intake of labor migrants.",
            "3|I don't believe that the U.S. should increase its intake of unspecialized labor migrants. While I support temporary residency programs for refugees seeking asylum and work authorization programs for immigrants brought to the U.S. before the age of five, I do not support a pathway to citizenship for undocumented immigrants, regardless of criminal record or work history.",
            "2|I do not believe that the U.S. should increase the intake of unspecialized labor migrants. I support some family reunification measures and residency programs for refugees seeking asylum, but do not support the long-term residency of undocumented immigrants. I believe we should increase deportations of those who came to the country undocumented.",
            "1|I do not believe in any residency programs for any undocumented immigrants or refugees. I do not believe that it is the U.S.'s responsibility to coordinate family reunification measures. I believe that we should increase deportations of those that came here undocumented, regardless of work history or criminal record. I also believe we should build a wall on America's Southern border."
        ]
    };
// === Randomize candidate positions ===
function randIndex() { return Math.floor(Math.random() * 5); }

const A_vals = [
    randIndex(),
    randIndex(),
    randIndex()
];
const B_vals = [
    randIndex(),
    randIndex(),
    randIndex()
];

// === Extract numeric values for embedded data ===
function getNumber(str) { return parseInt(str.split("|")[0]); }
const fields = {
    "A1": getNumber(stances.abortion[A_vals[0]]),
    "A2": getNumber(stances.healthcare[A_vals[1]]),
    "A3": getNumber(stances.immigration[A_vals[2]]),
    "B1": getNumber(stances.abortion[B_vals[0]]),
    "B2": getNumber(stances.healthcare[B_vals[1]]),
    "B3": getNumber(stances.immigration[B_vals[2]])
};
fields["A_Avg"] = Math.round((fields.A1 + fields.A2 + fields.A3)/3);
fields["B_Avg"] = Math.round((fields.B1 + fields.B2 + fields.B3)/3);

for (const key in fields) {
    Qualtrics.SurveyEngine.setEmbeddedData(key, fields[key]);
}

// === Display text only (without numbers) ===
const container = document.createElement("div");
this.getQuestionContainer().appendChild(container);

function displayVignette(prompt, array, indexA, indexB) {
    const pPrompt = document.createElement("p");
    pPrompt.style.fontWeight = "bold";
    pPrompt.textContent = "Moderator: " + prompt;
    container.appendChild(pPrompt);
New contributor
Audrey G. is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • 1
    That isn't Java Commented yesterday
  • 1
    Furthermore the code looks incomplete - Please edit the question to create an actual minimal reproducible example Commented yesterday
  • You must clearly add an Embedded Data element at the very top of you Survey flow containing every variable name (like A-Avg) to initialize the storage columns before the code runs. Commented 21 hours ago
  • I added my survey workflow; is the embedded data section in the right place? I have it right before the vignette block, but should it be at the top? Also, is there a test I should run to make sure the data is embedding? Thank you! Commented 20 hours ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.