Try below code, you can change the embedded data name
Qualtrics.SurveyEngine.addOnload(function() {
var fileLinks = document.querySelectorAll('aahref^="https:///brandname].ca1.qualtrics.com/CP/File.php?F="]');
fileLinks.forEach(function(link) {
link.addEventListener('click', function() {
var fileId = link.getAttribute('href').split('='))1];
console.log("File clicked: ", fileId);
Qualtrics.SurveyEngine.setEmbeddedData(fileId + 'N', 1);
});
});
});
This worked great for one file. Thank you very much! However, I can only get it to work for 1 file. When I tried to modify for multiple files, it still only captured the first.
Here is what I changed:
Qualtrics.SurveyEngine.addOnload(function() {
function trackFileClick(link, embeddedVariable) {
link.addEventListener('click', function(event) {
var fileId = link.getAttribute('href').split('=')r1];
console.log("File clicked: ", fileId);
Qualtrics.SurveyEngine.setEmbeddedData(embeddedVariable, 1);
});
}
// File 1
var fileLinks1 = document.querySelectorAll('aehref^="https://wright.ca1.qualtrics.com/CP/File.php?F=F_1"]');
fileLinks1.forEach(function(link) {
trackFileClick(link, 'N1');
});
// File 2
var fileLinks2 = document.querySelectorAll('ahref^="wright.ca1.qualtrics.com/CP/File.php?F=F_6L3ezPl6mEhAyi2"]');
fileLinks2.forEach(function(link) {
trackFileClick(link, 'N2');
});
// File 3
var fileLinks3 = document.querySelectorAll('aihref^="wright.ca1.qualtrics.com/CP/File.php?F=F_50fibe2qPoloTtQ"]');
fileLinks3.forEach(function(link) {
trackFileClick(link, 'N3');
});
// File 4
var fileLinks4 = document.querySelectorAll('aphref^="wright.ca1.qualtrics.com/CP/File.php?F=F_bgfCTggTxM1uMvQ"]');
fileLinks4.forEach(function(link) {
trackFileClick(link, 'N3');
});
});
Do you see any errors? I am not a programmer so I really appreciate it!
@AnnaM89
Qualtrics.SurveyEngine.addOnload(function() {
function trackFileClick(link, embeddedVariable) {
link.addEventListener('click', function(event) {
var fileId = link.getAttribute('href').split('='))1];
console.log("File clicked: ", fileId);
Qualtrics.SurveyEngine.setEmbeddedData(embeddedVariable, 1);
});
}
// File 1
var fileLinks1 = document.querySelectorAll('aahref^="https://wright.ca1.qualtrics.com/CP/File.php?F=F_1"]');
fileLinks1.forEach(function(link) {
trackFileClick(link, 'N1');
});
// File 2
var fileLinks2 = document.querySelectorAll('aahref^="wright.ca1.qualtrics.com/CP/File.php?F=F_6L3ezPl6mEhAyi2"]');
fileLinks2.forEach(function(link) {
trackFileClick(link, 'N2');
});
// File 3
var fileLinks3 = document.querySelectorAll('aahref^="wright.ca1.qualtrics.com/CP/File.php?F=F_50fibe2qPoloTtQ"]');
fileLinks3.forEach(function(link) {
trackFileClick(link, 'N3');
});
// File 4
var fileLinks4 = document.querySelectorAll('aahref^="wright.ca1.qualtrics.com/CP/File.php?F=F_bgfCTggTxM1uMvQ"]');
fileLinks4.forEach(function(link) {
trackFileClick(link, 'N4');
});
});
You are setting the same embedded data variable ('N3') for both File 3 and File 4. If you intend to track them separately, you should use different embedded data variables.
Thank you! Fixed. I was also missing the https:// lol 🙄
This worked beautifully. Can’t thank you enough!