SOLVED

How to delete the Marketo tracking cookie right after it was set?

Go to solution
Anonymous
Not applicable

How to delete the Marketo tracking cookie right after it was set?

To meet German privacy laws (don’t ask for the details – we have such strange laws in Germany) I have do delete the Marketo tracking cookie right after it was set – on the same page. What I’m looking for is a JavaScript event to use as a trigger.

Here is the setup:
  1. On a thank you page, which is shown after a non-Marketo form is submitted, I check via JavaScript if the _mkto_trk exists and store this information in a variable (“formerlyKnownLead”).
  2. Then the Munchkin code (with “cookieAnon: false”) is executed, expanded by the associateLead part.
  3. Because of the associateLead part the tracking cookie is set.
  4. After the tracking cookie is set I have to check if the formerlyKnownLead variable is true or false. If it is false, I have to delete the tracking cookie instantly.
My problem:
Just inserting the “delete cookie” JS code after the
Munchkin.init and Munchkin.munchkinFunction calls does not work. Obviously the cookie is set shortly after the delete command is executed.

Possible solution:
I think an event listener would do the trick. So my question is: Is there any easy way to hook on the cookie handling of the Munchkin script? Or do I have to build a listener which checks for cookie changes every second or so (as described here)? To be honest, I have not yet worked much with event listeners in JS.
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: How to delete the Marketo tracking cookie right after it was set?

The Munchkin function calls (including init(), wihich calls visitWebPage() internally) do not fire callbacks when they are complete.  Since they involve doing a remote GET of a tracking pixel, naturally they won't be complete before your immediate call to deleteCookie(). (Not to mention that JS is single-threaded, so they can't even start to run until after your code block is done executing.)

If you can't even wait for the document to load before trashing the cookies (window.onBeforeUnload), then yes, you'd have to poll for cookie changes every second.  

You could actually do all the tracking within an IFRAME, which would allow the window to close itself (and thus run its own cleanup code on onBeforeUnload). However, if you're not an experienced JS dev this is probably too rough a road.

View solution in original post

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: How to delete the Marketo tracking cookie right after it was set?

The Munchkin function calls (including init(), wihich calls visitWebPage() internally) do not fire callbacks when they are complete.  Since they involve doing a remote GET of a tracking pixel, naturally they won't be complete before your immediate call to deleteCookie(). (Not to mention that JS is single-threaded, so they can't even start to run until after your code block is done executing.)

If you can't even wait for the document to load before trashing the cookies (window.onBeforeUnload), then yes, you'd have to poll for cookie changes every second.  

You could actually do all the tracking within an IFRAME, which would allow the window to close itself (and thus run its own cleanup code on onBeforeUnload). However, if you're not an experienced JS dev this is probably too rough a road.

View solution in original post

Anonymous
Not applicable

Re: How to delete the Marketo tracking cookie right after it was set?

To keep it simple I just ckeck for the cookie to be set every 0.1 second, delete the cookie as soon as it is set and then stop the search:
 
var refreshIntervalId = setInterval(function() {
  if ((formerlyKnownLead == false) && ($.cookie('_mkto_trk') != null)) {
    $.removeCookie('_mkto_trk', { path: '/' });
    clearInterval(refreshIntervalId);
  }
}, 100);

($.removeCookie works because I use the jQuery cookie plugin.)