Skip to content

Instantly share code, notes, and snippets.

@vosters
Last active January 3, 2018 03:05
Show Gist options
  • Save vosters/4c918e659e527640ced690e148350e18 to your computer and use it in GitHub Desktop.
Save vosters/4c918e659e527640ced690e148350e18 to your computer and use it in GitHub Desktop.
Show/hide html based on URL parameter using js-cookie
{% comment %}
USES JS-COOKIE
https://github.com/js-cookie/js-cookie/blob/master/README.md
{% endcomment %}
// Make sure 'js-cookie.js' is uploaded to your Shopify Theme's assets folder
{{ 'js-cookie.js' | asset_url | script_tag }}
{% comment %}
Adjust display based on URL parameters
{% endcomment %}
<script>
// Specify URL parameter here // Specify name of cookie here
if (window.location.search.indexOf('utm_campiagn=test') > -1 || document.cookie.indexOf("parameter-cookie") >= 0) {
// Html class you want to hide if user has certain URL parameter
$(".parameter-hide").hide();
// Html class you want to show if user has certain URL parameter
$(".parameter-show").show();
// Specify same cookie name as above here, set the value to '1', and have it expire in 7 days
Cookies.set('parameter-cookie', '1', { expires: 7 });
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment