Conditionally loading Munchkin on Marketo LPs (for GDPR & related policies)

I’ve written before about conditionally loading Munchkin based on IP ranges (which isn't a native feature), and about adding custom Munchkin options on Marketo LPs (where Munchkin is injected automatically with special options).

As Community user GF points out, anti-cookie/anti-tracking legislation like GDPR requires a combo of these earlier approaches:

  1. the end user must consent before Munchkin runs on Marketo LPs
  2. you need to preserve the LP-specific special options if they do consent
  3. you may need to add custom options, most often domainLevel

To get this working, we need to hack around Munchkin's hard-coded load-and-run logic on Marketo LPs. Luckily, it's not horrendously complex, though it'll look obscure to people inexperienced with such shenanigans.☺

Here are the scripts to put as high up in your page as possible (in <head> ideally, but near the top of <body> is fine too, like you get with an HTML element on a Free-Form LP):

<script type="application/javascript" 
  src="//munchkin.marketo.net/munchkin-beta.js">
</script>
<script>
(function munchkinInitDeferred(customOptions){
  var nativeMunchkinInit = Munchkin.init;
  Munchkin.init = function cacheInitOptions(id, options){
    if (customOptions) { 
      Object.keys(customOptions)
      .forEach(function(key){
        options[key] = customOptions[key];
      });
    }
    Munchkin.init = nativeMunchkinInit.bind(Munchkin, id, options);
    console.log("Munchkin: Cached options", options);
  };
})( /* custom init options here if necessary */ );
</script>

Notice I'm not passing any args to the function in the basic usage example above, there's just a comment:

...( /* custom init options here if necessary */ )`

If you need custom Munchkin initialization options (most commonly domainLevel) then pass those in an object:

...( {domainLevel: 2, someOtherOption: true} )

What does it do?

The code works by caching the Munchkin config for later — including both Marketo's special options and your custom options — while stopping the page from immediately running Munchkin.init().

You can see the cached config in the browser console:

ss

This gives you complete control. Once someone opts in to being tracked, then run Munchkin.init() manually (call it with no arguments, since all the arguments, including the 123-ABC-456 Munchkin ID, are already stored).

Enjoy staying legal!