analytics-event
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -42,2 +42,540 @@ (function (global, factory) { | ||
var namespaceId = function namespaceId(event, options) { | ||
if (typeof options['getters.namespaceId'] === 'function') { | ||
return options['getters.namespaceId'](event, options); | ||
} | ||
return event.namespaceId || event.nsc_id || event.namespace; | ||
}; | ||
var _typeof$1 = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
var slicedToArray$1 = function () { | ||
function sliceIterator(arr, i) { | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"]) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
return function (arr, i) { | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} else if (Symbol.iterator in Object(arr)) { | ||
return sliceIterator(arr, i); | ||
} else { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
} | ||
}; | ||
}(); | ||
var toConsumableArray$1 = function (arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} else { | ||
return Array.from(arr); | ||
} | ||
}; | ||
var log$1 = function log(_ref) { | ||
var debug = _ref.debug; | ||
return function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
if (debug) { | ||
var _console; | ||
var msg = args[0], | ||
otherArgs = args.slice(1); | ||
(_console = console).log.apply(_console, ["analyticsId." + msg].concat(toConsumableArray$1(otherArgs))); | ||
} | ||
}; | ||
}; | ||
var dec2hex = []; | ||
for (var i = 0; i <= 15; i++) { | ||
dec2hex.push(i.toString(16)); | ||
} | ||
function uuid() { | ||
var value = ''; | ||
for (var i = 1; i <= 36; i++) { | ||
if (i === 9 || i === 14 || i === 19 || i === 24) { | ||
value += '-'; | ||
} else if (i === 15) { | ||
value += 4; | ||
} else if (i === 20) { | ||
value += dec2hex[Math.random() * 4 | 0 + 8]; | ||
} else { | ||
value += dec2hex[Math.random() * 15 | 0]; | ||
} | ||
} | ||
return value; | ||
} | ||
var uuidV4 = uuid; | ||
var segment = { | ||
stores: ['cookie', 'localStorage'], | ||
key: 'ajs_user_id', | ||
generateId: function generateId(_ref) { | ||
var _ref$prefix = _ref.prefix, | ||
prefix = _ref$prefix === undefined ? 'ajs' : _ref$prefix; | ||
return [prefix, uuidV4()].join('-'); | ||
}, | ||
mutate: true, | ||
persist: true | ||
}; | ||
var presets = { | ||
segment: segment | ||
}; | ||
function getAll() { | ||
var cookie = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
var cookieParts = cookie.split('; '); | ||
var cookies = {}; | ||
if (cookieParts.length === 0 || cookieParts.length === 1 && cookieParts[0] === '') { | ||
return cookies; | ||
} | ||
for (var i = 0; i < cookieParts.length; i++) { | ||
var _cookieParts$i$split = cookieParts[i].split('='), | ||
_cookieParts$i$split2 = slicedToArray$1(_cookieParts$i$split, 2), | ||
key = _cookieParts$i$split2[0], | ||
value = _cookieParts$i$split2[1]; | ||
cookies[key] = decodeURIComponent(value); | ||
} | ||
return cookies; | ||
} | ||
function get$1() { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var key = opts.key, | ||
cookie = opts.cookie; | ||
var cookies = getAll(cookie); | ||
return cookies[key]; | ||
} | ||
function browserSet(_ref) { | ||
var name = _ref.name, | ||
value = _ref.value, | ||
expires = _ref.expires, | ||
domain = _ref.domain, | ||
path = _ref.path, | ||
secure = _ref.secure; | ||
var valueToUse = void 0; | ||
if (value !== undefined && (typeof value === 'undefined' ? 'undefined' : _typeof$1(value)) === 'object') valueToUse = JSON.stringify(value);else valueToUse = encodeURIComponent(value); | ||
document.cookie = name + '=' + valueToUse + (expires ? '; expires=' + new Date(expires).toUTCString() : '') + '; path=' + (path || '/') + (domain ? '; domain=' + domain : '') + (secure ? '; secure' : ''); | ||
} | ||
function set$1() { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var key = opts.key, | ||
id = opts.id, | ||
mutate = opts.mutate, | ||
domain = opts.domain; | ||
var cookie = opts.cookie; | ||
var isBrowser = false; | ||
try { | ||
cookie = document.cookie; | ||
isBrowser = true; | ||
} catch (err) { | ||
// silence dom error | ||
cookie = ''; | ||
} | ||
if (isBrowser) { | ||
browserSet({ name: key, value: id, path: '/', domain: domain }); | ||
} | ||
if (!mutate) { | ||
cookie = '' + cookie; | ||
} | ||
var newCookie = key + '=' + encodeURIComponent(id); | ||
return [cookie, newCookie].filter(Boolean).join('; '); | ||
} | ||
var cookie = { | ||
get: get$1, | ||
set: set$1 | ||
}; | ||
function get$2() { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var key = opts.key, | ||
_opts$localStorage = opts.localStorage, | ||
localStorage = _opts$localStorage === undefined ? {} : _opts$localStorage; | ||
return localStorage.getItem ? localStorage.getItem(key) : localStorage[key]; | ||
} | ||
function set$2() { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var key = opts.key, | ||
id = opts.id, | ||
_opts$localStorage2 = opts.localStorage, | ||
localStorage = _opts$localStorage2 === undefined ? {} : _opts$localStorage2, | ||
mutate = opts.mutate; | ||
var storage = mutate ? localStorage : Object.assign({}, localStorage); | ||
if (storage.setItem) { | ||
storage.setItem(key, id); | ||
} else { | ||
storage[key] = id; | ||
} | ||
return storage; | ||
} | ||
var localStorage = { | ||
get: get$2, | ||
set: set$2 | ||
}; | ||
var stores = { | ||
get: { | ||
cookie: cookie.get, | ||
localStorage: localStorage.get | ||
}, | ||
set: { | ||
cookie: cookie.set, | ||
localStorage: localStorage.set | ||
} | ||
}; | ||
var storeSetters = stores.set; | ||
var set$3 = function set() { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var id = opts.id, | ||
debug = opts.debug, | ||
_opts$preset = opts.preset, | ||
preset = _opts$preset === undefined ? 'segment' : _opts$preset, | ||
cookie = opts.cookie, | ||
localStorage = opts.localStorage, | ||
domain = opts.domain, | ||
prefix = opts.prefix; | ||
var log$$1 = log$1({ debug: debug }); | ||
if (!presets[preset]) { | ||
// TODO: notify unknown preset | ||
preset = 'segment'; | ||
} | ||
var _presets$preset = presets[preset], | ||
generateId = _presets$preset.generateId, | ||
key = _presets$preset.key, | ||
stores$$1 = _presets$preset.stores, | ||
mutate = _presets$preset.mutate; | ||
if (!id) { | ||
id = generateId({ prefix: prefix }); | ||
log$$1('set: created new id {id: "' + id + '"}'); | ||
} else { | ||
log$$1('set: using {id: "' + id + '"}'); | ||
} | ||
var result = void 0; | ||
if (!localStorage) { | ||
try { | ||
localStorage = window.localStorage; | ||
} catch (err) { | ||
// silence window error | ||
} | ||
} | ||
try { | ||
result = storeId({ | ||
debug: debug, | ||
key: key, | ||
id: id, | ||
stores: stores$$1, | ||
mutate: mutate, | ||
cookie: cookie, | ||
localStorage: localStorage, | ||
domain: domain | ||
}); | ||
result.status = 'success'; | ||
} catch (error) { | ||
log$$1('set: error', error); | ||
result = { | ||
status: 'error', | ||
error: error | ||
}; | ||
} | ||
return result; | ||
}; | ||
function storeId() { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var debug = opts.debug, | ||
key = opts.key, | ||
id = opts.id, | ||
stores$$1 = opts.stores, | ||
mutate = opts.mutate, | ||
cookie = opts.cookie, | ||
localStorage = opts.localStorage, | ||
domain = opts.domain; | ||
var result = {}; | ||
var log$$1 = log$1({ debug: debug }); | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = stores$$1[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var store = _step.value; | ||
try { | ||
var storeSet = storeSetters[store]; | ||
var value = storeSet({ key: key, id: id, mutate: mutate, cookie: cookie, localStorage: localStorage, domain: domain }); | ||
if (value) { | ||
log$$1('set.' + store + ': success {id: "' + id + '"}'); | ||
result[store] = value; | ||
} else { | ||
log$$1('set.' + store + ': fail {id: "' + id + '"}'); | ||
} | ||
} catch (err) { | ||
// TODO: Handle unknown error trying to store id | ||
log$$1('set.' + store + ': fail {id: "' + id + '"}'); | ||
console.error(err); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
var storeGetters = stores.get; | ||
var get$3 = function get() { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var debug = opts.debug, | ||
_opts$preset = opts.preset, | ||
preset = _opts$preset === undefined ? 'segment' : _opts$preset, | ||
_opts$env = opts.env, | ||
env = _opts$env === undefined ? '' : _opts$env, | ||
cookie = opts.cookie, | ||
localStorage = opts.localStorage, | ||
prefix = opts.prefix; | ||
var log$$1 = log$1({ debug: debug }); | ||
if (!presets[preset]) { | ||
// TODO: notify unknown preset | ||
preset = 'segment'; | ||
} | ||
var _presets$preset = presets[preset], | ||
generateId = _presets$preset.generateId, | ||
key = _presets$preset.key, | ||
stores$$1 = _presets$preset.stores, | ||
persist = _presets$preset.persist; | ||
if (isBrowser({ env: env })) { | ||
if (!localStorage) { | ||
try { | ||
localStorage = window.localStorage; | ||
} catch (err) { | ||
// silence error | ||
} | ||
} | ||
if (!cookie) { | ||
try { | ||
cookie = document.cookie; | ||
} catch (err) { | ||
// silence error | ||
} | ||
} | ||
var browserId = getBrowserId({ | ||
debug: debug, | ||
stores: stores$$1, | ||
key: key, | ||
cookie: cookie, | ||
localStorage: localStorage | ||
}); | ||
if (browserId) { | ||
log$$1('get: found browser id {id: "' + browserId + '"}'); | ||
return browserId; | ||
} | ||
} | ||
var newId = generateId({ prefix: prefix }); | ||
if (persist) { | ||
log$$1('get: persisting new id {id: "' + newId + '"}'); | ||
set$3({ | ||
debug: debug, | ||
key: key, | ||
id: newId, | ||
// pass references directly | ||
cookie: opts.cookie, | ||
localStorage: opts.localStorage | ||
}); | ||
} | ||
return newId; | ||
}; | ||
function isBrowser() { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var env = opts.env; | ||
return env === 'browser' || typeof window !== 'undefined'; | ||
} | ||
function getBrowserId(_ref) { | ||
var debug = _ref.debug, | ||
stores$$1 = _ref.stores, | ||
key = _ref.key, | ||
cookie = _ref.cookie, | ||
localStorage = _ref.localStorage; | ||
var log$$1 = log$1({ debug: debug }); | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = stores$$1[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var store = _step.value; | ||
var storeGet = storeGetters[store]; | ||
var value = storeGet({ key: key, cookie: cookie, localStorage: localStorage }); | ||
if (value) { | ||
log$$1('get.browser: hit {store: "' + store + '", key: "' + key + '"}'); | ||
return value; | ||
} else { | ||
log$$1('get.browser: miss {store: "' + store + '", key: "' + key + '"}'); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return ''; | ||
} | ||
function analyticsId(opts) { | ||
if (typeof opts === 'string' || typeof opts === 'number') { | ||
return set$3({ id: opts }); | ||
} else if ((typeof opts === 'undefined' ? 'undefined' : _typeof$1(opts)) === 'object') { | ||
if (typeof opts.id !== 'undefined') { | ||
return set$3(opts); | ||
} else { | ||
return get$3(opts); | ||
} | ||
} else { | ||
return get$3(); | ||
} | ||
} | ||
analyticsId.get = get$3; | ||
analyticsId.set = set$3; | ||
var src = analyticsId; | ||
var index_es = /*#__PURE__*/Object.freeze({ | ||
default: src | ||
}); | ||
var analyticsId$1 = ( index_es && src ) || index_es; | ||
var messageId = function messageId(event, options) { | ||
if (typeof options['getters.messageId'] === 'function') { | ||
return options['getters.messageId'](event, options); | ||
} | ||
return event.messageId || event.originalMessageId || analyticsId$1(); | ||
}; | ||
var properties = function getProperties(event) { | ||
@@ -48,3 +586,27 @@ // TODO: handle combining properties and traits | ||
var name = function getName(event) { | ||
var timestamp = function timestamp(event, options) { | ||
if (typeof options['getters.timestamp'] === 'function') { | ||
return options['getters.timestamp'](event, options); | ||
} | ||
var inputTimestamp = event.timestamp || event.ts; | ||
try { | ||
var d = new Date(inputTimestamp); | ||
return d.toISOString(); | ||
} catch (err) { | ||
if (inputTimestamp) { | ||
return inputTimestamp; | ||
} | ||
} | ||
return new Date().toISOString(); | ||
}; | ||
var name = function getName(event, options) { | ||
if (typeof options['getters.name'] === 'function') { | ||
return options['getters.name'](event, options); | ||
} | ||
return event.event || event.name || event.Event; | ||
@@ -54,4 +616,7 @@ }; | ||
var getters = { | ||
properties: properties, | ||
name: name | ||
name: name, | ||
timestamp: timestamp, | ||
messageId: messageId, | ||
namespaceId: namespaceId, | ||
properties: properties | ||
}; | ||
@@ -61,12 +626,50 @@ | ||
var load = function loadFormat(event, options) { | ||
log('loadFormat', 'not implemented'); | ||
var short_1 = function formatShort(event) { | ||
var newEvent = {}; | ||
if (event.name) { | ||
newEvent.name = event.name; | ||
} | ||
if (event.timestamp) { | ||
newEvent.ts = event.timestamp; | ||
} | ||
if (event.messageId) { | ||
newEvent.msg_id = event.messageId; | ||
} | ||
if (event.namespaceId) { | ||
newEvent.nsc_id = event.namespaceId; | ||
} | ||
if (event.properties) { | ||
newEvent.props = event.properties; | ||
} | ||
return newEvent; | ||
}; | ||
var formatByType = { | ||
short: short_1 | ||
}; | ||
function format(event, options) { | ||
log('format', 'not implemented'); | ||
var formatter = formatByType[options.format]; | ||
return formatter(event, options); | ||
} | ||
format.load = load; | ||
format.load = function (formatType, formatter) { | ||
if (!formatter) { | ||
return log.error('format.load', 'missing formatter function'); | ||
} | ||
if (formatByType[formatType]) { | ||
return log.error('format.load', 'format type already exists'); | ||
} | ||
formatByType[formatType] = formatter; | ||
}; | ||
var format_1 = format; | ||
@@ -76,2 +679,3 @@ | ||
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -82,4 +686,7 @@ // TODO: handle DX around input | ||
// - provide handler to trigger alert on invalid event | ||
var eventName = getters_1.name(input); | ||
var eventProps = getters_1.properties(input); | ||
var namespaceId = getters_1.namespaceId(input, options); | ||
var messageId = getters_1.messageId(input, options); | ||
var timestamp = getters_1.timestamp(input, options); | ||
var eventName = getters_1.name(input, options); | ||
var eventProps = getters_1.properties(input, options); | ||
@@ -96,13 +703,38 @@ var newEvent = {}; | ||
if (timestamp) { | ||
newEvent.timestamp = timestamp; | ||
} | ||
if (namespaceId) { | ||
newEvent.namespaceId = namespaceId; | ||
} | ||
if (messageId) { | ||
newEvent.messageId = messageId; | ||
} | ||
if (options.format) { | ||
return format_1(newEvent, options); | ||
} | ||
return newEvent; | ||
} | ||
function withOptions() { | ||
var overrides = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return function (input, options) { | ||
return analyticsEvent(input, Object.assign({}, options, overrides)); | ||
}; | ||
} | ||
analyticsEvent.format = format_1; | ||
analyticsEvent.validate = validate_1; | ||
analyticsEvent.loadFormat = format_1.load.bind(format_1); | ||
analyticsEvent.validate = validate_1; | ||
analyticsEvent.withOptions = withOptions; | ||
var src = analyticsEvent; | ||
var src$1 = analyticsEvent; | ||
return src; | ||
return src$1; | ||
}))); |
@@ -1,1 +0,1 @@ | ||
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):n.AnalyticsEvent=r()}(this,function(){"use strict";var t=function(n){var i=n.debug;return function(){for(var n=arguments.length,r=Array(n),t=0;t<n;t++)r[t]=arguments[t];if(i){var e,o=r[0],a=r.slice(1);(e=console).log.apply(e,["analyticsEvent."+o].concat(function(n){if(Array.isArray(n)){for(var r=0,t=Array(n.length);r<n.length;r++)t[r]=n[r];return t}return Array.from(n)}(a)))}}};var n=function(n,r){t("validate")},o={properties:function(n){return n.properties||n.traits},name:function(n){return n.event||n.name||n.Event}};function r(n,r){t("format")}r.load=function(n,r){t("loadFormat")};var e=r;function a(){var n=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},r=o.name(n),t=o.properties(n),e={};return r&&(e.name=r),t&&(e.properties=t),e}return a.format=e,a.loadFormat=e.load.bind(e),a.validate=n,a}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.AnalyticsEvent=t()}(this,function(){"use strict";var r=function(e){var i=e.debug;return function(){for(var e=arguments.length,t=Array(e),r=0;r<e;r++)t[r]=arguments[r];if(i){var o,n=t[0],a=t.slice(1);(o=console).log.apply(o,["analyticsEvent."+n].concat(function(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}(a)))}}};for(var e=function(e,t){r("validate")},v="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},s=function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var r=[],o=!0,n=!1,a=void 0;try{for(var i,s=e[Symbol.iterator]();!(o=(i=s.next()).done)&&(r.push(i.value),!t||r.length!==t);o=!0);}catch(e){n=!0,a=e}finally{try{!o&&s.return&&s.return()}finally{if(n)throw a}}return r}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")},b=function(e){var i=e.debug;return function(){for(var e=arguments.length,t=Array(e),r=0;r<e;r++)t[r]=arguments[r];if(i){var o,n=t[0],a=t.slice(1);(o=console).log.apply(o,["analyticsId."+n].concat(function(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}(a)))}}},o=[],t=0;t<=15;t++)o.push(t.toString(16));var n=function(){for(var e="",t=1;t<=36;t++)e+=9===t||14===t||19===t||24===t?"-":15===t?4:20===t?o[4*Math.random()|8]:o[15*Math.random()|0];return e},p={segment:{stores:["cookie","localStorage"],key:"ajs_user_id",generateId:function(e){var t=e.prefix;return[void 0===t?"ajs":t,n()].join("-")},mutate:!0,persist:!0}};var a={get:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=e.key;return function(){var e=(0<arguments.length&&void 0!==arguments[0]?arguments[0]:"").split("; "),t={};if(0===e.length||1===e.length&&""===e[0])return t;for(var r=0;r<e.length;r++){var o=e[r].split("="),n=s(o,2),a=n[0],i=n[1];t[a]=decodeURIComponent(i)}return t}(e.cookie)[t]},set:function(){var e,t,r,o,n,a,i,s,c=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},u=c.key,d=c.id,f=c.mutate,l=c.domain,g=c.cookie,m=!1;try{g=document.cookie,m=!0}catch(e){g=""}return m&&(t=(e={name:u,value:d,path:"/",domain:l}).name,r=e.value,o=e.expires,n=e.domain,a=e.path,i=e.secure,s=(s=void 0)!==r&&"object"===(void 0===r?"undefined":v(r))?JSON.stringify(r):encodeURIComponent(r),document.cookie=t+"="+s+(o?"; expires="+new Date(o).toUTCString():"")+"; path="+(a||"/")+(n?"; domain="+n:"")+(i?"; secure":"")),f||(g=""+g),[g,u+"="+encodeURIComponent(d)].filter(Boolean).join("; ")}};var i={get:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=e.key,r=e.localStorage,o=void 0===r?{}:r;return o.getItem?o.getItem(t):o[t]},set:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=e.key,r=e.id,o=e.localStorage,n=void 0===o?{}:o,a=e.mutate?n:Object.assign({},n);return a.setItem?a.setItem(t,r):a[t]=r,a}},c={get:{cookie:a.get,localStorage:i.get},set:{cookie:a.set,localStorage:i.set}},k=c.set,h=function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=e.id,r=e.debug,o=e.preset,n=void 0===o?"segment":o,a=e.cookie,i=e.localStorage,s=e.domain,c=e.prefix,u=b({debug:r});p[n]||(n="segment");var d=p[n],f=d.generateId,l=d.key,g=d.stores,m=d.mutate;u(t?'set: using {id: "'+t+'"}':'set: created new id {id: "'+(t=f({prefix:c}))+'"}');var v=void 0;if(!i)try{i=window.localStorage}catch(e){}try{(v=function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=e.debug,r=e.key,o=e.id,n=e.stores,a=e.mutate,i=e.cookie,s=e.localStorage,c=e.domain,u={},d=b({debug:t}),f=!0,l=!1,g=void 0;try{for(var m,v=n[Symbol.iterator]();!(f=(m=v.next()).done);f=!0){var y=m.value;try{var p=k[y],h=p({key:r,id:o,mutate:a,cookie:i,localStorage:s,domain:c});h?(d("set."+y+': success {id: "'+o+'"}'),u[y]=h):d("set."+y+': fail {id: "'+o+'"}')}catch(e){d("set."+y+': fail {id: "'+o+'"}'),console.error(e)}}}catch(e){l=!0,g=e}finally{try{!f&&v.return&&v.return()}finally{if(l)throw g}}return u}({debug:r,key:l,id:t,stores:g,mutate:m,cookie:a,localStorage:i,domain:s})).status="success"}catch(e){u("set: error",e),v={status:"error",error:e}}return v};var S=c.get,u=function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=e.debug,r=e.preset,o=void 0===r?"segment":r,n=e.env,a=void 0===n?"":n,i=e.cookie,s=e.localStorage,c=e.prefix,u=b({debug:t});p[o]||(o="segment");var d=p[o],f=d.generateId,l=d.key,g=d.stores,m=d.persist;if(function(){return"browser"===(0<arguments.length&&void 0!==arguments[0]?arguments[0]:{}).env||"undefined"!=typeof window}({env:a})){if(!s)try{s=window.localStorage}catch(e){}if(!i)try{i=document.cookie}catch(e){}var v=function(e){var t=e.debug,r=e.stores,o=e.key,n=e.cookie,a=e.localStorage,i=b({debug:t}),s=!0,c=!1,u=void 0;try{for(var d,f=r[Symbol.iterator]();!(s=(d=f.next()).done);s=!0){var l=d.value,g=S[l],m=g({key:o,cookie:n,localStorage:a});if(m)return i('get.browser: hit {store: "'+l+'", key: "'+o+'"}'),m;i('get.browser: miss {store: "'+l+'", key: "'+o+'"}')}}catch(e){c=!0,u=e}finally{try{!s&&f.return&&f.return()}finally{if(c)throw u}}return""}({debug:t,stores:g,key:l,cookie:i,localStorage:s});if(v)return u('get: found browser id {id: "'+v+'"}'),v}var y=f({prefix:c});return m&&(u('get: persisting new id {id: "'+y+'"}'),h({debug:t,key:l,id:y,cookie:e.cookie,localStorage:e.localStorage})),y};function d(e){return"string"==typeof e||"number"==typeof e?h({id:e}):"object"===(void 0===e?"undefined":v(e))?void 0!==e.id?h(e):u(e):u()}d.get=u,d.set=h;var f=d,l=Object.freeze({default:f}),g=l&&f||l,m={name:function(e,t){return"function"==typeof t["getters.name"]?t["getters.name"](e,t):e.event||e.name||e.Event},timestamp:function(e,t){if("function"==typeof t["getters.timestamp"])return t["getters.timestamp"](e,t);var r=e.timestamp||e.ts;try{return new Date(r).toISOString()}catch(e){if(r)return r}return(new Date).toISOString()},messageId:function(e,t){return"function"==typeof t["getters.messageId"]?t["getters.messageId"](e,t):e.messageId||e.originalMessageId||g()},namespaceId:function(e,t){return"function"==typeof t["getters.namespaceId"]?t["getters.namespaceId"](e,t):e.namespaceId||e.nsc_id||e.namespace},properties:function(e){return e.properties||e.traits}},y={short:function(e){var t={};return e.name&&(t.name=e.name),e.timestamp&&(t.ts=e.timestamp),e.messageId&&(t.msg_id=e.messageId),e.namespaceId&&(t.nsc_id=e.namespaceId),e.properties&&(t.props=e.properties),t}};function I(e,t){return(0,y[t.format])(e,t)}I.load=function(e,t){return t?y[e]?r.error("format.load","format type already exists"):void(y[e]=t):r.error("format.load","missing formatter function")};var w=I;function x(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},r=m.namespaceId(e,t),o=m.messageId(e,t),n=m.timestamp(e,t),a=m.name(e,t),i=m.properties(e,t),s={};return a&&(s.name=a),i&&(s.properties=i),n&&(s.timestamp=n),r&&(s.namespaceId=r),o&&(s.messageId=o),t.format?w(s,t):s}return x.format=w,x.validate=e,x.loadFormat=w.load.bind(w),x.withOptions=function(){var r=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};return function(e,t){return x(e,Object.assign({},t,r))}},x}); |
@@ -0,1 +1,3 @@ | ||
import analyticsId from 'analytics-id'; | ||
var toConsumableArray = function (arr) { | ||
@@ -36,2 +38,18 @@ if (Array.isArray(arr)) { | ||
var namespaceId = function namespaceId(event, options) { | ||
if (typeof options['getters.namespaceId'] === 'function') { | ||
return options['getters.namespaceId'](event, options); | ||
} | ||
return event.namespaceId || event.nsc_id || event.namespace; | ||
}; | ||
var messageId = function messageId(event, options) { | ||
if (typeof options['getters.messageId'] === 'function') { | ||
return options['getters.messageId'](event, options); | ||
} | ||
return event.messageId || event.originalMessageId || analyticsId(); | ||
}; | ||
var properties = function getProperties(event) { | ||
@@ -42,3 +60,27 @@ // TODO: handle combining properties and traits | ||
var name = function getName(event) { | ||
var timestamp = function timestamp(event, options) { | ||
if (typeof options['getters.timestamp'] === 'function') { | ||
return options['getters.timestamp'](event, options); | ||
} | ||
var inputTimestamp = event.timestamp || event.ts; | ||
try { | ||
var d = new Date(inputTimestamp); | ||
return d.toISOString(); | ||
} catch (err) { | ||
if (inputTimestamp) { | ||
return inputTimestamp; | ||
} | ||
} | ||
return new Date().toISOString(); | ||
}; | ||
var name = function getName(event, options) { | ||
if (typeof options['getters.name'] === 'function') { | ||
return options['getters.name'](event, options); | ||
} | ||
return event.event || event.name || event.Event; | ||
@@ -48,4 +90,7 @@ }; | ||
var getters = { | ||
properties: properties, | ||
name: name | ||
name: name, | ||
timestamp: timestamp, | ||
messageId: messageId, | ||
namespaceId: namespaceId, | ||
properties: properties | ||
}; | ||
@@ -55,12 +100,50 @@ | ||
var load = function loadFormat(event, options) { | ||
log('loadFormat', 'not implemented'); | ||
var short_1 = function formatShort(event) { | ||
var newEvent = {}; | ||
if (event.name) { | ||
newEvent.name = event.name; | ||
} | ||
if (event.timestamp) { | ||
newEvent.ts = event.timestamp; | ||
} | ||
if (event.messageId) { | ||
newEvent.msg_id = event.messageId; | ||
} | ||
if (event.namespaceId) { | ||
newEvent.nsc_id = event.namespaceId; | ||
} | ||
if (event.properties) { | ||
newEvent.props = event.properties; | ||
} | ||
return newEvent; | ||
}; | ||
var formatByType = { | ||
short: short_1 | ||
}; | ||
function format(event, options) { | ||
log('format', 'not implemented'); | ||
var formatter = formatByType[options.format]; | ||
return formatter(event, options); | ||
} | ||
format.load = load; | ||
format.load = function (formatType, formatter) { | ||
if (!formatter) { | ||
return log.error('format.load', 'missing formatter function'); | ||
} | ||
if (formatByType[formatType]) { | ||
return log.error('format.load', 'format type already exists'); | ||
} | ||
formatByType[formatType] = formatter; | ||
}; | ||
var format_1 = format; | ||
@@ -70,2 +153,3 @@ | ||
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -76,4 +160,7 @@ // TODO: handle DX around input | ||
// - provide handler to trigger alert on invalid event | ||
var eventName = getters_1.name(input); | ||
var eventProps = getters_1.properties(input); | ||
var namespaceId = getters_1.namespaceId(input, options); | ||
var messageId = getters_1.messageId(input, options); | ||
var timestamp = getters_1.timestamp(input, options); | ||
var eventName = getters_1.name(input, options); | ||
var eventProps = getters_1.properties(input, options); | ||
@@ -90,8 +177,33 @@ var newEvent = {}; | ||
if (timestamp) { | ||
newEvent.timestamp = timestamp; | ||
} | ||
if (namespaceId) { | ||
newEvent.namespaceId = namespaceId; | ||
} | ||
if (messageId) { | ||
newEvent.messageId = messageId; | ||
} | ||
if (options.format) { | ||
return format_1(newEvent, options); | ||
} | ||
return newEvent; | ||
} | ||
function withOptions() { | ||
var overrides = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return function (input, options) { | ||
return analyticsEvent(input, Object.assign({}, options, overrides)); | ||
}; | ||
} | ||
analyticsEvent.format = format_1; | ||
analyticsEvent.validate = validate_1; | ||
analyticsEvent.loadFormat = format_1.load.bind(format_1); | ||
analyticsEvent.validate = validate_1; | ||
analyticsEvent.withOptions = withOptions; | ||
@@ -98,0 +210,0 @@ var src = analyticsEvent; |
134
lib/index.js
'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var analyticsId = _interopDefault(require('analytics-id')); | ||
var toConsumableArray = function (arr) { | ||
@@ -38,2 +42,18 @@ if (Array.isArray(arr)) { | ||
var namespaceId = function namespaceId(event, options) { | ||
if (typeof options['getters.namespaceId'] === 'function') { | ||
return options['getters.namespaceId'](event, options); | ||
} | ||
return event.namespaceId || event.nsc_id || event.namespace; | ||
}; | ||
var messageId = function messageId(event, options) { | ||
if (typeof options['getters.messageId'] === 'function') { | ||
return options['getters.messageId'](event, options); | ||
} | ||
return event.messageId || event.originalMessageId || analyticsId(); | ||
}; | ||
var properties = function getProperties(event) { | ||
@@ -44,3 +64,27 @@ // TODO: handle combining properties and traits | ||
var name = function getName(event) { | ||
var timestamp = function timestamp(event, options) { | ||
if (typeof options['getters.timestamp'] === 'function') { | ||
return options['getters.timestamp'](event, options); | ||
} | ||
var inputTimestamp = event.timestamp || event.ts; | ||
try { | ||
var d = new Date(inputTimestamp); | ||
return d.toISOString(); | ||
} catch (err) { | ||
if (inputTimestamp) { | ||
return inputTimestamp; | ||
} | ||
} | ||
return new Date().toISOString(); | ||
}; | ||
var name = function getName(event, options) { | ||
if (typeof options['getters.name'] === 'function') { | ||
return options['getters.name'](event, options); | ||
} | ||
return event.event || event.name || event.Event; | ||
@@ -50,4 +94,7 @@ }; | ||
var getters = { | ||
properties: properties, | ||
name: name | ||
name: name, | ||
timestamp: timestamp, | ||
messageId: messageId, | ||
namespaceId: namespaceId, | ||
properties: properties | ||
}; | ||
@@ -57,12 +104,50 @@ | ||
var load = function loadFormat(event, options) { | ||
log('loadFormat', 'not implemented'); | ||
var short_1 = function formatShort(event) { | ||
var newEvent = {}; | ||
if (event.name) { | ||
newEvent.name = event.name; | ||
} | ||
if (event.timestamp) { | ||
newEvent.ts = event.timestamp; | ||
} | ||
if (event.messageId) { | ||
newEvent.msg_id = event.messageId; | ||
} | ||
if (event.namespaceId) { | ||
newEvent.nsc_id = event.namespaceId; | ||
} | ||
if (event.properties) { | ||
newEvent.props = event.properties; | ||
} | ||
return newEvent; | ||
}; | ||
var formatByType = { | ||
short: short_1 | ||
}; | ||
function format(event, options) { | ||
log('format', 'not implemented'); | ||
var formatter = formatByType[options.format]; | ||
return formatter(event, options); | ||
} | ||
format.load = load; | ||
format.load = function (formatType, formatter) { | ||
if (!formatter) { | ||
return log.error('format.load', 'missing formatter function'); | ||
} | ||
if (formatByType[formatType]) { | ||
return log.error('format.load', 'format type already exists'); | ||
} | ||
formatByType[formatType] = formatter; | ||
}; | ||
var format_1 = format; | ||
@@ -72,2 +157,3 @@ | ||
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -78,4 +164,7 @@ // TODO: handle DX around input | ||
// - provide handler to trigger alert on invalid event | ||
var eventName = getters_1.name(input); | ||
var eventProps = getters_1.properties(input); | ||
var namespaceId = getters_1.namespaceId(input, options); | ||
var messageId = getters_1.messageId(input, options); | ||
var timestamp = getters_1.timestamp(input, options); | ||
var eventName = getters_1.name(input, options); | ||
var eventProps = getters_1.properties(input, options); | ||
@@ -92,8 +181,33 @@ var newEvent = {}; | ||
if (timestamp) { | ||
newEvent.timestamp = timestamp; | ||
} | ||
if (namespaceId) { | ||
newEvent.namespaceId = namespaceId; | ||
} | ||
if (messageId) { | ||
newEvent.messageId = messageId; | ||
} | ||
if (options.format) { | ||
return format_1(newEvent, options); | ||
} | ||
return newEvent; | ||
} | ||
function withOptions() { | ||
var overrides = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return function (input, options) { | ||
return analyticsEvent(input, Object.assign({}, options, overrides)); | ||
}; | ||
} | ||
analyticsEvent.format = format_1; | ||
analyticsEvent.validate = validate_1; | ||
analyticsEvent.loadFormat = format_1.load.bind(format_1); | ||
analyticsEvent.validate = validate_1; | ||
analyticsEvent.withOptions = withOptions; | ||
@@ -100,0 +214,0 @@ var src = analyticsEvent; |
{ | ||
"name": "analytics-event", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Utility for working with analytics events server-side and in the browser.", | ||
@@ -55,3 +55,5 @@ "main": "lib/index.js", | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"analytics-id": "^1.0.2" | ||
} | ||
} |
<p align="center"> | ||
<a href="https://www.npmjs.com/package/analytics-event"><img src="./docs/images/banner.png?cache=1" alt="analyticsEvent" /></a> | ||
<a href="https://www.npmjs.com/package/analytics-event"><img src="./docs/images/banner.png?cache=2" alt="analyticsEvent" /></a> | ||
</p> | ||
@@ -13,6 +13,6 @@ | ||
<a href="https://unpkg.com/analytics-event/dist/analytics-event.min.js"> | ||
<img src="https://img.badgesize.io/https://unpkg.com/analytics-event/dist/analytics-event.min.js?compression=gzip&label=analytics--id&cache=2"> | ||
<img src="https://img.badgesize.io/https://unpkg.com/analytics-event/dist/analytics-event.min.js?compression=gzip&label=analytics--event&cache=3"> | ||
</a> | ||
<a href="https://www.npmjs.com/package/analytics-event"> | ||
<img src="https://img.shields.io/npm/v/analytics-event.svg?maxAge=3600&label=analytics-event&colorB=007ec6"> | ||
<img src="https://img.shields.io/npm/v/analytics-event.svg?maxAge=3600&label=analytics-event&colorB=007ec6&cache=3"> | ||
</a> | ||
@@ -42,6 +42,6 @@ </p> | ||
```javascript | ||
import Event from 'analytics-event' | ||
import AEvent from 'analytics-event' | ||
// generate event from options | ||
Event({ | ||
// Generate event from options | ||
AEvent({ | ||
name: 'User Signed Up', | ||
@@ -54,3 +54,3 @@ properties: { | ||
// Use your favorite analytics library to send events | ||
analytics.track(Event({ | ||
analytics.track(AEvent({ | ||
name: 'User Signed Up', | ||
@@ -68,6 +68,6 @@ properties: { | ||
```javascript | ||
import Event from 'analytics-event' | ||
import AEvent from 'analytics-event' | ||
// Generate an event that matches https://schema.org/AnalyticsEvent | ||
Event({ | ||
// Generate an event that follows https://schema.org/AnalyticsEvent | ||
AEvent({ | ||
name: 'User Signed Up', | ||
@@ -83,3 +83,4 @@ properties: { | ||
```javascript | ||
import Event from 'analytics-event' | ||
import AEvent from 'analytics-event' | ||
// You can also just import the format function with: | ||
@@ -89,6 +90,6 @@ // import { format, loadFormat } from 'analytics-event' | ||
loadFormat('internal-data', internalDataFormat) | ||
AEvent.loadFormat('internal-data', internalDataFormat) | ||
function receiveIncomingEvents(batch) { | ||
return batch.map(msg => Event.format(msg, { | ||
return batch.map(msg => AEvent.format(msg, { | ||
preset: 'internal-data' | ||
@@ -95,0 +96,0 @@ })) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
57797
919
97
1
1
+ Addedanalytics-id@^1.0.2
+ Addedanalytics-id@1.3.1(transitive)