Switch a Marketo form’s Thank You page based on the current URL

SanfordWhiteman
Level 10 - Community Moderator
Level 10 - Community Moderator

At first read, the post title might make you think, “Why is Sandy spending time on this — it’s built-in, right?”

 

Well, no. Form Editor lets you set Advanced Thank You Pages based on values submitted with the form. But, without special setup, the page hosting the form isn’t one of the submitted values!

 

It’s easy to add it in, though.

 

Add a field

First, create a new Text Area[1] field Last Marketo Form URL:

2020-10-30-19_42_52-Marketo-_-Field-Management---Admin

 

 

Add that field to your form(s)

Note the field does need to be added to the form in Form Editor. It can’t be set only on the JS side with addHiddenFields, since fields added that way won’t show up in Thank You choices.

2020-10-30-19_52_42-fO

 

 

Add some Forms 2.0 JavaScript

Then a little bit o’ JS on the page:

 

MktoForms2.whenReady(function(mktoForm){
  mktoForm.setValues({
    lastMarketoFormURL : document.location.href
  });
});

 

(If you used the exact field name given above, I can safely predict your form/SOAP field name will be lastMarketoFormURL. You can export your fields from Field Management if you want to double-check.)

 

As usual, put this code (in a <script> tag, of course) either:

  • just before the closing </body> tag on a Marketo LP
  • anywhere after the Forms 2.0 embed code on a non-Marketo LP


The load order is important with any Forms 2.0 code, as you need the global MktoForms2 object to have been created first by the forms2.min.js library.

 

Now, you can use Add Choice in Form Editor to select an Advanced Thank You:

2020-10-30-19_55_42-fO

 

 

Alternate approach: Use just the /pathname part of the URL

You may not want to bother passing the protocol and hostname, if those are always the same and/or they don’t factor into your Thank You decisions (makes the Advanced Thank You dialog easier to read). And the query string may not be relevant, either.

 

So you can trim down the value from https://www.example.com/pages/lp01.html?utm_campaign=blah-blah to just /pages/lp01.html.

 

In this case you probably could get away with a String instead of a Text Area, since paths alone are usually < 255 characters (though they don’t have to be). Definitely change the field name to the more informative Last Marketo Form Path.

 

Then the JS becomes:

 

MktoForms2.whenReady(function(mktoForm){
  mktoForm.setValues({
    lastMarketoFormPath : document.location.pathname
  });
});

 

And everything else is as above.

 

NOTES

[1] A String doesn’t suffice, since a full URL may well be longer than the official limit of 255 characters.

1481
0