analytics-utils
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -6,2 +6,13 @@ # Change Log | ||
## [0.1.2](https://github.com/DavidWells/analytics/compare/analytics-utils@0.1.1...analytics-utils@0.1.2) (2019-10-03) | ||
### Bug Fixes | ||
* storage import ref ([5b44ea6](https://github.com/DavidWells/analytics/commit/5b44ea6)) | ||
## [0.1.1](https://github.com/DavidWells/analytics/compare/analytics-utils@0.1.0...analytics-utils@0.1.1) (2019-08-14) | ||
@@ -8,0 +19,0 @@ |
var analyticsUtils = (function (exports) { | ||
'use strict'; | ||
var inBrowser = typeof window !== 'undefined'; | ||
function cookie(name, value, ttl, path, domain, secure) { | ||
if (typeof window === 'undefined') return; | ||
/** | ||
* Check if browser has access to cookies | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
if (arguments.length > 1) { | ||
/* eslint-disable no-return-assign */ | ||
return document.cookie = "".concat(name, "=").concat(encodeURIComponent(value)).concat(!ttl ? '' : "; expires=".concat(new Date(+new Date() + ttl * 1000).toUTCString())).concat(!path ? '' : "; path=".concat(path)).concat(!domain ? '' : "; domain=".concat(domain)).concat(!secure ? '' : '; secure'); | ||
/* eslint-enable */ | ||
} | ||
function hasCookies() { | ||
return decodeURIComponent(("; ".concat(document.cookie).split("; ".concat(name, "="))[1] || '').split(';')[0]); | ||
} | ||
function hasCookieSupport() { | ||
try { | ||
if (!inBrowser) return false; | ||
var key = 'cookietest='; // Try to set cookie | ||
var key = '___c'; // Try to set cookie | ||
document.cookie = "".concat(key, "1"); | ||
var cookiesEnabled = document.cookie.indexOf(key) !== -1; // Cleanup cookie | ||
cookie(key, '1'); | ||
var valueSet = document.cookie.indexOf(key) !== -1; // Cleanup cookie | ||
document.cookie = "".concat(key, "1; expires=Thu, 01-Jan-1970 00:00:01 GMT"); | ||
return cookiesEnabled; | ||
cookie(key, '', -1); | ||
return valueSet; | ||
} catch (e) { | ||
@@ -26,49 +29,264 @@ return false; | ||
} | ||
/** | ||
* Get a cookie value | ||
* @param {string} name - key of cookie | ||
* @return {string} value of cookie | ||
*/ | ||
var cookiesSupported = hasCookies(); | ||
function setCookie(name, value, days) { | ||
if (!cookiesSupported) return false; | ||
var expires = ''; | ||
if (days) { | ||
var date = new Date(); | ||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); | ||
expires = "; expires=".concat(date.toGMTString()); | ||
var getCookie = cookie; | ||
/** | ||
* Set a cookie value | ||
* @param {string} name - key of cookie | ||
* @param {string} value - value of cookie | ||
* @param {string} days - days to keep cookie | ||
*/ | ||
var setCookie = cookie; | ||
/** | ||
* Remove a cookie value. | ||
* @param {string} name - key of cookie | ||
*/ | ||
function removeCookie(name) { | ||
return cookie(name, '', -1); | ||
} | ||
function hasLocalStorage() { | ||
return false; | ||
try { | ||
if (typeof localStorage === 'undefined' || typeof JSON === 'undefined') { | ||
return false; | ||
} // test for safari private | ||
localStorage.setItem('__test', '1'); | ||
localStorage.removeItem('__test'); | ||
} catch (err) { | ||
return false; | ||
} | ||
document.cookie = "".concat(name, "=").concat(value).concat(expires, "; path=/"); | ||
return true; | ||
} | ||
function getCookie(name) { | ||
if (!cookiesSupported) return false; | ||
var find = "".concat(name, "="); | ||
var allCookies = document.cookie.split(';'); | ||
for (var i = 0; i < allCookies.length; i++) { | ||
var cookie = allCookies[i]; | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
while (cookie.charAt(0) === ' ') { | ||
cookie = cookie.substring(1, cookie.length); | ||
return _typeof(obj); | ||
} | ||
function parse(input) { | ||
var value; | ||
try { | ||
value = JSON.parse(input); | ||
if (typeof value === 'undefined') { | ||
value = input; | ||
} | ||
if (cookie.indexOf(find) === 0) { | ||
return cookie.substring(find.length, cookie.length); | ||
if (value === 'true') { | ||
value = true; | ||
} | ||
if (value === 'false') { | ||
value = false; | ||
} | ||
if (parseFloat(value) === value && _typeof(value) !== 'object') { | ||
value = parseFloat(value); | ||
} | ||
} catch (e) { | ||
value = input; | ||
} | ||
return null; | ||
return value; | ||
} | ||
function removeCookie(name) { | ||
if (!cookiesSupported) return false; | ||
setCookie(name, '', -1); | ||
/* global self globalThis */ | ||
function getGlobalThis() { | ||
if (typeof globalThis !== 'undefined') return globalThis; | ||
if (typeof global !== 'undefined') return global; | ||
if (typeof self !== 'undefined') return self; | ||
/* eslint-disable-line no-restricted-globals */ | ||
if (typeof window !== 'undefined') return window; | ||
if (typeof this !== 'undefined') return this; | ||
return {}; // should never happen | ||
} | ||
var cookie = { | ||
getCookie: getCookie, | ||
setCookie: setCookie, | ||
removeCookie: removeCookie | ||
/* tinier from https://github.com/purposeindustries/window-or-global/blob/master/lib/index.js | ||
const context = (typeof self === 'object' && self.self === self && self) || // eslint-disable-line | ||
(typeof global === 'object' && global.global === global && global) || | ||
this | ||
export default context | ||
*/ | ||
var LOCAL_STORAGE = 'localStorage'; | ||
var COOKIE = 'cookie'; | ||
var GLOBAL = 'global'; // Verify support | ||
var hasStorage = hasLocalStorage(); | ||
var hasCookies = hasCookieSupport(); | ||
/** | ||
* Get storage item from localStorage, cookie, or window | ||
* @param {string} key - key of item to get | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @return {Any} the value of key | ||
*/ | ||
function getItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return null; | ||
var storageType = getStorageType(options); // Get value from all locations | ||
if (storageType === 'all') return getAll(key); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storageType)) { | ||
var value = localStorage.getItem(key); | ||
if (value || storageType === LOCAL_STORAGE) return parse(value); | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storageType)) { | ||
var _value = getCookie(key); | ||
if (_value || storageType === COOKIE) return parse(_value); | ||
} | ||
/* 3. Fallback to window/global. */ | ||
return getGlobalThis[key] || null; | ||
} | ||
function getAll(key) { | ||
return { | ||
cookie: parse(getCookie(key)), | ||
localStorage: parse(localStorage.getItem(key)), | ||
global: getGlobalThis[key] || null | ||
}; | ||
} | ||
/** | ||
* Store values in localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {*} value - value of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @returns {object} returns old value, new values, & location of storage | ||
*/ | ||
function setItem(key, value) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
if (!key || !value) return false; | ||
var storageType = getStorageType(options); | ||
var saveValue = JSON.stringify(value); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storageType)) { | ||
// console.log('SET as localstorage', saveValue) | ||
var _oldValue = parse(localStorage.getItem(key)); | ||
localStorage.setItem(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue, | ||
location: LOCAL_STORAGE | ||
}; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storageType)) { | ||
// console.log('SET as cookie', saveValue) | ||
var _oldValue2 = parse(getCookie(key)); | ||
setCookie(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue2, | ||
location: COOKIE | ||
}; | ||
} | ||
/* 3. Fallback to window/global */ | ||
var oldValue = getGlobalThis[key]; | ||
getGlobalThis[key] = value; | ||
return { | ||
value: value, | ||
oldValue: oldValue, | ||
location: GLOBAL | ||
}; | ||
} | ||
/** | ||
* Remove values from localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
*/ | ||
function removeItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return false; | ||
var storageType = getStorageType(options); | ||
if (useLocal(storageType)) { | ||
/* 1. Try localStorage */ | ||
localStorage.removeItem(key); | ||
return LOCAL_STORAGE; | ||
} else if (useCookie(storageType)) { | ||
/* 2. Fallback to cookie */ | ||
removeCookie(key); | ||
return COOKIE; | ||
} | ||
/* 3. Fallback to window/global */ | ||
getGlobalThis[key] = null; | ||
return GLOBAL; | ||
} | ||
function getStorageType(options) { | ||
return typeof options === 'string' ? options : options.storage; | ||
} | ||
function useLocal(storage) { | ||
return hasStorage && (!storage || storage === LOCAL_STORAGE); | ||
} | ||
function useCookie(storage) { | ||
return hasCookies && (!storage || storage === COOKIE); | ||
} | ||
var index = { | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem | ||
}; | ||
function decode(s) { | ||
return decodeURIComponent(s).replace(/\+/g, ' '); | ||
try { | ||
return decodeURIComponent(s.replace(/\+/g, ' ')); | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
var inBrowser = typeof window !== 'undefined'; | ||
function getBrowserLocale() { | ||
@@ -153,3 +371,3 @@ if (!inBrowser) return null; | ||
function paramsClean(url, param) { | ||
var search = (url.split('?') || [,])[1]; | ||
var search = (url.split('?') || [,])[1]; // eslint-disable-line | ||
@@ -161,3 +379,3 @@ if (!search || search.indexOf(param) === -1) { | ||
var regex = new RegExp("(\\&|\\?)".concat(param, "([_A-Za-z0-9\"+=.%]+)"), 'g'); | ||
var regex = new RegExp("(\\&|\\?)".concat(param, "([_A-Za-z0-9\"+=.\\/\\-@%]+)"), 'g'); | ||
var cleanSearch = "?".concat(search).replace(regex, '').replace(/^&/, '?'); // replace search params with clean params | ||
@@ -313,3 +531,3 @@ | ||
* @example | ||
* getDomainHost('https://subdomain.my-site.com/') | ||
* getDomainBase('https://subdomain.my-site.com/') | ||
* > my-site.com | ||
@@ -444,25 +662,27 @@ */ | ||
var Q = 'q'; | ||
var QUERY = 'query'; | ||
var searchEngines = { | ||
'daum.net': 'q', | ||
'daum.net': Q, | ||
'eniro.se': 'search_word', | ||
'naver.com': 'query', | ||
'naver.com': QUERY, | ||
'yahoo.com': 'p', | ||
'msn.com': 'q', | ||
'aol.com': 'q', | ||
'lycos.com': 'q', | ||
'ask.com': 'q', | ||
'cnn.com': 'query', | ||
'msn.com': Q, | ||
'aol.com': Q, | ||
'lycos.com': Q, | ||
'ask.com': Q, | ||
'cnn.com': QUERY, | ||
'about.com': 'terms', | ||
'baidu.com': 'wd', | ||
'yandex.com': 'text', | ||
'seznam.cz': 'q', | ||
'search.com': 'q', | ||
'seznam.cz': Q, | ||
'search.com': Q, | ||
'yam.com': 'k', | ||
'kvasir.no': 'q', | ||
'terra.com': 'query', | ||
'mynet.com': 'q', | ||
'kvasir.no': Q, | ||
'terra.com': QUERY, | ||
'mynet.com': Q, | ||
'rambler.ru': 'words', | ||
'google': 'q', | ||
'google': Q, | ||
'bing.com': { | ||
'p': 'q', | ||
'p': Q, | ||
'n': 'live' | ||
@@ -472,42 +692,23 @@ } | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
function uuid() { | ||
var lut = []; | ||
for (var i = 0; i < 256; i++) { | ||
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | ||
} | ||
return _typeof(obj); | ||
return function () { | ||
var d0 = genNumber(); | ||
var d1 = genNumber(); | ||
var d2 = genNumber(); | ||
var d3 = genNumber(); | ||
/* eslint-disable */ | ||
return "".concat(lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff], "-").concat(lut[d1 & 0xff]).concat(lut[d1 >> 8 & 0xff], "-").concat(lut[d1 >> 16 & 0x0f | 0x40]).concat(lut[d1 >> 24 & 0xff], "-").concat(lut[d2 & 0x3f | 0x80]).concat(lut[d2 >> 8 & 0xff], "-").concat(lut[d2 >> 16 & 0xff]).concat(lut[d2 >> 24 & 0xff]).concat(lut[d3 & 0xff]).concat(lut[d3 >> 8 & 0xff]).concat(lut[d3 >> 16 & 0xff]).concat(lut[d3 >> 24 & 0xff]); | ||
/* eslint-enable */ | ||
}(); | ||
} | ||
function parse(input) { | ||
var value; | ||
try { | ||
value = JSON.parse(input); | ||
if (typeof value === 'undefined') { | ||
value = input; | ||
} | ||
if (value === 'true') { | ||
value = true; | ||
} | ||
if (value === 'false') { | ||
value = false; | ||
} | ||
if (parseFloat(value) === value && _typeof(value) !== 'object') { | ||
value = parseFloat(value); | ||
} | ||
} catch (e) { | ||
value = input; | ||
} | ||
return value; | ||
function genNumber() { | ||
return Math.random() * 0xffffffff | 0; | ||
} | ||
@@ -545,185 +746,6 @@ | ||
/** | ||
* Check if browser has access to LocalStorage | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function hasLocalStorage() { | ||
if (!inBrowser) return false; | ||
try { | ||
if (typeof localStorage === 'undefined' || typeof JSON === 'undefined') { | ||
return false; | ||
} // test for safari private | ||
localStorage.setItem('_test_', '1'); | ||
localStorage.removeItem('_test_'); | ||
} catch (err) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
var hasLocalStorage$1 = hasLocalStorage(); | ||
/** | ||
* Get storage item from localStorage, cookie, or window | ||
* @param {string} key - key of item to get | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @return {Any} the value of key | ||
*/ | ||
function getItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return null; | ||
var storage = getStorageType(options); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
var value = localStorage.getItem(key); | ||
if (value || storage === 'localStorage') return parse(value); | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
var _value = getCookie(key); | ||
if (_value || storage === 'cookie') return parse(_value); | ||
} | ||
/* 3. Fallback to window/global. TODO verify AWS lambda & check for conflicts */ | ||
return globalContext[key] || null; | ||
} | ||
/** | ||
* Store values in localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {*} value - value of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @returns {object} returns old value, new values, & location of storage | ||
*/ | ||
function setItem(key, value) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
if (!key || !value) return false; | ||
var storage = getStorageType(options); | ||
var saveValue = JSON.stringify(value); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
// console.log('SET as localstorage', saveValue) | ||
var _oldValue = parse(localStorage.getItem(key)); | ||
localStorage.setItem(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue, | ||
type: 'localStorage' | ||
}; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
// console.log('SET as cookie', saveValue) | ||
var _oldValue2 = parse(getCookie(key)); | ||
setCookie(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue2, | ||
type: 'cookie' | ||
}; | ||
} | ||
/* 3. Fallback to window/global */ | ||
var oldValue = globalContext[key]; // console.log('SET as window', value) | ||
globalContext[key] = value; | ||
return { | ||
value: value, | ||
oldValue: oldValue, | ||
type: 'window' | ||
}; | ||
} | ||
/** | ||
* Remove values from localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
*/ | ||
function removeItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return false; | ||
var storage = getStorageType(options); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
localStorage.removeItem(key); | ||
return null; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
removeCookie(key); | ||
return null; | ||
} | ||
/* 3. Fallback to window/global */ | ||
globalContext[key] = null; | ||
return null; | ||
} | ||
function getStorageType(options) { | ||
return typeof options === 'string' ? options : options.storage; | ||
} | ||
function useLocal(storage) { | ||
return hasLocalStorage$1 && (!storage || storage === 'localStorage'); | ||
} | ||
function useCookie(storage) { | ||
return cookiesSupported && (!storage || storage === 'cookie'); | ||
} | ||
var index = { | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem | ||
}; | ||
function uuid() { | ||
var lut = []; | ||
for (var i = 0; i < 256; i++) { | ||
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | ||
} | ||
return function () { | ||
var d0 = genNumber(); | ||
var d1 = genNumber(); | ||
var d2 = genNumber(); | ||
var d3 = genNumber(); | ||
/* eslint-disable */ | ||
return "".concat(lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff], "-").concat(lut[d1 & 0xff]).concat(lut[d1 >> 8 & 0xff], "-").concat(lut[d1 >> 16 & 0x0f | 0x40]).concat(lut[d1 >> 24 & 0xff], "-").concat(lut[d2 & 0x3f | 0x80]).concat(lut[d2 >> 8 & 0xff], "-").concat(lut[d2 >> 16 & 0xff]).concat(lut[d2 >> 24 & 0xff]).concat(lut[d3 & 0xff]).concat(lut[d3 >> 8 & 0xff]).concat(lut[d3 >> 16 & 0xff]).concat(lut[d3 >> 24 & 0xff]); | ||
/* eslint-enable */ | ||
}(); | ||
} | ||
function genNumber() { | ||
return Math.random() * 0xffffffff | 0; | ||
} | ||
exports.cookie = cookie; | ||
exports.storage = index; | ||
exports.getCookie = getCookie; | ||
exports.setCookie = setCookie; | ||
exports.removeCookie = removeCookie; | ||
exports.decodeUri = decode; | ||
@@ -741,3 +763,2 @@ exports.getBrowserLocale = getBrowserLocale; | ||
exports.parseReferrer = parseReferrer; | ||
exports.storage = index; | ||
exports.url = url; | ||
@@ -744,0 +765,0 @@ exports.uuid = uuid; |
@@ -1,1 +0,1 @@ | ||
var analyticsUtils=function(e){"use strict";var u="undefined"!=typeof window;var a=function(){try{if(!u)return!1;var e="cookietest=";document.cookie="".concat(e,"1");var t=-1!==document.cookie.indexOf(e);return document.cookie="".concat(e,"1; expires=Thu, 01-Jan-1970 00:00:01 GMT"),t}catch(e){return!1}}();function l(e,t,n){if(!a)return!1;var r="";if(n){var o=new Date;o.setTime(o.getTime()+24*n*60*60*1e3),r="; expires=".concat(o.toGMTString())}document.cookie="".concat(e,"=").concat(t).concat(r,"; path=/")}function f(e){if(!a)return!1;for(var t="".concat(e,"="),n=document.cookie.split(";"),r=0;r<n.length;r++){for(var o=n[r];" "===o.charAt(0);)o=o.substring(1,o.length);if(0===o.indexOf(t))return o.substring(t.length,o.length)}return null}function r(e){if(!a)return!1;l(e,"",-1)}var t={getCookie:f,setCookie:l,removeCookie:r};function s(e){return decodeURIComponent(e).replace(/\+/g," ")}function m(e){if(!u)return!1;var t=e||document.referrer;if(t){var n=window.document.location.port,r=t.split("/")[2];return n&&(r=r.replace(":".concat(n),"")),r!==window.location.hostname}return!1}function c(e,t){var n=(e.split("?")||[,])[1];if(!n||-1===n.indexOf(t))return e;var r=new RegExp("(\\&|\\?)".concat(t,'([_A-Za-z0-9"+=.%]+)'),"g"),o="?".concat(n).replace(r,"").replace(/^&/,"?");return e.replace("?".concat(n),o)}function i(e){var t=function(e){if(e){var t=e.match(/\?(.*)/);return t&&t[1]?t[1].split("#")[0]:""}return u&&window.location.search.substring(1)}(e);return t?function(e){var t,n=/([^&=]+)=?([^&]*)/g,r={};for(;t=n.exec(e);){var o=s(t[1]),a=s(t[2]);if("[]"===o.substring(o.length-2))o=o.substring(0,o.length-2),(r[o]||(r[o]=[])).push(a);else{var c=""===a||a;r[o]=c}}for(var i in r){var u=i.split("[");if(1<u.length){var l=[];u.forEach(function(e,t){var n=e.replace(/[?[\]\\ ]/g,"");l.push(n)}),g(r,l,r[i]),delete r[i]}}return r}(t):{}}function g(e,t,n){for(var r=t.length-1,o=0;o<r;++o){var a=t[o];a in e||(e[a]={}),e=e[a]}e[t[r]]=n}function n(e){if(!u)return null;var t=document.createElement("a");return t.setAttribute("href",e),t.hostname}function d(e){return(n(e)||"").split(".").slice(-2).join(".")}function v(e){var t=e.split(".");return 1<t.length?t.slice(0,-1).join("."):e}var o={trimTld:v,getDomainBase:d,getDomainHost:n},p="google";var h={"daum.net":"q","eniro.se":"search_word","naver.com":"query","yahoo.com":"p","msn.com":"q","aol.com":"q","lycos.com":"q","ask.com":"q","cnn.com":"query","about.com":"terms","baidu.com":"wd","yandex.com":"text","seznam.cz":"q","search.com":"q","yam.com":"k","kvasir.no":"q","terra.com":"query","mynet.com":"q","rambler.ru":"words",google:"q","bing.com":{p:"q",n:"live"}};function y(e){return(y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function w(t){var n;try{void 0===(n=JSON.parse(t))&&(n=t),"true"===n&&(n=!0),"false"===n&&(n=!1),parseFloat(n)===n&&"object"!==y(n)&&(n=parseFloat(n))}catch(e){n=t}return n}var b="undefined"!=typeof self&&self?self:"undefined"!=typeof window&&window?window:"undefined"!=typeof global&&global?global:"undefined"!=typeof globalThis&&globalThis?globalThis:void 0;var S=function(){if(!u)return!1;try{if("undefined"==typeof localStorage||"undefined"==typeof JSON)return!1;localStorage.setItem("_test_","1"),localStorage.removeItem("_test_")}catch(e){return!1}return!0}();function k(e){return"string"==typeof e?e:e.storage}function x(e){return S&&(!e||"localStorage"===e)}function O(e){return a&&(!e||"cookie"===e)}var q={getItem:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};if(!e)return null;var n=k(t);if(x(n)){var r=localStorage.getItem(e);if(r||"localStorage"===n)return w(r)}if(O(n)){var o=f(e);if(o||"cookie"===n)return w(o)}return b[e]||null},setItem:function(e,t){var n=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};if(!e||!t)return!1;var r=k(n),o=JSON.stringify(t);if(x(r)){var a=w(localStorage.getItem(e));return localStorage.setItem(e,o),{value:t,oldValue:a,type:"localStorage"}}if(O(r)){var c=w(f(e));return l(e,o),{value:t,oldValue:c,type:"cookie"}}var i=b[e];return{value:b[e]=t,oldValue:i,type:"window"}},removeItem:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};if(!e)return!1;var n=k(t);return x(n)?(localStorage.removeItem(e),null):O(n)?(r(e),null):b[e]=null}};function I(){return 4294967295*Math.random()|0}return e.cookie=t,e.decodeUri=s,e.getBrowserLocale=function(){if(!u)return null;var e=navigator,t=e.language,n=e.languages,r=e.userLanguage;return n&&n.length?n[0]:r||t},e.getTimeZone=function(){return"undefined"==typeof Intl||"function"!=typeof Intl.DateTimeFormat||"function"!=typeof Intl.DateTimeFormat().resolvedOptions?null:Intl.DateTimeFormat().resolvedOptions().timeZone},e.inBrowser=u,e.isExternalReferrer=m,e.isScriptLoaded=function(n){if(!u)return!0;var r=document.getElementsByTagName("script");return!!Object.keys(r).filter(function(e){var t=r[e].src;return"string"==typeof n?-1!==t.indexOf(n):n instanceof RegExp&&t.match(n)}).length},e.noOp=function(){},e.paramsClean=c,e.paramsGet=function(e,t){return s((RegExp("".concat(e,"=(.+?)(&|$)")).exec(t)||[,""])[1])},e.paramsParse=i,e.paramsRemove=function(o,a){return u?new Promise(function(e,t){if(window.history&&window.history.replaceState){var n=window.location.href,r=c(n,o);n!==r&&history.replaceState({},"",r)}return a&&a(),e()}):Promise.resolve()},e.parseReferrer=function(e,t){if(!u)return!1;var n={source:"(direct)",medium:"(none)",campaign:"(not set)"};e&&m(e)&&(n.referrer=e);var r=function(e){if(!e||!u)return!1;var t=d(e),n=document.createElement("a");if(n.href=e,-1<n.hostname.indexOf(p)&&(t=p),h[t]){var r=h[t],o="string"==typeof r?r:r.p,a=new RegExp(o+"=.*?([^&#]*|$)","gi"),c=n.search.match(a);return{source:r.n||v(t),medium:"organic",term:(c?c[0].split("=")[1]:"")||"(not provided)"}}var i=m(e)?"referral":"internal";return{source:n.hostname,medium:i}}(e);r&&Object.keys(r).length&&(n=Object.assign({},n,r));var o=i(t),a=Object.keys(o);if(a.length){var c=a.reduce(function(e,t){return t.match(/^utm_/)&&(e["".concat(t.replace(/^utm_/,""))]=o[t]),t.match(/^(d|g)clid/)&&(e.source=p,e.medium=o.gclid?"cpc":"cpm",e[t]=o[t]),e},{});n=Object.assign({},n,c),(o.dclid||o.gclid)&&(n.source=p,n.medium=o.gclid?"cpc":"cpm")}return n},e.storage=q,e.url=o,e.uuid=function(){for(var e,t,n,r,o=[],a=0;a<256;a++)o[a]=(a<16?"0":"")+a.toString(16);return e=I(),t=I(),n=I(),r=I(),"".concat(o[255&e]+o[e>>8&255]+o[e>>16&255]+o[e>>24&255],"-").concat(o[255&t]).concat(o[t>>8&255],"-").concat(o[t>>16&15|64]).concat(o[t>>24&255],"-").concat(o[63&n|128]).concat(o[n>>8&255],"-").concat(o[n>>16&255]).concat(o[n>>24&255]).concat(o[255&r]).concat(o[r>>8&255]).concat(o[r>>16&255]).concat(o[r>>24&255])},e.globalContext=b,e}({}); | ||
var analyticsUtils=function(e){"use strict";function n(e,t,n,r,o,a){if("undefined"!=typeof window)return 1<arguments.length?document.cookie="".concat(e,"=").concat(encodeURIComponent(t)).concat(n?"; expires=".concat(new Date(+new Date+1e3*n).toUTCString()):"").concat(r?"; path=".concat(r):"").concat(o?"; domain=".concat(o):"").concat(a?"; secure":""):decodeURIComponent(("; ".concat(document.cookie).split("; ".concat(e,"="))[1]||"").split(";")[0])}var u=n,l=n;function r(e){return n(e,"",-1)}function o(e){return(o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function f(t){var n;try{void 0===(n=JSON.parse(t))&&(n=t),"true"===n&&(n=!0),"false"===n&&(n=!1),parseFloat(n)===n&&"object"!==o(n)&&(n=parseFloat(n))}catch(e){n=t}return n}function s(){return"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:void 0!==this?this:{}}var m="localStorage",d="cookie",g="global",t=!1,a=function(){try{var e="___c";n(e,"1");var t=-1!==document.cookie.indexOf(e);return n(e,"",-1),t}catch(e){return!1}}();function p(e){return"string"==typeof e?e:e.storage}function v(e){return t&&(!e||e===m)}function h(e){return a&&(!e||e===d)}var c={getItem:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};if(!e)return null;var n,r=p(t);if("all"===r)return{cookie:f(u(n=e)),localStorage:f(localStorage.getItem(n)),global:s[n]||null};if(v(r)){var o=localStorage.getItem(e);if(o||r===m)return f(o)}if(h(r)){var a=u(e);if(a||r===d)return f(a)}return s[e]||null},setItem:function(e,t){var n=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};if(!e||!t)return!1;var r=p(n),o=JSON.stringify(t);if(v(r)){var a=f(localStorage.getItem(e));return localStorage.setItem(e,o),{value:t,oldValue:a,location:m}}if(h(r)){var c=f(u(e));return l(e,o),{value:t,oldValue:c,location:d}}var i=s[e];return{value:s[e]=t,oldValue:i,location:g}},removeItem:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};if(!e)return!1;var n=p(t);return v(n)?(localStorage.removeItem(e),m):h(n)?(r(e),d):(s[e]=null,g)}};function y(e){try{return decodeURIComponent(e.replace(/\+/g," "))}catch(e){return null}}var w="undefined"!=typeof window;function b(e){if(!w)return!1;var t=e||document.referrer;if(t){var n=window.document.location.port,r=t.split("/")[2];return n&&(r=r.replace(":".concat(n),"")),r!==window.location.hostname}return!1}function i(e,t){var n=(e.split("?")||[,])[1];if(!n||-1===n.indexOf(t))return e;var r=new RegExp("(\\&|\\?)".concat(t,'([_A-Za-z0-9"+=.\\/\\-@%]+)'),"g"),o="?".concat(n).replace(r,"").replace(/^&/,"?");return e.replace("?".concat(n),o)}function S(e){var t=function(e){if(e){var t=e.match(/\?(.*)/);return t&&t[1]?t[1].split("#")[0]:""}return w&&window.location.search.substring(1)}(e);return t?function(e){var t,n=/([^&=]+)=?([^&]*)/g,r={};for(;t=n.exec(e);){var o=y(t[1]),a=y(t[2]);if("[]"===o.substring(o.length-2))o=o.substring(0,o.length-2),(r[o]||(r[o]=[])).push(a);else{var c=""===a||a;r[o]=c}}for(var i in r){var u=i.split("[");if(1<u.length){var l=[];u.forEach(function(e,t){var n=e.replace(/[?[\]\\ ]/g,"");l.push(n)}),x(r,l,r[i]),delete r[i]}}return r}(t):{}}function x(e,t,n){for(var r=t.length-1,o=0;o<r;++o){var a=t[o];a in e||(e[a]={}),e=e[a]}e[t[r]]=n}function I(e){if(!w)return null;var t=document.createElement("a");return t.setAttribute("href",e),t.hostname}function k(e){return(I(e)||"").split(".").slice(-2).join(".")}function O(e){var t=e.split(".");return 1<t.length?t.slice(0,-1).join("."):e}var T={trimTld:O,getDomainBase:k,getDomainHost:I},R="google";var C="q",E="query",j={"daum.net":C,"eniro.se":"search_word","naver.com":E,"yahoo.com":"p","msn.com":C,"aol.com":C,"lycos.com":C,"ask.com":C,"cnn.com":E,"about.com":"terms","baidu.com":"wd","yandex.com":"text","seznam.cz":C,"search.com":C,"yam.com":"k","kvasir.no":C,"terra.com":E,"mynet.com":C,"rambler.ru":"words",google:C,"bing.com":{p:C,n:"live"}};function D(){return 4294967295*Math.random()|0}var _="undefined"!=typeof self&&self?self:"undefined"!=typeof window&&window?window:"undefined"!=typeof global&&global?global:"undefined"!=typeof globalThis&&globalThis?globalThis:void 0;return e.storage=c,e.getCookie=u,e.setCookie=l,e.removeCookie=r,e.decodeUri=y,e.getBrowserLocale=function(){if(!w)return null;var e=navigator,t=e.language,n=e.languages,r=e.userLanguage;return n&&n.length?n[0]:r||t},e.getTimeZone=function(){return"undefined"==typeof Intl||"function"!=typeof Intl.DateTimeFormat||"function"!=typeof Intl.DateTimeFormat().resolvedOptions?null:Intl.DateTimeFormat().resolvedOptions().timeZone},e.inBrowser=w,e.isExternalReferrer=b,e.isScriptLoaded=function(n){if(!w)return!0;var r=document.getElementsByTagName("script");return!!Object.keys(r).filter(function(e){var t=r[e].src;return"string"==typeof n?-1!==t.indexOf(n):n instanceof RegExp&&t.match(n)}).length},e.noOp=function(){},e.paramsClean=i,e.paramsGet=function(e,t){return y((RegExp("".concat(e,"=(.+?)(&|$)")).exec(t)||[,""])[1])},e.paramsParse=S,e.paramsRemove=function(o,a){return w?new Promise(function(e,t){if(window.history&&window.history.replaceState){var n=window.location.href,r=i(n,o);n!==r&&history.replaceState({},"",r)}return a&&a(),e()}):Promise.resolve()},e.parseReferrer=function(e,t){if(!w)return!1;var n={source:"(direct)",medium:"(none)",campaign:"(not set)"};e&&b(e)&&(n.referrer=e);var r=function(e){if(!e||!w)return!1;var t=k(e),n=document.createElement("a");if(n.href=e,-1<n.hostname.indexOf(R)&&(t=R),j[t]){var r=j[t],o="string"==typeof r?r:r.p,a=new RegExp(o+"=.*?([^&#]*|$)","gi"),c=n.search.match(a);return{source:r.n||O(t),medium:"organic",term:(c?c[0].split("=")[1]:"")||"(not provided)"}}var i=b(e)?"referral":"internal";return{source:n.hostname,medium:i}}(e);r&&Object.keys(r).length&&(n=Object.assign({},n,r));var o=S(t),a=Object.keys(o);if(a.length){var c=a.reduce(function(e,t){return t.match(/^utm_/)&&(e["".concat(t.replace(/^utm_/,""))]=o[t]),t.match(/^(d|g)clid/)&&(e.source=R,e.medium=o.gclid?"cpc":"cpm",e[t]=o[t]),e},{});n=Object.assign({},n,c),(o.dclid||o.gclid)&&(n.source=R,n.medium=o.gclid?"cpc":"cpm")}return n},e.url=T,e.uuid=function(){for(var e,t,n,r,o=[],a=0;a<256;a++)o[a]=(a<16?"0":"")+a.toString(16);return e=D(),t=D(),n=D(),r=D(),"".concat(o[255&e]+o[e>>8&255]+o[e>>16&255]+o[e>>24&255],"-").concat(o[255&t]).concat(o[t>>8&255],"-").concat(o[t>>16&15|64]).concat(o[t>>24&255],"-").concat(o[63&n|128]).concat(o[n>>8&255],"-").concat(o[n>>16&255]).concat(o[n>>24&255]).concat(o[255&r]).concat(o[r>>8&255]).concat(o[r>>16&255]).concat(o[r>>24&255])},e.globalContext=_,e}({}); |
@@ -5,71 +5,17 @@ 'use strict'; | ||
var inBrowser = typeof window !== 'undefined'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
/** | ||
* Check if browser has access to cookies | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
var storageUtils = require('@analytics/storage-utils'); | ||
var storageUtils__default = _interopDefault(storageUtils); | ||
function hasCookies() { | ||
function decode(s) { | ||
try { | ||
if (!inBrowser) return false; | ||
var key = 'cookietest='; // Try to set cookie | ||
document.cookie = "".concat(key, "1"); | ||
var cookiesEnabled = document.cookie.indexOf(key) !== -1; // Cleanup cookie | ||
document.cookie = "".concat(key, "1; expires=Thu, 01-Jan-1970 00:00:01 GMT"); | ||
return cookiesEnabled; | ||
return decodeURIComponent(s.replace(/\+/g, ' ')); | ||
} catch (e) { | ||
return false; | ||
return null; | ||
} | ||
} | ||
var cookiesSupported = hasCookies(); | ||
function setCookie(name, value, days) { | ||
if (!cookiesSupported) return false; | ||
var expires = ''; | ||
var inBrowser = typeof window !== 'undefined'; | ||
if (days) { | ||
var date = new Date(); | ||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); | ||
expires = "; expires=".concat(date.toGMTString()); | ||
} | ||
document.cookie = "".concat(name, "=").concat(value).concat(expires, "; path=/"); | ||
} | ||
function getCookie(name) { | ||
if (!cookiesSupported) return false; | ||
var find = "".concat(name, "="); | ||
var allCookies = document.cookie.split(';'); | ||
for (var i = 0; i < allCookies.length; i++) { | ||
var cookie = allCookies[i]; | ||
while (cookie.charAt(0) === ' ') { | ||
cookie = cookie.substring(1, cookie.length); | ||
} | ||
if (cookie.indexOf(find) === 0) { | ||
return cookie.substring(find.length, cookie.length); | ||
} | ||
} | ||
return null; | ||
} | ||
function removeCookie(name) { | ||
if (!cookiesSupported) return false; | ||
setCookie(name, '', -1); | ||
} | ||
var cookie = { | ||
getCookie: getCookie, | ||
setCookie: setCookie, | ||
removeCookie: removeCookie | ||
}; | ||
function decode(s) { | ||
return decodeURIComponent(s).replace(/\+/g, ' '); | ||
} | ||
function getBrowserLocale() { | ||
@@ -154,3 +100,3 @@ if (!inBrowser) return null; | ||
function paramsClean(url, param) { | ||
var search = (url.split('?') || [,])[1]; | ||
var search = (url.split('?') || [,])[1]; // eslint-disable-line | ||
@@ -162,3 +108,3 @@ if (!search || search.indexOf(param) === -1) { | ||
var regex = new RegExp("(\\&|\\?)".concat(param, "([_A-Za-z0-9\"+=.%]+)"), 'g'); | ||
var regex = new RegExp("(\\&|\\?)".concat(param, "([_A-Za-z0-9\"+=.\\/\\-@%]+)"), 'g'); | ||
var cleanSearch = "?".concat(search).replace(regex, '').replace(/^&/, '?'); // replace search params with clean params | ||
@@ -314,3 +260,3 @@ | ||
* @example | ||
* getDomainHost('https://subdomain.my-site.com/') | ||
* getDomainBase('https://subdomain.my-site.com/') | ||
* > my-site.com | ||
@@ -445,25 +391,27 @@ */ | ||
var Q = 'q'; | ||
var QUERY = 'query'; | ||
var searchEngines = { | ||
'daum.net': 'q', | ||
'daum.net': Q, | ||
'eniro.se': 'search_word', | ||
'naver.com': 'query', | ||
'naver.com': QUERY, | ||
'yahoo.com': 'p', | ||
'msn.com': 'q', | ||
'aol.com': 'q', | ||
'lycos.com': 'q', | ||
'ask.com': 'q', | ||
'cnn.com': 'query', | ||
'msn.com': Q, | ||
'aol.com': Q, | ||
'lycos.com': Q, | ||
'ask.com': Q, | ||
'cnn.com': QUERY, | ||
'about.com': 'terms', | ||
'baidu.com': 'wd', | ||
'yandex.com': 'text', | ||
'seznam.cz': 'q', | ||
'search.com': 'q', | ||
'seznam.cz': Q, | ||
'search.com': Q, | ||
'yam.com': 'k', | ||
'kvasir.no': 'q', | ||
'terra.com': 'query', | ||
'mynet.com': 'q', | ||
'kvasir.no': Q, | ||
'terra.com': QUERY, | ||
'mynet.com': Q, | ||
'rambler.ru': 'words', | ||
'google': 'q', | ||
'google': Q, | ||
'bing.com': { | ||
'p': 'q', | ||
'p': Q, | ||
'n': 'live' | ||
@@ -473,42 +421,23 @@ } | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
function uuid() { | ||
var lut = []; | ||
for (var i = 0; i < 256; i++) { | ||
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | ||
} | ||
return _typeof(obj); | ||
return function () { | ||
var d0 = genNumber(); | ||
var d1 = genNumber(); | ||
var d2 = genNumber(); | ||
var d3 = genNumber(); | ||
/* eslint-disable */ | ||
return "".concat(lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff], "-").concat(lut[d1 & 0xff]).concat(lut[d1 >> 8 & 0xff], "-").concat(lut[d1 >> 16 & 0x0f | 0x40]).concat(lut[d1 >> 24 & 0xff], "-").concat(lut[d2 & 0x3f | 0x80]).concat(lut[d2 >> 8 & 0xff], "-").concat(lut[d2 >> 16 & 0xff]).concat(lut[d2 >> 24 & 0xff]).concat(lut[d3 & 0xff]).concat(lut[d3 >> 8 & 0xff]).concat(lut[d3 >> 16 & 0xff]).concat(lut[d3 >> 24 & 0xff]); | ||
/* eslint-enable */ | ||
}(); | ||
} | ||
function parse(input) { | ||
var value; | ||
try { | ||
value = JSON.parse(input); | ||
if (typeof value === 'undefined') { | ||
value = input; | ||
} | ||
if (value === 'true') { | ||
value = true; | ||
} | ||
if (value === 'false') { | ||
value = false; | ||
} | ||
if (parseFloat(value) === value && _typeof(value) !== 'object') { | ||
value = parseFloat(value); | ||
} | ||
} catch (e) { | ||
value = input; | ||
} | ||
return value; | ||
function genNumber() { | ||
return Math.random() * 0xffffffff | 0; | ||
} | ||
@@ -546,185 +475,6 @@ | ||
/** | ||
* Check if browser has access to LocalStorage | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function hasLocalStorage() { | ||
if (!inBrowser) return false; | ||
try { | ||
if (typeof localStorage === 'undefined' || typeof JSON === 'undefined') { | ||
return false; | ||
} // test for safari private | ||
localStorage.setItem('_test_', '1'); | ||
localStorage.removeItem('_test_'); | ||
} catch (err) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
var hasLocalStorage$1 = hasLocalStorage(); | ||
/** | ||
* Get storage item from localStorage, cookie, or window | ||
* @param {string} key - key of item to get | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @return {Any} the value of key | ||
*/ | ||
function getItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return null; | ||
var storage = getStorageType(options); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
var value = localStorage.getItem(key); | ||
if (value || storage === 'localStorage') return parse(value); | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
var _value = getCookie(key); | ||
if (_value || storage === 'cookie') return parse(_value); | ||
} | ||
/* 3. Fallback to window/global. TODO verify AWS lambda & check for conflicts */ | ||
return globalContext[key] || null; | ||
} | ||
/** | ||
* Store values in localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {*} value - value of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @returns {object} returns old value, new values, & location of storage | ||
*/ | ||
function setItem(key, value) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
if (!key || !value) return false; | ||
var storage = getStorageType(options); | ||
var saveValue = JSON.stringify(value); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
// console.log('SET as localstorage', saveValue) | ||
var _oldValue = parse(localStorage.getItem(key)); | ||
localStorage.setItem(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue, | ||
type: 'localStorage' | ||
}; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
// console.log('SET as cookie', saveValue) | ||
var _oldValue2 = parse(getCookie(key)); | ||
setCookie(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue2, | ||
type: 'cookie' | ||
}; | ||
} | ||
/* 3. Fallback to window/global */ | ||
var oldValue = globalContext[key]; // console.log('SET as window', value) | ||
globalContext[key] = value; | ||
return { | ||
value: value, | ||
oldValue: oldValue, | ||
type: 'window' | ||
}; | ||
} | ||
/** | ||
* Remove values from localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
*/ | ||
function removeItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return false; | ||
var storage = getStorageType(options); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
localStorage.removeItem(key); | ||
return null; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
removeCookie(key); | ||
return null; | ||
} | ||
/* 3. Fallback to window/global */ | ||
globalContext[key] = null; | ||
return null; | ||
} | ||
function getStorageType(options) { | ||
return typeof options === 'string' ? options : options.storage; | ||
} | ||
function useLocal(storage) { | ||
return hasLocalStorage$1 && (!storage || storage === 'localStorage'); | ||
} | ||
function useCookie(storage) { | ||
return cookiesSupported && (!storage || storage === 'cookie'); | ||
} | ||
var index = { | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem | ||
}; | ||
function uuid() { | ||
var lut = []; | ||
for (var i = 0; i < 256; i++) { | ||
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | ||
} | ||
return function () { | ||
var d0 = genNumber(); | ||
var d1 = genNumber(); | ||
var d2 = genNumber(); | ||
var d3 = genNumber(); | ||
/* eslint-disable */ | ||
return "".concat(lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff], "-").concat(lut[d1 & 0xff]).concat(lut[d1 >> 8 & 0xff], "-").concat(lut[d1 >> 16 & 0x0f | 0x40]).concat(lut[d1 >> 24 & 0xff], "-").concat(lut[d2 & 0x3f | 0x80]).concat(lut[d2 >> 8 & 0xff], "-").concat(lut[d2 >> 16 & 0xff]).concat(lut[d2 >> 24 & 0xff]).concat(lut[d3 & 0xff]).concat(lut[d3 >> 8 & 0xff]).concat(lut[d3 >> 16 & 0xff]).concat(lut[d3 >> 24 & 0xff]); | ||
/* eslint-enable */ | ||
}(); | ||
} | ||
function genNumber() { | ||
return Math.random() * 0xffffffff | 0; | ||
} | ||
exports.cookie = cookie; | ||
exports.storage = storageUtils__default; | ||
exports.getCookie = storageUtils.getCookie; | ||
exports.setCookie = storageUtils.setCookie; | ||
exports.removeCookie = storageUtils.removeCookie; | ||
exports.decodeUri = decode; | ||
@@ -742,5 +492,4 @@ exports.getBrowserLocale = getBrowserLocale; | ||
exports.parseReferrer = parseReferrer; | ||
exports.storage = index; | ||
exports.url = url; | ||
exports.uuid = uuid; | ||
exports.globalContext = globalContext; |
@@ -1,70 +0,13 @@ | ||
var inBrowser = typeof window !== 'undefined'; | ||
export { default as storage, getCookie, setCookie, removeCookie } from '@analytics/storage-utils'; | ||
/** | ||
* Check if browser has access to cookies | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function hasCookies() { | ||
function decode(s) { | ||
try { | ||
if (!inBrowser) return false; | ||
var key = 'cookietest='; // Try to set cookie | ||
document.cookie = "".concat(key, "1"); | ||
var cookiesEnabled = document.cookie.indexOf(key) !== -1; // Cleanup cookie | ||
document.cookie = "".concat(key, "1; expires=Thu, 01-Jan-1970 00:00:01 GMT"); | ||
return cookiesEnabled; | ||
return decodeURIComponent(s.replace(/\+/g, ' ')); | ||
} catch (e) { | ||
return false; | ||
return null; | ||
} | ||
} | ||
var cookiesSupported = hasCookies(); | ||
function setCookie(name, value, days) { | ||
if (!cookiesSupported) return false; | ||
var expires = ''; | ||
var inBrowser = typeof window !== 'undefined'; | ||
if (days) { | ||
var date = new Date(); | ||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); | ||
expires = "; expires=".concat(date.toGMTString()); | ||
} | ||
document.cookie = "".concat(name, "=").concat(value).concat(expires, "; path=/"); | ||
} | ||
function getCookie(name) { | ||
if (!cookiesSupported) return false; | ||
var find = "".concat(name, "="); | ||
var allCookies = document.cookie.split(';'); | ||
for (var i = 0; i < allCookies.length; i++) { | ||
var cookie = allCookies[i]; | ||
while (cookie.charAt(0) === ' ') { | ||
cookie = cookie.substring(1, cookie.length); | ||
} | ||
if (cookie.indexOf(find) === 0) { | ||
return cookie.substring(find.length, cookie.length); | ||
} | ||
} | ||
return null; | ||
} | ||
function removeCookie(name) { | ||
if (!cookiesSupported) return false; | ||
setCookie(name, '', -1); | ||
} | ||
var cookie = { | ||
getCookie: getCookie, | ||
setCookie: setCookie, | ||
removeCookie: removeCookie | ||
}; | ||
function decode(s) { | ||
return decodeURIComponent(s).replace(/\+/g, ' '); | ||
} | ||
function getBrowserLocale() { | ||
@@ -149,3 +92,3 @@ if (!inBrowser) return null; | ||
function paramsClean(url, param) { | ||
var search = (url.split('?') || [,])[1]; | ||
var search = (url.split('?') || [,])[1]; // eslint-disable-line | ||
@@ -157,3 +100,3 @@ if (!search || search.indexOf(param) === -1) { | ||
var regex = new RegExp("(\\&|\\?)".concat(param, "([_A-Za-z0-9\"+=.%]+)"), 'g'); | ||
var regex = new RegExp("(\\&|\\?)".concat(param, "([_A-Za-z0-9\"+=.\\/\\-@%]+)"), 'g'); | ||
var cleanSearch = "?".concat(search).replace(regex, '').replace(/^&/, '?'); // replace search params with clean params | ||
@@ -309,3 +252,3 @@ | ||
* @example | ||
* getDomainHost('https://subdomain.my-site.com/') | ||
* getDomainBase('https://subdomain.my-site.com/') | ||
* > my-site.com | ||
@@ -440,25 +383,27 @@ */ | ||
var Q = 'q'; | ||
var QUERY = 'query'; | ||
var searchEngines = { | ||
'daum.net': 'q', | ||
'daum.net': Q, | ||
'eniro.se': 'search_word', | ||
'naver.com': 'query', | ||
'naver.com': QUERY, | ||
'yahoo.com': 'p', | ||
'msn.com': 'q', | ||
'aol.com': 'q', | ||
'lycos.com': 'q', | ||
'ask.com': 'q', | ||
'cnn.com': 'query', | ||
'msn.com': Q, | ||
'aol.com': Q, | ||
'lycos.com': Q, | ||
'ask.com': Q, | ||
'cnn.com': QUERY, | ||
'about.com': 'terms', | ||
'baidu.com': 'wd', | ||
'yandex.com': 'text', | ||
'seznam.cz': 'q', | ||
'search.com': 'q', | ||
'seznam.cz': Q, | ||
'search.com': Q, | ||
'yam.com': 'k', | ||
'kvasir.no': 'q', | ||
'terra.com': 'query', | ||
'mynet.com': 'q', | ||
'kvasir.no': Q, | ||
'terra.com': QUERY, | ||
'mynet.com': Q, | ||
'rambler.ru': 'words', | ||
'google': 'q', | ||
'google': Q, | ||
'bing.com': { | ||
'p': 'q', | ||
'p': Q, | ||
'n': 'live' | ||
@@ -468,42 +413,23 @@ } | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
function uuid() { | ||
var lut = []; | ||
for (var i = 0; i < 256; i++) { | ||
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | ||
} | ||
return _typeof(obj); | ||
return function () { | ||
var d0 = genNumber(); | ||
var d1 = genNumber(); | ||
var d2 = genNumber(); | ||
var d3 = genNumber(); | ||
/* eslint-disable */ | ||
return "".concat(lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff], "-").concat(lut[d1 & 0xff]).concat(lut[d1 >> 8 & 0xff], "-").concat(lut[d1 >> 16 & 0x0f | 0x40]).concat(lut[d1 >> 24 & 0xff], "-").concat(lut[d2 & 0x3f | 0x80]).concat(lut[d2 >> 8 & 0xff], "-").concat(lut[d2 >> 16 & 0xff]).concat(lut[d2 >> 24 & 0xff]).concat(lut[d3 & 0xff]).concat(lut[d3 >> 8 & 0xff]).concat(lut[d3 >> 16 & 0xff]).concat(lut[d3 >> 24 & 0xff]); | ||
/* eslint-enable */ | ||
}(); | ||
} | ||
function parse(input) { | ||
var value; | ||
try { | ||
value = JSON.parse(input); | ||
if (typeof value === 'undefined') { | ||
value = input; | ||
} | ||
if (value === 'true') { | ||
value = true; | ||
} | ||
if (value === 'false') { | ||
value = false; | ||
} | ||
if (parseFloat(value) === value && _typeof(value) !== 'object') { | ||
value = parseFloat(value); | ||
} | ||
} catch (e) { | ||
value = input; | ||
} | ||
return value; | ||
function genNumber() { | ||
return Math.random() * 0xffffffff | 0; | ||
} | ||
@@ -541,184 +467,2 @@ | ||
/** | ||
* Check if browser has access to LocalStorage | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function hasLocalStorage() { | ||
if (!inBrowser) return false; | ||
try { | ||
if (typeof localStorage === 'undefined' || typeof JSON === 'undefined') { | ||
return false; | ||
} // test for safari private | ||
localStorage.setItem('_test_', '1'); | ||
localStorage.removeItem('_test_'); | ||
} catch (err) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
var hasLocalStorage$1 = hasLocalStorage(); | ||
/** | ||
* Get storage item from localStorage, cookie, or window | ||
* @param {string} key - key of item to get | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @return {Any} the value of key | ||
*/ | ||
function getItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return null; | ||
var storage = getStorageType(options); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
var value = localStorage.getItem(key); | ||
if (value || storage === 'localStorage') return parse(value); | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
var _value = getCookie(key); | ||
if (_value || storage === 'cookie') return parse(_value); | ||
} | ||
/* 3. Fallback to window/global. TODO verify AWS lambda & check for conflicts */ | ||
return globalContext[key] || null; | ||
} | ||
/** | ||
* Store values in localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {*} value - value of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @returns {object} returns old value, new values, & location of storage | ||
*/ | ||
function setItem(key, value) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
if (!key || !value) return false; | ||
var storage = getStorageType(options); | ||
var saveValue = JSON.stringify(value); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
// console.log('SET as localstorage', saveValue) | ||
var _oldValue = parse(localStorage.getItem(key)); | ||
localStorage.setItem(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue, | ||
type: 'localStorage' | ||
}; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
// console.log('SET as cookie', saveValue) | ||
var _oldValue2 = parse(getCookie(key)); | ||
setCookie(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue2, | ||
type: 'cookie' | ||
}; | ||
} | ||
/* 3. Fallback to window/global */ | ||
var oldValue = globalContext[key]; // console.log('SET as window', value) | ||
globalContext[key] = value; | ||
return { | ||
value: value, | ||
oldValue: oldValue, | ||
type: 'window' | ||
}; | ||
} | ||
/** | ||
* Remove values from localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
*/ | ||
function removeItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return false; | ||
var storage = getStorageType(options); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
localStorage.removeItem(key); | ||
return null; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
removeCookie(key); | ||
return null; | ||
} | ||
/* 3. Fallback to window/global */ | ||
globalContext[key] = null; | ||
return null; | ||
} | ||
function getStorageType(options) { | ||
return typeof options === 'string' ? options : options.storage; | ||
} | ||
function useLocal(storage) { | ||
return hasLocalStorage$1 && (!storage || storage === 'localStorage'); | ||
} | ||
function useCookie(storage) { | ||
return cookiesSupported && (!storage || storage === 'cookie'); | ||
} | ||
var index = { | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem | ||
}; | ||
function uuid() { | ||
var lut = []; | ||
for (var i = 0; i < 256; i++) { | ||
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | ||
} | ||
return function () { | ||
var d0 = genNumber(); | ||
var d1 = genNumber(); | ||
var d2 = genNumber(); | ||
var d3 = genNumber(); | ||
/* eslint-disable */ | ||
return "".concat(lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff], "-").concat(lut[d1 & 0xff]).concat(lut[d1 >> 8 & 0xff], "-").concat(lut[d1 >> 16 & 0x0f | 0x40]).concat(lut[d1 >> 24 & 0xff], "-").concat(lut[d2 & 0x3f | 0x80]).concat(lut[d2 >> 8 & 0xff], "-").concat(lut[d2 >> 16 & 0xff]).concat(lut[d2 >> 24 & 0xff]).concat(lut[d3 & 0xff]).concat(lut[d3 >> 8 & 0xff]).concat(lut[d3 >> 16 & 0xff]).concat(lut[d3 >> 24 & 0xff]); | ||
/* eslint-enable */ | ||
}(); | ||
} | ||
function genNumber() { | ||
return Math.random() * 0xffffffff | 0; | ||
} | ||
export { cookie, decode as decodeUri, getBrowserLocale, getTimeZone, inBrowser, isExternalReferrer, isScriptLoaded, noOp, paramsClean, getValueParamValue as paramsGet, paramsParse, paramsRemove, parseReferrer, index as storage, url, uuid, globalContext }; | ||
export { decode as decodeUri, getBrowserLocale, getTimeZone, inBrowser, isExternalReferrer, isScriptLoaded, noOp, paramsClean, getValueParamValue as paramsGet, paramsParse, paramsRemove, parseReferrer, url, uuid, globalContext }; |
@@ -5,71 +5,17 @@ 'use strict'; | ||
var inBrowser = typeof window !== 'undefined'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
/** | ||
* Check if browser has access to cookies | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
var storageUtils = require('@analytics/storage-utils'); | ||
var storageUtils__default = _interopDefault(storageUtils); | ||
function hasCookies() { | ||
function decode(s) { | ||
try { | ||
if (!inBrowser) return false; | ||
var key = 'cookietest='; // Try to set cookie | ||
document.cookie = "".concat(key, "1"); | ||
var cookiesEnabled = document.cookie.indexOf(key) !== -1; // Cleanup cookie | ||
document.cookie = "".concat(key, "1; expires=Thu, 01-Jan-1970 00:00:01 GMT"); | ||
return cookiesEnabled; | ||
return decodeURIComponent(s.replace(/\+/g, ' ')); | ||
} catch (e) { | ||
return false; | ||
return null; | ||
} | ||
} | ||
var cookiesSupported = hasCookies(); | ||
function setCookie(name, value, days) { | ||
if (!cookiesSupported) return false; | ||
var expires = ''; | ||
var inBrowser = typeof window !== 'undefined'; | ||
if (days) { | ||
var date = new Date(); | ||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); | ||
expires = "; expires=".concat(date.toGMTString()); | ||
} | ||
document.cookie = "".concat(name, "=").concat(value).concat(expires, "; path=/"); | ||
} | ||
function getCookie(name) { | ||
if (!cookiesSupported) return false; | ||
var find = "".concat(name, "="); | ||
var allCookies = document.cookie.split(';'); | ||
for (var i = 0; i < allCookies.length; i++) { | ||
var cookie = allCookies[i]; | ||
while (cookie.charAt(0) === ' ') { | ||
cookie = cookie.substring(1, cookie.length); | ||
} | ||
if (cookie.indexOf(find) === 0) { | ||
return cookie.substring(find.length, cookie.length); | ||
} | ||
} | ||
return null; | ||
} | ||
function removeCookie(name) { | ||
if (!cookiesSupported) return false; | ||
setCookie(name, '', -1); | ||
} | ||
var cookie = { | ||
getCookie: getCookie, | ||
setCookie: setCookie, | ||
removeCookie: removeCookie | ||
}; | ||
function decode(s) { | ||
return decodeURIComponent(s).replace(/\+/g, ' '); | ||
} | ||
function getBrowserLocale() { | ||
@@ -154,3 +100,3 @@ if (!inBrowser) return null; | ||
function paramsClean(url, param) { | ||
var search = (url.split('?') || [,])[1]; | ||
var search = (url.split('?') || [,])[1]; // eslint-disable-line | ||
@@ -162,3 +108,3 @@ if (!search || search.indexOf(param) === -1) { | ||
var regex = new RegExp("(\\&|\\?)".concat(param, "([_A-Za-z0-9\"+=.%]+)"), 'g'); | ||
var regex = new RegExp("(\\&|\\?)".concat(param, "([_A-Za-z0-9\"+=.\\/\\-@%]+)"), 'g'); | ||
var cleanSearch = "?".concat(search).replace(regex, '').replace(/^&/, '?'); // replace search params with clean params | ||
@@ -314,3 +260,3 @@ | ||
* @example | ||
* getDomainHost('https://subdomain.my-site.com/') | ||
* getDomainBase('https://subdomain.my-site.com/') | ||
* > my-site.com | ||
@@ -445,25 +391,27 @@ */ | ||
var Q = 'q'; | ||
var QUERY = 'query'; | ||
var searchEngines = { | ||
'daum.net': 'q', | ||
'daum.net': Q, | ||
'eniro.se': 'search_word', | ||
'naver.com': 'query', | ||
'naver.com': QUERY, | ||
'yahoo.com': 'p', | ||
'msn.com': 'q', | ||
'aol.com': 'q', | ||
'lycos.com': 'q', | ||
'ask.com': 'q', | ||
'cnn.com': 'query', | ||
'msn.com': Q, | ||
'aol.com': Q, | ||
'lycos.com': Q, | ||
'ask.com': Q, | ||
'cnn.com': QUERY, | ||
'about.com': 'terms', | ||
'baidu.com': 'wd', | ||
'yandex.com': 'text', | ||
'seznam.cz': 'q', | ||
'search.com': 'q', | ||
'seznam.cz': Q, | ||
'search.com': Q, | ||
'yam.com': 'k', | ||
'kvasir.no': 'q', | ||
'terra.com': 'query', | ||
'mynet.com': 'q', | ||
'kvasir.no': Q, | ||
'terra.com': QUERY, | ||
'mynet.com': Q, | ||
'rambler.ru': 'words', | ||
'google': 'q', | ||
'google': Q, | ||
'bing.com': { | ||
'p': 'q', | ||
'p': Q, | ||
'n': 'live' | ||
@@ -473,42 +421,23 @@ } | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
function uuid() { | ||
var lut = []; | ||
for (var i = 0; i < 256; i++) { | ||
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | ||
} | ||
return _typeof(obj); | ||
return function () { | ||
var d0 = genNumber(); | ||
var d1 = genNumber(); | ||
var d2 = genNumber(); | ||
var d3 = genNumber(); | ||
/* eslint-disable */ | ||
return "".concat(lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff], "-").concat(lut[d1 & 0xff]).concat(lut[d1 >> 8 & 0xff], "-").concat(lut[d1 >> 16 & 0x0f | 0x40]).concat(lut[d1 >> 24 & 0xff], "-").concat(lut[d2 & 0x3f | 0x80]).concat(lut[d2 >> 8 & 0xff], "-").concat(lut[d2 >> 16 & 0xff]).concat(lut[d2 >> 24 & 0xff]).concat(lut[d3 & 0xff]).concat(lut[d3 >> 8 & 0xff]).concat(lut[d3 >> 16 & 0xff]).concat(lut[d3 >> 24 & 0xff]); | ||
/* eslint-enable */ | ||
}(); | ||
} | ||
function parse(input) { | ||
var value; | ||
try { | ||
value = JSON.parse(input); | ||
if (typeof value === 'undefined') { | ||
value = input; | ||
} | ||
if (value === 'true') { | ||
value = true; | ||
} | ||
if (value === 'false') { | ||
value = false; | ||
} | ||
if (parseFloat(value) === value && _typeof(value) !== 'object') { | ||
value = parseFloat(value); | ||
} | ||
} catch (e) { | ||
value = input; | ||
} | ||
return value; | ||
function genNumber() { | ||
return Math.random() * 0xffffffff | 0; | ||
} | ||
@@ -546,185 +475,6 @@ | ||
/** | ||
* Check if browser has access to LocalStorage | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function hasLocalStorage() { | ||
if (!inBrowser) return false; | ||
try { | ||
if (typeof localStorage === 'undefined' || typeof JSON === 'undefined') { | ||
return false; | ||
} // test for safari private | ||
localStorage.setItem('_test_', '1'); | ||
localStorage.removeItem('_test_'); | ||
} catch (err) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
var hasLocalStorage$1 = hasLocalStorage(); | ||
/** | ||
* Get storage item from localStorage, cookie, or window | ||
* @param {string} key - key of item to get | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @return {Any} the value of key | ||
*/ | ||
function getItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return null; | ||
var storage = getStorageType(options); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
var value = localStorage.getItem(key); | ||
if (value || storage === 'localStorage') return parse(value); | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
var _value = getCookie(key); | ||
if (_value || storage === 'cookie') return parse(_value); | ||
} | ||
/* 3. Fallback to window/global. TODO verify AWS lambda & check for conflicts */ | ||
return globalContext[key] || null; | ||
} | ||
/** | ||
* Store values in localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {*} value - value of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @returns {object} returns old value, new values, & location of storage | ||
*/ | ||
function setItem(key, value) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
if (!key || !value) return false; | ||
var storage = getStorageType(options); | ||
var saveValue = JSON.stringify(value); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
// console.log('SET as localstorage', saveValue) | ||
var _oldValue = parse(localStorage.getItem(key)); | ||
localStorage.setItem(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue, | ||
type: 'localStorage' | ||
}; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
// console.log('SET as cookie', saveValue) | ||
var _oldValue2 = parse(getCookie(key)); | ||
setCookie(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue2, | ||
type: 'cookie' | ||
}; | ||
} | ||
/* 3. Fallback to window/global */ | ||
var oldValue = globalContext[key]; // console.log('SET as window', value) | ||
globalContext[key] = value; | ||
return { | ||
value: value, | ||
oldValue: oldValue, | ||
type: 'window' | ||
}; | ||
} | ||
/** | ||
* Remove values from localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
*/ | ||
function removeItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return false; | ||
var storage = getStorageType(options); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
localStorage.removeItem(key); | ||
return null; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
removeCookie(key); | ||
return null; | ||
} | ||
/* 3. Fallback to window/global */ | ||
globalContext[key] = null; | ||
return null; | ||
} | ||
function getStorageType(options) { | ||
return typeof options === 'string' ? options : options.storage; | ||
} | ||
function useLocal(storage) { | ||
return hasLocalStorage$1 && (!storage || storage === 'localStorage'); | ||
} | ||
function useCookie(storage) { | ||
return cookiesSupported && (!storage || storage === 'cookie'); | ||
} | ||
var index = { | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem | ||
}; | ||
function uuid() { | ||
var lut = []; | ||
for (var i = 0; i < 256; i++) { | ||
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | ||
} | ||
return function () { | ||
var d0 = genNumber(); | ||
var d1 = genNumber(); | ||
var d2 = genNumber(); | ||
var d3 = genNumber(); | ||
/* eslint-disable */ | ||
return "".concat(lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff], "-").concat(lut[d1 & 0xff]).concat(lut[d1 >> 8 & 0xff], "-").concat(lut[d1 >> 16 & 0x0f | 0x40]).concat(lut[d1 >> 24 & 0xff], "-").concat(lut[d2 & 0x3f | 0x80]).concat(lut[d2 >> 8 & 0xff], "-").concat(lut[d2 >> 16 & 0xff]).concat(lut[d2 >> 24 & 0xff]).concat(lut[d3 & 0xff]).concat(lut[d3 >> 8 & 0xff]).concat(lut[d3 >> 16 & 0xff]).concat(lut[d3 >> 24 & 0xff]); | ||
/* eslint-enable */ | ||
}(); | ||
} | ||
function genNumber() { | ||
return Math.random() * 0xffffffff | 0; | ||
} | ||
exports.cookie = cookie; | ||
exports.storage = storageUtils__default; | ||
exports.getCookie = storageUtils.getCookie; | ||
exports.setCookie = storageUtils.setCookie; | ||
exports.removeCookie = storageUtils.removeCookie; | ||
exports.decodeUri = decode; | ||
@@ -742,5 +492,4 @@ exports.getBrowserLocale = getBrowserLocale; | ||
exports.parseReferrer = parseReferrer; | ||
exports.storage = index; | ||
exports.url = url; | ||
exports.uuid = uuid; | ||
exports.globalContext = globalContext; |
@@ -1,70 +0,13 @@ | ||
var inBrowser = typeof window !== 'undefined'; | ||
export { default as storage, getCookie, setCookie, removeCookie } from '@analytics/storage-utils'; | ||
/** | ||
* Check if browser has access to cookies | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function hasCookies() { | ||
function decode(s) { | ||
try { | ||
if (!inBrowser) return false; | ||
var key = 'cookietest='; // Try to set cookie | ||
document.cookie = "".concat(key, "1"); | ||
var cookiesEnabled = document.cookie.indexOf(key) !== -1; // Cleanup cookie | ||
document.cookie = "".concat(key, "1; expires=Thu, 01-Jan-1970 00:00:01 GMT"); | ||
return cookiesEnabled; | ||
return decodeURIComponent(s.replace(/\+/g, ' ')); | ||
} catch (e) { | ||
return false; | ||
return null; | ||
} | ||
} | ||
var cookiesSupported = hasCookies(); | ||
function setCookie(name, value, days) { | ||
if (!cookiesSupported) return false; | ||
var expires = ''; | ||
var inBrowser = typeof window !== 'undefined'; | ||
if (days) { | ||
var date = new Date(); | ||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); | ||
expires = "; expires=".concat(date.toGMTString()); | ||
} | ||
document.cookie = "".concat(name, "=").concat(value).concat(expires, "; path=/"); | ||
} | ||
function getCookie(name) { | ||
if (!cookiesSupported) return false; | ||
var find = "".concat(name, "="); | ||
var allCookies = document.cookie.split(';'); | ||
for (var i = 0; i < allCookies.length; i++) { | ||
var cookie = allCookies[i]; | ||
while (cookie.charAt(0) === ' ') { | ||
cookie = cookie.substring(1, cookie.length); | ||
} | ||
if (cookie.indexOf(find) === 0) { | ||
return cookie.substring(find.length, cookie.length); | ||
} | ||
} | ||
return null; | ||
} | ||
function removeCookie(name) { | ||
if (!cookiesSupported) return false; | ||
setCookie(name, '', -1); | ||
} | ||
var cookie = { | ||
getCookie: getCookie, | ||
setCookie: setCookie, | ||
removeCookie: removeCookie | ||
}; | ||
function decode(s) { | ||
return decodeURIComponent(s).replace(/\+/g, ' '); | ||
} | ||
function getBrowserLocale() { | ||
@@ -149,3 +92,3 @@ if (!inBrowser) return null; | ||
function paramsClean(url, param) { | ||
var search = (url.split('?') || [,])[1]; | ||
var search = (url.split('?') || [,])[1]; // eslint-disable-line | ||
@@ -157,3 +100,3 @@ if (!search || search.indexOf(param) === -1) { | ||
var regex = new RegExp("(\\&|\\?)".concat(param, "([_A-Za-z0-9\"+=.%]+)"), 'g'); | ||
var regex = new RegExp("(\\&|\\?)".concat(param, "([_A-Za-z0-9\"+=.\\/\\-@%]+)"), 'g'); | ||
var cleanSearch = "?".concat(search).replace(regex, '').replace(/^&/, '?'); // replace search params with clean params | ||
@@ -309,3 +252,3 @@ | ||
* @example | ||
* getDomainHost('https://subdomain.my-site.com/') | ||
* getDomainBase('https://subdomain.my-site.com/') | ||
* > my-site.com | ||
@@ -440,25 +383,27 @@ */ | ||
var Q = 'q'; | ||
var QUERY = 'query'; | ||
var searchEngines = { | ||
'daum.net': 'q', | ||
'daum.net': Q, | ||
'eniro.se': 'search_word', | ||
'naver.com': 'query', | ||
'naver.com': QUERY, | ||
'yahoo.com': 'p', | ||
'msn.com': 'q', | ||
'aol.com': 'q', | ||
'lycos.com': 'q', | ||
'ask.com': 'q', | ||
'cnn.com': 'query', | ||
'msn.com': Q, | ||
'aol.com': Q, | ||
'lycos.com': Q, | ||
'ask.com': Q, | ||
'cnn.com': QUERY, | ||
'about.com': 'terms', | ||
'baidu.com': 'wd', | ||
'yandex.com': 'text', | ||
'seznam.cz': 'q', | ||
'search.com': 'q', | ||
'seznam.cz': Q, | ||
'search.com': Q, | ||
'yam.com': 'k', | ||
'kvasir.no': 'q', | ||
'terra.com': 'query', | ||
'mynet.com': 'q', | ||
'kvasir.no': Q, | ||
'terra.com': QUERY, | ||
'mynet.com': Q, | ||
'rambler.ru': 'words', | ||
'google': 'q', | ||
'google': Q, | ||
'bing.com': { | ||
'p': 'q', | ||
'p': Q, | ||
'n': 'live' | ||
@@ -468,42 +413,23 @@ } | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
function uuid() { | ||
var lut = []; | ||
for (var i = 0; i < 256; i++) { | ||
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | ||
} | ||
return _typeof(obj); | ||
return function () { | ||
var d0 = genNumber(); | ||
var d1 = genNumber(); | ||
var d2 = genNumber(); | ||
var d3 = genNumber(); | ||
/* eslint-disable */ | ||
return "".concat(lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff], "-").concat(lut[d1 & 0xff]).concat(lut[d1 >> 8 & 0xff], "-").concat(lut[d1 >> 16 & 0x0f | 0x40]).concat(lut[d1 >> 24 & 0xff], "-").concat(lut[d2 & 0x3f | 0x80]).concat(lut[d2 >> 8 & 0xff], "-").concat(lut[d2 >> 16 & 0xff]).concat(lut[d2 >> 24 & 0xff]).concat(lut[d3 & 0xff]).concat(lut[d3 >> 8 & 0xff]).concat(lut[d3 >> 16 & 0xff]).concat(lut[d3 >> 24 & 0xff]); | ||
/* eslint-enable */ | ||
}(); | ||
} | ||
function parse(input) { | ||
var value; | ||
try { | ||
value = JSON.parse(input); | ||
if (typeof value === 'undefined') { | ||
value = input; | ||
} | ||
if (value === 'true') { | ||
value = true; | ||
} | ||
if (value === 'false') { | ||
value = false; | ||
} | ||
if (parseFloat(value) === value && _typeof(value) !== 'object') { | ||
value = parseFloat(value); | ||
} | ||
} catch (e) { | ||
value = input; | ||
} | ||
return value; | ||
function genNumber() { | ||
return Math.random() * 0xffffffff | 0; | ||
} | ||
@@ -541,184 +467,2 @@ | ||
/** | ||
* Check if browser has access to LocalStorage | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function hasLocalStorage() { | ||
if (!inBrowser) return false; | ||
try { | ||
if (typeof localStorage === 'undefined' || typeof JSON === 'undefined') { | ||
return false; | ||
} // test for safari private | ||
localStorage.setItem('_test_', '1'); | ||
localStorage.removeItem('_test_'); | ||
} catch (err) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
var hasLocalStorage$1 = hasLocalStorage(); | ||
/** | ||
* Get storage item from localStorage, cookie, or window | ||
* @param {string} key - key of item to get | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @return {Any} the value of key | ||
*/ | ||
function getItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return null; | ||
var storage = getStorageType(options); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
var value = localStorage.getItem(key); | ||
if (value || storage === 'localStorage') return parse(value); | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
var _value = getCookie(key); | ||
if (_value || storage === 'cookie') return parse(_value); | ||
} | ||
/* 3. Fallback to window/global. TODO verify AWS lambda & check for conflicts */ | ||
return globalContext[key] || null; | ||
} | ||
/** | ||
* Store values in localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {*} value - value of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
* @returns {object} returns old value, new values, & location of storage | ||
*/ | ||
function setItem(key, value) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
if (!key || !value) return false; | ||
var storage = getStorageType(options); | ||
var saveValue = JSON.stringify(value); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
// console.log('SET as localstorage', saveValue) | ||
var _oldValue = parse(localStorage.getItem(key)); | ||
localStorage.setItem(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue, | ||
type: 'localStorage' | ||
}; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
// console.log('SET as cookie', saveValue) | ||
var _oldValue2 = parse(getCookie(key)); | ||
setCookie(key, saveValue); | ||
return { | ||
value: value, | ||
oldValue: _oldValue2, | ||
type: 'cookie' | ||
}; | ||
} | ||
/* 3. Fallback to window/global */ | ||
var oldValue = globalContext[key]; // console.log('SET as window', value) | ||
globalContext[key] = value; | ||
return { | ||
value: value, | ||
oldValue: oldValue, | ||
type: 'window' | ||
}; | ||
} | ||
/** | ||
* Remove values from localStorage, cookie, or window | ||
* @param {string} key - key of item to set | ||
* @param {object|string} [options] - storage options. If string location of where to get storage | ||
* @param {string} [options.storage] - Define type of storage to pull from. | ||
*/ | ||
function removeItem(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!key) return false; | ||
var storage = getStorageType(options); | ||
/* 1. Try localStorage */ | ||
if (useLocal(storage)) { | ||
localStorage.removeItem(key); | ||
return null; | ||
} | ||
/* 2. Fallback to cookie */ | ||
if (useCookie(storage)) { | ||
removeCookie(key); | ||
return null; | ||
} | ||
/* 3. Fallback to window/global */ | ||
globalContext[key] = null; | ||
return null; | ||
} | ||
function getStorageType(options) { | ||
return typeof options === 'string' ? options : options.storage; | ||
} | ||
function useLocal(storage) { | ||
return hasLocalStorage$1 && (!storage || storage === 'localStorage'); | ||
} | ||
function useCookie(storage) { | ||
return cookiesSupported && (!storage || storage === 'cookie'); | ||
} | ||
var index = { | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem | ||
}; | ||
function uuid() { | ||
var lut = []; | ||
for (var i = 0; i < 256; i++) { | ||
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | ||
} | ||
return function () { | ||
var d0 = genNumber(); | ||
var d1 = genNumber(); | ||
var d2 = genNumber(); | ||
var d3 = genNumber(); | ||
/* eslint-disable */ | ||
return "".concat(lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff], "-").concat(lut[d1 & 0xff]).concat(lut[d1 >> 8 & 0xff], "-").concat(lut[d1 >> 16 & 0x0f | 0x40]).concat(lut[d1 >> 24 & 0xff], "-").concat(lut[d2 & 0x3f | 0x80]).concat(lut[d2 >> 8 & 0xff], "-").concat(lut[d2 >> 16 & 0xff]).concat(lut[d2 >> 24 & 0xff]).concat(lut[d3 & 0xff]).concat(lut[d3 >> 8 & 0xff]).concat(lut[d3 >> 16 & 0xff]).concat(lut[d3 >> 24 & 0xff]); | ||
/* eslint-enable */ | ||
}(); | ||
} | ||
function genNumber() { | ||
return Math.random() * 0xffffffff | 0; | ||
} | ||
export { cookie, decode as decodeUri, getBrowserLocale, getTimeZone, inBrowser, isExternalReferrer, isScriptLoaded, noOp, paramsClean, getValueParamValue as paramsGet, paramsParse, paramsRemove, parseReferrer, index as storage, url, uuid, globalContext }; | ||
export { decode as decodeUri, getBrowserLocale, getTimeZone, inBrowser, isExternalReferrer, isScriptLoaded, noOp, paramsClean, getValueParamValue as paramsGet, paramsParse, paramsRemove, parseReferrer, url, uuid, globalContext }; |
{ | ||
"name": "analytics-utils", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Analytics utility functions used by 'analytics' module", | ||
@@ -59,3 +59,6 @@ "author": "David Wells <hello@davidwells.io>", | ||
}, | ||
"gitHead": "0608c76283e53842be0ca5a54533e5c011c53734" | ||
"dependencies": { | ||
"@analytics/storage-utils": "^0.2.2" | ||
}, | ||
"gitHead": "bb1865b49b78e9aae3acac4018c46cea35ef2695" | ||
} |
85476
1
2192
+ Added@analytics/cookie-utils@0.2.12(transitive)
+ Added@analytics/global-storage-utils@0.1.7(transitive)
+ Added@analytics/localstorage-utils@0.1.10(transitive)
+ Added@analytics/storage-utils@0.2.17(transitive)
+ Added@analytics/type-utils@0.5.40.6.2(transitive)