UNPKG

simple-tracker

Version:

Easy client-side tracking library to log events, metrics, errors, and messages

4 lines 2.81 kB
/*! simple-tracker - v1.2.3 | MIT * https://github.com/codeniko/simple-tracker */ !function(){"use strict";function e(r){var i,s,a,o="trcksesh",c=o.length+1,l=r.document,d=!0,u=!0,p=!1,f={};function n(e){return e?(e^16*Math.random()>>e/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,n)}function h(e){var t;s=e||s||function(){var e=l.cookie,t=e.indexOf(o);if(0<=t){var n=e.indexOf(";",t+1);return n=n<0?e.length:n,e.slice(t+c,n)}}()||n(),t=s,l.cookie=o+"="+t}function e(){this.clientContext={url:r.location.href,userAgent:r.navigator.userAgent||null,platform:r.navigator.platform||null}}e.prototype={onerror:function(e,t,n,o,i){var r={message:e,lineno:n,colno:o,stack:i?i.stack:"n/a"};this.logException(r)},logEvent:function(e,t){var n={type:"event",event:e};if("object"==typeof t)for(var o in t)t.hasOwnProperty(o)&&(n[o]=t[o]);this.push(n)},logException:function(e){this.push({level:"error",type:"exception",exception:e})},logMessage:function(e,t){var n={type:"message",message:e};t&&(n.level=t),this.push(n)},logMetric:function(e,t){this.push({type:"metric",metric:e,value:t})},startTimer:function(e){var t=r.performance;t.now&&(f[e]&&p&&console.warn("Timing metric '"+e+"' already started"),p&&console.debug("timer started for:",e),f[e]=t.now())},stopTimer:function(e){var t=r.performance;if(t.now){var n=t.now(),o=f[e];if(void 0!==o){var i=Math.round(n-o);f[e]=void 0,p&&console.debug("timer stopped for:",e,"time="+i),this.logMetric(e,i)}else p&&console.warn("Timing metric '"+e+"' wasn't started")}},push:function(e){var t,n,o=typeof e;"object"!==o&&"string"!==o||("string"===o?e={text:e}:(void 0!==e.devMode&&(p=!!e.devMode,delete e.devMode),void 0!==e.attachClientContext&&(d=!!e.attachClientContext,delete e.attachClientContext),void 0!==e.attachSessionId&&(u=!!e.attachSessionId,delete e.attachSessionId),e.sessionId&&(h(e.sessionId),delete e.sessionId),e.endpoint&&(s||h(),i=e.endpoint,delete e.endpoint),void 0!==e.sendCaughtExceptions&&(!!e.sendCaughtExceptions&&(t=this.onerror,n=r.onerror,r.onerror=function(){t.apply(a,arguments),"function"==typeof n&&n.apply(r,arguments)}),delete e.sendCaughtExceptions)),function(e){if(i&&0<Object.keys(e).length)if(u&&(e.sessionId=s),d&&(e.context=a.clientContext),p)console.debug("SimpleTracker: POST "+i,e);else try{var t=new r.XMLHttpRequest;t.open("POST",i,!0),t.setRequestHeader("Content-Type","application/json"),t.send(JSON.stringify(e))}catch(e){}}(e))}};var t=r.tracker;if(t&&t.length){a=new e;var v=0,g=t.length;for(v=0;v<g;v++)a.push(t[v])}else a=new e;return r.tracker=a,r.SimpleTracker=e,a}var t="undefined"!=typeof module&&module.exports;if("undefined"!=typeof window){var n=e(window);t&&(e.default=n,module.exports=n)}else{if(!t)throw new Error("");e.default=e,module.exports=e}}(); //# sourceMappingURL=simple-tracker.min.map