This repository was archived by the owner on Jan 5, 2025. It is now read-only.
forked from HubSpot/BuckyClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucky.min.js
2 lines (2 loc) · 10.9 KB
/
bucky.min.js
1
2
/*! bucky 0.3.0 */
(function(){var a,b,c,d,e,f,g,h=[].slice;e="undefined"!=typeof module&&null!==module&&!("undefined"!=typeof window&&null!==window?window.module:void 0),e?(a=require("xmlhttprequest").XMLHttpRequest,g=function(){var a;return a=process.hrtime(),1e3*(a[0]+a[1]/1e9)}):g=function(){var a,b;return null!=(a=null!=(b=window.performance)&&"function"==typeof b.now?b.now():void 0)?a:+new Date},d=+new Date,c=function(){var a,b,c,d,e,f,g;for(a=arguments[0],d=2<=arguments.length?h.call(arguments,1):[],f=0,g=d.length;g>f;f++){c=d[f];for(b in c)e=c[b],a[b]=e}return a},f=function(){var a,b;return a=1<=arguments.length?h.call(arguments,0):[],null!=("undefined"!=typeof console&&null!==console&&null!=(b=console.log)?b.call:void 0)?console.log.apply(console,a):void 0},f.error=function(){var a,b;return a=1<=arguments.length?h.call(arguments,0):[],null!=("undefined"!=typeof console&&null!==console&&null!=(b=console.error)?b.call:void 0)?console.error.apply(console,a):void 0},b=function(){var b,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,H,I;if(n={host:"/bucky",maxInterval:3e4,aggregationInterval:5e3,decimalPrecision:3,sendLatency:!1,sample:1,active:!0,json:!1,influxLineProtocol:!1,queryString:null},B={},!e&&(b="function"==typeof document.querySelector?document.querySelector("[data-bucky-host],[data-bucky-page],[data-bucky-requests],[data-bucky-json],[data-bucky-influx-line-protocol],[data-bucky-query-string]"):void 0))for(B={host:b.getAttribute("data-bucky-host"),pagePerformanceKey:b.getAttribute("data-bucky-page"),requestsKey:b.getAttribute("data-bucky-requests"),json:b.getAttribute("data-bucky-json"),influxLineProtocol:b.getAttribute("data-bucky-influx-line-protocol"),queryString:b.getAttribute("data-bucky-query-string")},G=["pagePerformanceKey","requestsKey","json","influxLineProtocol","queryString"],E=0,F=G.length;F>E;E++)q=G[E],"true"===(null!=(H=B[q])?H.toString().toLowerCase():void 0)||""===B[q]?B[q]=!0:"false"===(null!=(I=B[q])?I.toString().toLowerCase:void 0)&&(B[q]=null);return v=c({},n,B),k={timer:"ms",gauge:"g",counter:"c"},i=v.active,(C=function(){return i=v.active&&Math.random()<v.sample})(),j=[],A=function(a){return c(v,a),("sample"in a||"active"in a)&&C(),v},x=function(a,b){return null==b&&(b=v.decimalPrecision),Math.round(a*Math.pow(10,b))/Math.pow(10,b)},w={},o=function(a,b,c){var d,e;if(i)return d=1,a in w&&("counter"===c?b+=w[a].value:(d=null!=(e=w[a].count)?e:d,d++,b=w[a].value+(b-w[a].value)/d)),w[a]={value:b,type:c,count:d},m()},z=null,u=null,p=function(){return clearTimeout(z),clearTimeout(u),u=null,z=null,y()},m=function(){return clearTimeout(z),z=setTimeout(p,v.aggregationInterval),null==u?u=setTimeout(p,v.maxInterval):void 0},t=function(b){var c,d,f,h,i,j,k,l,m,n,o;if(d=e||window.XMLHttpRequest&&(window.XMLHttpRequest.defake||"withCredentials"in new window.XMLHttpRequest),e?l=!0:(h=/^(https?:\/\/[^\/]+)/i.exec(v.host),h?(j=h[1],l=j===""+document.location.protocol+"//"+document.location.host?!0:!1):l=!0),m=g(),v.json===!0)c=JSON.stringify(b);else{c="";for(i in b)n=b[i],c+=""+i+":"+n+"\n"}return k=l||d||null==("undefined"!=typeof window&&null!==window?window.XDomainRequest:void 0)?new(null!=(o="undefined"!=typeof window&&null!==window?window.XMLHttpRequest:void 0)?o:a):new window.XDomainRequest,k._bucky&&(k._bucky.track=!1),f=""+v.host+"/v1/send",v.json===!0&&(f+="/json"),k.open("POST",f,!0),k.addEventListener?k.addEventListener("load",function(){return D(g()-m)},!1):k.attachEvent?k.attachEvent("onload",function(){return D(g()-m)}):k.onload=function(){return D(g()-m)},k.send(c),k},y=function(){var a,b,c,d;if(!i)return void f("Would send bucky queue");a={};for(q in w)b=w[q],j.push({path:q,count:b.count,type:b.type,value:b.value}),null!=k[b.type]?(c=b.value,("gauge"===(d=b.type)||"timer"===d)&&(c=x(c)),a[q]=""+c+"|"+k[b.type],1!==b.count&&(a[q]+="@"+x(1/b.count,5))):f.error("Type "+b.type+" not understood by Bucky");return t(a),w={}},r=!1,D=function(a){return v.sendLatency&&!r?(o("bucky.latency",a,"timer"),r=!0,setTimeout(function(){return r=!1},3e5)):void 0},s=function(a){var b,c,e,k,l,m,n,r,t,u,w;null==a&&(a=""),b=function(b){return(null!=a?a.length:void 0)?a+"."+b:b},n=function(a,c,d){return null==d&&(d="gauge"),null==c||null==a?void f.error("Can't log "+a+":"+c):o(b(a),c,d)},u={TIMES:{},send:function(a,b){return n(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?h.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?h.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?h.call(arguments,0):[],u.timeSync.apply(u,[a,b,this].concat(h.call(c)))}:function(b){return function(){var c;return c=1<=arguments.length?h.call(arguments,0):[],u.timeSync.apply(u,[a,b,this].concat(h.call(c)))}}},start:function(a){return u.TIMES[a]=g()},stop:function(a){var b;return null==u.TIMES[a]?void f.error("Timer "+a+" ended without having been started"):(b=g()-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=g,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,c;return null!=(a="undefined"!=typeof window&&null!==window&&null!=(b=window.performance)&&null!=(c=b.timing)?c.navigationStart:void 0)?a:d},responseEnd:function(){var a,b,c;return null!=(a="undefined"!=typeof window&&null!==window&&null!=(b=window.performance)&&null!=(c=b.timing)?c.responseEnd:void 0)?a:d},now:function(){return g()}},c=function(a,b){return null==b&&(b=1),n(a,b,"counter")},t=!1,r=function(a){var b,c,d,f,g,h,i=this;if(null==("undefined"!=typeof window&&null!==window&&null!=(f=window.performance)?f.timing:void 0))return!1;if(t)return!1;if(a&&a!==!0||(a=m.urlToKey(document.location.toString())+".page"),v.influxLineProtocol!==!0||b||(a+=",url="+e(document.location.toString())+",timing=",b=!0),"uninitialized"===(g=document.readyState)||"loading"===g)return window.addEventListener?window.addEventListener("load",function(){return setTimeout(function(){return r.call(i,a)},500)},!1):window.attachEvent?window.attachEvent("onload",function(){return setTimeout(function(){return r.call(i,a)},500)}):window.onload=function(){return setTimeout(function(){return r.call(i,a)},500)},!1;t=!0,c=window.performance.timing.navigationStart,h=window.performance.timing;for(q in h)d=h[q],"number"==typeof d&&(v.influxLineProtocol===!0?u.send(a+q,d-c):u.send(""+a+"."+q,d-c));return!0},m={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,f,g,h,i,j,k;if(null!=b){d={1:"sending",2:"headers",3:"waiting",4:"receiving"},f={},g=null;for(c in b)i=b[c],null!=g&&null!=d[c]&&(f[d[c]]=i-g),g=i;k=[];for(h in f)j=f[h],v.influxLineProtocol===!0?k.push(u.send(""+a+",status="+e(h),j)):k.push(u.send(""+a+"."+h,j));return k}},urlToKey:function(a,b,c){var d,e,g,h,i,j,k,l,n,o;for(a=a.replace(/https?:\/\//i,""),h=/([^/:]*)(?::\d+)?(\/[^\?#]*)?.*/i.exec(a),d=h[1],i=null!=(n=h[2])?n:"",o=m.transforms.enabled,k=0,l=o.length;l>k;k++)g=o[k],e=m.transforms.mapping[g],null!=e?"function"!=typeof e?(e instanceof RegExp&&(e=[e,""]),i=i.replace(e[0],e[1])):i=e(i,a,b,c):f.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=d+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,d,h,i;return a&&a!==!0||(a=m.urlToKey(document.location.toString())+".requests"),d=this,b=function(b,f){var h,i;if(null!=b._bucky.startTime)return h=g()-b._bucky.startTime,v.influxLineProtocol===!0?i=""+a+",url="+e(b._bucky.url)+",endpoint="+e(b._bucky.endpoint)+",method="+e(b._bucky.type):(b._bucky.url=d.getFullUrl(b._bucky.url),i=d.urlToKey(b._bucky.url,b._bucky.type,a)),n(i,h,"timer"),d.sendReadyStateTimes(i,b._bucky.readyStateTimes),null!=(null!=b?b.status:void 0)?(b.status>12e3?c(v.influxLineProtocol===!0?""+i+",status=0":""+i+".0"):0!==b.status&&c(v.influxLineProtocol===!0?""+i+",status="+b.status.toString().charAt(0)+"xx":""+i+"."+b.status.toString().charAt(0)+"xx"),c(v.influxLineProtocol===!0?""+i+",status="+b.status:""+i+"."+b.status)):void 0},h=function(){var a,c,d,e;c=new i;try{c._bucky={},c._bucky.startTime=null,c._bucky.readyStateTimes={},c._bucky.isDone=!1,c._bucky.track=!0,d=c.open,c.open=function(a,e,h){var i;try{c._bucky.type=a,c._bucky.readyStateTimes[0]=g(),c._bucky.endpoint=e,c._bucky.url=document.location.toString(),c.addEventListener?c.addEventListener("readystatechange",function(a){return c._bucky.track!==!1?(c._bucky.readyStateTimes[c.readyState]=g(),4===c.readyState&&c._bucky.isDone!==!0?(c._bucky.isDone=!0,b(c,a)):void 0):void 0},!1):c.attachEvent?c.attachEvent("onreadystatechange",function(a){return c._bucky.track!==!1?(c._bucky.readyStateTimes[c.readyState]=g(),4===c.readyState&&c._bucky.isDone!==!0?(c._bucky.isDone=!0,b(c,a)):void 0):void 0}):c.onreadystatechange=function(a){return c._bucky.track!==!1?(c._bucky.readyStateTimes[c.readyState]=g(),4===c.readyState&&c._bucky.isDone!==!0?(c._bucky.isDone=!0,b(c,a)):void 0):void 0}}catch(j){i=j,f.error("Bucky error monitoring XHR open call",i)}return d.apply(c,arguments)},e=c.send,c.send=function(){return c._bucky.startTime=g(),e.apply(c,arguments)}}catch(h){a=h,f.error("Bucky error monitoring XHR",a)}return c},i=window.XMLHttpRequest,window.XMLHttpRequest=h}},e=function(a){return a=a.replace(/\\?( |,)/g,"\\$1"),"replace"===v.queryString&&(a=a.replace(/(\?|&)/g,",")),"escape"===v.queryString&&(a=a.replace(/\\=/g,"\\=")),a},l=function(b){var c;return null==b&&(b=""),c=null!=a?a:"",c&&b&&(c+="."),b&&(c+=b),s(c)},k={send:n,count:c,timer:u,now:g,requests:m,sendPagePerformance:r,flush:p,setOptions:A,options:v,history:j,active:i};for(q in k)w=k[q],l[q]=w;return l},l=s(),v.pagePerformanceKey&&l.sendPagePerformance(v.pagePerformanceKey),v.requestsKey&&l.requests.monitor(v.requestsKey),l},"function"==typeof define&&define.amd?define(b):"object"==typeof exports?module.exports=b():window.Bucky=b()}).call(this);