Safely customizing Munchkin options on Marketo LPs [Updated 2016-10-02]

Whether you're using a .ly or .io domain for coolness's sake, or if you legitimately operate out of a ccTLD like .fr or .mx, you need to ensure the Munchkin option domainLevel is set to 2 or you're guaranteed to lose tracking.

I explored the problem in this recent post and shared the JS function findPrivateSuffix, which can be used on any page (Marketo-hosted or external) to detect the most appropriate cookie domain. Using the function is much better than hard-coding the value, because when you use domain aliases, the correct value will be different depending on how someone goes to the page. (If the same page is available as http://pages.mybrand.com and http://pages.mybrand.co.uk, there is more than one correct value.)

It's easy to plug in findPrivateSuffix on an external website, where you control where/when Munchkin.init is called:

Munchkin.init( 'AAA-BBB-CCC', { 
  domainLevel : findPrivateSuffix({cache:true}) 
});

But what about on a Marketo-hosted LP? There, by default, Munchkin is loaded for you, including special LP-specific options:

ss

But the auto-inserted <script> tag doesn't include the special options you might need for your domain.

On the flipside, if you disable automatic Munchkin insertion on the LP, you can call Munchkin yourself with the domainLevel you want — but this way won't include the right values for customName and wsInfo.

In order to get your own options and Marketo's options at the same time, use a JS closure. Leave Munchkin enabled in the LP setup and add this custom HEAD HTML:

<script type="text/javascript" src="//pages.example/com/rs/AAA-BBB-CCC/findPrivateSuffix-0.3.7.min.js"></script>
<script type="text/javascript" src="//munchkin.marketo.net//munchkin-beta.js"></script>
<script>
(function(munchkinLib){
  var nativeMunchkinInit = munchkinLib.init;
  munchkinLib.init = function(id,options){
    options['domainLevel'] = findPrivateSuffix({cache:true}).domainLevel;
    nativeMunchkinInit.apply(munchkinLib,arguments); 
  };
})(Munchkin);
</script>

This preloads the Munchkin bootstrapper and wraps Munchkin.init with a custom domainLevel (and any other options you need). It doesn't run init, though: we let the auto-injected script run it as usual, and it will combine their options like customName with our options before initializing. Cool? Cool.