A great way to do it! I will say though that the `defer` attribute is not quite enough if you've set your sites on performance. Anything under 3 seconds of attempting to load the Marketo library will cause Lighthouse to include it in its performance report. Something you can add to make sure you get those three seconds:
<script type="text/javascript">setTimeout(function(){var mscript = document.createElement("script");mscript.src="//forms.mysite.com/js/forms2/js/forms2.min.js";var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(mscript, s);},3000);</script>
Just as I mentioned in the other thread, page speed score doesn't really work like that. When you put it into a professional marketing site that is already going to struggle for a 90+ score depending on its UX, the weight of the Marketo library is game changing.
Sure, it would be great if we could ignore automated tests, but that's not the age we're in. Try explaining that fear to an SEO team or really even a CRM team that understands the SEO impact of performance scores when you're dealing with top brands, and you'll have a better understanding of that "fear" and a more rational reasoning for balance. Getting people to your Marketo forms is debatably more important than how fast they load.
This is the exact same page with only 1 difference. The 84 score is with Marketo deferred as in your example here, the second is with Marketo delayed 3 seconds after page load.
The last image are the performance flags that google page speed throws for the Marketo script.
This is the best explanation I've come across so far. After implementing your suggested solution, it works great besides the "on page" thank you message never appears (not using follow-up thank you landing page). The form simply reloads after the user submits the form. Any idea/guidance how to go about making sure the thank you confirmation message appears without the form reloading? I'm using the method mentioned on the Marketo blog here (Show Thank You Message Without a Follow-Up Landing Page) in addition to your method above to show thank you message after form submission. Thanks in advance for the help!
But that page doesn’t use the method in this post to add the onSuccess. It tries to just use the MktoForms2 without waiting for it to be ready.
//Make confirmation message appear after form submit
MktoForms2.whenReady(function (form){
//Add an onSuccess handler
form.onSuccess(function(values, followUpUrl){
//get the form's jQuery element and hide it
form.getFormElem().hide();
document.getElementsByClassName('form-msg').style.visibility = 'visible';
//return false to prevent the submission handler from taking the lead to the follow up url.
return false;
});
My apologies. I was testing to see if this would fix the issue by adding the code from the Marketo blog directly into the code via your post and accidentally updated the live version of the page. I reverted back to the original code following your recommendation. Cache is purged as well so you should see the updated version. https://www.billtrust.com/begin-your-accounts-receivable-automation-journey-sem/ . Do you have a recommend way to accomplish this with your suggestion? Any advise would be greatly appreciated.
Does someone who knows more think that the onSuccess listener may help with issues we are having with Marketo reCaptcha "submission failed, please try again later" issues?