From 7cd19fbed3caa51aefa6219ed2eb0c31f19bf1d2 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 19 Jan 2024 12:15:31 +0100 Subject: [PATCH] wip #6496 @1 --- js/libs/fluidbook/fluidbook.stats.js | 79 ++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/js/libs/fluidbook/fluidbook.stats.js b/js/libs/fluidbook/fluidbook.stats.js index dae210c4..1bd43b42 100644 --- a/js/libs/fluidbook/fluidbook.stats.js +++ b/js/libs/fluidbook/fluidbook.stats.js @@ -1,5 +1,6 @@ function FluidbookStats() { this.hasMatomoTagManager = this.setupMatomoTagManager(); + this.pixel_pv_id; this.setupMatomo(); } @@ -10,7 +11,7 @@ FluidbookStats.prototype = { _mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'}); var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0]; g.async = true; - g.src = '//' + SETTINGS.matomoTagManager; + g.src = 'https://' + SETTINGS.matomoTagManager; s.parentNode.insertBefore(g, s); return true; } @@ -19,7 +20,7 @@ FluidbookStats.prototype = { setupMatomo: function () { this.matomoServers = []; - if (SETTINGS.statsMatomo && !this.hasMatomoTagManager) { + if (SETTINGS.statsMatomo) { if (SETTINGS.statsMatomoServer === undefined || SETTINGS.statsMatomoServer === null) { SETTINGS.statsMatomoServer = '3'; } @@ -30,6 +31,7 @@ FluidbookStats.prototype = { referer: '', fullURL: false, mtm: false, + pixel: this.hasMatomoTagManager ? this.getPixelUUID() : false, }); } if (SETTINGS.matomoServer && SETTINGS.matomoSiteId && !this.hasMatomoTagManager) { @@ -61,10 +63,47 @@ FluidbookStats.prototype = { } if (this.hasMatomoTagManager) { - this.matomoServers.push({mtm: true, fullURL: true, lastURL: '', referer: ''}); + this.matomoServers.push({mtm: true, fullURL: true, lastURL: '', referer: '', pixel: false}); } }, + getPixelUUID: function () { + let res = localStorage.getItem('matomo_pixel_uuid') + if (res === null || !res) { + res = this.generatePixelUUID(); + localStorage.setItem('matomo_pixel_uuid', res); + } + return res; + }, + + generatePixelUUID: function () { + // Generate random hexadecimal digits + var digits = "0123456789abcdef"; + var n = digits.length; + + // Generate random hexadecimal digits and concatenate them to form the UUID + var uuid = ""; + for (var i = 0; i < 16; i++) { + uuid += digits[Math.floor(Math.random() * n)]; + } + + return uuid; + }, + + generatePixelPVID: function () { + // Generate random hexadecimal digits + var digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + var n = digits.length; + + // Generate random hexadecimal digits and concatenate them to form the UUID + var uuid = ""; + for (var i = 0; i < 6; i++) { + uuid += digits[Math.floor(Math.random() * n)]; + } + + return uuid; + }, + matomoTrack(type, page, extra, url) { if (url === undefined) { url = window.location; @@ -121,7 +160,39 @@ FluidbookStats.prototype = { }, matomoPushToServer: function (data, server) { - if (this.matomoServers.length > 1) { + if (server.pixel) { + let url = 'https://' + server.server + '/matomo.php?idsite=' + server.siteId + '&rec=1&apiv=1'; + url += '&url=' + encodeURIComponent(server.lastURL); + url += '&rand=' + Math.round(Math.random() * 1000000000); + url += '&_id=' + server.pixel; + url += '&ua=' + encodeURIComponent(window.navigator.userAgent); + url += '&cookie=1'; + let now = new Date(); + url += '&h=' + now.getHours() + '&m=' + now.getMinutes() + '&s=' + now.getSeconds(); + url += '&lang=' + navigator.language; + if (server.referer !== '' && server.referer !== server.lastURL) { + url += '&urlref=' + encodeURIComponent(server.referer); + } + url += '&res=' + screen.width + 'x' + screen.height; + if (data[0] === 'trackPageView') { + url += '&action_name=' + data[1]; + this.pixel_pv_id = this.generatePixelPVID(); + } else if (data[0] === 'trackSiteSearch') { + url += '&search=' + encodeURIComponent(data[1]); + } else if (data[0] === 'trackLink') { + url += '&link=' + encodeURIComponent(data[1]); + } else if (data[0] === 'trackEvent') { + let order = ['c', 'a', 'n', 'v']; + for (let i = 1; i < data.length; i++) { + url += '&e_' + order[i - 1] + '=' + encodeURIComponent(data[i]); + } + } + url += '&pv_id=' + this.pixel_pv_id; + let i = new Image(); + i.src = url; + return; + } + if (!this.hasMatomoTagManager && this.matomoServers.length > 1) { window._paq.push(['setTrackerUrl', 'https://' + server.server + '/matomo.php']); window._paq.push(['setSiteId', server.siteId]); if (server.referer !== '' && server.referer !== server.lastURL) { -- 2.39.5