Skip to content

Instantly share code, notes, and snippets.

@ieatfood
Created July 19, 2017 20:42
Show Gist options
  • Save ieatfood/7b63c4ee24a54a52e301944d7feb0d05 to your computer and use it in GitHub Desktop.
Save ieatfood/7b63c4ee24a54a52e301944d7feb0d05 to your computer and use it in GitHub Desktop.
Set Theatre Manager's `tmsource` cookie based on `tmsource` or `utm_source` query parameters.
(setTimeout(function () {
var TM_PARAM_NAME = 'tmsource';
var UTM_PARAMS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];
var COOKIE_NAME = 'tmsource';
var COOKIE_EXPIRES_DAYS = 60;
var LOCATION = window.location;
var URL = LOCATION.href;
var COOKIE_DOMAIN = LOCATION.hostname.replace(/^www\./, '');
function getParameterByName(name) {
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(value) {
var date = new Date();
date.setTime(date.getTime() + (COOKIE_EXPIRES_DAYS * 24 * 60 * 60 * 1000));
document.cookie =
COOKIE_NAME + "=" + encodeURIComponent(value) + ";" +
"expires=" + date.toUTCString() + ";" +
"domain=" + COOKIE_DOMAIN + ";";
}
var utmParams = [];
UTM_PARAMS.forEach(param => {
var param = getParameterByName(param);
if (param) {
utmParams.push(param);
}
});
var cookieValue = utmParams.join('-');
if (!cookieValue) {
cookieValue = getParameterByName(TM_PARAM_NAME);
}
if (!cookieValue) {
return;
}
setCookie(cookieValue);
}, 300))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment