forked from HubSpot/BuckyClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucky.min.js
2 lines (2 loc) · 8.77 KB
/
bucky.min.js
1
2
/*! bucky 0.2.8 */
(function(){var a,b,c,d,e,f=[].slice;e=function(){var a,b;return null!=(a=null!=(b=window.performance)&&"function"==typeof b.now?b.now():void 0)?a:+new Date},c=+new Date,b=function(){var a,b,c,d,e,g,h;for(a=arguments[0],d=2<=arguments.length?f.call(arguments,1):[],g=0,h=d.length;h>g;g++){c=d[g];for(b in c)e=c[b],a[b]=e}return a},d=function(){var a,b;return a=1<=arguments.length?f.call(arguments,0):[],null!=("undefined"!=typeof console&&null!==console&&null!=(b=console.log)?b.call:void 0)?console.log.apply(console,a):void 0},d.error=function(){var a,b;return a=1<=arguments.length?f.call(arguments,0):[],null!=("undefined"!=typeof console&&null!==console&&null!=(b=console.error)?b.call:void 0)?console.error.apply(console,a):void 0},a=function(){var a,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G;if(l={host:"/bucky",maxInterval:3e4,aggregationInterval:5e3,decimalPrecision:3,sendLatency:!1,sample:1,active:!0},z={},a="function"==typeof document.querySelector?document.querySelector("[data-bucky-host],[data-bucky-page],[data-bucky-requests]"):void 0)for(z={host:a.getAttribute("data-bucky-host"),pagePerformanceKey:a.getAttribute("data-bucky-page"),requestsKey:a.getAttribute("data-bucky-requests")},E=["pagePerformanceKey","requestsKey"],C=0,D=E.length;D>C;C++)o=E[C],"true"===(null!=(F=z[o])?F.toString().toLowerCase():void 0)||""===z[o]?z[o]=!0:"false"===(null!=(G=z[o])?G.toString().toLowerCase():void 0)&&(z[o]=null);return t=b({},l,z),i={timer:"ms",gauge:"g",counter:"c"},g=t.active,(A=function(){return g=t.active&&Math.random()<t.sample})(),h=[],y=function(a){return b(t,a),("sample"in a||"active"in a)&&A(),t},v=function(a,b){return null==b&&(b=t.decimalPrecision),Math.round(a*Math.pow(10,b))/Math.pow(10,b)},u={},m=function(a,b,c){var d,e;if(g)return d=1,a in u&&("counter"===c?b+=u[a].value:(d=null!=(e=u[a].count)?e:d,d++,b=u[a].value+(b-u[a].value)/d)),u[a]={value:b,type:c,count:d},k()},x=null,s=null,n=function(){return clearTimeout(x),clearTimeout(s),s=null,x=null,w()},k=function(){return clearTimeout(x),x=setTimeout(n,t.aggregationInterval),null==s?s=setTimeout(n,t.maxInterval):void 0},r=function(a){var b,c,d,f,g,h,i,j,k,l;c=window.XMLHttpRequest&&(window.XMLHttpRequest.defake||"withCredentials"in new window.XMLHttpRequest),d=/^(https?:\/\/[^\/]+)/i.exec(t.host),d?(g=d[1],i=g===""+document.location.protocol+"//"+document.location.host?!0:!1):i=!0,j=e(),b="";for(f in a)k=a[f],b+=""+f+":"+k+"\n";return h=i||c||null==("undefined"!=typeof window&&null!==window?window.XDomainRequest:void 0)?new(null!=(l="undefined"!=typeof window&&null!==window?window.XMLHttpRequest:void 0)?l:XMLHttpRequest):new window.XDomainRequest,h.bucky={track:!1},h.open("POST",""+t.host+"/v1/send",!0),h.setRequestHeader("Content-Type","text/plain"),h.addEventListener("load",function(){return B(e()-j)},!1),h.send(b),h},w=function(){var a,b,c,e;if(!g)return void d("Would send bucky queue");a={};for(o in u)b=u[o],h.push({path:o,count:b.count,type:b.type,value:b.value}),null!=i[b.type]?(c=b.value,("gauge"===(e=b.type)||"timer"===e)&&(c=v(c)),a[o]=""+c+"|"+i[b.type],1!==b.count&&(a[o]+="@"+v(1/b.count,5))):d.error("Type "+b.type+" not understood by Bucky");return r(a),u={}},p=!1,B=function(a){return t.sendLatency&&!p?(m("bucky.latency",a,"timer"),p=!0,setTimeout(function(){return p=!1},3e5)):void 0},q=function(a){var b,i,j,k,l,p,r,s,u,v;null==a&&(a=""),b=function(b){return(null!=a?a.length:void 0)?a+"."+b:b},p=function(a,c,e){return null==e&&(e="gauge"),null==c||null==a?void d.error("Can't log "+a+":"+c):m(b(a),c,e)},u={TIMES:{},send:function(a,b){return p(a,b,"timer")},time:function(){var a,b,c,d,e;return e=arguments[0],a=arguments[1],c=arguments[2],b=4<=arguments.length?f.call(arguments,3):[],u.start(e),d=function(){return u.stop(e)},b.splice(0,0,d),a.apply(c,b)},timeSync:function(){var a,b,c,d,e;return d=arguments[0],a=arguments[1],c=arguments[2],b=4<=arguments.length?f.call(arguments,3):[],u.start(d),e=a.apply(c,b),u.stop(d),e},wrap:function(a,b){return null!=b?function(){var c;return c=1<=arguments.length?f.call(arguments,0):[],u.timeSync.apply(u,[a,b,this].concat(f.call(c)))}:function(b){return function(){var c;return c=1<=arguments.length?f.call(arguments,0):[],u.timeSync.apply(u,[a,b,this].concat(f.call(c)))}}},start:function(a){return u.TIMES[a]=e()},stop:function(a){var b;return null==u.TIMES[a]?void d.error("Timer "+a+" ended without having been started"):(b=e()-u.TIMES[a],u.TIMES[a]=void 0,u.send(a,b))},stopwatch:function(a,b){var c,d;return null!=b?d=function(){return+new Date}:(d=e,b=d()),c=b,{mark:function(c,e){var f;return null==e&&(e=0),f=d(),a&&(c=a+"."+c),u.send(c,f-b+e)},split:function(b,e){var f;return null==e&&(e=0),f=d(),a&&(b=a+"."+b),u.send(b,f-c+e),c=f}}},mark:function(a,b){var c;return null==b&&(b=+new Date),c=u.navigationStart(),u.send(a,b-c)},navigationStart:function(){var a,b,d;return null!=(a="undefined"!=typeof window&&null!==window&&null!=(b=window.performance)&&null!=(d=b.timing)?d.navigationStart:void 0)?a:c},responseEnd:function(){var a,b,d;return null!=(a="undefined"!=typeof window&&null!==window&&null!=(b=window.performance)&&null!=(d=b.timing)?d.responseEnd:void 0)?a:c},now:function(){return e()}},i=function(a,b){return null==b&&(b=1),p(a,b,"counter")},s=!1,r=function(a){var b,c,d,e,f,g=this;if(null==("undefined"!=typeof window&&null!==window&&null!=(d=window.performance)?d.timing:void 0))return!1;if(s)return!1;if(a&&a!==!0||(a=l.urlToKey(document.location.toString())+".page"),"uninitialized"===(e=document.readyState)||"loading"===e)return"function"==typeof window.addEventListener&&window.addEventListener("load",function(){return setTimeout(function(){return r.call(g,a)},500)},!1),!1;s=!0,b=window.performance.timing.navigationStart,f=window.performance.timing;for(o in f)c=f[o],"number"==typeof c&&u.send(""+a+"."+o,c-b);return!0},l={transforms:{mapping:{guid:/\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,sha1:/\/[0-9a-f]{40}/gi,md5:/\/[0-9a-f]{32}/gi,id:/\/[0-9;_\-]+/g,email:/\/[^\/]+@[^\/]+/g,domain:[/\/[^\/]+\.[a-z]{2,3}\//gi,"/"]},enabled:["guid","sha1","md5","id","email","domain"],enable:function(a,b,c){return null==c&&(c=""),null!=b&&(this.mapping[a]=[b,c]),this.enabled.splice(0,0,a)},disable:function(a){var b,c,d;d=this.enabled;for(b in d)if(c=d[b],c===a)return void this.enabled.splice(b,1)}},sendReadyStateTimes:function(a,b){var c,d,e,f,g,h,i,j;if(null!=b){d={1:"sending",2:"headers",3:"waiting",4:"receiving"},e={},f=null;for(c in b)h=b[c],null!=f&&null!=d[c]&&(e[d[c]]=h-f),f=h;j=[];for(g in e)i=e[g],j.push(u.send(""+a+"."+g,i));return j}},urlToKey:function(a,b,c){var e,f,g,h,i,j,k,m,n,o;for(a=a.replace(/https?:\/\//i,""),h=/([^\/:]*)(?::\d+)?(\/[^\?#]*)?.*/i.exec(a),e=h[1],i=null!=(n=h[2])?n:"",o=l.transforms.enabled,k=0,m=o.length;m>k;k++)g=o[k],f=l.transforms.mapping[g],null!=f?"function"!=typeof f?(f instanceof RegExp&&(f=[f,""]),i=i.replace(f[0],f[1])):i=f(i,a,b,c):d.error("Bucky Error: Attempted to enable a mapping which is not defined: "+g);return i=decodeURIComponent(i),i=i.replace(/[^a-zA-Z0-9\-\.\/ ]+/g,"_"),j=e+i.replace(/[\/ ]/g,"."),j=j.replace(/(^\.)|(\.$)/g,""),j=j.replace(/\.com/,""),j=j.replace(/www\./,""),c&&(j=c+"."+j),b&&(j=j+"."+b.toLowerCase()),j=j.replace(/\.\./g,".")},getFullUrl:function(a,b){return null==b&&(b=document.location),/^\//.test(a)?b.hostname+a:/https?:\/\//i.test(a)?a:b.toString()+a},monitor:function(a){var b,c,f;return a&&a!==!0||(a=l.urlToKey(document.location.toString())+".requests"),c=this,b=function(b){var d,f,g,h,j,k,l,m;return l=b.type,m=b.url,f=b.event,h=b.request,g=b.readyStateTimes,j=b.startTime,null!=j?(d=e()-j,m=c.getFullUrl(m),k=c.urlToKey(m,l,a),p(k,d,"timer"),c.sendReadyStateTimes(k,g),null!=(null!=h?h.status:void 0)?(h.status>12e3?i(""+k+".0"):0!==h.status&&i(""+k+"."+h.status.toString().charAt(0)+"xx"),i(""+k+"."+h.status)):void 0):void 0},f=window.XMLHttpRequest,window.XMLHttpRequest=function(){var a,c,g,h,i,j;g=new f;try{h=null,c={},i=g.open,g.open=function(a,f,j){var k;try{c[0]=e(),g.addEventListener("readystatechange",function(){return c[g.readyState]=e()},!1),g.addEventListener("loadend",function(d){return null==g.bucky||g.bucky.track!==!1?b({type:a,url:f,event:d,startTime:h,readyStateTimes:c,request:g}):void 0},!1)}catch(l){k=l,d.error("Bucky error monitoring XHR open call",k)}return i.apply(g,arguments)},j=g.send,g.send=function(){return h=e(),j.apply(g,arguments)}}catch(k){a=k,d.error("Bucky error monitoring XHR",a)}return g}}},k=function(b){var c;return null==b&&(b=""),c=null!=a?a:"",c&&b&&(c+="."),b&&(c+=b),q(c)},j={send:p,count:i,timer:u,now:e,requests:l,sendPagePerformance:r,flush:n,setOptions:y,options:t,history:h,active:g};for(o in j)v=j[o],k[o]=v;return k},j=q(),t.pagePerformanceKey&&j.sendPagePerformance(t.pagePerformanceKey),t.requestsKey&&j.requests.monitor(t.requestsKey),j},"function"==typeof define&&define.amd?define(a):"object"==typeof exports?module.exports=a():window.Bucky=a()}).call(this);