Re: Is there a way to add JS to a Marketo form button?

future_vision
Level 1

I am looking for a way to add a datalayer push to a Marketo form button. I thought I had seen a way to add code awhile back in design studio but I must have been mistaken. Is there a way to push a datalayer event with the click of the form button?

11 REPLIES 11
Jasbir_Kaur
Level 5

I am sharing a link, if it might be helpful.

 

https://support.google.com/tagmanager/answer/7679219?hl=en

SanfordWhiteman
Level 10 - Community Moderator
Problem with this is you'll log an event for people who forgot to fill in a field or in any other way failed to pass validation, in addition to people who successfully submit. Not a very useful event.
future_vision
Level 1

Agreed. It is ultimately useful when comparing clicks on the button vs successful submissions which would clue us in as to whether or not we are doing a good job making sure that it is understood as to what fields are required. If users are clicking and the form is not submitting that could mean we failed. That or there is an error with the form submission. They fill all the fields but somehow we end up blocking a successful submission. Maybe a hidden field we marked as required that they can't fill out. Of course, we should probably catch an issue like this during QA but sometimes things slip by or additional code on the page breaks exiting JS.

SanfordWhiteman
Level 10 - Community Moderator

Possibly, but in my experience it just results in people saying “we’re missing leads!” and going on a wild goose chase.

SanfordWhiteman
Level 10 - Community Moderator

"Adding to the button" isn't the way to think about it. You're adding code that fires upon form success.

 

You can do anything you want in onSuccess:

 

MktoForms2.whenReady(function(readyForm){
  readyForm.onSuccess(function(submittedValues,originalThankYouHref){
    // do whatever you want here, then set
    // document.location.href = originalThankYouHref 
    // when other tasks are done
    return false;
  });
});

 

future_vision
Level 1

I already have code in place for a datalayer push for the onsuccess event. I guess what I was looking at were clicks regardless of whether the form was submitted successfully or not but I can see just tracking successful clicks the way you described.

Dave_Roberts
Level 10

All buttons on all Marketo forms should natively have the "mktoButton" class built into them. This might not exist if you're using a script to strip classes or something like that, but it should be a fairly reliable selector to script against. Another option would be to build a selector in the context of the "form.mktoForm" element, something like "form.mktoForm button"

 

Dave_Roberts_0-1724345374450.png


My Javascript isn't as good as it could be, but in jQuery you could write this something like:

MktoForms2.whenReady(function(readyForm){
  $('form.mktoForm button.mktoButton').click(function(){
    // data layer push stuff here
  });
});

 

As Sanford was mentioning you might want to consider the implications of tracking this type of behavior -- I can see it as being useful in the scenario you're outlining (to identify friction w/ form fills) but it could open another can of worms down the road with chasing down "leads" that were actually just errors in data-entry on the form. You may be able to query the form when the button is clicked to check for fields that contain the "mktoInvalid" class to determine if the form has any errors in the fields to further qualify a "button click b/c of errors" from "any button click"?

 

Maybe the logic would look something like:
>> When the button is clicked, IF there are any fields w/ "mktoInvalid" class, push something that suggests "there is an error in the form entry" -- ELSE, onFormSuccess, push a success event instead.

 

Dave_Roberts_1-1724345973527.png

 

SanfordWhiteman
Level 10 - Community Moderator

Don’t need any of that, onValidate runs whenever form submission is attempted.

 

MktoForms2.whenReady(function(readyForm){
  readyForm.onValidate(function(nativeValid){
     // form submission was *attempted*, log accordingly
  });
});

 

 

Also note a click on a particular button isn’t the only way to submit a form.

Dave_Roberts
Level 10

Thanks for keeping me honest here Sanford, I'm always learning a little something more from you -- appreciate it!

Ishita_Chawra
Level 3

Another discussion on custom form buttons, could be helpful Solved: Custom Form Buttons - Marketing Nation (marketo.com)

SanfordWhiteman
Level 10 - Community Moderator

Wouldn't really apply, though (that thread is about custom styling, this is about JS code).