Using the Get Multiple Leads by Filter Type REST endpoint, you can query lead data based on a user’s cookie id. For example, you can use this approach to prefill a form on a non-Marketo landing page. This post will show you how to capture the user’s cookie value during a webpage visit, query Get Multiple Leads REST API with that cookie id, and then return the user’s lead data.
First, we will need the value of the user’s Munchkin cookie, ‘_mkto_trk’. Here is an example JavaScript function that you can use to get the cookie value. Please see this StackOverflow page for more information about this approach. I recommend setting a delay of 500ms after page load event before calling this function. This will give Munchkin time to load, and cookie the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Function to read value of a cookie function readCookie(name) { var cookiename = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length); } return null; } //Call readCookie function to get value of user's Marketo cookie var value = readCookie('_mkto_trk'); |
Next, pass the value of the ‘_mkto_trk’ cookie to your server. To retrieve lead data, from your server you will make a call to the Get Multiple Leads REST API with this cookie value. You’ll need your Authentication and REST endpoints from your instance. Your call should be structured as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#NOTE: The _mkto_trk cookie value includes an ampersand and needs to be URL encoded to '%26' in order to be properly accepted by the Marketo endpoint. require 'rest_client' require 'json' #Build request URL #Replace AAA-BBB-CCC with your Marketo instance marketo_instance = "https://AAA-BBB-CCC.mktorest.com" endpoint = "/rest/v1/leads.json" #Replace with your access token auth_token = "?access_token=" + "cde42eff-aca0-48cf-a1ac-576ffec65a84:ab" #Replace with filter type and values filter_type_and_values = "&filterType=cookies&filterValues=id:AAA-BBB-CCC%26token:_mch-marketo.com-1418418733122-51548&fields=cookies,email" request_url = marketo_instance + endpoint + auth_token + filter_type_and_values #Make request response = RestClient.get request_url #Returns Marketo API response puts response |
The example above will return the email and all cookies associated with the user. You can then use this data to personalize the subsequent page the user visits.
1 |
{"requestId":"aa00#14a405aa786","result":[{"id":583,"email":"testaccount@gmail.com","cookies":"_mch-marketo.com-1418418733122-51548"}],"success":true} |