Dynamic Redirect based on more than 1 field

  • 16 March 2022
  • 1 reply
  • 75 views

Let’s say I have a form like this

form

I want 3 cases to happen upon form submission

  1. Device Type = Apple & redirect <= 5
    action → redirect to 1st url

  2. Device Type = Android & redirect <= 5
    action → redirect to 2nd url

  3. Any other combination of Device Type and redirect
    submit to pardot form handler

I want to ensure that all form data is captured in Unbounce as well

This is the script i came up with based on various other community posts, I have the form setting to post to pardot handler by defaut

<script>
<script>
  window.ub.hooks.beforeFormSubmit.push(function() {
    var number = args.formElement.querySelector('#redirect').val();
	var units = parseInt(number);
	var device = args.formElement.querySelector('#device_type').val();
	
    console.log(number);
    console.log(units);
    console.log(device);
  });
  
  window.ub.hooks.afterFormSubmit.push(function(){
	if(isNaN(units)){
      alert("not a valid number");
	}
	
	else if( device ==='Apple' && units <= 5){
		window.location.href = "https://www.apple.com";
	}
	
	else if( device ==='Android' && units <=5){
		window.location.href = "https://www.google.com";
	}
	
	else{
	}
  });
</script>

doesn’t even come close to working… any help would be appreciated 🙂


1 reply

Okay FWIW I got this mostly working

<script>
  window.ub.hooks.afterFormSubmit.push(function(args){
    var delay = '2000'  //Enter redirect delay time in milliseconds
    var number = args.formElement.querySelector('#number_of_vehicles').value;
    console.log(number);
	var device = args.formElement.querySelector('#device_type').value;
    console.log(device);
    
	if( device ==='Apple Mobile Device' && number <= 5){
      window.setTimeout(function(){
		window.location.href = "https://www.apple.com";
      }, delay);
	}
	
	else if( device ==='Android Mobile Device' && number <=5){
      window.setTimeout(function(){
		window.location.href = "https://www.google.com";
      }, delay);
	}
	
	else{
       window.setTimeout(function(){
		window.location.href = "https://www.facebook.com";
      }, delay);
	}
  });
</script>

Still can’t get it to simultaneously submit to a Pardot form handler… guess I’ll have to use a webhook, what a gigantic pain in the ass.

Reply