Skip to content

Instantly share code, notes, and snippets.

@advance512
Created May 27, 2018 16:23
Show Gist options
  • Save advance512/0218ac0b0c2beb8c64d24f385e99955e to your computer and use it in GitHub Desktop.
Save advance512/0218ac0b0c2beb8c64d24f385e99955e to your computer and use it in GitHub Desktop.
Store the first-touch UTM parameters to cookie of main domain
var SelectomCookieToUTM = function() {
/* Cookie based on https://developers.livechatinc.com/blog/setting-cookies-to-subdomains-in-javascript/ */
var Cookie=function(){var a={set:function(b,d,e){var f,g,h,j,k;e?(h=new Date,h.setTime(h.getTime()+1e3*(60*(60*(24*e)))),j="; expires="+h.toGMTString()):j="",k=location.host,1===k.split(".").length?document.cookie=b+"="+d+j+"; path=/":(g=k.split("."),g.shift(),f="."+g.join("."),document.cookie=b+"="+d+j+"; path=/; domain="+f,(null==a.get(b)||a.get(b)!=d)&&(f="."+k,document.cookie=b+"="+d+j+"; path=/; domain="+f))},get:function(b){for(var g,d=b+"=",e=document.cookie.split(";"),f=0;f<e.length;f++){for(g=e[f];" "==g.charAt(0);)g=g.substring(1,g.length);if(0==g.indexOf(d))return g.substring(d.length,g.length)}return null},erase:function(b){a.set(b,"",-1)}};return a}();
var s = {
Cookie: Cookie,
getParameterByName: function(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
},
setToCookie: function (utm_param, utm_val) {
if (utm_val != "" && utm_val && s.Cookie.get('_selectom_utm_' + utm_param) == null) {
s.Cookie.set('_selectom_utm_'+utm_param, utm_val, 30);
}
},
execute: function() {
s.setToCookie('source', getParameterByName('utm_source'));
s.setToCookie('medium', getParameterByName('utm_medium'));
s.setToCookie('campaign', getParameterByName('utm_campaign'));
s.setToCookie('content', getParameterByName('utm_content'));
s.setToCookie('term', getParameterByName('utm_term'));
}
};
return s;
}();
SelectomCookieToUTM.execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment