Rayyis
Rayyis

Reputation: 28

Auto fill Marketo form fields from lead info derrived from Marketo Cookie

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 = 'http://www.1234.com/rs/xxx-xxx-123/images/somepdffile.pdf'; 
    return false;
  });

     //Get LEAD info from cookie
     var mktoGet = new XMLHttpRequest();
     mktoGet.open("GET", "https://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 and values and once I paste the link in the browser I get a sucess result.

Upvotes: 0

Views: 592

Answers (2)

Rayyis
Rayyis

Reputation: 28

<script type="text/javascript">  
  document.write(unescape("%3Cscript src='//munchkin.marketo.net/munchkin-beta.js' type='text/javascript'%3E%3C/script%3E"));
</script> 
<script>  
  Munchkin.init('xxx-xxx-xxx'); 
</script> 
<script>
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(obj, start) {
         for (var i = (start || 0), j = this.length; i < j; i++) {
             if (this[i] === obj) { return i; }
         }
         return -1;
    }
}  
  (function(redirectTarget){
    var allowedOrigins = [
         'https://aaa.bbb.com',
         'https://aaa.com',
         'http://bbb.ccc.com',
         ], // which domains are allowed for redirection
        redirectMs = 3500, // how long before redirecting  
        progressMs = 500,  // how long between updates of the "progress meter"
        progressChar = '.', // progress character
        errNoAsset = 'URL not found.', // message when no asset in hash
        errInvalidAsset = 'URL not allowed.', // when asset not our domain
        progress = setInterval(function(){
          if (redirectTarget) {
            document.body.insertAdjacentHTML('beforeend',progressChar);
          } else {
            clearInterval(progress), clearTimeout(redirect);          
            document.body.insertAdjacentHTML('beforeend',errNoAsset);          
          }
        }, progressMs),
        redirect = setTimeout(function(){         
            var redirectLoc = document.createElement('a');
            redirectLoc.href = redirectTarget;
            redirectLoc.origin = redirectLoc.origin ||
                [redirectLoc.protocol,
                  '//',
                  redirectLoc.hostname, 
                  ['http:','http:80','https:','https:443']
                    .indexOf(redirectLoc.protocol+redirectLoc.port) != -1
                      ? ''
                      : ':' + redirectLoc.port
                ].join('');

            clearInterval(progress);
            if (allowedOrigins.indexOf(redirectLoc.origin) != -1) {            
              document.location.href = redirectTarget;
            } else {
              document.body.insertAdjacentHTML('beforeend',errInvalidAsset);                   
            }
        }, redirectMs);
    })(document.location.hash.substring(1));
</script>

Upvotes: 0

Yanir Calisar
Yanir Calisar

Reputation: 475

You won't be able to use the REST API this way since it requires you generating a new access token every time, and more problematic, exposing it in the client side. The only option I can think of would be to use a re-direction link to a marketo landing page. You'll create an empty landing page and insert the following code that will simple make the redirect to your pdf.

<script>location.href = 'http://www.1234.com/rs/xxx-xxx 123/images/somepdffile.pdf';</script>

This activity would be recorded in the activity log for known leads and

Upvotes: 0

Related Questions