Instantly share code, notes, and snippets.

Embed
What would you like to do?
JS code to generate, set and read 1st party cookies on the highest available domain level
/*
(c) Copyright Screen6 BV - The Netherlands
All rights reserved
It is illegal to modify, disassemble, sell, copy or publish this software
or any part thereof. The use of this software is only permitted with the
prior and express written permission of Screen6 BV, the Netherlands.
for more information please contact: info@screen6.io
*/
(function(doc) {
"use strict";
var cookieName = 'uid';
var cookieExpireDelay = 2419200000; // 28 days in ms
var getTopDomain = function(hostname) {
var nodot = hostname.match(/\.([a-zA-Z]*?)$/);
var dotted = hostname.match(/\.(((com|gov|mil|net|org).ac)|((ac|co|gov|mil|name|net|org|pro|sch).ae)|((com|edu|gov|net|org).af)|((com|edu|gov|mil|net|org).al)|((co|ed|gv|it|og|pb).ao)|((com|edu|gob|gov|int|mil|net|org|tur).ar)|((ac|co|gv|or).at)|((asn|com|csiro|edu|gov|id|net|org).au)|((co|com|edu|gov|mil|net|org|rs|unbi|unmo|unsa|untz|unze).ba)|((biz|co|com|edu|gov|info|net|org|store|tv).bb)|((biz|cc|com|edu|gov|info|net|org).bh)|((com|edu|gov|net|org).bn)|((com|edu|gob|gov|int|mil|net|org|tv).bo)|((adm|adv|agr|am|arq|art|ato|b|bio|blog|bmd|cim|cng|cnt|com|coop|ecn|edu|eng|esp|etc|eti|far|flog|fm|fnd|fot|fst|g12|ggf|gov|imb|ind|inf|jor|jus|lel|mat|med|mil|mus|net|nom|not|ntr|odo|org|ppg|pro|psc|psi|qsl|rec|slg|srv|tmp|trd|tur|tv|vet|vlog|wiki|zlg).br)|((com|edu|gov|net|org).bs)|((du|et|om|ov|rg).bz)|((ab|bc|mb|nb|nf|nl|ns|nt|nu|on|pe|qc|sk|yk).ca)|((biz|co|edu|gen|gov|info|net|org).ck)|((ac|ah|bj|com|cq|edu|fj|gd|gov|gs|gx|gz|ha|hb|he|hi|hl|hn|jl|js|jx|ln|mil|net|nm|nx|org|qh|sc|sd|sh|sn|sx|tj|tw|xj|xz|yn|zj).cn)|((com|edu|gov|mil|net|nom|org).co)|((ac|c|co|ed|fi|go|or|sa).cr)|((ac|biz|com|gov|ltd|name|net|org|press|pro|tm).cy)|((art|com|edu|gob|gov|mil|net|org|sld|web).do)|((art|asso|com|edu|gov|net|org|pol).dz)|((com|edu|fin|gov|info|med|mil|net|org|pro).ec)|((com|edu|eun|gov|mil|name|net|org|sci).eg)|((com|edu|gov|ind|mil|net|org|w).er)|((com|edu|gob|nom|org).es)|((biz|com|edu|gov|info|name|net|org).et)|((ac|biz|com|info|mil|name|net|org|pro).fj)|((ac|co|gov|net|nom|org).fk)|((asso|com|f|gouv|nom|prd|presse|tm).fr)|((co|net|org).gg)|((com|edu|gov|mil|org).gh)|((ac|com|gov|net|org).gn)|((com|edu|gov|mil|net|org).gr)|((com|edu|gob|ind|mil|net|org).gt)|((com|edu|gov|net|org).gu)|((com|edu|gov|idv|net|org).hk)|((ac|co|go|mil|net|or|sch|web).id)|((ac|co|gov|idf|k12|muni|net|org).il)|((ac|co|edu|ernet|firm|gen|gov|i|ind|mil|net|nic|org|res).in)|((com|edu|gov|i|mil|net|org).iq)|((ac|co|dnssec|gov|i|id|net|org|sch).ir)|((edu|gov).it)|((co|net|org).je)|((com|edu|gov|mil|name|net|org|sch).jo)|((ac|ad|co|ed|go|gr|lg|ne|or).jp)|((ac|co|go|info|me|mobi|ne|or|sc).ke)|((com|edu|gov|mil|net|org|per).kh)|((biz|com|de|edu|gov|info|mob|net|org|tel).ki)|((asso|com|coop|edu|gouv|k|mil|nom|presse|tm).km)|((edu|gov|net|org).kn)|((ac|busan|co|daegu|es|go|hs|jeju|k|kg|mil|ms|ne|or|pe|re|sc|seoul|ulsan).kr)|((com|edu|gov|net|org).kw)|((com|edu|gov|net|org).ky)|((com|edu|gov|mil|net|org).kz)|((com|edu|gov|net|org).lb)|((assn|com|edu|gov|grp|hotel|int|ltd|net|ngo|org|sch|soc|web).lk)|((com|edu|gov|net|org).lr)|((asn|com|conf|edu|gov|id|mil|net|org).lv)|((com|edu|gov|id|med|net|org|plc|sch).ly)|((ac|co|gov|m|net|org|press).ma)|((asso|tm).mc)|((ac|co|edu|gov|its|net|org|priv).me)|((com|edu|gov|mil|nom|org|prd|tm).mg)|((com|edu|gov|inf|name|net|org|pro).mk)|((com|edu|gov|net|org|presse).ml)|((edu|gov|org).mn)|((com|edu|gov|net|org).mo)|((com|edu|gov|net|org).mt)|((aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro).mv)|((ac|co|com|coop|edu|gov|int|museum|net|org).mw)|((com|edu|gob|net|org).mx)|((com|edu|gov|mil|name|net|org|sch).my)|((arts|com|firm|info|net|other|per|rec|store|web).nf)|((biz|com|edu|gov|mil|mobi|name|net|org|sch).ng)|((ac|co|com|edu|gob|mil|net|nom|org).ni)|((com|edu|gov|mil|net|org).np)|((biz|com|edu|gov|info|net|org).nr)|((ac|biz|co|com|edu|gov|med|mil|museum|net|org|pro|sch).om)|((com|edu|gob|mil|net|nom|org|sld).pe)|((com|edu|gov|i|mil|net|ngo|org).ph)|((biz|com|edu|fam|gob|gok|gon|gop|gos|gov|net|org|web).pk)|((art|biz|com|edu|gda|gdansk|gorzow|gov|info|krakow|lodz|lublin|mil|net|ngo|org|poznan|pwr|radom|slupsk|torun|waw|wroc|zgora).pl)|((ac|biz|com|edu|est|gov|info|isla|name|net|org|pro|prof).pr)|((com|edu|gov|net|org|plo|sec).ps)|((belau|co|ed|go|ne|or).pw)|((arts|com|firm|info|nom|nt|org|rec|store|tm|www).ro)|((ac|co|edu|gov|in|org).rs)|((com|edu|gov|net|org).sb)|((com|edu|gov|net|org).sc)|((co|com|edu|gov|net|nom|org).sh)|((com|edu|gov|net|org).sl)|((co|com|edu|gov|mil|net|org|store).st)|((com|edu|gob|org|red).sv)|((ac|co|org).sz)|((av|bbs|bel|biz|com|dr|edu|gen|gov|info|k12|name|net|org|pol|tel|tsk|tv|web).tr)|((aero|biz|cat|co|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel).tt)|((club|com|ebiz|edu|game|gov|idv|mil|net|org).tw)|((ac|co|com|gov|net|or|org).mu)|((ac|co|edu|gov|org).mz)|((co|com).na)|((ac|co|cri|geek|gen|govt|health|iwi|maori|mil|net|org|school).nz)|((abo|ac|com|edu|gob|ing|med|net|nom|org|sld).pa)|((com|edu|gov|int|net|nome|org|publ).pt)|((com|edu|gov|mil|net|org).py)|((com|edu|gov|mil|net|org).qa)|((asso|com|nom).re)|((ac|altai|amur|bir|cbg|chel|chita|com|e-burg|edu|gov|grozny|int|jar|kaluga|kazan|kchr|khv|kirov|koenig|komi|kuban|kurgan|kursk|mari|marine|mil|mosreg|msk|net|nnov|nov|nsk|omsk|org|oryol|penza|perm|pp|pskov|ptz|rnd|ryazan|samara|spb|stv|surgut|tambov|tom|tomsk|tsk|tula|tuva|tver|tyumen|udm|vrn|vyatka|yamal).ru)|((ac|co|com|edu|gouv|gov|int|mil|net).rw)|((com|edu|gov|med|net|org|pub|sch).sa)|((com|edu|gov|info|med|net|org|tv).sd)|((a|ac|b|bd|c|d|e|f|g|h|i|k|l|m|n|o|org|p|parti|pp|press|r|s|t|tm|u|w|x|y|z).se)|((com|edu|gov|idn|net|org|per).sg)|((art|com|edu|gouv|org|perso|univ).sn)|((com|edu|gov|mil|net|news|org).sy)|((ac|co|go|in|mi|net|or).th)|((ac|biz|co|com|edu|go|gov|info|int|mil|name|net|nic|org|test|web).tj)|((agrinet|com|edunet|ens|fin|gov|ind|info|intl|mincom|nat|net|org|perso|rnrt|rns|rnu).tn)|((ac|co|go|ne|or).tz)|((biz|ck|cn|co|com|crimea|cv|dn|dp|edu|gov|if|in|kh|kiev|km|kr|ks|kv|lg|lutsk|lviv|me|mk|net|od|odessa|org|pl|pp|rovno|rv|sumy|te|vn|zp|zt).ua)|((ac|co|go|ne|or|org|sc).ug)|((ac|bl|co|cym|gov|govt|icnet|jet|lea|ltd|me|mil|mod|nel|net|nhs|nic|nls|org|orgn|plc|police|sch|scot|soc).uk)|((dni|fed|isa|kids|nsn).us)|((com|edu|gub|mil|net|org).uy)|((co|com|edu|gob|info|mil|net|org|web).ve)|((co|com|k12|net|org).vi)|((ac|biz|com|edu|gov|health|info|int|name|net|org|pro).vn)|((co|com|gov|ltd|me|net|org|plc).ye)|((ac|co|edu|gov|org).yu)|((ac|agric|alt|bourse|city|co|db|edu|gov|imt|inca|law|mil|net|ngo|nis|nom|org|pix|school|tm|web).za)|((ac|co|com|edu|gov|net|org|sch).zm))$/i);
var tld = (dotted)?dotted[1]:((nodot)?nodot[1]:'');
var notld = hostname.substr(0, hostname.indexOf(tld) - 1);
return notld.substr(Math.max(0, notld.lastIndexOf('.') + 1)) + '.' + tld;
};
var getCookie = function(cookie, cName) {
if (cookie.length > 0) {
var cStart = cookie.indexOf( cName + "=");
if (cStart != -1) {
cStart = cStart + cName.length + 1;
var cEnd = cookie.indexOf(";", cStart);
if (cEnd == -1) {
cEnd = cookie.length;
}
return unescape(cookie.substring(cStart, cEnd));
}
}
return false;
};
var setCookie = function(cName, cValue, options) {
options = options || {};
return cName + "=" + cValue + ";domain=" + (options.domain || '.') + "; path=" + (options.path || '/') + " ; expires=" + (options.expires || new Date()).toGMTString() + ";";
};
var createUUID = function() {
return Date.now().toString(36).substring(3) + Math.random().toString(36).substring(2, 15);
};
// only execute the following code when in the browser
if (doc) {
var topdomain = getTopDomain(location.hostname);
var uid = getCookie(doc.cookie, cookieName) || createUUID(topdomain);
doc.cookie = setCookie(cookieName, uid, {
domain: topdomain,
expires: new Date(Date.now() + cookieExpireDelay)
});
// pixel call the tracking server, passing on the 1st party cookie uid, the domain of this uid and a cache-buster
(new Image()).src = location.protocol + '//yourtrackingdomain.com/?uid=' + uid + '&ns=' + topdomain + '&cb=' + Math.random().toString(36).substring(2, 15);
}
})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment