Hi Josh,
in the admin section I only see settings for “do not track“ requests. While this is nice, it’s not sufficient for German regulations because older browser versions don’t support the “do not track” headers.
I really have to respond to a tracking opt-out cookie. With a few lines of PHP this would be easy. But custom PHP is not possible on Marketo landing pages.
So I developed a working JavaScript solution:
<noscript id="myEndElements">
<?php echo $mContext['endElements']; ?>
</noscript>
<script>
function escapeRegExp(string) { return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); }
function replaceAll(string, find, replace) { return string.replace(new RegExp(escapeRegExp(find), 'g'), replace); }
function getCookie(c){var b=document.cookie;var e=c+"=";var d=b.indexOf("; "+e);if(d==-1){d=b.indexOf(e);if(d!=0){return null}}else{d+=2;var a=document.cookie.indexOf(";",d);if(a==-1){a=b.length}}return unescape(b.substring(d+e.length,a))};
var myMarketoEndElementsIn = document.getElementById('myEndElements').innerHTML;
var myCookie = getCookie('marketoTrackingOptOut');
var myMarketoEndElementsOut = '';
if (myCookie == null) {
myMarketoEndElementsOut = myMarketoEndElementsIn;
} else {
myMarketoEndElementsOut = myMarketoEndElementsIn.replace("Munchkin.init('###-###-###', {", "Munchkin.init('###-###-###', {cookieAnon: false, ");
}
myMarketoEndElementsOut = replaceAll(myMarketoEndElementsOut, '<', '<');
myMarketoEndElementsOut = replaceAll(myMarketoEndElementsOut, '>', '>');
document.write(myMarketoEndElementsOut);
</script>
The script picks up the $mContext['endElements'] output which is inserted inside a <noscript> element – and thus not executed while JavaScript is enabled. Then it alters only the required part of the Munchkin call and inserts the altered
$mContext['endElements'] output into the DOM – so the Munchkin scripts are executed instantly.
As far as I could test it this solution works fine. The only minor drawback is that the “marketoTrackingOptOut” cookie is ignored if JavaScript is disabled. But I can live with that
😉