Skip to content

Instantly share code, notes, and snippets.

@pjrvs
Last active November 7, 2017 09:33
Show Gist options
  • Save pjrvs/e29a63acf22f1fb1a3be to your computer and use it in GitHub Desktop.
Save pjrvs/e29a63acf22f1fb1a3be to your computer and use it in GitHub Desktop.
a jquery cookie that shows a signup form for people that aren't signed up and an alternate message for people that came from the list or signed up.
<!DOCTYPE html>
<html>
<head>
<title>jQuery Cookie + MailChimp</title>
<meta charset="utf-8">
</head>
<body>
<form action="XXX" method="post" class="signup">
<input type="text" placeholder="Your Name..." name="FNAME" id="XXX">
<input type="email" placeholder="Your Email..." name="EMAIL" id="XXX">
<button type="submit" class="set">GO</button>
</form>
<div class="subbed">You've already signed up, <a href="#">check this out</a> instead.</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
$(".set").on("click", function () {
// where a user subscribes from the form on your site
$.cookie('subscriberCookie', 'subbed', { expires: 99999 });
});
if (location.href.match(/utm\_source\=mailchimp/)){
// where you use url.com?utm_source=mailchimp from within your campaigns
$.cookie('subscriberCookie', 'subbed', { expires: 99999 });
var newSearch = location.search.replace(/utm_.*?(&|$)/g,""), newSearch = newSearch.length > 1 ? newSearch : "";
newSearch != location.search && window.history.replaceState({},'', location.href.replace(location.search, newSearch));
}
if ($.cookie('subscriberCookie')) {
$('.signup').hide();
$(".subbed").show();
} else {
$('.signup').show();
$('.subbed').hide();
}
</script>
</body>
</html>
@pjrvs
Copy link
Author

pjrvs commented May 10, 2014

I've tested this and it seems to work, just want to confirm with a few other programmers before tell other folks that it's good to go.

it also strips out the UTM so if a subscriber shares the URL, it doesn't set a cookie for new folks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment