Setting expiration in an anonymous link using javascript custom code | XM Community
Skip to main content

We want to set expiration in an anonymous survey link by calculating the Date wherein the survey was created (we have embedded data for this already) and the Date wherein the survey was taken. 

  1. In the survey flow, we can use this code to get the current date, when the survey is being taken in Date format: ${date://CurrentDate/c}
  2. Then in the survey flow,  I can also replicate the CreationDate of survey being passed on thru query strings, so I can change this to Date format and be compatible with the Current Date
  3. Then I will need to use Javascript to calculate the difference between the two dates. Once I get the difference, I can use branch logic to end the survey if the time difference is more than 24h.

Questions:

  • Would you know which part in the Qualtrics account we can set-up this Javascript custom code calculation?
  • Also, do we have a specific formula we can follow so Qualtrics can also recognize this calculation?

We want to set expiration in an anonymous survey link by calculating the Date wherein the survey was created (we have embedded data for this already) and the Date wherein the survey was taken. 

  1. In the survey flow, we can use this code to get the current date, when the survey is being taken in Date format: ${date://CurrentDate/c}
  2. Then in the survey flow,  I can also replicate the CreationDate of survey being passed on thru query strings, so I can change this to Date format and be compatible with the Current Date
  3. Then I will need to use Javascript to calculate the difference between the two dates. Once I get the difference, I can use branch logic to end the survey if the time difference is more than 24h.

Questions:

  • Would you know which part in the Qualtrics account we can set-up this Javascript custom code calculation?
  • Also, do we have a specific formula we can follow so Qualtrics can also recognize this calculation?

Hi if you set the survey expiration from the time the survey goes live it will stop taking responses. Wouldn’t that also accomplish this?


Hi @Cham ,

 

You can set up this javascript on the very first question on the onLoad event. You can click on the first question and then click on “Javascript”.


To set an expiration date for an anonymous survey link, you can calculate the difference between the survey creation date and the survey completion date. 

 

Qualtrics.SurveyEngine.addOnload(function() {
  var creationDate = "${e://Field/CreationDate}";
  var completionDate = "${e://Field/CompletionDate}";
  var creationDateTime = new Date(creationDate);
  var completionDateTime = new Date(completionDate);

  // Calculate the time difference in milliseconds
  var timeDifference = completionDateTime.getTime() - creationDateTime.getTime();

  // Set the expiration period in days (e.g., 30 days)
  var expirationPeriod = 30;

   var expirationDateTime = new Date(completionDateTime.getTime() + expirationPeriod * 24 * 60 * 60 * 1000);

  // Set the expiration date for the anonymous link
  Qualtrics.SurveyEngine.setSurveyExpirationDate(expirationDateTime);
});
 

we assume that the creation date and completion date are stored as embedded data fields in Qualtrics (CreationDate and CompletionDate). You need to replace ${e://Field/CreationDate} and ${e://Field/CompletionDate} with the appropriate embedded data field names in your survey.

The script calculates the time difference between the creation date and completion date in milliseconds. It then sets the expiration period (in this case, 30 days) and calculates the expiration date by adding the expiration period to the completion date. Finally, it sets the expiration date for the anonymous link using Qualtrics.SurveyEngine.setSurveyExpirationDate().


Thank you @Sowrabh1993. Can this be set-up on our end without the need for the services of the Engineering team? And if we set the custom javascript in the Survey Builder, will this not be visible within the survey questionnaire?

 

Appreciate your feedback, thank you.


Hi @Nanditha MM, thank you for this.

 

I'm thinking of using the Survey StartDate instead of the Completion Date. Is that possible?

Basically, the Survey StartDate will reflect the date wherein they started answering the survey. If they did not meet the duration (let's say 3 days survey link validity), all those who will access the links after 3 days will not be able to access the link anymore.

 

Also I have another concern, the CreationDate (7/22/2023) has a different format than the Survey StartDate (Jul 22, 2023) , is there a way to create another embedded data out of CreationDate that will produce the same format as the Survey start date?

 


Leave a Reply