Script to Display Follow Up Items for Highest Ranked Max Diff Attributes

Important Update to Custom Scripting

SurveyGizmo's CustomScript Action now supports the LUA programming language. Visit our NEW Lua Scripting Resources!

Legacy Custom Scripting Language Deprecation Plans 

  1. New accounts (created after October 29, 2018) will only have the option to use Lua in scripts.
  2. As of October 29, 2018 Custom Scripting Actions will default to Lua as the scripting type in the Custom Scripting Action for accounts created before this date. You will be able to switch to the Legacy Custom Scripting; though we highly encourage using Lua.
  3. In the long term, Legacy Custom Scripting Actions will be switched to read-only. The exact date on this is to be determined; we will send notifications well ahead of time.

In this example we cover a script to conditionally show text elements based on the attributes that are rated highest in a Max Diff question.

We'll be using the following SurveyGizmo features in this example:

  • Max Diff Question
  • Our custom scripting function sgapiGetValue
  • Our custom scripting function sgapiHideQuestion
  • PHP foreach loops

The Survey

Let's start with the survey setup. On the first page we have a Drag & Drop Rank question.

On the second page, is a Custom Script action, which we'll cover below.

Also on the second page, we created a text element for attribute in our Max Diff question. One for Passion, Honesty, Integrity and so on. We are using text elements here, but these could easily be follow-up questions.

We want to show the text elements that corresponds to the highest ranked attributes in the Max-Diff question on page one. This way, we can show only follow-up text (or questions) for the attributes the respondent feels are the most important qualities when looking for a mate!

Test out the survey here:

http://www.surveygizmo.com/s3/1757396/Max-Diff-Custom-Scripting-Example

Survey and Script Setup

1. Add a Max-Diff ion to the first page in your survey.

2. Create the follow-up text elements or questions that you would like to conditionally show based on the highest ranked attributes in the Max Diff Question. In our example we have twelve; Humor, Strength, Attractiveness, Beauty, Compassion, Love, Kindness, Integrity, Empathy, Ambition, Passion and Honesty.

3.  Add a Custom Script action. The script will need to be on a separate page from the Max Diff question. 

4. For your Custom Script you'll need the option skus for each of the options in your Max Diff question. In this example the option skus are in order starting with 10003 through 10014. 

5. You will also need the IDs of each of the corresponding text elements. IDs are available ion the right for each element. Turn them on by selecting Customize at the top of the Build tab, then Question IDs. Our text IDs are in order starting with 5 through 16.

6. Edit your Custom Script action and paste the below script. Change the 3, in the example below to the ID of your Max Diff question.

// GET THE ARRAY OF MAX DIFF ATTRIBUTES

%%highest = sgapiGetValue(3);
%%output .= sgapiPrint_R(%%highest);

7. Collect a text response, and note the values output for the array. Here is an example:

Array ( [10003] => 66.6667 [10004] => 33.3333 [10005] => 50 [10006] => 33.3333 [10007] => 50 [10008] => 50 [10009] => 50 [10010] => 66.6667 [10011] => 66.6667 [10012] => 66.6667 [10013] => 33.3333 [10014] => 33.3333 )

The attributes ranked for this example have the following values - 33.3333, 50, 66.6667

Your example may have more or less ranked values depending on how the Max Diff question was set up (i.e. number of attributes, attributes per set, and sets are shown to respondents).

In our example, we will show all of the attributes with a 66.6667 score or greater.

8. With your options skus and page ID's noted, Replace the option skus in the below example script ( [10003]-[10014] ) with your option skus. Replace the page IDs (5-16 in the example script) in both the HIDE ALL TEXT/QUESTIONS section and the DETERMINE WHICH ATTRIBUTES... section of the script and save your Custom Script action.

9. Test to verify it is working!

In our example, the four attributes with values greater than or equal to 66.6667 are Humor, Integrity, Empathy, Ambition.

// SHOW TEXT/QUESTIONS BASED ON HIGHEST RANKED FOR MAX DIFF

// HIDE ALL TEXT/QUESTIONS

sgapiHideQuestion(5,true);
sgapiHideQuestion(6,true); 
sgapiHideQuestion(7,true);
sgapiHideQuestion(8,true); 
sgapiHideQuestion(9,true); 
sgapiHideQuestion(10,true);
sgapiHideQuestion(11,true);
sgapiHideQuestion(12,true);
sgapiHideQuestion(13,true);
sgapiHideQuestion(14,true);
sgapiHideQuestion(15,true);
sgapiHideQuestion(16,true);

// GET THE ARRAY OF MAX DIFF ATTRIBUTES

%%highest = sgapiGetValue(3);
%%output .= sgapiPrint_R(%%highest);

// DETERMINE WHICH ATTRIBUTES ARE RANKED HIGHEST AND SET THE IDS TO SHOW

foreach (%%highest as %%key => %%value){
  if (%%value >= '66.6667'){
    if (%%key == '10003'){
     sgapiHideQuestion(5,false);
    }
    if (%%key == '10004'){
     sgapiHideQuestion(6,false);
    }
    if (%%key == '10005'){
     sgapiHideQuestion(7,false);
    }
    if (%%key == '10006'){
     sgapiHideQuestion(8,false);
    }
    if (%%key == '10007'){
     sgapiHideQuestion(9,false);
    }
    if (%%key == '10008'){
     sgapiHideQuestion(10,false);
    }
    if (%%key == '10009'){
     sgapiHideQuestion(11,false);
    }
    if (%%key == '10010'){
     sgapiHideQuestion(12,false);
    }
    if (%%key == '10011'){
     sgapiHideQuestion(13,false);
    }
    if (%%key == '10012'){
     sgapiHideQuestion(14,false);
    }
    if (%%key == '10013'){
     sgapiHideQuestion(15,false);
    }
    if (%%key == '10014'){
     sgapiHideQuestion(16,false);
    }
  } 
} 

How does it work?

If you'd like to know more about how the script works in order to adapt or modify this script for your particular scenario, read on!

In the first portion of the script we hide the conditional text elements (IDs 5-16) using the sgapiHideQuestion function.

In the second portion of the script we create a variable called %%highest and set it equal to the ranking values from the Max Diff question (question ID 3 in this case). The sgapiGetValue function for a Max Diff Question will pull an array that looks like this:

Array ( [10003] => 66.6667 [10004] => 33.3333 [10005] => 50 [10006] => 33.3333 [10007] => 50 [10008] => 50 [10009] => 50 [10010] => 66.6667 [10011] => 66.6667 [10012] => 66.6667 [10013] => 33.3333 [10014] => 33.3333 ) 

Think of this array like a file cabinet where the indexes or keys are like the file folders and the values are the papers in the folders.

Next we create what is called a foreach loop which takes the Max Diff array and for each answer option or "%%key" it evaluates whether its rank or "%%value" is greater than or equal to '66.6667'. If the value is greater than or equal to 66.6667, the corresponding text element will be shown.

Scripting and Other Custom Solutions

We’re always happy to help you debug any documented script that is used as is. That said, we do not have the resources to write scripts on demand or to debug a customized script.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for SurveyGizmo features and functionality ideas that might meet your customization. Beyond this, you might want to consult with someone on our Programming Services Team; these folks might have the scripting chops to help you to achieve what you are looking for!