Caputuring _mtko_trk cookie and passing to hidden fieid
Hi,
I have a custom form builder function that renders a marketo form using json data from marketo.
To render out the correct form the i pass the 4 digit form if to the form builder plugin which calls the correct json data.
As part of this development I have been advised by marketo to add in the munchkin tracker js file and a hidden field to each form for the munchkin cookie value.
<input type="hidden" name="_mkt_trk" id="_mkt_trk">
In a standard form the munchkin tracker script passes the _mkto_trk cookie value to the hidden _mkt_trk form field.
My form builder function renders the form once the page has rendered and the munchkin track script triggered, meaning that the hidden form field will never be populated.
In order to solve this I have referenced this readCookie function, i then call the readCookie function inside my form builder function, which then sets the _mkt_trk hidden field value
http://developers.marketo.com/blog/get-a-visitors-cookie-id-and-then-query-associated-lead-data/
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;
}
readCookie('_mkto_trk')
A working demo can be found here:JSFiddle
If _mkto_trk is in the cookie exists in document.cookie, the readCookie function will grab the value.
Based on my form implementation is this a suitable approach to grab the _mkto_trk cookie value and populate the form hidden field?