// default params /* source utm_source utm_medium utm_campaign utm_content */ let defParams = {source: "1799473754", utm_source: "tcb", utm_medium: "-1", utm_campaign: "3-4-", utm_content: "0-22935902-1", plimit: "1", skip: "10"}; if(typeof utmObjectHref === 'undefined'){ let utmObjectHref = '.thumb-v a'; } // UTM params system utmObject(false, utmObjectHref); // start Fn function utmObject(typeReturn = false, elemId) { let addtoUrl = []; // registration the params const arr = ['utm_source','utm_medium','utm_campaign','utm_content', 'source', 'plimit', 'skip']; let obj = {}; // each arr.forEach(function(el) { if(getQueryParam(el)) { // if not empty add to the url addtoUrl.push(el+'='+getCookie(el)); } }); if(typeof addtoUrl !== 'undefined' && addtoUrl.length > '0') { // each let returnstr = ''; if(typeReturn){ returnstr = addtoUrl.join('&'); } else { jQuery(elemId).each(function () { // this url without params var urlhref = jQuery(this).attr('href').split('?')[0]; console.log(urlhref); // add or update new params jQuery(this).attr('href', urlhref + '?' + addtoUrl.join('&') ); }); } if(typeReturn){ return returnstr; } } } // get params of url function queryString(name){ // Further parsing: let searchParams = new URLSearchParams(window.location.search); let q = searchParams.get(name); if(q){ return q; }else{ return defParams[name]; } } // save in the cookie if not empty function getQueryParam(name) { if(queryString(name)) { setCookie(name, queryString(name), "/", 525600); return queryString(name); }else if(getCookie(name)){ return getCookie(name); } return false; } function setCookie(cookieName, cookieValue, cookieMinutes) { var date = new Date() let domain = location.host.split('.').slice(-2).join('.'); date.setTime(date.getTime() + (cookieMinutes * 60 * 1000)) var cookie = [ cookieName + '=' + cookieValue, 'domain=.' + domain, 'expires=' + date.toUTCString(), 'path=/' ] document.cookie = cookie.join(';'); return undefined; } function getCookie(cookieName){ var cookieParam = cookieName + "=" var cookieSplit = document.cookie.split(';') for (var i = 0; i < cookieSplit.length; i++) { var cookiePart = cookieSplit[i]; while (cookiePart.charAt(0) == ' ') { cookiePart = cookiePart.substring(1) } if (cookiePart.indexOf(cookieParam) == 0) { return cookiePart.substring(cookieParam.length, cookiePart.length) } } return undefined; }