diff --git a/lib/analytics.js b/lib/analytics.js index 099255d..62a1ddc 100644 --- a/lib/analytics.js +++ b/lib/analytics.js @@ -1,192 +1,172 @@ 'use strict'; -this.OstrioTrackerClass; -(function() { - var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; - - var ScreenDetector = (function() { - function ScreenDetector() { - var _orientation; - this.screen = window.screen || screen; - this.height = this.screen.height; - this.width = this.screen.width; - this.colorDepth = this.screen.colorDepth; - this.pixelDepth = this.screen.pixelDepth; - _orientation = screen.orientation || screen.mozOrientation || screen.msOrientation; - if (_orientation && _orientation.type) { - this.orientation = _orientation.type.split('-')[0]; +;(function(root) { + var SD = (function() { + function SD() { + this.s = window.screen || screen || {}; + this.h = this.s.height; + this.w = this.s.width; + this.cD = this.s.colorDepth; + this.pD = this.s.pixelDepth; + var _rtt = this.s.orientation || this.s.mozOrientation || this.s.msOrientation || null; + if (_rtt && _rtt.type) { + this.rtt = _rtt.type.split('-')[0]; + if (!!~this.rtt.indexOf('landscape')) { + this.rtt = '-'; + } + if (!!~this.rtt.indexOf('portrait')) { + this.rtt = '|'; + } } else { - this.orientation = this.detect(); + this.rtt = this.d(); } } - ScreenDetector.prototype.detect = function() { - if (this.width >= this.height) { - return 'landscape'; + SD.prototype.d = function() { + if (this.w >= this.h) { + return '-'; } else { - return 'portrait'; + return '|'; } }; - ScreenDetector.prototype.toJson = function() { - var wW = window.innerWidth || window.clientHeight; - var wH = window.innerHeight || window.clientHeight; + SD.prototype.toJ = function() { var obj = {}; - if (this.width && this.height) { - obj.screen = this.width+'x'+this.height; - } - if (wW && wH) { - obj.window = wW+'x'+wH; - } - if (this.pixelDepth) { - obj.pixelDepth = this.pixelDepth; - } - if (this.colorDepth) { - obj.colorDepth = this.colorDepth; - } - if (this.orientation) { - obj.orientation = this.orientation; - } + obj['4a'] = this.w + 'x' + this.h; + obj['4b'] = (window.innerWidth || window.clientHeight) + 'x' + (window.innerHeight || window.clientHeight); + if (this.pD) { obj['4c'] = this.pD; } + if (this.cD) { obj['4d'] = this.cD; } + obj['4e'] = this.rtt; return JSON.stringify(obj); }; - - return ScreenDetector; + return SD; })(); - OstrioTrackerClass = (function() { - function OstrioTrackerClass(serverId) { - var self = this; - self.track = bind(self.track, self); - self.getHref = bind(self.getHref, self); - self.addEvent = bind(self.addEvent, self); - self.pushEvent = bind(self.pushEvent, self); - self.removeEvent = bind(self.removeEvent, self); - self.screen = encodeURIComponent((new ScreenDetector()).toJson()); - self.location = location || window.location || document.location; - self.currentHref = ''; - self.followClick = false; - self.serverId = serverId; - - var tracking = document.getElementById('__tracking__'); - if (!tracking) { - tracking = document.createElement("div"); - tracking.id = '__tracking__'; - tracking.style.position = 'absolute'; - tracking.style.top = '-1000px'; - tracking.style.left = '-1000px'; - document.body.appendChild(tracking); + var OTC = (function() { + function OTC(sid, auto) { + if (auto === undefined) { auto = true; } + this.scr = (new SD()).toJ(); + this.loc = location || window.location || document.location; + this.ch = ''; + this.fc = false; + this.sid = sid; + this.DNT = !!parseInt(navigator.doNotTrack || 0); + this.ver = 102; + + if (window.Image === undefined) { + this.iL = document.createElement('img'); + } else { + this.iL = new Image(); } - setTimeout(self.track, 1); - - var handleClick = function (e) { - var target = e.currentTarget; - if (target && target.href && target.href !== self.currentHref + '#' && target.href !== self.currentHref + '/#') { - self.followClick = true; - if (!!~target.href.indexOf(self.location.origin)) { - self.track('&event=click'); - } else { - self.track('&event=click&exitPage=' + self.getHref() + '&follow=' + target.href, false); - } + if (!this.DNT) { + if (auto) { + this._s(); } - }; + } else { + console.log('We are respect you decision to be not tracked. DNT policy: https://en.wikipedia.org/wiki/Do_Not_Track'); + console.log('For better experience we recommend to turn off DNT on your browser, more info: https://allaboutdnt.com/#adjust-settings'); + console.log('Sincerely, ostrio: https://ostr.io'); + } + } - self.listenLinks = function() { - var elements = Array.prototype.slice.call(document.getElementsByTagName('a')); - for (var i = elements.length - 1; i >= 0; i--) { - self.removeEvent(elements[i], 'click', handleClick); - self.addEvent(elements[i], 'click', handleClick); + OTC.prototype._hc = function hcl(e) { + var target = e.currentTarget; + if (target && target.href && target.href !== this.ch + '#' && target.href !== this.ch + '/#') { + this.fc = true; + if (!!~target.href.indexOf(this.loc.origin)) { + this.track(); + } else { + this.track('&7=' + this._gH() + '&8=' + target.href, false); } - }; + } + }; + + OTC.prototype._lL = function() { + var self = this; + var elements = Array.prototype.slice.call(document.getElementsByTagName('a')); + var handleClick = function (e) { self._hc(e); }; - self.listenLinks(); + for (var i = elements.length - 1; i >= 0; i--) { + this._rE(elements[i], 'click', handleClick); + this._aE(elements[i], 'click', handleClick); + } + }; + + OTC.prototype._s = function() { + var self = this; + setTimeout(function(){ return self.track.apply(self); }, 1); + self._lL(); var MO = window.MutationObserver || window.WebKitMutationObserver; if (MO) { var observer = new MO(function(mutations){ - - for (var i = mutations.length - 1; i >= 0; i--) { - for (var j = mutations[i].addedNodes.length - 1; j >= 0; j--) { - if (mutations[i].addedNodes[j].nodeName) { - if (mutations[i].addedNodes[j].nodeName === 'A') { - self.addEvent(mutations[i].addedNodes[j], 'click', handleClick); + for (var i = mutations.length - 1; i >= 0; i--) { + for (var j = mutations[i].addedNodes.length - 1; j >= 0; j--) { + if (mutations[i].addedNodes[j].nodeName) { + if (mutations[i].addedNodes[j].nodeName === 'A') { + self._aE(mutations[i].addedNodes[j], 'click', self.handleClick); + } } } - } - for (var k = mutations[i].removedNodes.length - 1; k >= 0; k--) { - if (mutations[i].removedNodes[k].nodeName) { - if (mutations[i].removedNodes[k].nodeName === 'A') { - self.removeEvent(mutations[i].removedNodes[k], 'click', handleClick); + for (var k = mutations[i].removedNodes.length - 1; k >= 0; k--) { + if (mutations[i].removedNodes[k].nodeName) { + if (mutations[i].removedNodes[k].nodeName === 'A') { + self._rE(mutations[i].removedNodes[k], 'click', self.handleClick); + } } } } - } }); observer.observe(document.body, {childList: true, subtree: true}); } else { - self.addEvent(document.body, 'DOMSubtreeModified', self.listenLinks); + self._aE(document.body, 'DOMSubtreeModified', self.lL); } - self.addEvent(window, 'hashchange', function() { - if (!self.followClick) { - self.track('&event=hashchange'); - } - }); - - self.addEvent(window, 'popstate', function() { - if (!self.followClick) { - self.track('&event=popstate'); - } - }); - - self.addEvent(window, 'beforeunload', function() { - if (!self.followClick) { - self.track('&exitPage=' + self.getHref(), false); + var hc = function() { + if (!self.fc) { + self.track(); } - }); + }; - self.addEvent(document, 'beforeunload', function() { - if (!self.followClick) { - self.track('&exitPage=' + self.getHref(), false); + var hx = function() { + if (!self.fc) { + self.track('&7=' + self._gH(), false); } - }); + }; - self.addEvent(document.body, 'beforeunload', function() { - if (!self.followClick) { - self.track('&exitPage=' + self.getHref(), false); - } - }); + self._aE(window, 'hashchange', hc); + self._aE(window, 'popstate', hc); + self._aE(window, 'beforeunload', hx); + self._aE(document, 'beforeunload', hx); + self._aE(document.body, 'beforeunload', hx); var _history = window.History || History || window.history || history; if (_history && _history.Adapter) { - _history.Adapter.bind(window, 'statechange', function() { - if (!self.followClick) { - self.track('&event=statechange'); - } - }); + _history.Adapter.bind(window, 'statechange', hc); } setInterval(function (){ - if (self.currentHref !== self.location.href && !self.followClick) { - self.track('&event=interval'); - self.listenLinks(); + if (self.ch !== self.loc.href && !self.fc) { + self.track(); + self._lL(); } }, 750); - var origWOE = window.onerror; + var origWoE = window.onerror; window.onerror = function(msg, url, line) { - self.pushEvent('[Global Error]', 'Error: ' + msg + ' File: ' + url + ' On line: ' + line); - if (origWOE) { - origWOE.apply(this, arguments); + self.pushEvent('[Global Error]', 'Error: ' + msg + '. File: ' + url.replace(self.loc.origin, '') + ' On line: ' + line); + if (origWoE) { + origWoE.apply(this, arguments); } }; - } + }; - OstrioTrackerClass.prototype.addEvent = function(obj, type, fn) { + OTC.prototype._aE = function(obj, type, fn) { if (obj.addEventListener) { obj.addEventListener(type, fn, false); } else { @@ -194,7 +174,7 @@ this.OstrioTrackerClass; } }; - OstrioTrackerClass.prototype.removeEvent = function(obj, type, fn) { + OTC.prototype._rE = function(obj, type, fn) { if (obj.removeEventListener) { obj.removeEventListener(type, fn, false); } else { @@ -202,52 +182,86 @@ this.OstrioTrackerClass; } }; - OstrioTrackerClass.prototype.pushEvent = function(key, value) { - if (key && value && key.length && value.length) { - var event = {}; - if (key === '[Global Error]') { - value = value.trim().substring(0, 512); + OTC.prototype.pushEvent = function(key, value) { + if (!this.DNT) { + if (key && value && key.length && value.length) { + var event = {}; + if (key === '[Global Error]') { + value = value.trim().substring(0, 512); + } else { + value = value.trim().substring(0, 64); + } + event[key.trim().substring(0, 24)] = value; + this.track('&3=' + JSON.stringify(event)); } else { - value = value.trim().substring(0, 64); + console.warn('[ostrio] [pushEvent]: Can\'t add event without key or value!'); } - event[key.trim().substring(0, 24)] = value; - this.track('&pushEvent=' + encodeURIComponent(JSON.stringify(event))); - } else { - console.warn('[ostrio] [tracking] [pushEvent]: Can\'t add event without key or value, both arguments must present, and be a {String}!'); } }; - OstrioTrackerClass.prototype.getHref = function() { - return encodeURIComponent(this.location.href); + OTC.prototype._gH = function() { + return this.loc.href; }; - OstrioTrackerClass.prototype.track = function(query, delay) { - if (!query) { query = ''; } - var self = this; - var insertTracker = function() { - if (self.currentHref !== self.location.href || !!~query.indexOf('follow') || !!~query.indexOf('pushEvent')) { - var page = self.getHref(); - var title = encodeURIComponent(document.title); - var referral = encodeURIComponent(document.referrer); - var locale = navigator.language || navigator.userLanguage || navigator.browserLanguage || navigator.systemLanguage || 'null'; - - document.getElementById('__tracking__').innerHTML = ''; - - self.currentHref = self.location.href; - setTimeout(function() { - self.followClick = false; - self.listenLinks(); - }, 25); - } - }; + OTC.prototype.track = function(q, delay) { + if (!this.DNT) { + if (!q) { q = ''; } + var self = this; + var iT = function() { + if (self.ch !== self.loc.href || /&(8|3)=/.test(q)) { + var query = '?'; + query += '6=' + encodeURIComponent(self._gH()); + + if (!~q.indexOf('&3=')) { + query += '&2=' + encodeURIComponent(document.title); + query += '&4=' + encodeURIComponent(self.scr); + if (document.referrer && !~document.referrer.indexOf(self.loc.origin)) { + query += '&1=' + encodeURIComponent(document.referrer); + } - if (delay === false) { - insertTracker(); - } else { - setTimeout(insertTracker, 1); + var locale = navigator.language || navigator.userLanguage || navigator.browserLanguage || navigator.systemLanguage || false; + + if (locale) { + query += '&5=' + encodeURIComponent(locale); + } + self.ch = self.loc.href; + } + + self.iL.src = 'https://analytics.ostr.io/' + self.sid + '.gif' + query + q + '&9=' + ((''+(+new Date())).substr(-7))+ '&v=' + self.ver; + + setTimeout(function() { + self.fc = false; + self._lL(); + }, 25); + } + }; + + if (delay === false) { + iT(); + } else { + setTimeout(iT, 26); + } } }; - return OstrioTrackerClass; + return OTC; })(); -}).call(this); \ No newline at end of file + + if (typeof exports !== 'undefined') { + if (typeof module !== 'undefined' && module.exports) { + exports = module.exports = OTC; + } + exports.OstrioTrackerClass = OTC; + } else if (typeof Meteor !== 'undefined') { + OstrioTrackerClass = OTC; + } else { + root.OstrioTrackerClass = OTC; + } + + if (typeof define == 'function' && define.amd) { + define('OstrioTrackerClass', [], function() { + return OTC; + }); + } + +}(this)); \ No newline at end of file diff --git a/lib/analytics.min.js b/lib/analytics.min.js index cd48bb7..f27ff12 100644 --- a/lib/analytics.min.js +++ b/lib/analytics.min.js @@ -1 +1 @@ -"use strict";var OstrioTrackerClass;(function(){var e=function(e,t){return function(){return e.apply(t,arguments)}},t=function(){function e(){var e;this.screen=window.screen||screen,this.height=this.screen.height,this.width=this.screen.width,this.colorDepth=this.screen.colorDepth,this.pixelDepth=this.screen.pixelDepth,e=screen.orientation||screen.mozOrientation||screen.msOrientation,e&&e.type?this.orientation=e.type.split("-")[0]:this.orientation=this.detect()}return e.prototype.detect=function(){return this.width>=this.height?"landscape":"portrait"},e.prototype.toJson=function(){var e=window.innerWidth||window.clientHeight,t=window.innerHeight||window.clientHeight,n={};return this.width&&this.height&&(n.screen=this.width+"x"+this.height),e&&t&&(n.window=e+"x"+t),this.pixelDepth&&(n.pixelDepth=this.pixelDepth),this.colorDepth&&(n.colorDepth=this.colorDepth),this.orientation&&(n.orientation=this.orientation),JSON.stringify(n)},e}();OstrioTrackerClass=function(){function n(n){var o=this;o.track=e(o.track,o),o.getHref=e(o.getHref,o),o.addEvent=e(o.addEvent,o),o.pushEvent=e(o.pushEvent,o),o.removeEvent=e(o.removeEvent,o),o.screen=encodeURIComponent((new t).toJson()),o.location=location||window.location||document.location,o.currentHref="",o.followClick=!1,o.serverId=n;var r=document.getElementById("__tracking__");r||(r=document.createElement("div"),r.id="__tracking__",r.style.position="absolute",r.style.top="-1000px",r.style.left="-1000px",document.body.appendChild(r)),setTimeout(o.track,1);var i=function(e){var t=e.currentTarget;t&&t.href&&t.href!==o.currentHref+"#"&&t.href!==o.currentHref+"/#"&&(o.followClick=!0,~t.href.indexOf(o.location.origin)?o.track("&event=click"):o.track("&event=click&exitPage="+o.getHref()+"&follow="+t.href,!1))};o.listenLinks=function(){for(var e=Array.prototype.slice.call(document.getElementsByTagName("a")),t=e.length-1;t>=0;t--)o.removeEvent(e[t],"click",i),o.addEvent(e[t],"click",i)},o.listenLinks();var a=window.MutationObserver||window.WebKitMutationObserver;if(a){var s=new a(function(e){for(var t=e.length-1;t>=0;t--){for(var n=e[t].addedNodes.length-1;n>=0;n--)e[t].addedNodes[n].nodeName&&"A"===e[t].addedNodes[n].nodeName&&o.addEvent(e[t].addedNodes[n],"click",i);for(var r=e[t].removedNodes.length-1;r>=0;r--)e[t].removedNodes[r].nodeName&&"A"===e[t].removedNodes[r].nodeName&&o.removeEvent(e[t].removedNodes[r],"click",i)}});s.observe(document.body,{childList:!0,subtree:!0})}else o.addEvent(document.body,"DOMSubtreeModified",o.listenLinks);o.addEvent(window,"hashchange",function(){o.followClick||o.track("&event=hashchange")}),o.addEvent(window,"popstate",function(){o.followClick||o.track("&event=popstate")}),o.addEvent(window,"beforeunload",function(){o.followClick||o.track("&exitPage="+o.getHref(),!1)}),o.addEvent(document,"beforeunload",function(){o.followClick||o.track("&exitPage="+o.getHref(),!1)}),o.addEvent(document.body,"beforeunload",function(){o.followClick||o.track("&exitPage="+o.getHref(),!1)});var c=window.History||History||window.history||history;c&&c.Adapter&&c.Adapter.bind(window,"statechange",function(){o.followClick||o.track("&event=statechange")}),setInterval(function(){o.currentHref===o.location.href||o.followClick||(o.track("&event=interval"),o.listenLinks())},750);var d=window.onerror;window.onerror=function(e,t,n){o.pushEvent("[Global Error]","Error: "+e+" File: "+t+" On line: "+n),d&&d.apply(this,arguments)}}return n.prototype.addEvent=function(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent("on"+t,n)},n.prototype.removeEvent=function(e,t,n){e.removeEventListener?e.removeEventListener(t,n,!1):e.detachEvent("on"+t,n)},n.prototype.pushEvent=function(e,t){if(e&&t&&e.length&&t.length){var n={};t="[Global Error]"===e?t.trim().substring(0,512):t.trim().substring(0,64),n[e.trim().substring(0,24)]=t,this.track("&pushEvent="+encodeURIComponent(JSON.stringify(n)))}else console.warn("[ostrio] [tracking] [pushEvent]: Can't add event without key or value, both arguments must present, and be a {String}!")},n.prototype.getHref=function(){return encodeURIComponent(this.location.href)},n.prototype.track=function(e,t){e||(e="");var n=this,o=function(){if(n.currentHref!==n.location.href||~e.indexOf("follow")||~e.indexOf("pushEvent")){var t=n.getHref(),o=encodeURIComponent(document.title),r=encodeURIComponent(document.referrer),i=navigator.language||navigator.userLanguage||navigator.browserLanguage||navigator.systemLanguage||"null";document.getElementById("__tracking__").innerHTML='',n.currentHref=n.location.href,setTimeout(function(){n.followClick=!1,n.listenLinks()},25)}};t===!1?o():setTimeout(o,1)},n}()}).call(this); \ No newline at end of file +"use strict";!function(t){var e=function(){function t(){this.s=window.screen||screen||{},this.h=this.s.height,this.w=this.s.width,this.cD=this.s.colorDepth,this.pD=this.s.pixelDepth;var t=this.s.orientation||this.s.mozOrientation||this.s.msOrientation||null;t&&t.type?(this.rtt=t.type.split("-")[0],~this.rtt.indexOf("landscape")&&(this.rtt="-"),~this.rtt.indexOf("portrait")&&(this.rtt="|")):this.rtt=this.d()}return t.prototype.d=function(){return this.w>=this.h?"-":"|"},t.prototype.toJ=function(){var t={};return t["4a"]=this.w+"x"+this.h,t["4b"]=(window.innerWidth||window.clientHeight)+"x"+(window.innerHeight||window.clientHeight),this.pD&&(t["4c"]=this.pD),this.cD&&(t["4d"]=this.cD),t["4e"]=this.rtt,JSON.stringify(t)},t}(),o=function(){function t(t,o){void 0===o&&(o=!0),this.scr=(new e).toJ(),this.loc=location||window.location||document.location,this.ch="",this.fc=!1,this.sid=t,this.DNT=!!parseInt(navigator.doNotTrack||0),this.ver=102,void 0===window.Image?this.iL=document.createElement("img"):this.iL=new Image,this.DNT?(console.log("We are respect you decision to be not tracked. DNT policy: https://en.wikipedia.org/wiki/Do_Not_Track"),console.log("For better experience we recommend to turn off DNT on your browser, more info: https://allaboutdnt.com/#adjust-settings"),console.log("Sincerely, ostrio: https://ostr.io")):o&&this._s()}return t.prototype._hc=function(t){var e=t.currentTarget;e&&e.href&&e.href!==this.ch+"#"&&e.href!==this.ch+"/#"&&(this.fc=!0,~e.href.indexOf(this.loc.origin)?this.track():this.track("&7="+this._gH()+"&8="+e.href,!1))},t.prototype._lL=function(){for(var t=this,e=Array.prototype.slice.call(document.getElementsByTagName("a")),o=function(e){t._hc(e)},i=e.length-1;i>=0;i--)this._rE(e[i],"click",o),this._aE(e[i],"click",o)},t.prototype._s=function(){var t=this;setTimeout(function(){return t.track.apply(t)},1),t._lL();var e=window.MutationObserver||window.WebKitMutationObserver;if(e){var o=new e(function(e){for(var o=e.length-1;o>=0;o--){for(var i=e[o].addedNodes.length-1;i>=0;i--)e[o].addedNodes[i].nodeName&&"A"===e[o].addedNodes[i].nodeName&&t._aE(e[o].addedNodes[i],"click",t.handleClick);for(var n=e[o].removedNodes.length-1;n>=0;n--)e[o].removedNodes[n].nodeName&&"A"===e[o].removedNodes[n].nodeName&&t._rE(e[o].removedNodes[n],"click",t.handleClick)}});o.observe(document.body,{childList:!0,subtree:!0})}else t._aE(document.body,"DOMSubtreeModified",t.lL);var i=function(){t.fc||t.track()},n=function(){t.fc||t.track("&7="+t._gH(),!1)};t._aE(window,"hashchange",i),t._aE(window,"popstate",i),t._aE(window,"beforeunload",n),t._aE(document,"beforeunload",n),t._aE(document.body,"beforeunload",n);var r=window.History||History||window.history||history;r&&r.Adapter&&r.Adapter.bind(window,"statechange",i),setInterval(function(){t.ch===t.loc.href||t.fc||(t.track(),t._lL())},750);var s=window.onerror;window.onerror=function(e,o,i){t.pushEvent("[Global Error]","Error: "+e+". File: "+o.replace(t.loc.origin,"")+" On line: "+i),s&&s.apply(this,arguments)}},t.prototype._aE=function(t,e,o){t.addEventListener?t.addEventListener(e,o,!1):t.attachEvent("on"+e,o)},t.prototype._rE=function(t,e,o){t.removeEventListener?t.removeEventListener(e,o,!1):t.detachEvent("on"+e,o)},t.prototype.pushEvent=function(t,e){if(!this.DNT)if(t&&e&&t.length&&e.length){var o={};e="[Global Error]"===t?e.trim().substring(0,512):e.trim().substring(0,64),o[t.trim().substring(0,24)]=e,this.track("&3="+JSON.stringify(o))}else console.warn("[ostrio] [pushEvent]: Can't add event without key or value!")},t.prototype._gH=function(){return this.loc.href},t.prototype.track=function(t,e){if(!this.DNT){t||(t="");var o=this,i=function(){if(o.ch!==o.loc.href||/&(8|3)=/.test(t)){var e="?";if(e+="6="+encodeURIComponent(o._gH()),!~t.indexOf("&3=")){e+="&2="+encodeURIComponent(document.title),e+="&4="+encodeURIComponent(o.scr),document.referrer&&!~document.referrer.indexOf(o.loc.origin)&&(e+="&1="+encodeURIComponent(document.referrer));var i=navigator.language||navigator.userLanguage||navigator.browserLanguage||navigator.systemLanguage||!1;i&&(e+="&5="+encodeURIComponent(i)),o.ch=o.loc.href}o.iL.src="https://analytics.ostr.io/"+o.sid+".gif"+e+t+"&9="+(""+ +new Date).substr(-7)+"&v="+o.ver,setTimeout(function(){o.fc=!1,o._lL()},25)}};e===!1?i():setTimeout(i,26)}},t}();"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=o),exports.OstrioTrackerClass=o):"undefined"!=typeof Meteor?OstrioTrackerClass=o:t.OstrioTrackerClass=o,"function"==typeof define&&define.amd&&define("OstrioTrackerClass",[],function(){return o})}(this); \ No newline at end of file diff --git a/src/analytics.js b/src/analytics.js deleted file mode 100644 index 5c71f5b..0000000 --- a/src/analytics.js +++ /dev/null @@ -1,254 +0,0 @@ -'use strict'; -(function() { - - var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; - - var ScreenDetector = (function() { - function ScreenDetector() { - var _orientation; - this.screen = window.screen || screen; - this.height = this.screen.height; - this.width = this.screen.width; - this.colorDepth = this.screen.colorDepth; - this.pixelDepth = this.screen.pixelDepth; - _orientation = screen.orientation || screen.mozOrientation || screen.msOrientation; - if (_orientation && _orientation.type) { - this.orientation = _orientation.type.split('-')[0]; - } else { - this.orientation = this.detect(); - } - } - - ScreenDetector.prototype.detect = function() { - if (this.width >= this.height) { - return 'landscape'; - } else { - return 'portrait'; - } - }; - - ScreenDetector.prototype.toJson = function() { - var wW = window.innerWidth || window.clientHeight; - var wH = window.innerHeight || window.clientHeight; - var obj = {}; - if (this.width && this.height) { - obj.screen = this.width+'x'+this.height; - } - if (wW && wH) { - obj.window = wW+'x'+wH; - } - if (this.pixelDepth) { - obj.pixelDepth = this.pixelDepth; - } - if (this.colorDepth) { - obj.colorDepth = this.colorDepth; - } - if (this.orientation) { - obj.orientation = this.orientation; - } - return JSON.stringify(obj); - }; - - return ScreenDetector; - })(); - - var OstrioTrackerClass; - module.exports = OstrioTrackerClass = (function() { - function OstrioTrackerClass(serverId) { - var self = this; - self.track = bind(self.track, self); - self.getHref = bind(self.getHref, self); - self.addEvent = bind(self.addEvent, self); - self.pushEvent = bind(self.pushEvent, self); - self.removeEvent = bind(self.removeEvent, self); - self.screen = encodeURIComponent((new ScreenDetector()).toJson()); - self.location = location || window.location || document.location; - self.currentHref = ''; - self.followClick = false; - self.serverId = serverId; - - var tracking = document.getElementById('__tracking__'); - if (!tracking) { - tracking = document.createElement("div"); - tracking.id = '__tracking__'; - tracking.style.position = 'absolute'; - tracking.style.top = '-1000px'; - tracking.style.left = '-1000px'; - document.body.appendChild(tracking); - } - - setTimeout(self.track, 1); - - var handleClick = function (e) { - var target = e.currentTarget; - if (target && target.href && target.href !== self.currentHref + '#' && target.href !== self.currentHref + '/#') { - self.followClick = true; - if (!!~target.href.indexOf(self.location.origin)) { - self.track('&event=click'); - } else { - self.track('&event=click&exitPage=' + self.getHref() + '&follow=' + target.href, false); - } - } - }; - - self.listenLinks = function() { - var elements = Array.prototype.slice.call(document.getElementsByTagName('a')); - for (var i = elements.length - 1; i >= 0; i--) { - self.removeEvent(elements[i], 'click', handleClick); - self.addEvent(elements[i], 'click', handleClick); - } - }; - - self.listenLinks(); - - var MO = window.MutationObserver || window.WebKitMutationObserver; - - if (MO) { - var observer = new MO(function(mutations){ - - for (var i = mutations.length - 1; i >= 0; i--) { - for (var j = mutations[i].addedNodes.length - 1; j >= 0; j--) { - if (mutations[i].addedNodes[j].nodeName) { - if (mutations[i].addedNodes[j].nodeName === 'A') { - self.addEvent(mutations[i].addedNodes[j], 'click', handleClick); - } - } - } - - for (var k = mutations[i].removedNodes.length - 1; k >= 0; k--) { - if (mutations[i].removedNodes[k].nodeName) { - if (mutations[i].removedNodes[k].nodeName === 'A') { - self.removeEvent(mutations[i].removedNodes[k], 'click', handleClick); - } - } - } - } - }); - observer.observe(document.body, {childList: true, subtree: true}); - - } else { - self.addEvent(document.body, 'DOMSubtreeModified', self.listenLinks); - } - - self.addEvent(window, 'hashchange', function() { - if (!self.followClick) { - self.track('&event=hashchange'); - } - }); - - self.addEvent(window, 'popstate', function() { - if (!self.followClick) { - self.track('&event=popstate'); - } - }); - - self.addEvent(window, 'beforeunload', function() { - if (!self.followClick) { - self.track('&exitPage=' + self.getHref(), false); - } - }); - - self.addEvent(document, 'beforeunload', function() { - if (!self.followClick) { - self.track('&exitPage=' + self.getHref(), false); - } - }); - - self.addEvent(document.body, 'beforeunload', function() { - if (!self.followClick) { - self.track('&exitPage=' + self.getHref(), false); - } - }); - - var _history = window.History || History || window.history || history; - - if (_history && _history.Adapter) { - _history.Adapter.bind(window, 'statechange', function() { - if (!self.followClick) { - self.track('&event=statechange'); - } - }); - } - - setInterval(function (){ - if (self.currentHref !== self.location.href && !self.followClick) { - self.track('&event=interval'); - self.listenLinks(); - } - }, 750); - - var origWOE = window.onerror; - - window.onerror = function(msg, url, line) { - self.pushEvent('[Global Error]', 'Error: ' + msg + ' File: ' + url + ' On line: ' + line); - if (origWOE) { - origWOE.apply(this, arguments); - } - }; - } - - OstrioTrackerClass.prototype.addEvent = function(obj, type, fn) { - if (obj.addEventListener) { - obj.addEventListener(type, fn, false); - } else { - obj.attachEvent('on' + type, fn); - } - }; - - OstrioTrackerClass.prototype.removeEvent = function(obj, type, fn) { - if (obj.removeEventListener) { - obj.removeEventListener(type, fn, false); - } else { - obj.detachEvent('on' + type, fn); - } - }; - - OstrioTrackerClass.prototype.pushEvent = function(key, value) { - if (key && value && key.length && value.length) { - var event = {}; - if (key === '[Global Error]') { - value = value.trim().substring(0, 512); - } else { - value = value.trim().substring(0, 64); - } - event[key.trim().substring(0, 24)] = value; - this.track('&pushEvent=' + encodeURIComponent(JSON.stringify(event))); - } else { - console.warn('[ostrio] [tracking] [pushEvent]: Can\'t add event without key or value, both arguments must present, and be a {String}!'); - } - }; - - OstrioTrackerClass.prototype.getHref = function() { - return encodeURIComponent(this.location.href); - }; - - OstrioTrackerClass.prototype.track = function(query, delay) { - if (!query) { query = ''; } - var self = this; - var insertTracker = function() { - if (self.currentHref !== self.location.href || !!~query.indexOf('follow') || !!~query.indexOf('pushEvent')) { - var page = self.getHref(); - var title = encodeURIComponent(document.title); - var referral = encodeURIComponent(document.referrer); - var locale = navigator.language || navigator.userLanguage || navigator.browserLanguage || navigator.systemLanguage || 'null'; - - document.getElementById('__tracking__').innerHTML = ''; - - self.currentHref = self.location.href; - setTimeout(function() { - self.followClick = false; - self.listenLinks(); - }, 25); - } - }; - - if (delay === false) { - insertTracker(); - } else { - setTimeout(insertTracker, 1); - } - }; - - return OstrioTrackerClass; - })(); -}).call(this); \ No newline at end of file