From cca246c2ac15c3d0220a2b725fb9d10e4efc42b9 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <p.gawron@atcomp.pl>
Date: Fri, 18 Oct 2024 12:18:26 +0200
Subject: [PATCH] support for matomo added

---
 CHANGELOG                                     |  5 +++
 .../configuration/configuration.constants.ts  |  1 +
 src/redux/root/init.thunks.ts                 | 36 ++++++++++++++++++-
 3 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG b/CHANGELOG
index d88a916b..8d971a9d 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,8 @@
+minerva-front (19.0.0~alpha.0) stable; urgency=medium
+  * Feature: support for matomo (#289)
+
+ -- Piotr Gawron <piotr.gawron@uni.lu>  Thu, 19 Sep 2024 13:00:00 +0200
+
 minerva-front (18.0.0~beta.2) stable; urgency=medium
   * Feature: minerva frontend - first version
   * Bugfix: when opening overlay provide loading info (#285)
diff --git a/src/redux/configuration/configuration.constants.ts b/src/redux/configuration/configuration.constants.ts
index 59ef10da..c6084ddf 100644
--- a/src/redux/configuration/configuration.constants.ts
+++ b/src/redux/configuration/configuration.constants.ts
@@ -5,6 +5,7 @@ export const NEUTRAL_COLOR_VAL_NAME_ID = 'NEUTRAL_COLOR_VAL';
 export const OVERLAY_OPACITY_NAME_ID = 'OVERLAY_OPACITY';
 export const SEARCH_DISTANCE_NAME_ID = 'SEARCH_DISTANCE';
 export const REQUEST_ACCOUNT_EMAIL = 'REQUEST_ACCOUNT_EMAIL';
+export const MATOMO_URL = 'MATOMO_URL';
 
 export const LEGEND_FILE_NAMES_IDS = [
   'LEGEND_FILE_1',
diff --git a/src/redux/root/init.thunks.ts b/src/redux/root/init.thunks.ts
index 4e161b6e..41fa48fa 100644
--- a/src/redux/root/init.thunks.ts
+++ b/src/redux/root/init.thunks.ts
@@ -1,6 +1,6 @@
 import { PROJECT_ID } from '@/constants';
 import { openOverlaysDrawer, openSearchDrawerWithSelectedTab } from '@/redux/drawer/drawer.slice';
-import { AppDispatch } from '@/redux/store';
+import { AppDispatch, store } from '@/redux/store';
 import { QueryData } from '@/types/query';
 import { getDefaultSearchTab } from '@/components/FunctionalArea/TopBar/SearchBar/SearchBar.utils';
 import { PluginsManager } from '@/services/pluginsManager';
@@ -16,6 +16,7 @@ import { openSelectProjectModal } from '@/redux/modal/modal.slice';
 import { getProjects } from '@/redux/projects/projects.thunks';
 import { getSubmapConnectionsBioEntity } from '@/redux/bioEntity/thunks/getSubmapConnectionsBioEntity';
 import { getShapes } from '@/redux/shapes/shapes.thunks';
+import { MATOMO_URL } from '@/redux/configuration/configuration.constants';
 import { getAllBackgroundsByProjectId } from '../backgrounds/backgrounds.thunks';
 import { getConfiguration, getConfigurationOptions } from '../configuration/configuration.thunks';
 import {
@@ -62,6 +63,39 @@ export const fetchInitialAppData = createAsyncThunk<
     dispatch(getShapes()),
   ]);
 
+  try {
+    const configuration = store.getState().configuration.main.data;
+
+    if (configuration) {
+      const options = configuration.options.filter(option => option.type === MATOMO_URL);
+      let url = '';
+      if (options.length > ZERO) {
+        url = options[ZERO].value ? options[ZERO].value : '';
+      }
+      if (!url.startsWith('http')) {
+        url = '';
+      }
+      if (url !== '') {
+        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+        // @ts-expect-error
+        // eslint-disable-next-line no-underscore-dangle,no-multi-assign
+        const _mtm = (window._mtm = window._mtm || []);
+        _mtm.push({ 'mtm.startTime': new Date().getTime(), event: 'mtm.Start' });
+        const d = document;
+        const g = d.createElement('script');
+        const s = d.getElementsByTagName('script')[ZERO];
+        g.async = true;
+        g.src = url;
+        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+        // @ts-expect-error
+        s.parentNode.insertBefore(g, s);
+      }
+    }
+  } catch (e) {
+    // eslint-disable-next-line no-console
+    console.log(e);
+  }
+
   if (queryData.pluginsId) {
     await dispatch(
       getInitPlugins({
-- 
GitLab