Set embedded data: `urls`, `currentVal`
# Before loop/merge block
```js
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle...
while (currentIndex != 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
// change values accordingly
let headlines_ids = [...Array(40).keys()]; // 40 headlines (indices 0 to 39)
let falses = shuffle(headlines_ids.slice(0, 20)).slice(0, 10); // 0 to 19 are false (select 10 to present)
let trues = shuffle(headlines_ids.slice(20, 40)).slice(0, 10); // 20 to 39 are true (select 10 to present)
let headlines_idx = shuffle([...falses, ...trues]); // join and shuffle
console.log("headlines_idx", headlines_idx); // indices to present
// urls for all headlines (change accordingly): ["url1", "url2", "url3", etc.]
let urls = ["https://raw.githubusercontent.com/pb6191/40shortlistedItems/master/f10_Tucker%20Carlson%20forced%20to%20sell%20tie%20collection.png", "https://raw.githubusercontent.com/pb6191/40shortlistedItems/master/f11_euthanize%20senior%20citizens.png", "https://raw.githubusercontent.com/pb6191/40shortlistedItems/master/f12_lifesitenews-asymptomatic.png", "https://raw.githubusercontent.com/pb6191/40shortlistedItems/master/f13_omar%20spits%20on%20the%20tomb%20of%20the%20unknown%20soldier.jpg"]
console.log("total urls", urls.length);
// usually don't need to modify the code below
// select urls based on indices above
let selectedURLs = [];
headlines_idx.forEach(i => { selectedURLs.push(urls[i]); })
// save selected urls to embedded data
Qualtrics.SurveyEngine.setEmbeddedData("urls", JSON.stringify(selectedURLs));
console.log("selectedURLs", selectedURLs);
console.log(selectedURLs.length);
// set next selected URL as embedded data
Qualtrics.SurveyEngine.setEmbeddedData("currentVal", selectedURLs[0]);
console.log('nextURL', selectedURLs[0]);
let imgpath = selectedURLs[0].split("/master/")[1];
console.log("imgpath", imgpath);
});
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
```
## Loop/merge block
- turn on Loop & Merge
- create Field 1, starting from 0 to n-1, where n is the total number of stimuli to present/loop
- DO NOT randomize loop order
Show image HTML
```html
<div style="text-align: center;"><img src="${e://Field/currentVal}" /></div>
```
Sharing intentions
```js
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
});
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
let urls = JSON.parse(String(Qualtrics.SurveyEngine.getEmbeddedData('urls')));
let nextIdx = Number("${lm://Field/1}") + 1;
console.log('nextIdx', nextIdx);
let nextURL = urls[nextIdx];
console.log('nextURL', nextURL);
let imgpath = nextURL.split("/master/")[1];
console.log("imgpath", imgpath);
Qualtrics.SurveyEngine.setEmbeddedData('currentVal', nextURL);
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
```
# Data analysis
- resulting data/csv will have as many columns as the number of stimuli presented/looped
- 1share, 2share, 3share etc. refer to the trial number (1, 2, 3 etc.)
- the actual stimuli presented per trial (i.e., order of stimulus presentation) is saved in the `urls` columns of the data
- it's a string that contains a javascript array. need to split by `,`
```r
rm(list = ls())
library(data.table); library(dplyr); library(tidyr)
f <- "/Users/hause/Desktop/a.csv" # qualtrics output
dt0 <- fread(f)[-c(1, 2)]
glimpse(dt0)
dt1 <- select(dt0, ResponseId, `1_share`:`20_share`)
glimpse(dt1)
dt2 <- melt(dt1, id.vars = "ResponseId") %>% arrange(ResponseId, variable) %>% data.table()
dt2[, trialnum := as.integer(gsub("_share", "", variable))]
dturls <- dt0[, .(ResponseId, urls)]
dturls[, gsub("[", "", urls, fixed = TRUE)]
l <- list()
for (u in 1:nrow(dturls)) {
tempurl <- dturls$urls[u]
tempurl <- gsub('"\"\"', "", tempurl, fixed = TRUE)
tempurl <- gsub('\"\"', "", tempurl, fixed = TRUE)
tempurl <- gsub('[', "", tempurl, fixed = TRUE)
tempurl <- gsub(']', "", tempurl, fixed = TRUE)
tempdt <- data.table(ResponseId = dturls$ResponseId[u] ,
stim = strsplit(tempurl, ",", fixed = TRUE)[[1]],
trialnum = 1:20)
l[[u]] <- tempdt
rm(tempdt)
}
dt3 <- bind_rows(l)
dt4 <- left_join(dt2, dt3)
f2 <- gsub(".csv", "_stimorder.csv", f)
fwrite(dt4, f2)
```