Create custom (calculated) variable of region based on user-defined zip

0

Hi. I have a survey that is going to need to have certain lists of response options customized based on geography. We are going to ask for zip code, but these zip codes correspond to one (or in some cases two or three) service areas. I want to make a custom variable that assigns these service area values based on zip code. We will then use those region codes to execute the hide row logic later on that will customize the lists they see based on where they live. It will also give us a way to more easily track the number of responses from each area. This is so easy to do in the program we have used in the past, and we used it all the time, so I really need to be able to make these custom variables and use them within the survey! But I was told this could only be handled through Java script in SG.

So immediately following zip code, I have a hidden page with a checkbox set up with all the service areas (which I will just call “area”)

So I need some direction as to what Java commands to use to basically tell it:

 

Check the box for Area option C if zip=11111, 22222, or 33333

Check the boxfor Area option D if zip=11111, 44444, or 55555

 

I hope this makes sense! 

Jim W (Moderator) answered
    0

    I would try to avoid using custom scripts if possible.  I do not know how you are planning to distribute your survey links.  But if it possible to send custom links to your regions, I would look into using URL variables (https://help.surveygizmo.com/help/using-url-variables-in-surveys).  Use a different variable for each region and then use that to trigger the logic conditions within the survey.

    If this is not possible and you have to use a custom script, I recommend the following:

    Make sure that the question you use to collect the Zip Code from the respondents validates the response strictly.  JavaScript is very twitchy, so you want to make sure that any value you are passing into a script is formatted correctly, or the script will either choke on it or ignore it.

    You will have to create a hidden question that will receive the regional code you are going to use within the survey.

    The script itself is very simple and uses just the sgapiGetValue and sgapiSetValue functions. There are examples of how these functions are used in the Developer documentation The following is a rough sketch of the script in pseudo-code:

    %%xzip=sgapiGetValue (24);
    //the question id of the zip code question is 24
    if xzip >18999 and <20000
                    {sgapiSetValue(25,”Southeast Pennsylvania”);}
    //the question id of the hidden service region question is 25

    This script reads the value entered by the respondent into question id 24 and places it into a variable called xzip, tests the variable, and then sets the value of question id 25 to an appropriate value.  It might be possible to cover all of your service regions using one long IF – ELSE statement.  Our preference is to create a series of IF statements.  JavaScript is very sensitive to misplaced quotation marks or other punctuation errors, so we have found it easier to debug problems by writing very simple, clunky code. It may take a fractions of a second longer to run the code, but it is much easier when you have to edit the script six months later and try to figure out what you wrote.

    I would also recommend that you create a one-two page test survey to help you develop your code.  The survey should consist of just the questions you will need to make sure that the script is running correctly.  Once you are sure that you have a good working script you can copy/paste it over to your main survey.

    If this is your first time writing code you will find this to be a frustrating, but ultimately rewarding experience.  Once you have conquered this problem it will open your eyes to other possibilities for customizations.  Good luck!

    Jim W (Moderator) edited answer
      ×

      Login

      Question stats

      • Active
      • Views506 times
      • Answers1 answer
      • Followers1 follower