SOLVED

Re: Script for passing the page url on to a Marketo field

Go to solution
Sarin
Level 3

Script for passing the page url on to a Marketo field

We would like to capture and store the most recent page url (from which the lead filled out the form in a Marketo field).  Does anyone have the script for the same?

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Script for passing the page url on to a Marketo field

13 REPLIES 13
SanfordWhiteman
Level 10 - Community Moderator

Re: Script for passing the page url on to a Marketo field

Sarin
Level 3

Re: Script for passing the page url on to a Marketo field

Thanks a lot  @SanfordWhiteman 🙂

 

Could you provide the complete script please and also tell us how to implement it on both external as well as Marketo landing pages (Guided template)

 

I assumed we need to add some more code to the below:

MktoForms2.whenReady(function (form) {
    form.addHiddenFields({ LastFormURL : document.location.href });
});

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Script for passing the page url on to a Marketo field

No, that's the code. There's nothing else.

 

Simply make sure that code runs after the embed code. On non-Marketo pages: put it anywhere after the embed code (since you control placement). On Marketo LPs: put it just above the closing </body> tag.

Sarin
Level 3

Re: Script for passing the page url on to a Marketo field

Thanks @SanfordWhiteman , You are awesome!!!!

 

So just confirming we just have to add the below code on the page and just add the hidden field in the form by the name 'LastFormURL' (keep the autofill option to use default value and leave it blank)..

 

<script>
MktoForms2.whenReady(function (form) {
    form.addHiddenFields({ LastFormURL : document.location.href });
});
</script>
SanfordWhiteman
Level 10 - Community Moderator

Re: Script for passing the page url on to a Marketo field


... just add the hidden field in the form by the name 'LastFormURL' (keep the autofill option to use default value and leave it blank)..

No, you don't have to add the hidden field in Form Editor at all. The JS takes care of everything.

Sarin
Level 3

Re: Script for passing the page url on to a Marketo field

@SanfordWhiteman : I created the Url form field (LastFormURL) and also added the below script as suggested on the test page but the page url is not getting captured in the new field after filling out the form. Could you help please - http://au-sn01.marketo.com/lp/987-YUV-596/lastformurl-testing.html

 

<script>
MktoForms2.whenReady(function (form) {
    form.addHiddenFields({ LastFormURL : document.location.href });
});
</script>

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Script for passing the page url on to a Marketo field

It's adding the field as expected, as you can see if you inspect the page:

2021-02-22 03_01_08-Waterfox.png

 

Make sure in Admin » Field Management that the SOAP name of the field is correct. (Export the field names and check the resulting spreadsheet.)

Sarin
Level 3

Re: Script for passing the page url on to a Marketo field

Thanks @SanfordWhiteman 🙂

 

Actually just noticed that API name of the field is lastFormURL ('l' in lowercase) . As per my knowledge we would not be able to update the API name of a field once it is created so I updated the field name (API name) in the script. I assume we need to provide the API name instead of the field name in the script?

 

<script>
MktoForms2.whenReady(function (form) {
    form.addHiddenFields({ lastFormURL : document.location.href });
});
</script>

 

It is working now :), do we need to keep the type of field as string or will url type field will also work?

SanfordWhiteman
Level 10 - Community Moderator

Re: Script for passing the page url on to a Marketo field

If that's the SOAP API name, yes.

 

P.S. Please use the syntax highlighter (Insert/Edit code sample) when posting code. Each time you paste your code in as unformatted text, I've been editing your post for you and leaving you a note.