UNPKG

@gauf/tracker

Version:

Browser tracker with perfect customization

3 lines (2 loc) 7.24 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self)["@gauf/tracker"]={})}(this,function(t){"use strict";var r=function(t,e){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};function o(t,e){function n(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var e=function(){function t(t,e){this.listener=t,this.settings=e}return t.prototype.emit=function(t,e){this.listener({emitter:this.constructor.key,name:t,payload:e,timestamp:+new Date})},t.key="default",t}(),i=function(){return{height:window.document.body.clientHeight,width:window.document.body.clientWidth}},c=function(){return{x:window.scrollX,y:window.scrollY}},n=function(t){var e=t.clientX,n=t.clientY,r=i(),o=c();return{x:e,y:n,outside:e<o.x||e>o.x+r.width||n<o.y||n>o.y+r.height}},a=[{event:"keydown",useCapture:!0},{event:"click",extractor:n,useCapture:!0},{event:"mousemove",extractor:n,useCapture:!0},{event:"dblclick",extractor:n,useCapture:!0},{event:"deviceorientation",extractor:function(t){return{absolute:t.absolute,alpha:t.alpha,beta:t.beta,gamma:t.gamma}}},{event:"scroll",extractor:function(){return c()}},{event:"resize",extractor:function(){return i()}}],s=function(r){function t(t,e){void 0===e&&(e={});var n=r.call(this,t,e)||this;return n.metricSourceListeners=e.metrics&&e.metrics.length?a.filter(function(t){return-1!==e.metrics.indexOf(t.event)}).map(function(t){return n.createMetricSourceListener(t)}):a.map(function(t){return n.createMetricSourceListener(t)}),n}return o(t,r),t.prototype.activate=function(){this.metricSourceListeners.forEach(function(t){window.addEventListener(t.name,t.listener.handler,t.listener.useCapture)})},t.prototype.deactivate=function(){this.metricSourceListeners.forEach(function(t){window.removeEventListener(t.name,t.listener.handler,t.listener.useCapture)})},t.prototype.createMetricSourceListener=function(t){var e=t.event,n=t.useCapture,r=t.extractor;return{listener:{handler:this.createHandler(e,r),useCapture:n},name:t.event}},t.prototype.createHandler=function(e,n){var r=this;return function(t){return r.emit(e,"function"==typeof n?n(t):void 0)}},t.key="behavior",t}(e),u=["userAgent","appVersion","appName","appCodeName","product","productSub","vendor","vendorSub","platform","hardwareConcurrency","language","languages","onLine","doNotTrack","cookieEnabled","maxTouchPoints"],p=function(r){function t(t,e){void 0===e&&(e={});var n=r.call(this,t,e)||this;return n.headerbeat=e.heartbeat||1e4,n.metrics=e.metrics&&e.metrics.length?u.filter(function(t){return-1!==e.metrics.indexOf(t)}):u,n}return o(t,r),t.prototype.activate=function(){this.waitAndEmit()},t.prototype.deactivate=function(){this.timeout&&window.clearTimeout(this.timeout)},t.prototype.waitAndEmit=function(){var e=this;this.timeout=window.setTimeout(function(){u.map(function(t){return e.emit(t,navigator[t])}),e.waitAndEmit()},this.headerbeat)},t.key="navigator",t}(e),f=function(){function t(t,e){void 0===e&&(e={}),this.emitters=e.emitters?this.createCustomEmitters(t,e):this.createDefaultEmitters(t,e)}return t.prototype.activate=function(){this.emitters.forEach(function(t){return t.activate()})},t.prototype.deactivate=function(){this.emitters.forEach(function(t){return t.deactivate()})},t.prototype.extractSettings=function(t,e){return t.settings?t.settings[e.key]:void 0},t.prototype.createCustomEmitters=function(e,n){var r=this;return n.emitters.map(function(t){return new t(e,r.extractSettings(n,t))})},t.prototype.createDefaultEmitters=function(t,e){return[new p(t,this.extractSettings(e,p)),new s(t,this.extractSettings(e,s))]},t}(),h=function(){function t(){}return t.defaultPacker=function(t){return t},t}(),l=function(n){function t(t){var e=n.call(this)||this;return e.url=t,e}return o(t,n),t}(h),d=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.connect=function(t){t()},e}(l),v=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.send=function(t){console.log(t)},e}(h),m={headers:[],timeout:7500},y=function(r){function t(t,e){var n=r.call(this,t)||this;return n.settings=Object.assign({},m,e),n}return o(t,r),t.prototype.send=function(t){var e=this.settings,n=e.headers,r=e.timeout,o=new XMLHttpRequest;o.timeout=r,o.open("POST",this.url),o.onerror=function(){return console.log(o.statusText)},o.ontimeout=function(){return console.log(o.statusText)},n.forEach(function(t){var e=t.header,n=t.value;return o.setRequestHeader(e,n)}),o.send(t)},t.defaultPacker=JSON.stringify,t}(l),w=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.send=function(t){navigator.sendBeacon(this.url,t)},e.defaultPacker=JSON.stringify,e}(l),g=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.connect=function(t){this.connection=new WebSocket(this.url),this.connection.onopen=function(){t()},this.connection.onerror=console.error},e.prototype.send=function(t){this.connection&&this.connection.readyState===WebSocket.OPEN&&this.connection.send(t)},e.prototype.disconnect=function(){this.connection&&this.connection.readyState===WebSocket.OPEN&&this.connection.close()},e.defaultPacker=JSON.stringify,e}(d),b=function(){function t(){}return t.prototype.createTransport=function(t,e){var n,r=/^http[s]?:\/\//.test(n=t)?"http":/^ws[s]?:\/\//.test(n)?"ws":/^console:\/\//.test(n)?"console":void 0;if("http"===r){if("sendBeacon"in navigator)return new w(t);if("XMLHttpRequest"in window)return new y(t,e)}return"ws"===r?new g(t):new v},t}(),k={heartbeat:2e3},S=function(){function t(t,e){var n=this,r=Object.assign({},k,e);this.metrics=[],this.collector=this.createCollector(r.collector),this.transport=this.createTransport(t,r.transport),this.packer=this.createPacker(this.transport,r.packer),this.heartbeat=r.heartbeat,window.addEventListener("beforeunload",function(){n.deactivate()})}return t.prototype.activate=function(t){var e=this;this.payload=t,this.collector.activate(),this.transport instanceof d?this.transport.connect(function(){e.createSendInterval()}):this.createSendInterval()},t.prototype.deactivate=function(){this.collector.deactivate(),this.transport.send(this.packData()),this.transport instanceof d&&this.transport.disconnect(),this.interval&&window.clearInterval(this.interval),this.payload=void 0},t.prototype.packData=function(){return this.packer({metrics:this.metrics,payload:this.payload})},t.prototype.createSendInterval=function(){var t=this;this.interval=window.setInterval(function(){t.transport.send(t.packData()),t.metrics=[]},this.heartbeat)},t.prototype.createCollector=function(t){var e=this;return new f(function(t){return e.collect(t)},t)},t.prototype.createTransport=function(t,e){return(new b).createTransport(t,e)},t.prototype.createPacker=function(t,e){return e||t.constructor.defaultPacker},t.prototype.collect=function(t){this.metrics.push(t)},t}();t.default=S,t.MetricEmitter=e,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=tracker.js.map