Recording if someone has clicked external link that opens in new window | XM Community
Skip to main content

Hello, 


I am trying to record whether someone clicks an external link in the last question of a survey using embedded data. The ext link opens in a new window. 

Regardless of clicks, the embedded data always = 0 
 

I’ve tried all of the solutions I’ve found to this issue here + asked ChatGPT. 

Here is the HTML: <a href=“https://www.condense.live/studio-5?s5=1” target=“_blank” id=“extLink”>here</a>

Here is the javascript: Qualtrics.SurveyEngine.addOnload(function() {
    var extLink = document.getElementById(‘extLink’);
    if (extLink) {
        extLink.addEventListener(‘click’, function(event) {
            // Update the embedded data ‘s5’ to ‘1’
            Qualtrics.SurveyEngine.setEmbeddedData(‘s5’, ‘1’);            // Open the link in a new page
            window.open(extLink.href, ‘_blank’);
        });
    }
});

The embedded data for ‘s5’ is set to ‘0’ at the beginning of the survey flow 

In the above solution, I was trying to set the embedded data using the URL parameter (as it opens in new window), but have also tried options without this. 

I would be so grateful for any help on this, as I feel like I’ve tried and tested every option!

 

Thank you!

I have tested this code in a simple survey with just this question, and it works! The other embedded data I have in the survey is not affecting it. 

I have tried moving the embedded data into the same block as the question, but this doesn’t work.

I don’t know what else could be causing this code not to work!

 


Hi @Helen.B,
the following code works in my test survey:
 

Qualtrics.SurveyEngine.addOnReady(function()
{

var extLink = document.getElementById("extLink");
if (extLink) {
extLink.addEventListener("click", (event) => {
Qualtrics.SurveyEngine.setEmbeddedData("TestVariable", "1");
window.open(extLink.href, "_blank");
});
}

});

 

I’ve experienced one additional problem in my setup:

When copying your HTML code into my question text, Qualtrics automatically added additional quotation marks around “extLink”. So I ended up with … id=””extLink””.


Hi @ManfredBM ,

 

Thank you so much for your response. I’m afraid that still doesn’t work in my survey. 

 

The code I originally had does work when the survey is just 1 item - it only appears to run into issues with the full survey.

 

Is there any logic within a Qualtrics survey flow that could impact the code you’ve shared? 

 

Thanks again


Hi @Helen.B,
that’s more than strange!

This code should be independent of any other content or flow of the survey. I’ve also just tested it in a larger test survey with a more complex survey flow and it still worked for me.

Can you do me a favor and post a screenshot of your question text (in HTML view) and a screenshot of the JavaScript code as implemented on the question? 


Hi @ManfredBM ,

 

Haha, tell me about it! Thank you for getting back to me so quickly. 

 

I’m relieved to hear it works for you. I have just tried it once more, and the embedded values for ‘s5’ remain at ‘0’, despite clicking. 

 

Of course, here are screen shots of HTML and Javascript + embedded data setting.

One issue I have noticed is that when I click the link, two new tabs open for the same website. 

 

 


By the way - your code DOES work, when this single item question is in it’s own survey!


It’s a good sign that you get two new tabs when clicking on the link!
The first one is coming from the click itself, the second one is opened by the JavaScript.

(My pop-up blocker has blocked the second new tab, so I didn’t see it)

In the script you can just delete the window.open line (see below). After that, only one window opens.

 

In order to be really sure that the script runs as expected, can you please include the two “alert()” statements? If you see “Link clicked” on screen after clicking the link, the script runs as expected.

 

 


@ManfredBM , ok that’s good to know! 

 

So I did as you said, and I got the ‘linked clicked’ option (see image)

 

However, interestingly, I also got an alert (see image) when I moved on to the block of questions where this question features at the end of the block - should this happen?

 

The other thing that is different in this survey, compared to my single-item one, is that I have an ADDITIONAL ‘submit survey’ page, after this final question (see image). Could this reset the embedded data? 

 


