Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mpstenson/615fbf4b22807133cb26 to your computer and use it in GitHub Desktop.
Save mpstenson/615fbf4b22807133cb26 to your computer and use it in GitHub Desktop.
/*This script will handle a url input on a hubspot landing page such at http://hubpostlp.com/landing-page/?source=Newsletter10
It will then take that url variable store it in a 90 day cookie and then insert it into the marketing_source fields on the hubspot
forms. This script does account for multiple forms on the same page. */
function getQueryParams(qs) {
qs = qs.split('+').join(' ');
var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
return params;
}
function createCookie(name, value, days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
} else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
//
window.onload = function(){
var query = getQueryParams(document.location.search);
if (query.source !=undefined)
{
createCookie("source",query.source,90);
}
var elementIndex;
for(elementIndex in document.getElementsByName("marketing_source"))
{
var elem = document.getElementsByName("marketing_source")[elementIndex];
elem.value = getCookie("source");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment