UNPKG

fair-analytics-client-api

Version:
3 lines (2 loc) 3.3 kB
function createCommonjsModule(e,n){return n={exports:{}},e(n,n.exports),n.exports}function checkStatus(e){if(e.ok)return e;var n=new Error(e.statusText);return n.response=e,Promise.reject(n)}function fairAnalytics(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=e.url;if(!n)throw new Error('You must provide the "url" of your Fair Analytics instance');var t="NA";try{var r=window.localStorage.getItem("__fa__");if(r&&r.anonymousSessionId)t=r.anonymousSessionId;else try{var o={anonymousSessionId:browserCuid()};window.localStorage.setItem("__fa__",o),t=o.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 e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return e.event?(e.anonymousSessionId=t,browser(n,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}).then(checkStatus)):Promise.reject(new Error('You must provide the "event" parameter'))}}}var index="function"==typeof fetch?fetch:function(e,n){return n=n||{},new Promise(function(t,r){function o(){var e,n=[],t=[],r={};return s.getAllResponseHeaders().replace(/^(.*?):\s*([\s\S]*?)$/gm,function(o,s,i){n.push(s=s.toLowerCase()),t.push([s,i]),e=r[s],r[s]=e?e+","+i:i}),{ok:1==(s.status/200|0),status:s.status,statusText:s.statusText,url:s.responseURL,clone:o,text:function(){return Promise.resolve(s.responseText)},json:function(){return Promise.resolve(s.responseText).then(JSON.parse)},xml:function(){return Promise.resolve(s.responseXML)},blob:function(){return Promise.resolve(new Blob([s.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 s=new XMLHttpRequest;s.open(n.method||"get",e);for(var i in n.headers)s.setRequestHeader(i,n.headers[i]);s.withCredentials="include"==n.credentials,s.onload=function(){t(o())},s.onerror=r,s.send(n.body)})},unfetch_es=Object.freeze({default:index}),require$$0=unfetch_es&&index||unfetch_es,browser=window.fetch=require$$0,commonjsGlobal="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},browserCuid=createCommonjsModule(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)},s=function(){return o((Math.random()*r<<0).toString(36),4)},i=function(){return t=t<r?t:0,++t-1},u=function(){var e,n=(new Date).getTime().toString(36),t=u.fingerprint(),r=s()+s();return e=o(i().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=s().slice(-2);return e=i().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}(commonjsGlobal.applitude||commonjsGlobal)});module.exports=fairAnalytics; //# sourceMappingURL=fair-analytics-client-api.cjs.min.js.map