Where in your survey flow do you create your ‘s5’ embedded Data field?
Do you accidentially set it back to ‘0’ after the respective question?

 

It’s really weird, the execution of your code definitely goes past the line that sets the Embedded Data field. 

Can you please insert the following line after the “clicked link” alert:

alert("s5 is set to: " + Qualtrics.SurveyEngine.getEmbeddedData("s5"));

 

As for the second alert that you’ve posted:
This alert pops up twice for me when the respective questions appears on screen.


@ManfredBM the embedded data is set right at the beginning of the survey flow (s5=0). I tried including it in the same block before, but that didn’t work then. 

 

There is branching logic at a few places in the flow. This question is the very last question in the survey. 

 

The only page that comes after it once you click ‘Next’ as the bottom of the page, is a pre-end of survey page (see image), which you then need to click ‘Submit’ to end the survey. This seems unusual as it’s not on my single-item survey. 

 

I tried your code and the alert I got said s5 is set to=not defined (see image) --- but in the results, the value for the embedded data field is always 0. 

 

The URL link does have some embedded data in it (an earlier solution). Could this impact anything?

 

Thanks again for your continued support with this!

 

 


Hi @Helen.B ,

I am at my wit's end now - almost.

 

One last try:
In a different thread I just found the following comment (no 2) by @TomG 

For testing, you could try to add another page / block after your question.

If this also doesn’t help, I’m running out of ideas. 
I’m also a bit confused be the “undefined” value that you are getting in the alert. I’ve tested several things (misspelling of variables etc.) and I’m always getting “null” as a result.

@TomG is much more an expert than I am. Perhaps he can see something we have missed ...


Thank you for persevering @ManfredBM !

 

I tried adding a new block afterwards - no change 😞 . I can’t seem to add a page right at the end. 

 

I would have thought, if a single item version worked (where this item is the first and last item), that this would also work. 

 

I also can’t seem to find anything re. ‘undefined’ embedded data. 

 

I did find this thread, which is maybe irrelevant as my embedded data field does show up in the CSV, but sharing just in case. 

 


@ManfredBM, I have made some progress on at least identifying the errors in console (with the help of ChatGPT!)

 

The undefined variable was related to these errors: 

  • setEmbeddedData deprecated, please use `setJSEmbeddedData`
  • getEmbeddedData deprecated, please use `getJSEmbeddedData` 

I changed these, and in the console could now see ‘s5 is set to=1’

 

But now I have some new errors related to ‘Next page’ 

  • Uncaught TypeError: Qualtrics.SurveyEngine.pageNext is not a function
    • For this, I added:
      •  Qualtrics.SurveyEngine.clickNextButton();

Then I got a new error:

  • Uncaught TypeError: Qualtrics.SurveyEngine.clickNextButton is not a function at HTMLAnchorElement.
    • For this, I added:
      • Q_NextButton.click();

Then another:

  • Uncaught ReferenceError: Q_NextButton is not defined at HTMLAnchorElement
    • I am now going to try 
      • $('NextButton').click();  or
      • jQuery('#NextButton').click();

 

I still can’t work out why these errors are here for this one, and not the other!


Hi,

I have to admit that it’s the first time I heard about setEmbeddedDate being deprecated. It obviously only affects the Simple Layout (?). I’m using a different layout, which was probably the reason the ‘old’ code worked for me all the time.

Well done to find this out!

Here’s a discussion I found on this topic:

setEmbeddedData has it seems been deprecated. | XM Community (qualtrics.com)

 

There might be more changes to the JS API. It seems that those JS elements have to be used in a slightly different way.

Unfortunately I don’t know anything about this new API :(

Sorry I wasn’t much help!


Hi @ManfredBM !

 

Ah, thank you so much for pointing out that this is all to do with the Simple Layout!

 

I had no idea and wasn’t even aware I was using it. I changed it to ‘classic’ and now it works perfectly. Thank you so much for your help and perseverance! 


Leave a Reply