Javascript

Update expiry date of Mailchimp Pop-up

Mailchimp provides pop-up signup forms. Pop-up signup forms helps to connect the visitor of the website. Please click here to read the tutorial related to mailchimp pop-up signup form.

Mailchimp pop-up uses cookies to limit the number of times each site visitor sees your pop-up form. After the first time a visitor sees the form, it won’t display to them again for another year unless they clear their cookies or open your site in another browser. There is no any settings in the mailchimp to update/change the default expire time of cookie. As a owner of the site, we might want visitor to see the pop-up more frequently rather than once in a year. To update the mailchimp cookie, we shall write the script that overwrites the expiry time set by the mailchimp.

  1. For the first time, when user click the close button of mailchimp and browser reloads, set the expiry time in the localStorage and update the mailchimp expiry date.
  2. We can’t get the expiration date of a cookie through javascript. So, storing the expiry time in the local storge helps to compare the expiry time with current time.
  3. Next time when user visits the website, local storage expiry date is compared against the current date. If the current date is greater than expiry time, there will be no mailchimp cookie and localStorage is deleted. In my script, I have set the cookie expiry date to 24 hours.
    <script>
        var dt = new Date();
        //  dt.setHours( dt.getHours() + 2 );
        function getCookie(name) {
            // Split cookie string and get all individual name=value pairs in an array
            var cookieArr = document.cookie.split(";");
            // Loop through the array elements
            for(var i = 0; i < cookieArr.length; i++) {
            var cookiePair = cookieArr[i].split("=");
                /* Removing whitespace at the beginning of the cookie name
                and compare it with the given string */
                if(name == cookiePair[0].trim()) {
                // Decode the cookie value and return
                return decodeURIComponent(cookiePair[1]);
                }
            }
            return null;
        }

        function updateMcCookie(currentTime) {
            var expiryTime = currentTime + 24 * 60 * 60 * 1000; //ExpiryTime for 24 hours from current time in miliseconds
            dt.setTime(expiryTime);
            localStorage.setItem('MCPopupClosed', expiryTime)
            document.cookie = 'MCPopupClosed=yes;path=/;expires=' + dt;
        }

        function mcPopupCookie() {
            var mcCookie = getCookie("MCPopupClosed");
            // Get cookie using our custom function
            var dt = new Date();
            var currentTime = dt.getTime(); //Current Time in miliseconds
            var popupExpiryDate = localStorage.getItem('MCPopupClosed');
           // console.log(popupExpiryDate);
            if((popupExpiryDate!=null) &&  popupExpiryDate < currentTime) {
                localStorage.removeItem('MCPopupClosed');
                document.cookie = 'MCPopupClosed=yes;path=/;expires=Thu, 01 Jan 1970 00:00:00 UTC;';
            } else {

                if (mcCookie) {
                    if (popupExpiryDate && popupExpiryDate > currentTime) {
                    } else {
                        updateMcCookie(currentTime);
                    }
                } else {
                    //Remove the localStorage MCPopupClosed if there is no MCPopupClosed
                    localStorage.removeItem('MCPopupClosed');
                }
            }
        }
        mcPopupCookie();
    </script>

 

 

Views: 498

Standard