Deferred Munchkin - not logging Visit Web Page | Community
Skip to main content
Thorsten
Level 3
September 25, 2020
Question

Deferred Munchkin - not logging Visit Web Page

  • September 25, 2020
  • 1 reply
  • 4464 views

Hi everyone,

 

I tried to implement Sanford's solution for deferring the Munchkin.Init() call for GDPR compliance on Marketo Landing Pages, as per https://blog.teknkl.com/conditionally-loading-munchkin-on-marketo-lps-for-gdpr-related-policies/.

 

My problem was initially that - directly after accepting the cookie - the "Visit Web Page" event was not captured against the targeted customer, but as anonymous.

 

It was stripping the mkt_tok and lost the identification of the customer. I understand it's working as designed for good reasons, but it’s hitting me on the deferred scenario.

 

I now tried to bring back the mkt_tok as per Sanford’s solution here: https://blog.teknkl.com/restoring-the-mighty-mkt_tok/.

 

So I am running

if (window.__mktTokVal && history.replaceState) { var restoredLoc = document.createElement("a"); restoredLoc.href = document.location.href; restoredLoc.search += (restoredLoc.search ? "&" : "") + "mkt_tok=" + window.__mktTokVal; history.replaceState({}, null, restoredLoc.href); } Munchkin.init();

right after the cookie has been accepted – et voila – the “Visit Web Page” activity is being recorded again as expected now.

 

But – now I got the mkt_tok in the URL again… So I tried to remove it by running

window.history.pushState({}, document.title, document.location.origin + document.location.pathname);

after the Munchkin.Init().

 

But – you probably guessed it by this point – we’re back to square one where the “Visit Web Page” doesn’t get recorded anymore… It’s probably too fast removing the URL before the Munchkin.Init() can complete its stuff?

 

Does anyone have any clever idea on how to make it work?

 

Example:

If you want to give it a try, here’s the link for my test customer to play around with. My scenario starts with the customer receiving an email with a link to a Marketo Landing Page (https://exploremore-qa.techdata.com/Q0PB08Q0Zi25x041H00w3o0).

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

SanfordWhiteman
Level 10
September 25, 2020
(function(){ function detectMunchkinLibLoadComplete(e){ if( e.target.tagName === "SCRIPT" && /\/\d+\/munchkin\.js/.test(e.target.src) ) { // remove the mkt_tok here document.removeEventListener("load", detectMunchkinLibLoadComplete, true); } } document.addEventListener("load", detectMunchkinLibLoadComplete, true); Munchkin.init(); })();
Thorsten
ThorstenAuthor
Level 3
September 28, 2020

Hi Sanford,

first of all, thanks for the rapid response. I am just not sure if I implemented your code correctly. Apparently the way I did didn't work. I tried it in a couple of different ways as well then, but neither created the Visit Web Page activity right after accepting the cookie, we're back to anonymous now.

 

Here's the latest and how I understand I should implement your code in the greater context:

if (window.__mktTokVal && history.replaceState) { var restoredLoc = document.createElement("a"); restoredLoc.href = document.location.href; restoredLoc.search += (restoredLoc.search ? "&" : "") + "mkt_tok=" + window.__mktTokVal; history.replaceState({}, null, restoredLoc.href); } (function () { function detectMunchkinLibLoadComplete(e) { if (e.target.tagName === "SCRIPT" && /\/\d+\/munchkin\.js/.test(e.target.src)) { // remove the mkt_tok here window.history.pushState({}, document.title, document.location.origin + document.location.pathname); document.removeEventListener("load", detectMunchkinLibLoadComplete, true); } } document.addEventListener("load", detectMunchkinLibLoadComplete, true); Munchkin.init(); })();

Isn't that initializing the Munchkin after the mkt_tok has been removed from the URL again? Thanks for your help!

 

SanfordWhiteman
Level 10
September 29, 2020

Maybe we've been overthinking this.

 

If you switch to {apiOnly: true} — you can do this with the deferred version, too, by passing your additional options — then you have control over the logged Visit Web Page:

Munchkin.init( "123-ABC-456", {apiOnly: true} ); var restoredLoc = document.createElement("a"); restoredLoc.href = document.location.href; restoredLoc.search += (restoredLoc.search ? "&" : "") + "mkt_tok=" + window.__mktTokVal; Munchkin.munchkinFunction("visitWebPage",{ url : restoredLoc.protocol + "//" + restoredLoc.hostname + restoredLoc.pathname + restoredLoc.hash, params : restoredLoc.search });

 

This will log the restoredLoc metadata regardless of what's currently in the URL.