Tracking update

This commit is contained in:
silas
2018-10-01 21:51:24 +02:00
parent d37fc8254e
commit c0a977afd7
13 changed files with 230 additions and 98 deletions

View File

@@ -12,7 +12,7 @@ class Matomo {
static init() {
Matomo.isTrackingPromise = new Promise(async (resolve) => {
let shouldTrack = localStorage.getItem(Matomo.LOCAL_STORAGE_KEY);
let shouldTrack = Helper.nonNull(localStorage.getItem(Matomo.LOCAL_STORAGE_KEY), "1");
if (Helper.isNull(shouldTrack)) {
shouldTrack = await Matomo._askIsTracking();
localStorage.setItem(Matomo.LOCAL_STORAGE_KEY, shouldTrack);
@@ -24,10 +24,10 @@ class Matomo {
resolve(shouldTrack);
});
Matomo.isTrackingPromise.then(() => {
Matomo.push(['trackPageView']);
Matomo.push(['enableLinkTracking']);
Matomo.push(['setTrackerUrl', Matomo.TRACK_SITE + '/piwik.php']);
Matomo.push(['setSiteId', Matomo.SIDE_ID]);
Matomo.push(['trackPageView'], true);
Matomo.push(['enableLinkTracking'], true);
Matomo.push(['setTrackerUrl', Matomo.TRACK_SITE + '/piwik.php'], true);
Matomo.push(['setSiteId', Matomo.SIDE_ID + ""], true);
let d = document, g = d.createElement('script'), s = d.getElementsByTagName('head')[0];
g.type = 'text/javascript';
@@ -36,19 +36,52 @@ class Matomo {
g.src = Matomo.TRACK_SITE + '/piwik.js';
s.appendChild(g);
});
// window.addEventListener('hashchange', () => {
// Matomo.update()
// });
}
static update(title) {
if (Helper.nonNull(Matomo.currentUrl)){
Matomo.push(['setReferrerUrl', Matomo.currentUrl]);
}
Matomo.currentUrl = window.location.pathname+window.location.search;
Matomo.push(['setCustomUrl', Matomo.currentUrl]);
Matomo.push(['setDocumentTitle', title]);
// remove all previously assigned custom variables, requires Matomo (formerly Piwik) 3.0.2
Matomo.push(['deleteCustomVariables', 'page']);
Matomo.push(['setGenerationTimeMs', 0]);
Matomo.push(['trackPageView']);
// make Matomo aware of newly added content
var content = document.getElementById('site-content');
Matomo.push(['MediaAnalytics::scanForMedia', content]);
Matomo.push(['FormAnalytics::scanForForms', content]);
Matomo.push(['trackContentImpressionsWithinNode', content]);
Matomo.push(['enableLinkTracking']);
}
static async _askIsTracking() {
Matomo.isTrackingPromise = Matomo.query("isTracked")
.then(xml => {
let textContent = xml.firstChild.textContent;
// localStorage.setItem(Matomo.LOCAL_STORAGE_KEY, textContent);
return (textContent === "1")
});
Matomo.isTrackingPromise = new Promise(resolve => {
Matomo.push([function () {
resolve(!this["isUserOptedOut"]());
}]);
Matomo.push([function () {
resolve(!this["isUserOptedOut"]());
}]);
});
// Matomo.isTrackingPromise = Matomo.query("isTracked")
// .then(xml => {
// let textContent = xml.firstChild.textContent;
// // localStorage.setItem(Matomo.LOCAL_STORAGE_KEY, textContent);
// return (textContent === "1")
// });
return Matomo.isTrackingPromise;
}
static async query(method) {
static
async query(method) {
return fetch(Matomo.TRACK_SITE + Matomo.BASE_PATH + method, {
"mode": "cors",
"credentials": "include",
@@ -59,19 +92,34 @@ class Matomo {
return Matomo.isTrackingPromise;
}
static async setTrack(shouldTrack) {
static
async setTrack(shouldTrack) {
Matomo.isTrackingPromise = Promise.resolve(shouldTrack);
localStorage.setItem(Matomo.LOCAL_STORAGE_KEY, (shouldTrack === true) ? "1" : "0");
return await Matomo.query((shouldTrack) ? "doTrack" : "doIgnore");
}
static async push(arr) {
if (await Matomo.getTrackingPromise()) {
window["_paq"].push(arr);
// return await Matomo.query((shouldTrack) ? "doTrack" : "doIgnore");
if (shouldTrack) {
Matomo.push(["forgetUserOptOut"], true);
}
else {
Matomo.push(["optUserOut"], true);
}
}
static
async push(arr, force) {
// force = Helper.nonNull(force, false);
if (!Array.isArray(arr)) {
arr = [arr];
}
// if (force || await Matomo.getTrackingPromise()) {
window["_paq"].push(arr);
// }
}
}
Matomo.currentUrl = null;
Matomo.LOCAL_STORAGE_KEY = "matomoShouldTrack";
Matomo.TRACK_SITE = "//matomo.silas.link";
Matomo.BASE_PATH = "/index.php?module=API&method=AjaxOptOut.";
@@ -79,7 +127,6 @@ Matomo.SIDE_ID = "1";
InitPromise.addPromise(() => {
window["_paq"] = window["_paq"] || [];
Matomo.init();
});

View File

@@ -4156,6 +4156,11 @@ function applyPolyfills() {
elem.addEventListener("transitionend", transEndLis);
elem.addEventListener("transitioncancel", transCancelledLis);
});
//Fallback
setTimeout(() => {
resolve(false);
}, (time+delay)*1000);
//Nach Seitenneuzeichnen, damit chrome das immer macht (und FF auch)
requestAnimationFrame(function () {
requestAnimationFrame(function () {
@@ -4178,7 +4183,6 @@ function applyPolyfills() {
elem.removeEventListener("transitioncancel", transCancelledLis);
elem.style.opacity = null;
elem.style.transition = null;
console.log("transEnd");
resolve(true, e);
};
@@ -4187,13 +4191,11 @@ function applyPolyfills() {
elem.removeEventListener("transitioncancel", transCancelledLis);
elem.style.opacity = null;
elem.style.transition = null;
console.log("transCancelled");
resolve(false, e);
};
elem.addEventListener("transitionend", transEndLis);
elem.addEventListener("transitioncancel", transCancelledLis);
console.log(getComputedStyle(elem).getPropertyValue("opacity"));
if (getComputedStyle(elem).getPropertyValue("opacity") === "1"){
resolve(false);
}

View File

@@ -12,4 +12,4 @@ SystemSettings.setBasePath(basePath);
Translator.supportedLanguages = ["de"];
Translator.markTranslations = false;
Matomo.SIDE_ID = 2;
Matomo.SIDE_ID = "2";