var trackerUser = ''; var trackerSession = '5q6e92m3go0tde6io4jjeed6n1'; 'use strict'; var Tracker = function () { this.ajaxUrl = 'https://ajax.wogibtswas.de/tracker.php'; this.initialize(); $(window).on('unload', function () { window.tracker.windowUnload(); }); $(window).on('blur focus', function (event) { window.tracker.updateWindowState(event); }); this.setEventActionHandler(); this.setEventInterval(); this.sendRequest('request'); }; Tracker.prototype.initialize = function () { this.request = {}; this.cfg = {}; this.cfg.eventTypes = 'mousemove mousedown touchstart click scroll keypress'; this.cfg.tracking = true; // ist das tracking gerade in/aktiv this.cfg.windowState = 'focus'; // ist das fenster gerade in/aktiv this.cfg.eventCount = 0; // anzahl events seit dem letzten interfall this.cfg.requestCount = 0; // anzahl der bereits gemachten requests this.cfg.lastAction = Date.now(); // letzter intervall mit erkannter action this.cfg.requestInterval = 5000; // initialer intervall der requests this.cfg.idleTimeout = 120000; // maximale zeit ohne action/events this.cfg.idleCheck = 5000; // prüfintervall für actions/events this.cfg.requestFactor = 1.5; // faktor zur erhöhung der request abstände this.cfg.requestPeriod = 5; // alle wieviel requests der timeout erhöht wird }; Tracker.prototype.setEventActionHandler = function (active = true) { this.cfg.eventCount = 0; if (typeof this._countEventActionHandler !== 'function') { this._countEventActionHandler = function () { window.tracker.cfg.eventCount += 1; }; } if (active) $(window).on(this.cfg.eventTypes, this._countEventActionHandler); else $(window).off(this.cfg.eventTypes, this._countEventActionHandler); } Tracker.prototype.setEventInterval = function (active = true) { if (this.eventInterval) window.clearInterval(this.eventInterval); if (active) this.eventInterval = window.setInterval(function () { window.tracker.checkEventCount(); }, this.cfg.idleCheck); else window.clearInterval(this.eventInterval); }; Tracker.prototype.setRequestTimeout = function (active = true) { if (this.requestTimeout) window.clearTimeout(this.requestTimeout); if (active) this.requestTimeout = window.setTimeout(function () { window.tracker.sendRequest(); }, this.cfg.requestInterval); else window.clearTimeout(this.requestTimeout); }; Tracker.prototype.windowReactivate = function () { this.initialize(); this.sendRequest('request'); }; Tracker.prototype.windowInactive = function () { this.setRequestTimeout(false); this.sendRequest('inactive'); this.cfg.tracking = false; }; Tracker.prototype.windowUnload = function () { if (this.cfg.tracking) this.sendRequest('leaving', true); }; Tracker.prototype.updateWindowState = function (event) { if (this.cfg.windowState != event.type) { this.cfg.windowState = event.type; if (this.cfg.windowState == 'blur') { this.setEventActionHandler(false); this.setRequestTimeout(false); } if (this.cfg.windowState == 'focus') { this.setEventActionHandler(); if (this.cfg.tracking) this.sendRequest(); } } }; Tracker.prototype.checkEventCount = function () { var timestamp = Date.now(); if (this.cfg.eventCount > 0) { if (this.cfg.tracking === false) this.windowReactivate(); this.cfg.lastAction = timestamp; this.cfg.eventCount = 0; } if (timestamp > this.cfg.lastAction + this.cfg.idleTimeout && this.cfg.tracking) { this.windowInactive(); } }; Tracker.prototype.sendRequest = function (type = 'update', beacon = false) { var formData = new FormData(); formData.append('session', trackerSession); formData.append('href', window.location.href); formData.append('type', type); if (trackerUser != '') formData.append('user', trackerUser); if (type == 'request') { formData.append('data[width]', window.screen.width); formData.append('data[height]', window.screen.height); } if (type == 'inactive') { formData.append('bounce', Math.round(this.cfg.lastAction / 1000, 2)); } if (('navigator' in window) && ('sendBeacon' in window.navigator) && beacon) { navigator.sendBeacon(this.ajaxUrl, formData); } else $.ajax({ dataType: 'json', async: true, cache: false, type: 'post', url: this.ajaxUrl, data: formData, processData: false, contentType: false, }); this.cfg.requestCount += 1; if (this.cfg.requestCount % this.cfg.requestPeriod == 0) { this.cfg.requestInterval = Math.round(this.cfg.requestInterval * this.cfg.requestFactor); } if (type == 'request' || type == 'update') this.setRequestTimeout(); }; $(function () { window.tracker = new Tracker(); });