|
;(function(window,undefined){ |
|
/* |
|
See LICENSE.txt file included in this distribution. |
|
@version 0.2.0 |
|
*/ |
|
(function(k){function h(){g.debug&&k.console&&k.console.debug&&k.console.debug.apply(console,["%c[UniversalUtmz]","color:#ccc;border:1px solid #eee"].concat([].slice.call(arguments)))}function r(c,b){var a;if(!c||!b)return!c==!b;for(a in c)if(c.hasOwnProperty(a)&&(!b.hasOwnProperty(a)||c[a]!=b[a]))return!1;for(a in b)if(b.hasOwnProperty(a)&&(!c.hasOwnProperty(a)||c[a]!=b[a]))return!1;return!0}function t(){var c,b={},a,d=/(?:^|[^&]*&)([^=&]+)(?:=([^&]*))?/g;for(c=(c||k.location.search.substr(1)).split("+").join(" ");a= |
|
d.exec(c);)b[decodeURIComponent(a[1])]=a[2]&&decodeURIComponent(a[2]);if(b.gclid)return{medium:"cpc",source:"google",campaign:"{adwords}",keyword:"{adwords}",content:"{adwords}",gclid:b.gclid};if(b.gclsrc)return{medium:"cpc",source:"google",campaign:"{DoubleClick}",keyword:"{DoubleClick}",content:"{DoubleClick}",gclsrc:b.gclsrc};if(b.utm_source)return h("Using data from UTM parameters."),m(b,"url");if(b.utm_referrer)return h("Parsing traffic data from parameter: utm_referrer."),n(b.utm_referrer); |
|
if(k.document.referrer)return h("Parsing traffic data from referrer: "+k.document.referrer),n(k.document.referrer)}function n(c){var b,a,d,f,e;if(c){a=c.match(/^(?:https?:\/\/)?([^/?]+)(\/[^?]*)?/);if(!a)throw Error("Referrer not a URL: "+c);b=a[1];d=0<c.indexOf("?")?"\x26":"?";/^https?:\/\/(www\.)?google(\.com?)?(\.[a-z]{2}t?)?\/?$/i.test(c)?c+=d+"q\x3d(not provided)":/^https?:\/\/(r\.)?search\.yahoo\.com?(\.jp)?\/?[^?]*$/i.test(c)?c+=d+"p\x3d(not provided)":/^https?:\/\/(www\.)?bing\.com\/?$/i.test(c)&& |
|
(c+=d+"q\x3d(not provided)");f="daum:q eniro:search_word naver:query pchome:q images.google:q google:q yahoo:p yahoo:q msn:q bing:q aol:query aol:q lycos:q lycos:query ask:q cnn:query virgilio:qs baidu:wd baidu:word alice:qs yandex:text najdi:q seznam:q rakuten:qt biglobe:q goo.ne:MT search.smt.docomo:MT onet:qt onet:q kvasir:q terra:query rambler:query conduit:q babylon:q search-results:q avg:q comcast:q incredimail:q startsiden:q go.mail.ru:q centrum.cz:q 360.cn:q sogou:query tut.by:query globo:q ukr:q so.com:q haosou.com:q auone:q".split(" "); |
|
for(e=0;e<f.length;e++)if(d=f[e].split(":"),b.match(new RegExp("(?:^|\\.)"+d[0]+"(?:$|\\.)"))&&(a=new RegExp("^(?:http(s)?://)?(?:[^?/]*\\.)?"+d[0]+"(?:\\.[^?/]*)?(?:/[^?]*)?\\?(?:.*\x26)?"+d[1]+"\x3d([^\x26]*)"),a=c.match(a)))return{medium:"organic",source:d[0],keyword:a[2],campaign:"(organic)"};if(a=/^(.+\.)((?:facebook|twitter|linkedin|plus\.google|pinterest|instagram)\.com)$/.exec(b))return{medium:"social",source:a[2]};if(b==k.location.hostname)return h("Ignoring self-referral.");if(g.referralExclusionList){a= |
|
"";for(d=b.split(".");2<d.length;)a="("+a+d.shift()+".)?";a=new RegExp("(^|\\s|,)"+a+d.join(".")+"($|\\s|,)");if(g.referralExclusionList.match(a))return h("Referral is from domain in referral exclusion list.")}return{medium:"referral",source:b,campaign:"(referral)"}}}function p(){function c(){var d;b="";a=!0;var c=g.cookieName,e=[];d=document.cookie.split(";");for(var c=new RegExp("^\\s*"+c+"\x3d\\s*(.*?)\\s*$"),l=0;l<d.length;l++){var k=d[l].match(c);k&&e.push(k[1])}if(d=e.length?e[0]:""){h("Cookie found: ", |
|
d);g.emulateUtmzCookie&&(d=d.split(".").slice(4).join("."));e={};c=d.split("|");for(l=0;l<c.length;l++)d=c[l].indexOf("\x3d"),e[decodeURIComponent(c[l].substr(0,d))]=decodeURIComponent(c[l].substr(d+1));b=m(e,g.emulateUtmzCookie?"cookie":"name")}else h("No cookie found")}var b,a;this.set=function(d){if(!d||"object"!=typeof d)throw Error("Storage.set(): must be passed an object.");a||c();d=m(d);if(b&&r(b,d))return h("Session data unchanged."),!1;b=d;var f;d=m(b,"name",g.emulateUtmzCookie?"cookie": |
|
"name");var e=[];for(f in d)d[f]!=g.emptyValue&&e.push(encodeURIComponent(f)+"\x3d"+encodeURIComponent(d[f]));f=e.join("|");g.emulateUtmzCookie&&(f="000000000.000000000.0.0."+f);h("Saving cookie:",f);u(g.cookieName,f,null,null,180);return a=!0};this.get=function(d){a||c();return arguments.length?b&&b[d]:b};this.clear=function(){document.cookie=g.cookieName+"\x3d; path\x3d/; expires\x3dThu, 01 Jan 1970 00:00:01 GMT;";h('Deleted cookie "'+g.cookieName+'".')}}function v(){var c=null;this.set=function(b){c= |
|
m(b)};this.get=function(b){null===c&&(c=m(t(),"name"));return arguments.length?c&&c[b]:c}}var g,q={debug:!1,emulateUtmzCookie:!1,cookieName:"_uutmz",referralExclusionList:"",emptyValue:"(not set)"},u=function(){function c(b){if(!b)return b;var a=b.indexOf(";");-1!=a&&(h("WARNING: Truncating cookie value at first semicolon: "+b),b=b.substring(0,a));if(!(0<=k.navigator.userAgent.indexOf("Firefox")))return b;b=b.replace(/\n|\r/g," ");for(var a=0,d=b.length;a<d;++a){var c=b.charCodeAt(a)&255;if(10==c|| |
|
13==c)b=b.substring(0,a)+"?"+b.substring(a+1)}return b}return function(b,a,d,f,e){if(a=c(a))2E3<a.length&&(a=a.substring(0,2E3)),b=b+"\x3d"+a+"; path\x3d"+(d||"/")+"; ",e&&(b+="expires\x3d"+(new Date((new Date).getTime()+864E5*e)).toGMTString()+"; "),f&&(b+="domain\x3d"+f+";"),document.cookie=b}}(),m=function(){var c=[{url:"utm_source",cookie:"utmcsr",name:"source"},{url:"utm_campaign",cookie:"utmccn",name:"campaign"},{url:"utm_medium",cookie:"utmcmd",name:"medium"},{url:"utm_term",cookie:"utmctr", |
|
name:"keyword"},{url:"utm_content",cookie:"utmcct",name:"content"},{url:"gclid",cookie:"utmgclid",name:"gclid"},{url:"gclsrc",cookie:"utmgclsrc",name:"gclsrc"}];return function(b,a,d){var f={},e;if(b){d=d||"name";if(!a)a:{for(a=0;a<c.length;a++)for(e in c[a])if(b.hasOwnProperty(c[a][e])){a=e;break a}a=void 0}for(e=0;e<c.length;e++)a&&b.hasOwnProperty(c[e][a])?f[c[e][d]]=b[c[e][a]]:"undefined"!=typeof g.emptyValue&&(f[c[e][d]]=g.emptyValue);return f}}}();k.UniversalUtmz=function(c){function b(){d= |
|
new p;a=new v;g.debug&&d.get()&&h("Found saved traffic data: ",d.get());a.get()?(h("Detected incoming "+a.get("medium")+" traffic: ",a.get()),k=!1,d.set(a.get())?(e=!0,h("Saved traffic data overwritten.")):(e=!1,h("Saved traffic data unchanged."))):(h("Traffic is direct."),k=!0,a.set({medium:"(none)",source:"(direct)",campaign:"(direct)"}),d.get()||(h("No saved data exists; initialize as direct."),d.set(a.get())))}var a,d=null,f,e,k;g={};c=c||{};for(f in q)g[f]=f in c?c[f]:q[f];h("CONFIG:",g);this.get= |
|
function(c){var e;a||b();e=arguments.length?d.get(c):d.get();"undefined"==typeof e&&(e=g[c]);return e};this.deleteSaved=function(){(d||new p).clear()};this.isEntrance=function(){a||b();return e};this.isDirect=function(){a||b();return k}}})(window); |
|
}).call(this,window); |