Created
September 14, 2016 22:41
-
-
Save ccnixon/18976d8cc06e9ebe7706b6a46698ed97 to your computer and use it in GitHub Desktop.
Get and store a specific URL query param
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
function setCookie(name, value, days){ | |
var date = new Date(); | |
date.setTime(date.getTime() + (days*24*60*60*1000)); | |
var expires = "; expires=" + date.toGMTString(); | |
document.cookie = name + "=" + value + expires + ";path=/"; | |
} | |
function getParam(p){ | |
var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search); | |
return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); | |
} | |
var gclid = getParam('gclid'); | |
if(gclid){ | |
var gclsrc = getParam('gclsrc'); | |
if(!gclsrc || gclsrc.indexOf('aw') !== -1){ | |
setCookie('gclid', gclid, 90); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment