I want to offer a coupon code via a modal to visitors who have been the site before, but the last visit was more than 180 days ago.
Hashing out how to do this, I'm not sure if I should create a date variable to pass to the cookie or use timestamp. I'm generally new to javascript/jquery, so I'm not sure exactly how to code it, but I imagine it working something like this:
Check for cookie
-If cookie does not exist, set it with timestamp/date.
-If cookie does exist, check timestamp/date.
-If timestamp/date is less than or equal to 180 days ago, reset timestamp/date with current time.
-If timestamp/date is more than or equal to 181 days ago, show modal
and reset timestamp/date with current timestamp/date.
This is what I have so far:
// Check if the cookie exists.
if (jQuery.cookie('lastVisit') == null) {
var date = new Date();
// If the cookie doesn't exist, save the cookie with the value of 1
jQuery.cookie('lastVisit', 'date');
} else {
// If the cookie exists, take the value
var lastVisit_date = jQuery.cookie('lastVisit');
// If last visit is in the last 180 days
if (lastVisit_date <= 180) {
// Reset cookie with new date
jQuery.cookie('lastVisit', 'date');
} else {
// If last visit is past 180 days, show modal and reset cookie with new date
jQuery('#myModal').reveal();
jQuery.cookie('lastVisit', 'date');
}
}
I'm not quite sure how to code this, any suggestions or suggested resources?