The framework would be like so:
MktoForms2.whenReady(function(form){
var formEl = form.getFormElem()[0],
arrayify = getSelection.call.bind([].slice),
subscriptionCheckboxes = formEl.querySelectorAll("input[type='checkbox'][name^='subscription']"),
initialSubscriptionCount = getSubscriptionCount();
form.onSuccess(function(values,thankYouURL){
var thankYouLoc = document.createElement("a");
thankYouLoc.href = thankYouURL;
var netSubscriptionCount = getSubscriptionCount() - initialSubscriptionCount;
/*
netSubscriptionCount - Number, the net difference between previous and current # of subs
values.Unsubscribed - String, "yes"|"no"
Now you can decide Thank You URL based on these (and/or other) factors
and then
document.location.href = "https://example.com/best_fit_thank_you_page" + thankYouLoc.search;
*/
return false;
});
function getSubscriptionCount(){
return arrayify(subscriptionCheckboxes)
.filter(function(sub){
return sub.checked;
})
.length;
}
});
EDIT: While the code above works, realize I didn't heed some of my own Forms API best practices! This is better:
MktoForms2.whenReady(function(form){
var formEl = form.getFormElem()[0],
subscriptionFieldNamePattern = /^subscription/,
initialSubscriptionCount = getSubscriptionCount();
form.onSuccess(function(values,thankYouURL){
var thankYouLoc = document.createElement("a");
thankYouLoc.href = thankYouURL;
var netSubscriptionCount = getSubscriptionCount() - initialSubscriptionCount;
/*
netSubscriptionCount - Number, the net difference between previous and current # of subs
values.Unsubscribed - String, "yes"|"no"
Now you can decide Thank You URL based on these (and/or other) factors
and then
document.location.href = "https://example.com/best_fit_thank_you_page" + thankYouLoc.search;
*/
return false;
});
function getSubscriptionCount(){
var currentValues = form.getValues();
return Object.keys(currentValues)
.filter(function(field){
return subscriptionFieldNamePattern.test(field);
})
.filter(function(subscriptionField){
return ["yes","true"].some(function(truthy){
return currentValues[subscriptionField] == truthy;
});
})
.length;
}
});
This snippet assumes you'll maintain the naming convention where the subscription-related fields begin with "subscription". If not, you'll have to maintain that collection in more of a hard-coded way.