Auto populate form from Marketo cookie and auto submitting the form
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.