SOLVED

Want to populate a field that has the actual URL a web form is filled out on.

Go to solution
Nate_Oosterhous
Level 7

For our Marketo Landing Pages with Forms on them we want to capture what webpage url the form was filled out on.  {{trigger.Web Page}} does not work because that only give the Marketo description name.  We need the full url of the Landing Page.  How can we do this?

1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

Actually, our bad, do

form.addHiddenFields

not

form.vals

View solution in original post

28 REPLIES 28
Anonymous
Not applicable

Hi there, is there a way to implement this script on the form itself?

SanfordWhiteman
Level 10 - Community Moderator

Yes, but I wouldn't recommend it.   See my thoughts on this other thread from just today: Is it okay to use javascript in rich text fields in forms?

Anonymous
Not applicable

Sanford I think you can help me out.

I'm looking at creating a global form for gated content and using the URL to route users to the correct content.

I may be in a bit over my head right now, but maybe you can help me out.

I placed the below into a RT field in the form... based on this thread

<script>

MktoForms2.whenReady(function(form){

     form.addHiddenFields({"Latest_Form_Fill_Webpage":document.location});

})

</script>

I'm pretty sure my implementation is wrong for a few reasons.

1. when I save the RT field, marketo automatically adds to the code //[Cdata! ....

2. I think Mkto.Forms2.whenReady() only runs on submit? I'm not sure and don't know where to find documentation right now.

Thoughts?

Should I avoid using the JS in the Form Rt and just implement in the footer of all my wordpress pages + MKTO landing page templates?

SanfordWhiteman
Level 10 - Community Moderator

I'm looking at creating a global form for gated content and using the URL to route users to the correct content.

If you're not using the field outside of the form context, you already have the {{trigger.web page}} to work with, so be sure you need to save the value before you do this at all.

form.addHiddenFields({"Latest_Form_Fill_Webpage":document.location});

Use document.location.href if you want a string.  Even if document.location works because it gets toString()'d, it's not good to depend on that.

1. when I save the RT field, marketo automatically adds to the code //[Cdata! ....

That's fine, it's an old-fashioned compatibility hack that isn't necessary but it isn't affecting the code.

2. I think Mkto.Forms2.whenReady() only runs on submit? I'm not sure and don't know where to find documentation right now.

whenReady runs when the form is ready for use, not only on submit.  It can be used to add onSubmit listeners but the whenReady function itself will run immediately.  whenReady is the right place for what you're doing. See http://developers.marketo.com/javascript-api/forms/

Should I avoid using the JS in the Form Rt and just implement in the footer of all my wordpress pages + MKTO landing page templates?

If you only have one global form, you can keep it in the RT Area. The disadvantage (or advantage depending on your angle) is that you have to reapprove the form to make changes to the form behaviors.  This means also reapproving Marketo-hosted LPs.

Anonymous
Not applicable

Many Thanks! Going to take a dive into this  soon and I'll report back.

Anonymous
Not applicable

Thanks for the support!

Global form is live and logic for thank you page is implemented based on the custom field filled by the JS.

In my case looks like I ran into issues because the field had a different syntax when looking at the API convention.

Kenny_Elkington
Marketo Employee

You'll need to use a script to populate this field, something like this:

<script>

MktoForms2,whenReady(function(form){

     form.vals({"yourFieldApiName":document.location});

})

</script>

This will add the URL of the page into the specified field, yourFieldApiName.

Nate_Oosterhous
Level 7

Hi Kenny.  I am not a developer.  Where would I specifically add this script?  Thanks!

Kenny_Elkington
Marketo Employee

You can just add an HTML block to your page and input the script there.  The block should be ordered after your form.

Nate_Oosterhous
Level 7

This would just be the Marketo Landing Page template HTML correct?

Kenny_Elkington
Marketo Employee

No the page itself, not the template, unless you want this behavior to occur with all forms on pages based on that template.

Nate_Oosterhous
Level 7

Hello again Kenny.  Below is my script I added as an HTML block and it is below the form block.  For some reason it isn't populating that custom field.

<script>

MktoForms2,whenReady(function(form){

     form.vals({"Latest_Form_Form_Webpage__c":document.location});

})

</script>

SanfordWhiteman
Level 10 - Community Moderator

It's MktoForms2.whenReady (period, not comma).

Nate_Oosterhous
Level 7

Hi Sanford.  I made that switch and it still does not work.  Any ideas?

SanfordWhiteman
Level 10 - Community Moderator

Post a link to your page so I can see the JS console.  Hard to know without that.

Nate_Oosterhous
Level 7
SanfordWhiteman
Level 10 - Community Moderator

Actually, our bad, do

form.addHiddenFields

not

form.vals

Anonymous
Not applicable

Does this script have the option to be used globally on all webpages, or do we have to individually add to each webpage with a form?

SanfordWhiteman
Level 10 - Community Moderator

You can add it to all webpages as long as you pre-check if the forms library exists:

window.MktoForms2 && MktoForms2.whenReady(function(form){

     form.addHiddenFields({"Latest_Form_Form_Webpage__c":document.location.href.split('?')[0]});

});

Nate_Oosterhous
Level 7

You guys are awesome!  It worked.  I have one more thing.  Is there anyway to remove UTM parameters so it is a bare URL?