From 926aa793de0cfd764e7cd31fa763efbaf2c4ad0e Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 19 Sep 2025 10:26:29 +0200 Subject: [PATCH 01/11] added configuration about disability of matomo statistics --- MATOMO_CONFIGURATION.md | 38 +++++++ config/config.example.yml | 1 + .../clarin-matomo-statistics.component.html | 98 ++++++++++--------- .../clarin-matomo-statistics.component.ts | 12 +++ src/config/default-app-config.ts | 1 + src/config/matomo-config.ts | 2 + src/environments/environment.test.ts | 1 + src/environments/environment.ts | 8 ++ src/main.browser.ts | 45 +++++---- src/modules/app/browser-init.service.ts | 38 +++---- 10 files changed, 163 insertions(+), 81 deletions(-) create mode 100644 MATOMO_CONFIGURATION.md diff --git a/MATOMO_CONFIGURATION.md b/MATOMO_CONFIGURATION.md new file mode 100644 index 00000000000..ddd31bd3188 --- /dev/null +++ b/MATOMO_CONFIGURATION.md @@ -0,0 +1,38 @@ +# Matomo Statistics Configuration + +## Enable/Disable Matomo + +### To Disable Matomo (Default) +Edit `src/environments/environment.ts`: +```typescript +matomo: { + enabled: false, + hostUrl: 'http://localhost:8135/', + siteId: '1', + dimensionId: 1 +} +``` + +### To Enable Matomo +Edit `src/environments/environment.ts`: +```typescript +matomo: { + enabled: true, + hostUrl: 'https://your-matomo-server.com/', + siteId: '1', + dimensionId: 1 +} +``` + +## Files to Modify + +- **Development**: `src/environments/environment.ts` +- **Production**: `src/environments/environment.production.ts` + +## After Changes + +1. Restart the application +2. Clear browser cache + +When disabled: No Matomo scripts load, statistics page shows "disabled" message. +When enabled: Matomo tracking works normally. diff --git a/config/config.example.yml b/config/config.example.yml index 8b56711c7d2..c8500545df4 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -465,5 +465,6 @@ accessibility: cookieExpirationDuration: 7 matomo: + enabled: false hostUrl: http://localhost:8135/ siteId: 1 diff --git a/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.html b/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.html index 4e1e768abf7..d8097762f69 100644 --- a/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.html +++ b/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.html @@ -1,50 +1,56 @@
-
- - {{'item.matomo-statistics.info.message' | translate}} +
+ + Matomo statistics are currently disabled.
-
- - -
-
- {{chartMessage}} - -
-
- - -
-
- - - Filewise Statistics - - - - - - - -
{{fileData.value}}{{fileData.key}}
+
+
+ + {{'item.matomo-statistics.info.message' | translate}} +
+
+ + +
+
+ {{chartMessage}} + +
+
+ + +
+
+ + + Filewise Statistics + + + + + + + +
{{fileData.value}}{{fileData.key}}
+
diff --git a/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts b/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts index 8f73e51e766..d84bdf338d9 100644 --- a/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts +++ b/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts @@ -10,6 +10,7 @@ import { map } from 'rxjs/operators'; import { ActivatedRoute } from '@angular/router'; import { RemoteData } from '../../core/data/remote-data'; import { Item } from '../../core/shared/item.model'; +import { environment } from '../../../environments/environment'; @Component({ selector: 'ds-clarin-matomo-statistics', @@ -26,6 +27,9 @@ export class ClarinMatomoStatisticsComponent implements OnInit { @ViewChild(BaseChartDirective, { static: true }) chart: BaseChartDirective; itemRD$: BehaviorSubject = new BehaviorSubject(null); + + // Flag to track if Matomo is enabled + public matomoEnabled = false; // Month shortcut with full name public months = [ @@ -130,6 +134,14 @@ export class ClarinMatomoStatisticsComponent implements OnInit { ngOnInit(): void { + // Check if Matomo is enabled in the environment + this.matomoEnabled = environment.matomo?.enabled || false; + + // If Matomo is disabled, don't fetch statistics + if (!this.matomoEnabled) { + return; + } + this.route.data.pipe( map((data) => data.dso as RemoteData)) .subscribe(data => { diff --git a/src/config/default-app-config.ts b/src/config/default-app-config.ts index 38cb942e457..2fff7f31fc3 100644 --- a/src/config/default-app-config.ts +++ b/src/config/default-app-config.ts @@ -452,6 +452,7 @@ export class DefaultAppConfig implements AppConfig { // Matomo configuration matomo: MatomoConfig = { + enabled: false, hostUrl: 'http://localhost:8135/', siteId: '1', dimensionId: 1 diff --git a/src/config/matomo-config.ts b/src/config/matomo-config.ts index 548f02aee4c..cc71a3cb6fc 100644 --- a/src/config/matomo-config.ts +++ b/src/config/matomo-config.ts @@ -5,6 +5,8 @@ import { Config } from './config.interface'; */ export class MatomoConfig implements Config { + public enabled: boolean; + public hostUrl: string; public siteId: string; diff --git a/src/environments/environment.test.ts b/src/environments/environment.test.ts index a0cab16ef83..ce1998535d6 100644 --- a/src/environments/environment.test.ts +++ b/src/environments/environment.test.ts @@ -356,6 +356,7 @@ export const environment: BuildConfig = { signpostingEnabled: true, matomo: { + enabled: false, hostUrl: 'http://localhost:8135/', siteId: '1', dimensionId: 1 diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 8be4ee7dbfe..a229e75036b 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -49,6 +49,14 @@ export const environment: Partial = { }, signpostingEnabled: false, + + // Matomo configuration + matomo: { + enabled: false, + hostUrl: 'http://localhost:8135/', + siteId: '1', + dimensionId: 1 + }, }; /* diff --git a/src/main.browser.ts b/src/main.browser.ts index d17101d07a6..be856fbb4ac 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -44,30 +44,39 @@ const main = () => { }; function addMatomoStatistics() { - (window as any)._paq = (window as any)._paq || []; + // Debug: Log the matomo configuration + console.log('Matomo config check:', { + hasMatomo: !!environment.matomo, + enabled: environment.matomo?.enabled, + full: environment.matomo + }); + + // Check if Matomo is enabled in the environment configuration + if (!environment.matomo || !environment.matomo.enabled) { + console.log('Matomo is disabled, skipping initialization'); + return; + } - void fetch('assets/config.json') - .then((response) => response.json()) - .then((config) => { - const matomoConfig = config.matomo; + console.log('Matomo is enabled, initializing...'); + + (window as any)._paq = (window as any)._paq || []; - // Push all configuration commands first - (window as any)._paq.push(['setTrackerUrl', matomoConfig.hostUrl + 'matomo.php']); - (window as any)._paq.push(['setSiteId', matomoConfig.siteId]); - (window as any)._paq.push(['enableLinkTracking']); + // Push all configuration commands first + (window as any)._paq.push(['setTrackerUrl', environment.matomo.hostUrl + 'matomo.php']); + (window as any)._paq.push(['setSiteId', environment.matomo.siteId]); + (window as any)._paq.push(['enableLinkTracking']); - const g = document.createElement('script'); - g.type = 'text/javascript'; - g.async = true; - g.defer = true; - g.src = matomoConfig.hostUrl + 'matomo.js'; - document.getElementsByTagName('head')[0].appendChild(g); - }); + const g = document.createElement('script'); + g.type = 'text/javascript'; + g.async = true; + g.defer = true; + g.src = environment.matomo.hostUrl + 'matomo.js'; + document.getElementsByTagName('head')[0].appendChild(g); } // support async tag or hmr if (document.readyState === 'complete' && !hasTransferState) { - main(); + void main(); } else { - document.addEventListener('DOMContentLoaded', main); + document.addEventListener('DOMContentLoaded', () => void main()); } diff --git a/src/modules/app/browser-init.service.ts b/src/modules/app/browser-init.service.ts index 905485637d6..8b5546c6d5f 100644 --- a/src/modules/app/browser-init.service.ts +++ b/src/modules/app/browser-init.service.ts @@ -107,26 +107,30 @@ export class BrowserInitService extends InitService { this.initRouteListeners(); this.themeService.listenForThemeChanges(true); this.trackAuthTokenExpiration(); - // ideally we'd add the custom dimension to the 'trackPageView' action only, but don't have that information - // in pageTrack context. So we add it to page_view events, and remove it after the page view. - // page_view events are fired via view-track.component, and exposes dc.identifier.uri via properties - this.angulartics2Matomo.eventTrack = function (action: string, properties?: any) { - if (action === 'page_view') { - if (properties.dc_identifier) { - (window as any)._paq.push(['setCustomDimension', environment.matomo.dimensionId, properties.dc_identifier]); + + // Only initialize Matomo if it's enabled in configuration + if (this.appConfig.matomo && this.appConfig.matomo.enabled) { + // ideally we'd add the custom dimension to the 'trackPageView' action only, but don't have that information + // in pageTrack context. So we add it to page_view events, and remove it after the page view. + // page_view events are fired via view-track.component, and exposes dc.identifier.uri via properties + this.angulartics2Matomo.eventTrack = function (action: string, properties?: any) { + if (action === 'page_view') { + if (properties.dc_identifier) { + (window as any)._paq.push(['setCustomDimension', this.appConfig.matomo.dimensionId, properties.dc_identifier]); + } } - } - }; - let pageTrack = this.angulartics2Matomo.pageTrack; - this.angulartics2Matomo.pageTrack = function (path: string) { - pageTrack.call(this, path); - (window as any)._paq.push(['deleteCustomDimension', environment.matomo.dimensionId]); - }; - this.angulartics2Matomo.startTracking(); + }.bind(this); + let pageTrack = this.angulartics2Matomo.pageTrack; + this.angulartics2Matomo.pageTrack = function (path: string) { + pageTrack.call(this, path); + (window as any)._paq.push(['deleteCustomDimension', this.appConfig.matomo.dimensionId]); + }.bind(this); + this.angulartics2Matomo.startTracking(); + } this.initKlaro(); - await this.authenticationReady$().toPromise(); + await firstValueFrom(this.authenticationReady$()); return true; }; @@ -197,7 +201,7 @@ export class BrowserInitService extends InitService { * @private */ private closeAuthCheckSubscription() { - firstValueFrom(this.authenticationReady$()).then(() => { + void firstValueFrom(this.authenticationReady$()).then(() => { this.sub.unsubscribe(); }); } From fc18752fa45ffc05eec1f7648dd3a8d9f87d8ea3 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 19 Sep 2025 12:34:46 +0200 Subject: [PATCH 02/11] made doc more general, remove debug console --- MATOMO_CONFIGURATION.md | 2 +- src/main.browser.ts | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/MATOMO_CONFIGURATION.md b/MATOMO_CONFIGURATION.md index ddd31bd3188..c6c784429dc 100644 --- a/MATOMO_CONFIGURATION.md +++ b/MATOMO_CONFIGURATION.md @@ -7,7 +7,7 @@ Edit `src/environments/environment.ts`: ```typescript matomo: { enabled: false, - hostUrl: 'http://localhost:8135/', + hostUrl: 'https://your-matomo-server.com/', siteId: '1', dimensionId: 1 } diff --git a/src/main.browser.ts b/src/main.browser.ts index be856fbb4ac..abf3eeb35f4 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -57,8 +57,6 @@ function addMatomoStatistics() { return; } - console.log('Matomo is enabled, initializing...'); - (window as any)._paq = (window as any)._paq || []; // Push all configuration commands first From c144f6b8828d1809047949a0bc50064c30c41ca5 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 19 Sep 2025 14:05:08 +0200 Subject: [PATCH 03/11] prevent regression in runtime enablement --- src/main.browser.ts | 15 +++++++++------ src/modules/app/browser-init.service.ts | 9 +++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main.browser.ts b/src/main.browser.ts index abf3eeb35f4..b3af3de32c2 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -27,9 +27,9 @@ const main = () => { enableProdMode(); } - addMatomoStatistics(); if (hasTransferState) { // Configuration will be taken from transfer state during initialization + addMatomoStatistics(); return bootstrap(); } else { // Configuration must be fetched explicitly @@ -38,6 +38,7 @@ const main = () => { .then((appConfig: AppConfig) => { // extend environment with app config for browser when not prerendered extendEnvironmentWithAppConfig(environment, appConfig); + addMatomoStatistics(); return bootstrap(); }); } @@ -45,11 +46,13 @@ const main = () => { function addMatomoStatistics() { // Debug: Log the matomo configuration - console.log('Matomo config check:', { - hasMatomo: !!environment.matomo, - enabled: environment.matomo?.enabled, - full: environment.matomo - }); + if (!environment.production) { + console.log('Matomo config check:', { + hasMatomo: !!environment.matomo, + enabled: environment.matomo?.enabled, + full: environment.matomo + }); + } // Check if Matomo is enabled in the environment configuration if (!environment.matomo || !environment.matomo.enabled) { diff --git a/src/modules/app/browser-init.service.ts b/src/modules/app/browser-init.service.ts index 8b5546c6d5f..9b6de2a165a 100644 --- a/src/modules/app/browser-init.service.ts +++ b/src/modules/app/browser-init.service.ts @@ -113,18 +113,19 @@ export class BrowserInitService extends InitService { // ideally we'd add the custom dimension to the 'trackPageView' action only, but don't have that information // in pageTrack context. So we add it to page_view events, and remove it after the page view. // page_view events are fired via view-track.component, and exposes dc.identifier.uri via properties + const dimensionId = this.appConfig.matomo.dimensionId; this.angulartics2Matomo.eventTrack = function (action: string, properties?: any) { if (action === 'page_view') { if (properties.dc_identifier) { - (window as any)._paq.push(['setCustomDimension', this.appConfig.matomo.dimensionId, properties.dc_identifier]); + (window as any)._paq.push(['setCustomDimension', dimensionId, properties.dc_identifier]); } } - }.bind(this); + }; let pageTrack = this.angulartics2Matomo.pageTrack; this.angulartics2Matomo.pageTrack = function (path: string) { pageTrack.call(this, path); - (window as any)._paq.push(['deleteCustomDimension', this.appConfig.matomo.dimensionId]); - }.bind(this); + (window as any)._paq.push(['deleteCustomDimension', dimensionId]); + }; this.angulartics2Matomo.startTracking(); } From fc45d2956b5513e640e7cd5633bf0fd8b939c51a Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 19 Sep 2025 14:14:20 +0200 Subject: [PATCH 04/11] fix docs, log only when it is not production --- MATOMO_CONFIGURATION.md | 3 +++ src/main.browser.ts | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/MATOMO_CONFIGURATION.md b/MATOMO_CONFIGURATION.md index c6c784429dc..bb952d8c676 100644 --- a/MATOMO_CONFIGURATION.md +++ b/MATOMO_CONFIGURATION.md @@ -28,6 +28,9 @@ matomo: { - **Development**: `src/environments/environment.ts` - **Production**: `src/environments/environment.production.ts` +- **Global/Server-side**: `config/config.example.yml` (and `config/config.yml` in production) +- **Runtime (if applicable)**: `config.json` +> **Note:** The `enabled` flag for Matomo may be present in multiple configuration files. Ensure you update all relevant files (`environment.ts`, `environment.production.ts`, `config/config.example.yml`, and any runtime `config.json`) to avoid conflicts or unexpected behavior. ## After Changes diff --git a/src/main.browser.ts b/src/main.browser.ts index b3af3de32c2..f1a819a8a66 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -56,7 +56,9 @@ function addMatomoStatistics() { // Check if Matomo is enabled in the environment configuration if (!environment.matomo || !environment.matomo.enabled) { - console.log('Matomo is disabled, skipping initialization'); + if (!environment.production) { + console.log('Matomo is disabled, skipping initialization'); + } return; } From 6ffb29bd95b33513f6b638698f30765ab23177ba Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 19 Sep 2025 14:37:22 +0200 Subject: [PATCH 05/11] avoid void operator: --- src/main.browser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.browser.ts b/src/main.browser.ts index f1a819a8a66..3814717c5a0 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -81,5 +81,5 @@ function addMatomoStatistics() { if (document.readyState === 'complete' && !hasTransferState) { void main(); } else { - document.addEventListener('DOMContentLoaded', () => void main()); + document.addEventListener('DOMContentLoaded', () => main()); } From 72a7196d3400ea332a5fb6522f2e54ff130c753c Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 19 Sep 2025 15:14:33 +0200 Subject: [PATCH 06/11] fix brealcrumbs test --- src/main.browser.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main.browser.ts b/src/main.browser.ts index 3814717c5a0..c0a97fd1d49 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -62,24 +62,30 @@ function addMatomoStatistics() { return; } - (window as any)._paq = (window as any)._paq || []; + try { + (window as any)._paq = (window as any)._paq || []; - // Push all configuration commands first - (window as any)._paq.push(['setTrackerUrl', environment.matomo.hostUrl + 'matomo.php']); - (window as any)._paq.push(['setSiteId', environment.matomo.siteId]); - (window as any)._paq.push(['enableLinkTracking']); + // Push all configuration commands first + (window as any)._paq.push(['setTrackerUrl', environment.matomo.hostUrl + 'matomo.php']); + (window as any)._paq.push(['setSiteId', environment.matomo.siteId]); + (window as any)._paq.push(['enableLinkTracking']); - const g = document.createElement('script'); - g.type = 'text/javascript'; - g.async = true; - g.defer = true; - g.src = environment.matomo.hostUrl + 'matomo.js'; - document.getElementsByTagName('head')[0].appendChild(g); + const g = document.createElement('script'); + g.type = 'text/javascript'; + g.async = true; + g.defer = true; + g.src = environment.matomo.hostUrl + 'matomo.js'; + document.getElementsByTagName('head')[0].appendChild(g); + } catch (error) { + if (!environment.production) { + console.error('Error initializing Matomo:', error); + } + } } // support async tag or hmr if (document.readyState === 'complete' && !hasTransferState) { void main(); } else { - document.addEventListener('DOMContentLoaded', () => main()); + document.addEventListener('DOMContentLoaded', () => void main()); } From 28f23a160601495370c10beddec8b6bf8b30b4c7 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 19 Sep 2025 15:30:34 +0200 Subject: [PATCH 07/11] removed consol log, made info more important --- .../clarin-matomo-statistics.component.html | 2 +- src/main.browser.ts | 17 ++--------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.html b/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.html index d8097762f69..ab179b15869 100644 --- a/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.html +++ b/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.html @@ -1,6 +1,6 @@
- + Matomo statistics are currently disabled.
diff --git a/src/main.browser.ts b/src/main.browser.ts index c0a97fd1d49..ccbef239cb0 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -45,20 +45,8 @@ const main = () => { }; function addMatomoStatistics() { - // Debug: Log the matomo configuration - if (!environment.production) { - console.log('Matomo config check:', { - hasMatomo: !!environment.matomo, - enabled: environment.matomo?.enabled, - full: environment.matomo - }); - } - // Check if Matomo is enabled in the environment configuration if (!environment.matomo || !environment.matomo.enabled) { - if (!environment.production) { - console.log('Matomo is disabled, skipping initialization'); - } return; } @@ -77,9 +65,8 @@ function addMatomoStatistics() { g.src = environment.matomo.hostUrl + 'matomo.js'; document.getElementsByTagName('head')[0].appendChild(g); } catch (error) { - if (!environment.production) { - console.error('Error initializing Matomo:', error); - } + // Silently fail if Matomo initialization fails + return; } } From 7b2474a2b8cd0140fe031d39ffc285ee56a5ab4a Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 19 Sep 2025 15:45:31 +0200 Subject: [PATCH 08/11] copilot review --- src/main.browser.ts | 3 ++- src/modules/app/browser-init.service.ts | 27 +++++++++++++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main.browser.ts b/src/main.browser.ts index ccbef239cb0..4c819eb09ae 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -65,7 +65,8 @@ function addMatomoStatistics() { g.src = environment.matomo.hostUrl + 'matomo.js'; document.getElementsByTagName('head')[0].appendChild(g); } catch (error) { - // Silently fail if Matomo initialization fails + // Log the error to help with debugging Matomo initialization issues + console.warn('Matomo initialization failed:', error); return; } } diff --git a/src/modules/app/browser-init.service.ts b/src/modules/app/browser-init.service.ts index 9b6de2a165a..600ae1c3563 100644 --- a/src/modules/app/browser-init.service.ts +++ b/src/modules/app/browser-init.service.ts @@ -114,18 +114,23 @@ export class BrowserInitService extends InitService { // in pageTrack context. So we add it to page_view events, and remove it after the page view. // page_view events are fired via view-track.component, and exposes dc.identifier.uri via properties const dimensionId = this.appConfig.matomo.dimensionId; - this.angulartics2Matomo.eventTrack = function (action: string, properties?: any) { - if (action === 'page_view') { - if (properties.dc_identifier) { - (window as any)._paq.push(['setCustomDimension', dimensionId, properties.dc_identifier]); + + // Only set up custom dimensions if dimensionId is configured + if (dimensionId) { + this.angulartics2Matomo.eventTrack = function (action: string, properties?: any) { + if (action === 'page_view') { + if (properties.dc_identifier) { + (window as any)._paq.push(['setCustomDimension', dimensionId, properties.dc_identifier]); + } } - } - }; - let pageTrack = this.angulartics2Matomo.pageTrack; - this.angulartics2Matomo.pageTrack = function (path: string) { - pageTrack.call(this, path); - (window as any)._paq.push(['deleteCustomDimension', dimensionId]); - }; + }; + let pageTrack = this.angulartics2Matomo.pageTrack; + this.angulartics2Matomo.pageTrack = function (path: string) { + pageTrack.call(this, path); + (window as any)._paq.push(['deleteCustomDimension', dimensionId]); + }; + } + this.angulartics2Matomo.startTracking(); } From b2a2b669299367d04df0bd6edff78f2ae8d8b672 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 19 Sep 2025 16:50:04 +0200 Subject: [PATCH 09/11] added controls if host is defined --- src/main.browser.ts | 7 +++---- src/modules/app/browser-init.service.ts | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main.browser.ts b/src/main.browser.ts index 4c819eb09ae..99abf8b9b00 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -45,8 +45,8 @@ const main = () => { }; function addMatomoStatistics() { - // Check if Matomo is enabled in the environment configuration - if (!environment.matomo || !environment.matomo.enabled) { + // Check if Matomo is configured with required properties + if (!environment.matomo || !environment.matomo.hostUrl || !environment.matomo.siteId) { return; } @@ -65,8 +65,7 @@ function addMatomoStatistics() { g.src = environment.matomo.hostUrl + 'matomo.js'; document.getElementsByTagName('head')[0].appendChild(g); } catch (error) { - // Log the error to help with debugging Matomo initialization issues - console.warn('Matomo initialization failed:', error); + // Silently fail if Matomo initialization fails to avoid interfering with tests return; } } diff --git a/src/modules/app/browser-init.service.ts b/src/modules/app/browser-init.service.ts index 600ae1c3563..fd7716e570a 100644 --- a/src/modules/app/browser-init.service.ts +++ b/src/modules/app/browser-init.service.ts @@ -107,9 +107,9 @@ export class BrowserInitService extends InitService { this.initRouteListeners(); this.themeService.listenForThemeChanges(true); this.trackAuthTokenExpiration(); - - // Only initialize Matomo if it's enabled in configuration - if (this.appConfig.matomo && this.appConfig.matomo.enabled) { + + // Only initialize Matomo if it's configured with required properties + if (this.appConfig.matomo && this.appConfig.matomo.hostUrl && this.appConfig.matomo.siteId) { // ideally we'd add the custom dimension to the 'trackPageView' action only, but don't have that information // in pageTrack context. So we add it to page_view events, and remove it after the page view. // page_view events are fired via view-track.component, and exposes dc.identifier.uri via properties From eec6072f17ea9d21f82f1694971e74d755b23511 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 19 Sep 2025 17:02:52 +0200 Subject: [PATCH 10/11] added debug info to console, fix logic about matomo disability --- .../clarin-matomo-statistics.component.ts | 4 ++-- src/main.browser.ts | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts b/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts index d84bdf338d9..101105d2f88 100644 --- a/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts +++ b/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts @@ -135,8 +135,8 @@ export class ClarinMatomoStatisticsComponent implements OnInit { ngOnInit(): void { // Check if Matomo is enabled in the environment - this.matomoEnabled = environment.matomo?.enabled || false; - + this.matomoEnabled = environment.matomo?.enabled === true; + // If Matomo is disabled, don't fetch statistics if (!this.matomoEnabled) { return; diff --git a/src/main.browser.ts b/src/main.browser.ts index 99abf8b9b00..b96af5795cf 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -65,7 +65,12 @@ function addMatomoStatistics() { g.src = environment.matomo.hostUrl + 'matomo.js'; document.getElementsByTagName('head')[0].appendChild(g); } catch (error) { - // Silently fail if Matomo initialization fails to avoid interfering with tests + // Log error unless in a test environment to aid debugging in production + if (!('test' in environment && environment.test)) { + // Log the error to the console for visibility in production and development + console.error('Matomo initialization failed:', error); + } + // Silently fail in test environments to avoid interfering with tests return; } } From 044c41c7836bbaa585d74edc4d0c8d219e05ba00 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Tue, 23 Sep 2025 10:16:10 +0200 Subject: [PATCH 11/11] removed comments, fix error --- .../clarin-matomo-statistics.component.ts | 4 +--- src/main.browser.ts | 7 +------ 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts b/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts index 101105d2f88..858d26a7e7a 100644 --- a/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts +++ b/src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts @@ -27,8 +27,7 @@ export class ClarinMatomoStatisticsComponent implements OnInit { @ViewChild(BaseChartDirective, { static: true }) chart: BaseChartDirective; itemRD$: BehaviorSubject = new BehaviorSubject(null); - - // Flag to track if Matomo is enabled + public matomoEnabled = false; // Month shortcut with full name @@ -134,7 +133,6 @@ export class ClarinMatomoStatisticsComponent implements OnInit { ngOnInit(): void { - // Check if Matomo is enabled in the environment this.matomoEnabled = environment.matomo?.enabled === true; // If Matomo is disabled, don't fetch statistics diff --git a/src/main.browser.ts b/src/main.browser.ts index b96af5795cf..49e9242b8ab 100644 --- a/src/main.browser.ts +++ b/src/main.browser.ts @@ -65,12 +65,7 @@ function addMatomoStatistics() { g.src = environment.matomo.hostUrl + 'matomo.js'; document.getElementsByTagName('head')[0].appendChild(g); } catch (error) { - // Log error unless in a test environment to aid debugging in production - if (!('test' in environment && environment.test)) { - // Log the error to the console for visibility in production and development - console.error('Matomo initialization failed:', error); - } - // Silently fail in test environments to avoid interfering with tests + console.error('Matomo initialization failed:', error); return; } }