SOLVED

Re: Modify Munchkin code on landing pages?

Go to solution
Anonymous
Not applicable

Modify Munchkin code on landing pages?

I’m just setting up our own (responsive webdesign) template for landing pages. As far as I could see analyzing the standard templates, the Munchkin tracking code is rendered on a landing page as part of the $mContext['endElements'] output which is included via PHP.

Now my question is: Is there a way to modify or enhance the Munchkin code which is included via the $mContext['endElements'] output?

Or if this is not possible: Is there a way to get hold of the two parameters (“
customName” and “wsInfo”) which are in cluded in the Munchkin code call via the $mContext['endElements'] output? So I could use them in a custom Munchkin code call? Or are there more parameters which I couldn’t see in my test yet?


Here’s the reason why I want to do this:

As a German company we have to be compliant with German privacy laws. This means: Every web tracking system (which the Marketo Munchkin code is) we use on our site has to offer an Opt-out option, so anonymous visitors can choose to turn off being tracked.

Marketo doesn’t offer such a system by default. So I built one. In our new privacy policy (which isn’t online yet) I will include a “Marketo tracking opt-out” link. When an anonymous web lead clicks this link the Marketo tracking cookie “
_mkto_trk” for our domain is deleted and a new cookie “marketoTrackingOptOut” is set.

To prevent a new “_mkto_trk” to be set for an anonymous web lead which has opted out and has the “marketoTrackingOptOut” cookie stored in the browser, I have to modify the Munchkin call: The “cookieAnon: false” option has to be included if – and only if – the “marketoTrackingOptOut” cookie is present.

One way of doing this is a simple piece of JavaScript which replaces the original Munchkin code:
 
<script src='//munchkin.marketo.net/munchkin.js'></script>
<script type="text/javascript">
  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 myCookie = getCookie('marketoTrackingOptOut');
  if (myCookie == null) {
    Munchkin.init('###-###-###');
  } else {
    Munchkin.init('###-###-###', { cookieAnon: false });
  }
</script>

This works perfectly on our website. But to prevent new tracking cookies to be seton landing pages when visited by anonymous web leads who opted out, this if-then rule based on the existence of the “marketoTrackingOptOut” has to be incorporated in landing page templates, too. If I just use the above piece of JavaScript instead of the <?php echo $mContext['endElements']; ?> call I don’t get the “Program Type” and “Program Name” information as details of a known leads visit of this particular landing page.

I don’t know yet if I really need these two pieces of information in my reports. But it would be cool if there is an easy way to use my cookie-based enhanced Munchkin call on landing pages without having to omit the above mentioned additional information.
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Anonymous
Not applicable

Re: Modify Munchkin code on landing pages?

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, '&lt;', '<');
  myMarketoEndElementsOut = replaceAll(myMarketoEndElementsOut, '&gt;', '>');
  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 😉

View solution in original post

2 REPLIES 2
Josh_Hill13
Level 10 - Champion Alumni

Re: Modify Munchkin code on landing pages?

Michael,

Go to admin > munchkin and set Ignore Privacy to False.

But also look for Adam Waterston's slides on EU privacy and cookies from 2013 or 2014. He did an excellent job implementing a system to deal with this.

Also check out developer.marketo.com for details on Munchkin code.
Anonymous
Not applicable

Re: Modify Munchkin code on landing pages?

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, '&lt;', '<');
  myMarketoEndElementsOut = replaceAll(myMarketoEndElementsOut, '&gt;', '>');
  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 😉