var trackerUser = ''; var trackerSession = 'j579uio3956v9jetjfskdjuqoh'; 'use strict'; var Tracker = function () { this.ajaxUrl = 'https://ajax.wogibtswas.net/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.sendLinks = function () { if (!document.getElementById('trackerLinks')) return; let dataLinks = $('[data-tracker="link"]'); if (dataLinks.length < 0) return; let formData = new FormData(); let trackerLinks = $('#trackerLinks'); formData.append('session', trackerSession); formData.append('type', 'links'); formData.append('page', trackerLinks.data('page')); formData.append('term', trackerLinks.data('term')); formData.append('city', trackerLinks.data('city')); if (trackerUser != '') formData.append('user', trackerUser); let keys = []; dataLinks.each((_, elem) => { keys.push($(elem).data('key')); }); formData.append('keys', JSON.stringify(keys)); $.ajax({ dataType: 'json', async: true, cache: false, type: 'post', url: this.ajaxUrl, data: formData, processData: false, contentType: false, }); }; Tracker.prototype.sendAds = function () { if (!document.getElementById('trackerAds')) return; let dataAds = $('[data-tracker="ads"]'); if (dataAds.length < 0) return; let formData = new FormData(); let trackerAds = $('#trackerAds'); formData.append('session', trackerSession); formData.append('type', 'links'); formData.append('page', trackerAds.data('page')); formData.append('term', trackerAds.data('term')); formData.append('city', trackerAds.data('city')); if (trackerUser != '') formData.append('user', trackerUser); let keys = []; dataAds.each((_, elem) => { keys.push($(elem).data('key')); }); formData.append('keys', JSON.stringify(keys)); $.ajax({ dataType: 'json', async: true, cache: false, type: 'post', url: this.ajaxUrl, data: formData, processData: false, contentType: false, }); }; 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); let trackerSettings = $('#trackerSettings'); if (trackerSettings.length > 0) { formData.append('page', trackerSettings.data('page')); formData.append('key', trackerSettings.data('key')); } } 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, }).always(() => { if (type == 'request') this.sendLinks(); this.sendAds(); }); if (type == 'request' || type == 'update') { this.cfg.requestCount += 1; if (this.cfg.requestCount % this.cfg.requestPeriod == 0) { this.cfg.requestInterval = Math.round(this.cfg.requestInterval * this.cfg.requestFactor); } this.setRequestTimeout(); } }; $(function () { window.tracker = new Tracker(); });