From 19ba91fa53744b692304bac9b1b215be28e31a99 Mon Sep 17 00:00:00 2001 From: Alessia Peviani Date: Thu, 8 Jun 2023 18:18:23 +0200 Subject: [PATCH 1/4] Add matomo tracking --- apps/genetics/index.html | 3 +- .../public/matomo/get_logged_in_user.js | 24 ++++++++++++ apps/genetics/public/matomo/init_matomo.js | 37 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 apps/genetics/public/matomo/get_logged_in_user.js create mode 100644 apps/genetics/public/matomo/init_matomo.js diff --git a/apps/genetics/index.html b/apps/genetics/index.html index 0e5539149..ef751f3be 100644 --- a/apps/genetics/index.html +++ b/apps/genetics/index.html @@ -23,7 +23,8 @@ - + + Open Targets Genetics diff --git a/apps/genetics/public/matomo/get_logged_in_user.js b/apps/genetics/public/matomo/get_logged_in_user.js new file mode 100644 index 000000000..162e8e72e --- /dev/null +++ b/apps/genetics/public/matomo/get_logged_in_user.js @@ -0,0 +1,24 @@ +/** +Returns a promise of logged-in user id. +This function assumes response header contains 'user' entry. +*/ +function getLoggedInUser() { + return new Promise(function(resolve, reject) { + var request = new XMLHttpRequest(); + request.onreadystatechange = function() { + if (this.readyState === this.HEADERS_RECEIVED) { + var user = request.getResponseHeader('user'); + if (user != null) { + resolve(user); + } else { + reject(); + } + } + }; + request.onerror = function() { + reject(); + }; + request.open('HEAD', document.location, true); + request.send(null); + }); +} \ No newline at end of file diff --git a/apps/genetics/public/matomo/init_matomo.js b/apps/genetics/public/matomo/init_matomo.js new file mode 100644 index 000000000..b2c0f8855 --- /dev/null +++ b/apps/genetics/public/matomo/init_matomo.js @@ -0,0 +1,37 @@ +var _paq = window._paq || []; +/* tracker methods like "setCustomDimension" should be called before "trackPageView" */ + +(function() { + function registerUserVisit() { + getLoggedInUser() + .then(function(user) { + _paq.push(['setUserId', user]); + }) + .finally(function() { + _paq.push(['setCustomUrl', window.location.href]); + _paq.push(['setDocumentTitle', window.document.title]); + _paq.push(['trackPageView']); + _paq.push(['enableLinkTracking']); + }); + } + var pushState = history.pushState; + history.pushState = function() { + pushState.apply(history, arguments); + registerUserVisit(); + }; + registerUserVisit(); +})(); + +(function() { + var u = 'localhost:9443'; // '/matomo/'; + _paq.push(['setTrackerUrl', u + 'matomo.php']); + _paq.push(['setSiteId', '1']); + var d = document, + g = d.createElement('script'), + s = d.getElementsByTagName('script')[0]; + g.type = 'text/javascript'; + g.async = true; + g.defer = true; + g.src = u + 'matomo.js'; + s.parentNode.insertBefore(g, s); +})(); \ No newline at end of file From 402690915991bd6de1d0d7c15b34e96b8f704e78 Mon Sep 17 00:00:00 2001 From: Alessia Peviani Date: Mon, 12 Jun 2023 17:21:06 +0200 Subject: [PATCH 2/4] Use correct localhost matomo URL --- apps/genetics/public/matomo/init_matomo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/genetics/public/matomo/init_matomo.js b/apps/genetics/public/matomo/init_matomo.js index b2c0f8855..61566b336 100644 --- a/apps/genetics/public/matomo/init_matomo.js +++ b/apps/genetics/public/matomo/init_matomo.js @@ -23,7 +23,7 @@ var _paq = window._paq || []; })(); (function() { - var u = 'localhost:9443'; // '/matomo/'; + var u = 'https://localhost:9443/'; _paq.push(['setTrackerUrl', u + 'matomo.php']); _paq.push(['setSiteId', '1']); var d = document, From 16a2290cd5f935b7fd299b6fea01db360306ff8e Mon Sep 17 00:00:00 2001 From: Alessia Peviani Date: Wed, 14 Jun 2023 11:21:41 +0200 Subject: [PATCH 3/4] Import Matomo URL from config.js --- apps/genetics/public/matomo/init_matomo.js | 4 +++- apps/genetics/src/config.js | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/genetics/public/matomo/init_matomo.js b/apps/genetics/public/matomo/init_matomo.js index 61566b336..136b0ed96 100644 --- a/apps/genetics/public/matomo/init_matomo.js +++ b/apps/genetics/public/matomo/init_matomo.js @@ -1,3 +1,5 @@ +import config from '../../src/config'; + var _paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ @@ -23,7 +25,7 @@ var _paq = window._paq || []; })(); (function() { - var u = 'https://localhost:9443/'; + var u = config.matomoUrl; _paq.push(['setTrackerUrl', u + 'matomo.php']); _paq.push(['setSiteId', '1']); var d = document, diff --git a/apps/genetics/src/config.js b/apps/genetics/src/config.js index 00f8b4812..5c98831f4 100644 --- a/apps/genetics/src/config.js +++ b/apps/genetics/src/config.js @@ -9,6 +9,7 @@ const config = { ? window.configPlatformUrl.replace(/\/$/, '') : 'https://platform.opentargets.org', showTopBar: window.configShowTopBar ?? false, + matomoUrl: window.configMatomoUrl ?? 'https://localhost:9443/', }; export default config; From ef14ad9838440bed9630f958e7b1fad0955bee30 Mon Sep 17 00:00:00 2001 From: Alessia Peviani Date: Wed, 14 Jun 2023 16:34:25 +0200 Subject: [PATCH 4/4] Add placeholder for Matomo URL --- apps/genetics/public/matomo/init_matomo.js | 7 ++++--- apps/genetics/src/config.js | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/genetics/public/matomo/init_matomo.js b/apps/genetics/public/matomo/init_matomo.js index 136b0ed96..fc4786055 100644 --- a/apps/genetics/public/matomo/init_matomo.js +++ b/apps/genetics/public/matomo/init_matomo.js @@ -1,5 +1,3 @@ -import config from '../../src/config'; - var _paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ @@ -25,7 +23,10 @@ var _paq = window._paq || []; })(); (function() { - var u = config.matomoUrl; + var u = 'DISABLED'; + if (u === 'DISABLED') { + return + } _paq.push(['setTrackerUrl', u + 'matomo.php']); _paq.push(['setSiteId', '1']); var d = document, diff --git a/apps/genetics/src/config.js b/apps/genetics/src/config.js index 5c98831f4..00f8b4812 100644 --- a/apps/genetics/src/config.js +++ b/apps/genetics/src/config.js @@ -9,7 +9,6 @@ const config = { ? window.configPlatformUrl.replace(/\/$/, '') : 'https://platform.opentargets.org', showTopBar: window.configShowTopBar ?? false, - matomoUrl: window.configMatomoUrl ?? 'https://localhost:9443/', }; export default config;