I'm trying to store cookies for four different parameters in a URL. I have it working where it'll detect if the cookie is present, if it isn't, it'll add it. I also have it set detect if they come in from another URL string with those parameters but they're different–it'll overwrite the old cookie with the new cookie information, and if the parameter is blank, it'll return null
.
The thing I'm having trouble implementing is that I ONLY want it replacing the cookies if the URL string contains a different set of parameters where one at least one of them isn't null. If all four parameters are null, but there are cookies stored, I don't want it replacing the cookie.
Here's the script I have:
function queryParam(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var source = queryParam('utm_source');
var campaign = queryParam('utm_campaign');
var medium = queryParam('utm_medium');
var content = queryParam('utm_content');
function setCookie(utm, utmvar, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = utm + "=" + utmvar + ";" + expires + ";path=/";
}
function getCookie(utm) {
var name = utm + "=";
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 "";
}
function checkCookie() {
var utmSource = getCookie("utm_source");
if (utmSource != "") {
if (utmSource = source) {
utmSource = utmSource;
}
else {
setCookie("utm_source", utmSource, 7);
}
}
else {
utmSource = source;
if (utmSource != "" && utmSource != null) {
setCookie("utm_source", utmSource, 7);
}
}
var utmCampaign = getCookie("utm_campaign");
if (utmCampaign != "" ) {
if (utmCampaign = campaign) {
utmCampaign = utmCampaign;
}
else {
setCookie("utm_campaign", utmCampaign, 7);
}
}
else {
utmCampaign = campaign;
if (utmCampaign != "" && utmCampaign != null) {
setCookie("utm_campaign", utmCampaign, 7);
}
}
var utmMedium = getCookie("utm_medium");
if (utmMedium != "" ) {
if (utmMedium = medium) {
utmMedium = utmMedium;
}
else {
setCookie("utm_medium", utmMedium, 7);
}
}
else {
utmMedium = medium;
if (utmMedium != "" && utmMedium != null) {
setCookie("utm_medium", utmMedium, 7);
}
}
var utmContent = getCookie("utm_content");
if (utmContent != "") {
if (utmContent = content) {
utmContent = utmContent;
}
else {
setCookie("utm_content", utmContent, 7);
}
}
else {
utmContent = content;
if (utmContent != "" && utmContent != null) {
setCookie("utm_content", utmContent, 7);
}
}
var sbaValue = utmCampaign + ";" + utmSource + ";" + utmMedium + ";" + utmContent;
$("#00N0B000005VTnf").attr("value", sbaValue);
}
So an example of how I want to act would be - if the URL string is:
domain.com/?utm_source=b&utm_medium=a&utm_campaign=c&utm_content=d
=> sbaValue = a;b;c;d
Then they come in at:
domain.com/?utm_medium=a&utm_campaign=c&utm_content=d
=> sbaValue = a;null;c;d
Then they come in at:
domain.com/
=> sbaValue = a;null;c;d
(nothing changes from before)
I tried setting a variable when something isn't null, but no matter how I implemented it, I couldn't get it to work.
I ended up rewriting a lot of it and cleaned it up A LOT. Here's the final solution:
function queryParam(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function setCookie(utm, utmvar, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = utm + "=" + utmvar + ";" + expires + ";path=/";
}
function getCookie(utm) {
var name = utm + "=";
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 "";
}
function getQueryStringVariables(){
return {
source: queryParam('utm_source'),
campaign: queryParam('utm_campaign'),
medium: queryParam('utm_medium'),
content: queryParam('utm_content'),
}
}
function isQueryStringSet(){
var queryStringVariables = getQueryStringVariables();
if (queryStringVariables.source ||
queryStringVariables.campaign ||
queryStringVariables.medium ||
queryStringVariables.content) {
return true;
}
return false;
}
function fetchCookieData() {
return {
source: getCookie("utm_source"),
campaign: getCookie("utm_campaign"),
medium: getCookie("utm_medium"),
content: getCookie("utm_content"),
};
};
function updateCookie() {
var queryStringVariables = getQueryStringVariables();
if (isQueryStringSet()) {
setCookie("utm_source", queryStringVariables.source, 7);
setCookie("utm_campaign", queryStringVariables.campaign, 7);
setCookie("utm_medium", queryStringVariables.medium, 7);
setCookie("utm_content", queryStringVariables.content, 7);
}
}
function updateFormValue() {
var cookieData = fetchCookieData();
var sbaValue = cookieData.campaign + ";" + cookieData.source + ";" + cookieData.medium + ";" + cookieData.content;
$("#00N0B000005VTnf").attr("value", sbaValue);
}
updateCookie();
updateFormValue();