MailChimp “Evil” Popup/Over Embed Code Doesn’t Set Cookies Properly

This post will probably only interest you if you use MailChimp to send enewsletters.

A few days ago I signed up for a MailChimp account so that I can publish enewsletters for my political humor website, PoliticLOLz. One of the features that they offer is an “evil popup mode.” Basically, the first time you visit a website, a box pops up that encourages you to sign up for the email newsletter. When you close the box, a cookie is set that tells the website that you’ve already visited and seen the box. This way, you won’t be bothered for a year or until you delete your cookies.

However, there is a slight problem with the code. If you’re having trouble with the box popping up more than once, you should change the following line in the code that MailChimp gives you.

Original:
document.cookie = 'MCEvilPopupClosed=yes;expires=' + expires_date.toGMTString();

Fixed:
document.cookie = 'MCEvilPopupClosed=yes;expires=' + expires_date.toGMTString() + ';path=/';

That should fix your problem. You don’t really need to know any more. However, if you’re curious as to how this works, read on.

Basically, when a cookie is set, it is given a particular path that defines the scope of the cookie, (i.e. which pages the cookie affects) and the path defaults to the path of the page being viewed. For example, if you visit http://yoursite.com/games/new, any cookies that are set will have a default path of /games/new, meaning that they will only be used on that page and its subpages (i.e. /games/new/top).

Of course, you can give a cookie a different path when you set it. Thus, by adding “ + ';path=/'” to the code, I am setting the cookie’s path to the site’s root, meaning that it will be used for all pages on the site, not just the page that was visited and any subpages it may have.

I hope this helps you out. Let me know if you run into any issues.