Embed code on Forms 2.0

Anonymous
Not applicable

Embed code on Forms 2.0

Hey all,

I'm trying to use the Forms 2.0 API to push some data into GA. I saw the resource that explains how to do this, but I'm unclear where to put the code. Does this just sit as an HTML box on the landing page?

Thanks for the guidance!
-Trask
Tags (1)
3 REPLIES 3
Calvin_Lam
Level 4

Re: Embed code on Forms 2.0

The sample code on the developer site pretty much all deal with situations when you embed the code on your website.  Specificially, you specify a callback function to perform some actions when calling the loadForm function

However, if you are on a Mkto landing page, the loadForm function call is already generated by us so you need other ways to listen to form events.  To be safe, you should only reference the form object when either document.ready or window.onload fires.  Then in your event handler function, get the form object using MktoForms2.getForm(formId).  Once you have the form object, you can register more callback functions of any of the events specified under the Forms documentation section,  For example, onSubmit(callback)

And you are right that you will embed the code in the Custom HTML editor or in the LP template if you need to reuse it often.  In the LP template case, since you don't know all the form IDs yet, you will need to use MktoForms2.allForms() instead to get an array of the forms.


Calvin_Lam
Level 4

Re: Embed code on Forms 2.0

Following up on my last comment, I put together a very basic script such that when the form is submitted, I just alert the value of the email address.

My Test Landing Page - http://go.kokopop.com/Form2APILP.html and below is the script when you view source.

<script src="//munchkin.marketo.net/jquery-latest.min.js"></script>
<script>
var $jQ = jQuery.noConflict();
var myForm  = null; //will be set to the form object after the window is loaded
function printSomeValues()
{
var vals = myForm.getValues();
window.alert("Here is the email address - " + vals.Email);
myForm.submitable(true);
}
$jQ(window).load(function() {
console.log("Good news ... window is loaded");
if (MktoForms2.getForm(1944))
{
myForm = MktoForms2.getForm(1944);
myForm.onSubmit(function(){
printSomeValues();
});
}
});
</script>
 
Anonymous
Not applicable

Re: Embed code on Forms 2.0

I didn't get an alert.