In this tutorial, we will discuss some advanced customizations for merge codes, including formatting and transforming data in merge codes and replacing strings within merge codes. Let's begin by learning a little more about how merge codes work.
The Basic Structure of a Merge Code
Below is a very common Merge Code which will be used as example to discuss the various parts of a Merge Code:
[question("option value"), id="2"]
All Merge Codes follow this similar structure. Merge Codes always start and end in brackets [ ] and contain a section in parentheses surrounding double quotation marks. Finally, all the major areas of the Merge Code (covered below) should always be written in all lowercase letters.
Important Note on Styling Merge Codes
As mentioned above, merge codes have the following default format:
[question("option value"), id="2"]
At times, you may need to customize the look & feel of a merge code, or perhaps link the merge code to a url. Both of these actions introduce HTML code around the merge code (behind the scenes).
When HTML is introduced into a merge code, SurveyGizmo will convert any double quotes in the merge code to single quotes (on Save) to maintain the merge code functionality and avoid potentially compromising the HTML behind the scenes.
The result will convert the above merge code to the below merge code (note that double quotes have been replaced with single quotes):
[question('option value'), id='2']
This single-quote version of the merge code will function correctly - no need to try to convert it back to the default!
Now, let's break down each part of the Merge Code:
Merge Code Category
The bolded text below immediately following the opening bracket and ending before the opening parenthesis is the Merge Code category and defines the available properties (discussed in the next section).
[question("option value"), id="2"]
There are 9 different categories:
- Question: Dynamically pull and display data from questions or actions.
- Quiz: Quiz score merge codes dynamically pull and display about how the respondent did.
- Page: Access information about a specific page of the survey.
- Invite: Pull data about a specific email or sms campaign invitation or data unique to that invitation.
- Contact: Pull contact data from the specific contact; these work for either email invitation contacts, sms invitation contacts, or contacts within an account contact list.
- URL: Pull and displaying query string values.
- Survey: Pull data about a specific response or overall survey.
- System: Pull metadata about a survey response or from the SurveyGizmo system
- Account: Pull Data about your SurveyGizmo account.
Merge Code Property
The bolded text below is the property, found between the double quotation marks within the parenthesis. Available properties are determined by the Merge Code category and like the category, they must be all lowercase. If you think of a category as the city in a mailing address, then the property is the street address.
[question("option value"), id="2"]
Merge Code Attributes
The bolded text below is an attribute of the Merge Code and is matched to the specific property. If the property is the street address of a mailing address, the attributes are the house/apartment/suite number. However, unlike the category and property, you can have none or multiple attributes, all separated by commas in the latter case. Attributes are always paired as an identifier and its value. In the example above, id is the identifier and the value is inside double quotes. They are connected by an equals sign.
[question("option value"), id="2"]
The id attribute is the most common Merge Code attribute. It indicates the element that you are pulling data from. This is most often the question ID.
Other Attributes
- format - format a date in Merge Code - applies to [system("date")] merge code only. Does not apply to data collected via the Date question type.
- stringformat - format a data string in a Merge Code.
- numberformat - round to a specified number of decimal places
- delimiter - specify the delimiter for multi-value questions
- finaldelimiter - specify the last delimiter for multi-value questions
- questiondelimiter - specify the delimiter for sub-questions
See the Format and Transform Data in Merge Codes section of this tutorial below to learn more.
- questionpipe
- pagepipe
To learn more go to Merge Codes for Pulling Data from Piped Questions.
Formatting and Transforming Data in Merge Codes
There are several merge code attributes that allow you to format and/or transform data within merge codes. Including case, format, stringformat, numberformat, delimiter and more. Below we cover each attribute and demonstrate how to use it to format or transform data.
default
The default attribute, in bold text below, can be used to populate an answer when a question value isn't present.
[question("value"),id="3",default="anything"]
case
The case attribute, in bold text below, can be used to take the string pulled by the Merge Code and format the casing.
[question("value"),id="3",case="lower"]
The casing options are:
- lower - all alphabetic characters are converted to lowercase
- upper - all alphabetic characters are converted to uppercase
- proper - all alphabetic characters are converted to titlecase
format
The format attribute, in bold text below, is used to transform the date data being pulled by the Merge Code into different formats. This attribute applies to the [system("date")] merge code only. It does not apply to data collected via the Date question type.
[system("date"),format="Y-m-d H:i:s"] [system("date"),format="Y-m-d"]
Depending on how you'd like to format the date, you'll need to use different format characters. The format attribute uses basic PHP format parameters. The default format for dates in SurveyGizmo is MM/DD/YYYY but can be altered with the format attribute. If the date 11-06-2012 is being pulled by the Merge Code, then the following characters will format as follows:
- M - The written version of the Month: Nov
- m - The number of the month: 11
- D - The day of the week it falls on: Tue
- days return in three character format
- d - The number of the date: 06
- Y - The full four digits of the year: 2012
- y: The last two digits of the year: 12
- H: 24-hour format of an hour with leading zeros (Eastern Time): 12
- h: 12-hour format of an hour with leading zeros (Eastern time): 01
- i: The two digit minute with leading zero: 25
- s: The two digit second with leading zero: 06
These are case sensitive. To learn more about what other format characters output, please visit PHP.net
Examples:
- [system("date"),format="M d Y"] will output: Nov 06 2012
- [system("date"),format="Y/d/m"] will output: 2012/06/11
stringformat
The stringformat attribute, in bold text below, allows you to format a string of data being pulled by a Merge Code.
[question("value"), id="3",stringformat="%010s"]
The above example will take the value entered in question id 3 and will output a 10 character string with zero-padding.
Some other useful string formats:
- %s - standard string format
- %s more text - Add text to precede or follow a string
- %.10s - cut off after 10 characters
This attribute uses the PHP sprintf function. This is only the surface of what you can do with this function, to learn more visit PHP.net.
numberformat
The numberformat attribute, in bold text below, allows you to format the number of decimals for the number being pulled by a Merge Code. Numbers will be rounded to the specified decimal.
[question("value") id="3",numberformat="2"]
urlencode
The urlencode attribute, in bold text below, will urlencode the data in the merge code. This is useful to anyone that needs to include a URL in an email based on a response.
[question("value"), id="3",urlencode="true"]
delimiter
The delimiter attribute, in bold text below, allows you to specify the delimiter for piped values pulled by a Merge Code.
[question("value"), id="3",delimiter=";"]
Example:
Output the answer from a Multi Select question (such as a checkbox) as a list by setting the delimiter attribute equal to "<li>": [question("value"), id="3", delimiter="<li>"].
If a respondent answers options A and B, the output will be:
- A
- B
Note: When using the <li> delimiter option in a question title or text element, you'll need to turn off the HTML editor and wrap your merge code in the HTML for a bulleted list (see below) in order for this to work.
<ul> <li>[question("value"), id="2", delimiter="</li> <li>"] </ul>
- The <li> delimiter merge code can only be added to a brand new element (question title, text element).
- Any edits to the <li> delimiter merge code after the initial Save will result in the merge code and associated HTML being cleaned. This will effectively break the merge code. If you need to make edits, you will need to create a brand new element and paste the edited merge code there.
Example:
Output the answer from a Multi Select question semi-colon separated or use another character if you wish! Set the delimiter attribute equal to ";": [question("value"), id="3", delimiter=";"].
If a person answers options A and B, the output will be:
A;B
finaldelimiter
The finaldelimiter attribute, in bold text below, allows you to specify the delimiter between the last two piped values pulled by a Merge Code.
[question("value"), id="3",finaldelimiter=" or, "]
Example:
The above Merge Code will pipe the answers from a Multi Select question (which are delimited with a comma by default) and add an " or, " before the last piped value.
If a respondent selects all options a through e the output will be:
a, b, c, d, or, e
questiondelimiter
The question delimiter attribute, in bold text below, can be used to specify the delimiter between sub-questions when using the Merge Code for the overall question. For example, Custom Groups, Contact Forms, and Table questions.
[question("value"),id="3",questiondelimiter=";"]
Example:
The above Merge Code will pipe the answers from a List of Textboxes (which are delimited by a comma by default) and separate the values with semicolons.
Replacing Strings within Merge Codes
When using Merge Codes to pull answers from previous questions to use later in your survey, you can find and replace strings of text.
Here is an example:
[question("option value"), id="3", replace="string to find", with="string to replace"]
Let's say you want to refer to a previous answer but in a different format or tense. In the below example survey, the respondent answers "I live in the US". That's what we call the 'string to find'.
The ID in the above merge code is the question ID. Question IDs can be enabled under the View Settings link in the upper right of the survey Build tab.
- To do so, click View Settings and check the Question IDs option.
- Once enabled, question ID numbers will be visible directly on the Build tab below each question.
- In the subsequent question, we'd like to refer to this answer using the merge code. To do so, we replace the phrase "I live in the US" with just "in the US" to complete the sentence:[question("option value"), id="2", replace="I live in the US", with="in the US"]
Test it out in this survey:
http://www.surveygizmo.com/s3/1307950/replace
If you have multiple string values in a previous question that you want to transform in a follow up question, you will need a merge code for each option.
For example, in the following question we ask respondents what state they live in:
We will then want to transform CO to Colorado and AZ to Arizona in a follow-up question. To do so, we need to format the merge codes as follows:
[question("option value"), id="4", option="10001", replace="CO", with="Colorado"][question("option value"), id="4", option="10002", replace="AZ", with="Arizona"]
Note that each answer option that will be replaced requires its own dedicated merge code. The answer option is specified in the merge code via the option="10001" parameter. You can use the merge code helper in the follow-up question to automatically reference specific answer options.
To learn more visit our Merge Codes Tutorial!
Admin
— Dave Domagalski on 03/12/2018
@ Francisco and @ Christian: Thank you for exploring our documentation content!
I'm afraid that there is not a merge code available to pull specifically the first ranked option.
You could explore modifying one of our existing scripts to achieve what you are looking to do if you want to try your hand at scripting.
If you're not sure how to tackle this, our programming services team does this kind of work all the time!
Here is the script that shows and hides pages based off of a drag and drop question:
https://help.surveygizmo.com/help/drag-and-drop-show-when
Here is more information about our programming services team:
https://www.surveygizmo.com/programming-services/
I hope this gives you both some options!
David
Documentation Specialist
SurveyGizmo Customer Experience
— Christian on 03/12/2018
Hi there!
I would also like to know if there the a way to use only the 1st ranked option from a Drag&Drop Question into a merge code. :)
Best regards,
Christian
— Francisco on 02/21/2017
Hi, do you know if it is possible to use 1st ranked option from a drag and drop rank into a merge code? thanks
Admin
— Dave Domagalski on 02/06/2017
@Business: Thank you for your question!
When building hyperlinks that include SurveyGizmo merge codes, here are the main things that I advise:
- Use the standard merge code format: [question("value"), id="10"]
- Disable the HTML Editor under Account > My Preferences (this will disable the WYSIWYG editor and allow you to use html code to properly construct the link).
- Surround the linked text, including merge code, with single quotes. The double quotes should be kept within the merge code.
- De-select the Clean My HTML option for the element.
The following document touches on the above suggestions in a bit more detail:
https://help.surveygizmo.com/help/using-merge-codes-inside-html
I hope this helps!
David
Documentation Specialist/Survey Explorer
SurveyGizmo Customer Experience
— Junior on 02/06/2017
I asked a question earlier about merging response values into a dynamic URL displayed on the confirmation page or email. I've done it successfully a few times, but it seems to work inconsistently and occasionally breaks when no changes have been made.
Let's say for example I want to include a link that directs the respondent to a separate satisfaction survey. I want to include a URL variable in that link that pulls in a unique ID (string of numbers and letters, no spaces) stored as a response in the original survey ([question("value"), id="10"]. The URL should therefore look something like this: http://www.surveygizmo.com/s3/1234567/Satisfaction-Survey?uniqueid=[question("value"), id="10"]
I've tried the following formats for the merge code:
[question("value"), id="10"]
[question("value"), id="10", urlencode="true"]
[question("value"),id="10"] (no spaces)
[question("value"),id="10",urlencode="true"] (no spaces)
[question('value'), id='10'] (single quotations)
[question('value'), id='10', urlencode='true'] (single quotations)
[question('value'),id='10'] (single quotations and no spaces)
[question('value'),id='10',urlencode='true'] (single quotations and no spaces)
And I've tried both checking and unchecking the 'Clean My HTML' option in Layout. The standard merge code - [question("value"), id="10"] - seems to work the most consistently, but sometimes it will break at the first quotation mark - [question( - and when I check the hyperlinked url, it always displays as 'http://www.surveygizmo.com/s3/1234567/Satisfaction-Survey?uniqueid=[question(' whether it works or not, which makes it tougher to troubleshoot. Support recommended before that I use single quotation marks, but that doesn't seem to code correctly either (last I tried it turned into [question(%27value%27),%20id=%2710%27] in the browser). Do you have a standard recommendation on merge code formatting that will work consistently for URLs when there are no spaces in the merged response?
Admin
— Dave Domagalski on 01/17/2017
@My: Thank you for your question!
If needing to replace more than one string for a specific question, the following format would need to be used (example):
Have you always lived in [question("option value"), id="5", option="10001", replace="CO", with="Colorado"][question("option value"), id="5", option="10002", replace="AZ", with="Arizona"]?
I have expanded the last section in this article provide additional detail.
I hope this points you in the right direction!
David
Documentation Specialist/Survey Explorer
SurveyGizmo Customer Experience
— My on 01/15/2017
Hi. For this code [question("option value"), id="3", replace="string to find", with="string to replace"], Can you advise the code to have more than 1 replacing string? Thank you
Admin
— Dave Domagalski on 12/08/2016
@DHR: Happy to help out!
My apologies that there is not a more elegant solution! If you do want to explore a custom scripting option, our Programming Services Team is a great resource: https://www.surveygizmo.com/programming-services/
Cheers,
David
Documentation Specialist/Survey Explorer
SurveyGizmo Customer Experience
— DHR on 12/08/2016
Thanks, Dave! That was my first thought, but unfortunately, there are at least several hundred possible row headers and each contact could have up to 35 items in the delimited string. I accomplished this before by uploading each possibility in 35 subsequent columns through the Login/Password action and using those to prepopulate 35 hidden textboxes. I set up the radio button grid to merge the row labels drawing from each of the 35 textboxes and then used logic to hide the rows if the textbox was empty. This works, but I was hoping to find a more efficient method. Thanks again!
Admin
— Dave Domagalski on 12/07/2016
@DHR: I think you are on the right track!
Rather than piping the comma delimited string into a hidden textbox, I would suggest using the comma delimited string as the Default Answer for a Checkbox Question:
https://help.surveygizmo.com/help/preselect-answer-option#using-contact-data-as-default-answer-text
The Checkbox Question would need to contain all possible answer options that could theoretically become your grid rows (this option is under the assumption that you will know all possible row headers).
You could then use the Checkbox Question as the source question for piping into the rows of your grid.
As with any advanced piping setup, I would recommend thorough testing!
I hope this helps!
David
Documentation Specialist/Survey Explorer
SurveyGizmo Customer Experience
— DHR on 12/07/2016
This article was very helpful! I am trying to dynamically generate the row labels for a radio button grid based on a delimited string uploaded to a contact's metadata. For example, if a contact's Custom Field 1 were equal to "Example1,Example2,Example3,Example 4", I'd like the first row label of my radio button grid to be equal to "Example1" and so forth.
I attempted to pipe the comma delimited string into a hidden textbox and use that textbox as the row source via "Pipe Into Rows" on my radio button grid, but understandably, textbox questions are not populated int he "Pipe Into Rows" dropdown menu.
Is this something that would need to be done via custom scripting, or is there a simpler solution?
Admin
— Dave Domagalski on 12/07/2016
@Marcus: Thank you for your question!
There is a merge code to pull in the answer option title rather than the reporting value (if the two are different). For this, you would use a merge code containing the 'title' attribute.
The following link should help clarify:
https://help.surveygizmo.com/help/merge-code-intro#question-merge-codes
I hope this helps!
David Domagalski
Survey Explorer & Documentation Specialist
SurveyGizmo | http://surveygizmo.com
— MARCUS on 12/06/2016
Is there a merge code to pull the option data in a radio button or dropdown question rather than pulling the reporting values
Admin
— Bri Hillmer on 11/17/2016
@Business: I've done similar things before. You should be able to build out the base URL and merge in the record ID using a merge code. Try it out and let us know if you run into any snags!
Bri
Documentation Coordinator
SurveyGizmo Customer Experience Team
— Junior on 11/16/2016
Is it possible to build a URL with a merge code? Our use case is giving the survey-taker a link to a specific Salesforce record by pulling the record ID, pulled earlier in the survey into a question, into a URL on the "Thank you" page.
Admin
— Bri Hillmer on 05/19/2016
@Strasser: My apologies for the outdated navigation. I've updated the steps to the following:
Question IDs can be enabled under the Customize link in the upper right of the survey builder, simply check the Question IDs option.
I hope this helps!
Bri
Documentation Coordinator/Survey Sorceress
SurveyGizmo Customer Support
— Jan on 05/19/2016
The mentioned advanced list view is not available in the "customize" menu. How the switch on the question ID in the builder?
"The ID in the above merge code is the question ID, which can be found on the right side of any question once you've enabled the Advanced List View from the Customize link in the upper right of the survey builder."
Admin
— Bri Hillmer on 03/15/2016
@Gregorioetchevers: The delimiter option would only work in the case that the merge code is pulling multiple values, for example the merge code for the overall checkbox question. If you wish to just separate those 3 option values using a comma you could simply specify the comma in between like so:
[question("option value"), id="20", option="10158"], [question("option value"), id="20", option="10159"], [question("option value"), id="20", option="10160"]
Of course the trouble with this is that if any one of those options is not selected you'll end up with 2 commas with nothing in between. What you are trying to achieve is pretty advanced and would probably better suited for custom scripting. Here is our scripting documentation if you wish to try your hand at this:
http://script.surveygizmo.com/help
I hope this helps!
Bri
Documentation Coordinator/Survey Sorceress
SurveyGizmo Customer Support
— Gregorioetchevers on 03/14/2016
is it possible to use limiter in this way?
I want to merge codes selected in the previous question but does not work. Not all the codes, some of them must be excluded:
Example:
P1: Select all options you like:
-Opt 1
-Opt 2
-Opt 3
-Opt 4
-Opt 5
P2: Why did you select [merge only selected in P1 separated with comma but exclude opt 4 and 5]?
I tried something like this
[question("option value"), id="20", option="10158", delimiter=", "]
[question("option value"), id="20", option="10159", delimiter=", "]
[question("option value"), id="20", option="10160", delimiter=", "]
But it dosn't work. It still merge the items without the delimiter Opt1 Opt2
Opt3
Thanks!
Admin
— Bri Hillmer on 01/19/2016
@Linda: My pleasure! Sorry I didn't have a better answer!
Bri
Documentation Coordinator/Survey Sorceress
SurveyGizmo Customer Support