Skip to content

Instantly share code, notes, and snippets.

@ccnixon
Created September 14, 2016 22:41
Show Gist options
  • Save ccnixon/18976d8cc06e9ebe7706b6a46698ed97 to your computer and use it in GitHub Desktop.
Save ccnixon/18976d8cc06e9ebe7706b6a46698ed97 to your computer and use it in GitHub Desktop.
Get and store a specific URL query param
<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