Auto populate form from Marketo cookie and auto submitting the form | Community
Skip to main content
March 2, 2017
Question

Auto populate form from Marketo cookie and auto submitting the form

  • March 2, 2017
  • 1 reply
  • 6201 views

Hi,

we want a way to track a lead who clicks on a PDF link from a mail that is not sent out via Marketo. We also do not wish to Gate the PDF file (Require our clients to fill in a form) to access the file. After talking to support and scouring the Marketo fourms and support sites, I understood (I think) the only way to achieve this is to make a REST API call and try and get the lead information from the cookie file on their PC (People who will access our PDF's are known clients and not the general public) I am no expert coder, so I patched up this code from my research, suffice to say it is not working and any help will be appreciated.

<script src="//xxx.marketo.com/js/forms2/js/forms2.min.js"></script>

<form id="mktoForm_2244" style="display:none"></form>

<script>MktoForms2.loadForm("//xxx.marketo.com", "xxx-xxx-xxx", 2244);</script>

<script>

MktoForms2.whenReady(function(form) {

  //OnSuccess is optional - only if you need to make client-side decisions about Thank You URL

  form.onSuccess(function(vals, tyURL) {

    location.href = '1234.com/rs/xxx-xxx-123/images/somepdffile.pdf'; 

    return false;

  });

     //Get LEAD info from cookie

     var mktoGet = new XMLHttpRequest();

     mktoGet.open("GET", "xxx-xxx-xxx.mktorest.com/rest/v1/leads.json?filterType=cookie&filterValues=<cookie>&fields=email,firstName,lastName&access_token=<token>", false);

     mktoGet.send();

    //set the first result as local variable

    var mktoLeadFields = mktoLead.result[0];

    //map your results from REST call to the corresponding field name on the form

    var prefillFields = {

            "Email" : mktoLeadFields.email,

            "FirstName" : mktoLeadFields.firstName,

            "LastName" : mktoLeadFields.lastName

            };

    //pass our prefillFields objects into the form.vals method to fill our fields

    form.vals(prefillFields);

    });

  //Submit the form

  form.submit();

});

</script>

p.s. I replaced the <cookie> and <token> values and once I paste the link in the browser I get a sucess result.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

SanfordWhiteman
Level 10
March 2, 2017

Don't do this, period.

For the love of all that is secure, never call the REST API from the browser. Do you really want the whole world to able to read your entire database?

But also: don't use the REST API in response to individual end-user actions, even if you gateway the REST API call via your own server. This is a Denial of Service vulnerability for the record books.

March 2, 2017

So bottom line, theres simply no way to achieve this?

SanfordWhiteman
Level 10
March 2, 2017

So bottom line, theres simply no way to achieve this?

I didn't say that.

But it's critical that no one looks at your first take and tries to do it that way.

There's something not entirely clear from your description: if these people already have an associated cookie, then why not use the Known Lead HTML feature ("If Known Lead, show Custom HTML")?  That's specifically designed to let already-associated sessions bypass the form and get to the download link. When they click the download link, a Filled Out Form activity is logged. There's no reason to populate a form with data that's already known on the server.