UNPKG

owtlab-tracking

Version:
213 lines (180 loc) 5.38 kB
import Emitter from 'component-emitter'; import each from './utils/each'; import extend from './utils/extend'; import parseParams from './utils/parse-params'; import serialize from './utils/serialize'; (function (env) { const previousOwtlab = env.Owtlab || undefined; function Client(props) { if (this instanceof Client === false) { return new Client(props); } this.configure(props); // Set up event handling if (Client.debug) { this.on('error', Client.log); } this.emit('ready'); Client.emit('client', this); } if (previousOwtlab && typeof previousOwtlab.resources === 'undefined') { Client.legacyVersion = previousOwtlab; } Emitter(Client); Emitter(Client.prototype); extend(Client, { debug: false, enabled: true, loaded: false, version: '__VERSION__', }); // Set or extend helpers Client.helpers = Client.helpers || {}; // Set or extend resources Client.resources = Client.resources || {}; extend(Client.resources, { base: '{protocol}://{host}', version: '{protocol}://{host}', projects: '{protocol}://{host}', client_id: '{protocol}://{host}', events: '{protocol}://{host}', queries: '{protocol}://{host}', datasets: '{protocol}://{host}', }); // Set or extend utils Client.utils = Client.utils || {}; extend(Client.utils, { each, extend, parseParams, serialize, }); Client.extendLibrary = function (target, source) { const previous = previousOwtlab || source; if (isDefined(previous) && isDefined(previous.resources)) { each(previous, (value, key) => { if (typeof value === 'object') { target[key] = target[key] || {}; extend(target[key], value); } else { target[key] = target[key] || value; } }); extend(target.prototype, previous.prototype); } return target; }; Client.log = function (str) { if (Client.debug && typeof console === 'object') { console.log('[Owtlab]', str); } }; Client.noConflict = function () { if (typeof env.Owtlab !== 'undefined') { env.Owtlab = Client.legacyVersion || previousOwtlab; } return Client; }; Client.ready = function (fn) { if (Client.loaded) { fn(); } else { Client.once('ready', fn); } }; Client.prototype.configure = function (obj) { const config = obj || {}; this.config = this.config || { client_id: undefined, client_secret: undefined, host: 'us-central1-owtlab-dev.cloudfunctions.net/storeEvents', protocol: 'https', requestType: 'jsonp', resources: extend({}, Client.resources), }; // IE<10 request shim if (typeof window !== 'undefined' && window.navigator && window.navigator.userAgent && window.navigator.userAgent.indexOf('MSIE') > -1) { config.protocol = document.location.protocol.replace(':', ''); } if (config.host) { config.host.replace(/.*?:\/\//g, ''); } extend(this.config, config); return this; }; Client.prototype.masterKey = function (str) { if (!arguments.length) return this.config.masterKey; this.config.masterKey = str ? String(str) : null; return this; }; Client.prototype.client_id = function (str) { if (!arguments.length) return this.config.client_id; this.config.client_id = (str ? String(str) : null); return this; }; Client.prototype.resources = function (obj) { if (!arguments.length) return this.config.resources; const self = this; if (typeof obj === 'object') { each(obj, (value, key) => { self.config.resources[key] = (value || null); }); } return self; }; Client.prototype.url = function (name) { const args = Array.prototype.slice.call(arguments, 1); const baseUrl = this.config.resources.base || '{protocol}://{host}'; let path; if (name && typeof name === 'string') { if (this.config.resources[name]) { path = this.config.resources[name]; } else { path = baseUrl + name; } } else { path = baseUrl; } each(this.config, (value, key) => { if (typeof value !== 'object') { path = path.replace(`{${key}}`, value); } }); return path; }; domReady(() => { Client.loaded = true; Client.emit('ready'); }); function domReady(fn) { if (Client.loaded || typeof document === 'undefined') { fn(); return; } // Firefox 3.5 shim if (document.readyState == null && document.addEventListener) { document.addEventListener('DOMContentLoaded', function DOMContentLoaded() { document.removeEventListener('DOMContentLoaded', DOMContentLoaded, false); document.readyState = 'complete'; }, false); document.readyState = 'loading'; } testDom(fn); } function testDom(fn) { if (/in/.test(document.readyState)) { setTimeout(() => { testDom(fn); }, 9); } else { fn(); } } function isDefined(target) { return typeof target !== 'undefined'; } function isUndefined(target) { return typeof target === 'undefined'; } module.exports = Client; }).call(this, typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {});