Run code on start and submit with Marketo forms

Rocky_Patel
Level 1

Run code on start and submit with Marketo forms

Sanford Whiteman

Hello.  Once a user starts a form and submits I need to run the below code.  How can I do it?

When a user starts the form, run this code:

_satellite.track('form_start', {

      form_type:'[ type of the form ]',

      form_title:'[ document's title (if applicable) ]'

   }

);

When a user successfully submits the form, run this code:

_satellite.track('form_submit', {

      form_type:'[ type of the form ]',

      form_title:'[ document's title (if applicable) ]'

   }

);

Would this work for after successfully submitting the form?

MktoForms2.whenReady(function(form) {  
form.onSuccess(function(values, followUpUrl) {


_satellite.track('form_submit', {
form_type:'gated content',
form_title: '[documents title]'
}
);



return false;
});
}) ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I'm not sure how to go about the start piece though.  Any assistance would be appreciated.

4 REPLIES 4
SanfordWhiteman
Level 10 - Community Moderator

Re: Run code on start and submit with Marketo forms

Can you highlight all your code using the Syntax Highlighter? Thanks. Then we'll continue.

Rocky_Patel
Level 1

Re: Run code on start and submit with Marketo forms

Updated

SanfordWhiteman
Level 10 - Community Moderator

Re: Run code on start and submit with Marketo forms

Thanks for fixing that up.

"Start" is a little vague, but interpreting that as the first time anything in the form is focused, capture (literally) that event like this:

MktoForms2.whenReady(function(form){
var formEl = form.getFormElem()[0];

formEl.addEventListener("focus", logFirstFocus, true);

function logFirstFocus(e){
formEl.removeEventListener("focus", logFirstFocus, true);
// your _satellite.track() stuff goes here
}
});‍‍‍‍‍‍‍‍‍‍

For after successful submission, yes, you'd use the built-in onSuccess event.

Rocky_Patel
Level 1

Re: Run code on start and submit with Marketo forms

Thanks Sanford.