SOLVED

Re: Associate API

Go to solution
Demosvc
Level 1

Associate API

I am looking for suggestion if anyone has implemented the associated API call for non-Marketo form. What can be the workaround to fetch the cookie value from the website when user fill-out non-Marketo form so that I can call lead creation API as well as associated API back-to-back. The Munchkin code is already deployed across website pages.

Tags (1)
3 ACCEPTED SOLUTIONS

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Associate API

You do not need multiple API calls for a single form post. One call to the Submit Form endpoint will cover it.

 

BUT I would strongly caution against using the REST API here, as you are always creating a DoS vulnerability by using up valuable API calls in response to individual end user actions.

 

What is the exact reason (please be detailed) that you aren't using the Forms 2.0 JS API, i.e. the unlimited client-side API, to submit a hidden Marketo form instead?

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Associate API

It's a browser cookie accessible from JS. You read it like any other cookie, e.g. from document.cookie:

 

const cookieSnapshot = new URLSearchParams( document.cookie.replace(/%|&/g,encodeURIComponent).replace(/;\s*/g,"&") );
const munchkinCookie = cookieSnapshot.get("_mkto_trk");

 

 

Not sure what else you're asking about. Also, as noted above, why aren't you using the far more scalable hidden form method?

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Associate API


The reason behind not using hidden form method due to it requires more conditional based approached to send back data to downstream system more than one. However is there any such script which can really help to identify if Field value is XYZ then send lead data after form submit to different Marketo instances.   

Sure, you can load a form from any instance. No reason to “pre-load” one particular form.

 

Just catch the native form submit, then check conditions, like so:

htmlForm.addEventListener("submit",function(e){
  e.preventDefault();

  let mktoInstanceURL, mktoMunchkinId, mktoFormId;
  
  switch( htmlForm.someField ) {
    case "some value":
      mktoInstanceURL = "https://pages.instance1.example";
      mktoMunchkinId = "AAA-123-BBB";
      mktoFormId = 111; 
      break;
    case "some other value":
      mktoInstanceURL = "https://pages.instance2.example";
      mktoMunchkinId = "CCC-456-DDD";
      mktoFormId = 222;
      break;
  }

  MktoForms2.loadForm(mktoInstanceURL, mktoMunchkinId, mktoFormId);
  MktoForms2.whenReady(function(readyForm){
    readyForm.setValues({ /* whatever you need to set */ });
    readyForm.submit();
  });
});

 

View solution in original post

5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: Associate API

You do not need multiple API calls for a single form post. One call to the Submit Form endpoint will cover it.

 

BUT I would strongly caution against using the REST API here, as you are always creating a DoS vulnerability by using up valuable API calls in response to individual end user actions.

 

What is the exact reason (please be detailed) that you aren't using the Forms 2.0 JS API, i.e. the unlimited client-side API, to submit a hidden Marketo form instead?

Demosvc
Level 1

Re: Associate API

Hi @SanfordWhiteman 

Calling  Submit Form will also serve the purpose, but actual ask is how to retrieve the cookie value which can be mapped with visitor data when calling the submit form API. is there any workaround to read the cookie which is set by Munchkin code so that It can be passed correctly in the Submit Form API.

SanfordWhiteman
Level 10 - Community Moderator

Re: Associate API

It's a browser cookie accessible from JS. You read it like any other cookie, e.g. from document.cookie:

 

const cookieSnapshot = new URLSearchParams( document.cookie.replace(/%|&/g,encodeURIComponent).replace(/;\s*/g,"&") );
const munchkinCookie = cookieSnapshot.get("_mkto_trk");

 

 

Not sure what else you're asking about. Also, as noted above, why aren't you using the far more scalable hidden form method?

Demosvc
Level 1

Re: Associate API

Thank you @SanfordWhiteman !

The reason behind not using hidden form method due to it requires more conditional based approached to send back data to downstream system more than one. However is there any such script which can really help to identify if Field value is XYZ then send lead data after form submit to different Marketo instances.   

SanfordWhiteman
Level 10 - Community Moderator

Re: Associate API


The reason behind not using hidden form method due to it requires more conditional based approached to send back data to downstream system more than one. However is there any such script which can really help to identify if Field value is XYZ then send lead data after form submit to different Marketo instances.   

Sure, you can load a form from any instance. No reason to “pre-load” one particular form.

 

Just catch the native form submit, then check conditions, like so:

htmlForm.addEventListener("submit",function(e){
  e.preventDefault();

  let mktoInstanceURL, mktoMunchkinId, mktoFormId;
  
  switch( htmlForm.someField ) {
    case "some value":
      mktoInstanceURL = "https://pages.instance1.example";
      mktoMunchkinId = "AAA-123-BBB";
      mktoFormId = 111; 
      break;
    case "some other value":
      mktoInstanceURL = "https://pages.instance2.example";
      mktoMunchkinId = "CCC-456-DDD";
      mktoFormId = 222;
      break;
  }

  MktoForms2.loadForm(mktoInstanceURL, mktoMunchkinId, mktoFormId);
  MktoForms2.whenReady(function(readyForm){
    readyForm.setValues({ /* whatever you need to set */ });
    readyForm.submit();
  });
});