Using default token values in custom module for unknown contact
SOLVE
I have a custom module that replaces checklist values in a form with personalization tokens. If the contact doesn't have the properties coordinating with the personalization tokens filled out, it alters to the default property. However, I can't seem to make this work if the visitor is totally anonymous, because the code gives me an error unless I put it in an IF statement for contact being known.
My hubl/html:
Hubl & html
{% if widget.formQuestion %}
{% set formQuestion = widget.formQuestion %}
{% else %}
{% set formQuestion = "" %} <!-- assign a value to dynamicLabel1 in case contact is unknown -->
{% endif %}
{% if contact.email %}
{% set dynamicLabel1 = "{{contact." ~ widget.dynamicProp1 ~ "}}" %}
{% set label_list = [] %}
{% for text in module.dynamicProp1 %}
{% do label_list.append( "{{ contact." ~ text ~" }}" ) %}
{% endfor %}
{% set label_listb = [] %}
{% for text in module.dynamicProp2 %}
{% do label_listb.append( "{{ contact." ~ text ~" }}" ) %}
{% endfor %}
{% set combinedLabels = [] %}
{% for i in range(label_list|length) %}
{% do combinedLabels.append( "{{ label_list[" ~ i ~ "] }} {{ label_listb[" ~ i ~ "] }}" ) %}
{% endfor %}
{% if widget.need_to_combine_a_second_property_ == true %}
{% set labels = "{{ combinedLabels|join(', ') }}" %}
{% else %}
{% set labels = "{{ label_list|join(', ') }}" %}
{% endif %}
<data
id="dynamicProps"; data-question="{{ formQuestion }}" data-labels="{{ labels }}">
</data>
{% form
form_to_use="{{ module.form_field.form_id }}"
response_response_type="{{ module.form_field.response_type }}"
response_message="{{ module.form_field.message }}"
response_redirect_id="{{ module.form_field.redirect_id }}"
response_redirect_url="{{module.form_field.redirect_url}}"
gotowebinar_webinar_key="{{ module.module.form_field.gotowebinar_webinar_key }}"
%}
{% endif %}
My JS:
JS
$(window).load(function(){
var dynamicFields = document.getElementById('dynamicProps');
var labels = dynamicFields.dataset.labels
var formQuestion = dynamicFields.dataset.question;
var options = $('input[name='+formQuestion+']').toArray().map(function(checkbox) {
return $(checkbox).val()});
if (labels === "") {
} else {
var formLabels = labels.split(', ');
for (var i = -1; i < options.length; i++) {
$('input[value='+(options[i])+']').next().text(function() {
return formLabels[i];})
};
}
//document.write(formLabels)
});