MktoMunchkin :: Use Beacon w/external Munch

by sanford

HTML

<!-- Munchkin Beaconize v3 -->
<!-- This script is not for publication / use at your own risk -->
<script type="text/javascript">
document.write(unescape("%3Cscript src='//munchkin.marketo.net/munchkin-beta.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
/* 
* Add beacon capability to Munchkin object
*/
Munchkin.beaconize = function(){
    // first Munchkin vWP XHR is async, so translate to beacon; otherwise, recall native XHR
    if ( navigator.sendBeacon ) {
        var nativeXHRopen = window.XMLHttpRequest.prototype.open;
        window.XMLHttpRequest.prototype.open = function(method,url,async) {    
            if ( async && /\.mktoresp.com\/webevents\/visitWebPage\?/i.test(url) ) 
            {
                url += '&_method=GET&_reqtype=beacon';
                var beacon = navigator.sendBeacon(url,'');
                this.abort();
            } 
            else
            {
                nativeXHRopen.apply(this,arguments);
            }
       }
        console.log('enabling beacon for critical requests');
    }
}

// Munchkin.beaconize();
Munchkin.init('410-XOR-673',{clickTime:1000});
</script>
<a href="http://www.google.com">Go to Google</a>