fair-analytics-client-api
Version:
The Fair Analytics client API
3 lines (2 loc) • 3.25 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.fairAnalytics=n()}(this,function(){function e(e){if(e.ok)return e;var n=new Error(e.statusText);return n.response=e,Promise.reject(n)}function n(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=n.url;if(!t)throw new Error('You must provide the "url" of your Fair Analytics instance');var r="NA";try{var o=window.localStorage.getItem("__fa__");if(o&&o.anonymousSessionId)r=o.anonymousSessionId;else try{var s={anonymousSessionId:u()};window.localStorage.setItem("__fa__",s),r=s.anonymousSessionId}catch(e){console.warn('Error while setting anonymousSessionId "NA" will be used',e)}}catch(e){console.warn('Error while setting anonymousSessionId, "NA" will be used',e)}return{send:function(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return n.event?(n.anonymousSessionId=r,i(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)}).then(e)):Promise.reject(new Error('You must provide the "event" parameter'))}}}var t="function"==typeof fetch?fetch:function(e,n){return n=n||{},new Promise(function(t,r){function o(){var e,n=[],t=[],r={};return i.getAllResponseHeaders().replace(/^(.*?):\s*([\s\S]*?)$/gm,function(o,i,s){n.push(i=i.toLowerCase()),t.push([i,s]),e=r[i],r[i]=e?e+","+s:s}),{ok:1==(i.status/200|0),status:i.status,statusText:i.statusText,url:i.responseURL,clone:o,text:function(){return Promise.resolve(i.responseText)},json:function(){return Promise.resolve(i.responseText).then(JSON.parse)},xml:function(){return Promise.resolve(i.responseXML)},blob:function(){return Promise.resolve(new Blob([i.response]))},headers:{keys:function(){return n},entries:function(){return t},get:function(e){return r[e.toLowerCase()]},has:function(e){return e.toLowerCase()in r}}}}var i=new XMLHttpRequest;i.open(n.method||"get",e);for(var s in n.headers)i.setRequestHeader(s,n.headers[s]);i.withCredentials="include"==n.credentials,i.onload=function(){t(o())},i.onerror=r,i.send(n.body)})},r=Object.freeze({default:t}),o=r&&t||r,i=window.fetch=o,s="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},u=function(e,n){return n={exports:{}},e(n,n.exports),n.exports}(function(e){!function(n){"use strict";var t=0,r=Math.pow(36,4),o=function(e,n){var t="000000000"+e;return t.substr(t.length-n)},i=function(){return o((Math.random()*r<<0).toString(36),4)},s=function(){return t=t<r?t:0,++t-1},u=function(){var e,n=(new Date).getTime().toString(36),t=u.fingerprint(),r=i()+i();return e=o(s().toString(36),4),"c"+n+e+t+r};u.slug=function(){var e,n=(new Date).getTime().toString(36),t=u.fingerprint().slice(0,1)+u.fingerprint().slice(-1),r=i().slice(-2);return e=s().toString(36).slice(-4),n.slice(-2)+e+t+r},u.globalCount=function(){var e=function(){var e,n=0;for(e in window)n++;return n}();return u.globalCount=function(){return e},e},u.fingerprint=function(){return o((navigator.mimeTypes.length+navigator.userAgent.length).toString(36)+u.globalCount().toString(36),4)},n.register?n.register("cuid",u):e.exports=u}(s.applitude||s)});return n});
//# sourceMappingURL=fair-analytics-client-api.umd.min.js.map