UNPKG

analytics-utils

Version:

Analytics utility functions used by 'analytics' module

3 lines (2 loc) 5.75 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e||self).analyticUtil={})}(this,function(e){var n=function(e){var n={exports:{}};return function(e,n){e.exports=function(e,n,r,t,o){for(n=n.split?n.split("."):n,t=0;t<n.length;t++)e=e?e[n[t]]:o;return e===o?r:e}}(n),n.exports}();function r(e){try{return decodeURIComponent(e.replace(/\+/g," "))}catch(e){return null}}"undefined"==typeof process||process;var t="undefined"!=typeof document;function o(e,n){return n.charAt(0)[e]()+n.slice(1)}"undefined"!=typeof Deno&&Deno,t&&"nodejs"===window.name||"undefined"!=typeof navigator&&void 0!==navigator.userAgent&&(navigator.userAgent.includes("Node.js")||navigator.userAgent.includes("jsdom"));var i=o.bind(null,"toUpperCase"),u=o.bind(null,"toLowerCase");function a(e,n){void 0===n&&(n=!0);var r=function(e){return f(e)?i("null"):"object"==typeof e?function(e){return l(e.constructor)?e.constructor.name:null}(e):Object.prototype.toString.call(e).slice(8,-1)}(e);return n?u(r):r}function c(e,n){return typeof n===e}var l=c.bind(null,"function"),s=c.bind(null,"string");function f(e){return null===e}function d(e,n){if("object"!=typeof n||f(n))return!1;if(n instanceof e)return!0;var r=a(new e(""));if(function(e){return e instanceof Error||s(e.message)&&e.constructor&&function(e){return"number"===a(e)&&!isNaN(e)}(e.constructor.stackTraceLimit)}(n))for(;n;){if(a(n)===r)return!0;n=Object.getPrototypeOf(n)}return!1}function p(e,n){var r=e instanceof Element||e instanceof HTMLDocument;return r&&n?function(e,n){return void 0===n&&(n=""),e&&e.nodeName===n.toUpperCase()}(e,n):r}function m(e){var n=[].slice.call(arguments,1);return function(){return e.apply(void 0,[].slice.call(arguments).concat(n))}}function g(e){if(!t)return!1;var n=e||document.referrer;if(n){var r=window.document.location.port,o=n.split("/")[2];return r&&(o=o.replace(":"+r,"")),o!==window.location.hostname}return!1}function v(e,n){var r=(e.split("?")||[,])[1];if(!r||-1===r.indexOf(n))return e;var t=new RegExp("(\\&|\\?)"+n+'([_A-Za-z0-9"+=.\\/\\-@%]+)',"g"),o=("?"+r).replace(t,"").replace(/^&/,"?");return e.replace("?"+r,o)}function x(e){return function(e){for(var n,t=Object.create(null),o=/([^&=]+)=?([^&]*)/g;n=o.exec(e);){var i=r(n[1]),u=r(n[2]);if(i)if("[]"===i.substring(i.length-2)){var a=t[i=i.substring(0,i.length-2)]||(t[i]=[]);t[i]=Array.isArray(a)?a:[],t[i].push(u)}else t[i]=""===u||u}for(var c in t){var l=c.split("[");l.length>1&&(h(t,l.map(function(e){return e.replace(/[?[\]\\ ]/g,"")}),t[c]),delete t[c])}return t}(function(e){if(e){var n=e.match(/\?(.*)/);return n&&n[1]?n[1].split("#")[0]:""}return t&&window.location.search.substring(1)}(e))}function h(e,n,r){for(var t=n.length-1,o=0;o<t;++o){var i=n[o];if("__proto__"===i||"constructor"===i)break;i in e||(e[i]={}),e=e[i]}e[n[t]]=r}function y(e){if(!t)return null;var n=document.createElement("a");return n.setAttribute("href",e),n.hostname}function b(e){return(y(e)||"").split(".").slice(-2).join(".")}function w(e){var n=e.split(".");return n.length>1?n.slice(0,-1).join("."):e}c.bind(null,"undefined"),c.bind(null,"boolean"),c.bind(null,"symbol"),d.bind(null,TypeError),d.bind(null,SyntaxError),m(p,"form"),m(p,"button"),m(p,"input"),m(p,"select");var j={trimTld:w,getDomainBase:b,getDomainHost:y},O="google",E="q",T={"daum.net":E,"eniro.se":"search_word","naver.com":"query","yahoo.com":"p","msn.com":E,"aol.com":E,"ask.com":E,"baidu.com":"wd","yandex.com":"text","rambler.ru":"words",google:E,"bing.com":{p:E,n:"live"}};e.decodeUri=r,e.dotProp=n,e.getBrowserLocale=function(){if(t){var e=navigator,n=e.languages;return e.userLanguage||(n&&n.length?n[0]:e.language)}},e.getTimeZone=function(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch(e){}},e.isExternalReferrer=g,e.isScriptLoaded=function(e){if(!t)return!0;var n=document.getElementsByTagName("script");return!!Object.keys(n).filter(function(r){var t=n[r].src;return s(e)?-1!==t.indexOf(e):e instanceof RegExp&&t.match(e)}).length},e.paramsClean=v,e.paramsGet=function(e,n){return r((RegExp(e+"=(.+?)(&|$)").exec(n)||[,""])[1])},e.paramsParse=x,e.paramsRemove=function(e,n){return t?new Promise(function(r,t){if(window.history&&window.history.replaceState){var o=window.location.href,i=v(o,e);o!==i&&history.replaceState({},"",i)}return n&&n(),r()}):Promise.resolve()},e.parseReferrer=function(e,n){if(!t)return!1;var r={source:"(direct)",medium:"(none)",campaign:"(not set)"};e&&g(e)&&(r.referrer=e);var o=function(e){if(!e||!t)return!1;var n=b(e),r=document.createElement("a");if(r.href=e,r.hostname.indexOf(O)>-1&&(n=O),T[n]){var o=T[n],i=new RegExp(("string"==typeof o?o:o.p)+"=.*?([^&#]*|$)","gi"),u=r.search.match(i);return{source:o.n||w(n),medium:"organic",term:(u?u[0].split("=")[1]:"")||"(not provided)"}}var a=g(e)?"referral":"internal";return{source:r.hostname,medium:a}}(e);o&&Object.keys(o).length&&(r=Object.assign({},r,o));var i=x(n),u=Object.keys(i);if(!u.length)return r;var a=u.reduce(function(e,n){return n.match(/^utm_/)&&(e[""+n.replace(/^utm_/,"")]=i[n]),n.match(/^(d|g)clid/)&&(e.source=O,e.medium=i.gclid?"cpc":"cpm",e[n]=i[n]),e},{});return Object.assign({},r,a)},e.throttle=function(e,n){var r,t,o,i=null,u=0,a=function(){u=new Date,i=null,o=e.apply(r,t)};return function(){var c=new Date;u||(u=c);var l=n-(c-u);return r=this,t=arguments,l<=0?(clearTimeout(i),i=null,u=c,o=e.apply(r,t)):i||(i=setTimeout(a,l)),o}},e.url=j,e.uuid=function(){for(var e="",n=0,r=4294967295*Math.random()|0;n++<36;){var t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"[n-1],o=15&r;e+="-"==t||"4"==t?t:("x"==t?o:3&o|8).toString(16),r=n%8==0?4294967295*Math.random()|0:r>>4}return e}}); //# sourceMappingURL=analytics-utils.umd.js.map