Skip to content

Commit 926aa79

Browse files
committed
added configuration about disability of matomo statistics
1 parent 6e16f95 commit 926aa79

10 files changed

Lines changed: 163 additions & 81 deletions

File tree

MATOMO_CONFIGURATION.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Matomo Statistics Configuration
2+
3+
## Enable/Disable Matomo
4+
5+
### To Disable Matomo (Default)
6+
Edit `src/environments/environment.ts`:
7+
```typescript
8+
matomo: {
9+
enabled: false,
10+
hostUrl: 'http://localhost:8135/',
11+
siteId: '1',
12+
dimensionId: 1
13+
}
14+
```
15+
16+
### To Enable Matomo
17+
Edit `src/environments/environment.ts`:
18+
```typescript
19+
matomo: {
20+
enabled: true,
21+
hostUrl: 'https://your-matomo-server.com/',
22+
siteId: '1',
23+
dimensionId: 1
24+
}
25+
```
26+
27+
## Files to Modify
28+
29+
- **Development**: `src/environments/environment.ts`
30+
- **Production**: `src/environments/environment.production.ts`
31+
32+
## After Changes
33+
34+
1. Restart the application
35+
2. Clear browser cache
36+
37+
When disabled: No Matomo scripts load, statistics page shows "disabled" message.
38+
When enabled: Matomo tracking works normally.

config/config.example.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,5 +465,6 @@ accessibility:
465465
cookieExpirationDuration: 7
466466

467467
matomo:
468+
enabled: false
468469
hostUrl: http://localhost:8135/
469470
siteId: 1
Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,56 @@
11
<div class="container">
2-
<div class="row px-2 py-2 rounded alert-info">
3-
<i class="fas fa-info-circle pt-1"></i>
4-
<span class="pl-2">{{'item.matomo-statistics.info.message' | translate}}</span>
2+
<div *ngIf="!matomoEnabled" class="row px-2 py-2 rounded alert-warning">
3+
<i class="fas fa-exclamation-triangle pt-1"></i>
4+
<span class="pl-2">Matomo statistics are currently disabled.</span>
55
</div>
6-
<div class="row pt-4 clarin-bottom-line">
7-
<button type="button" class="col-md-2 clarin-chart-button btn btn-outline-success justify-content-start py-2 px-3"
8-
[ngClass]="viewsButtonClicked ? 'clarin-chart-views-clicked' : 'clarin-chart-views'"
9-
role="button"
10-
data-bs-toggle="button"
11-
(click)="toggleViews()">
12-
<div class="align-items-center justify-content-center d-flex"><i class="far fa-eye"></i></div>
13-
<div class="align-items-center justify-content-center d-flex">{{'item.page.matomo-statistics.views.button' | translate}}</div>
14-
</button>
15-
<button type="button" class="col-md-2 clarin-chart-button btn btn-outline-info py-2 px-3 ml-1"
16-
[ngClass]="downloadsButtonClicked ? 'clarin-chart-downloads-clicked' : 'clarin-chart-downloads'"
17-
data-bs-toggle="button"
18-
(click)="toggleDownload()">
19-
<div class="align-items-center justify-content-center d-flex"><i class="far fa-eye"></i></div>
20-
<div class="align-items-center justify-content-center d-flex">{{'item.page.matomo-statistics.downloads.button' | translate}}</div>
21-
</button>
22-
</div>
23-
<div class="row pt-2 justify-content-end">
24-
<span class="chart-message rounded px-2 py-2">{{chartMessage}}</span>
25-
<button class="btn clarin-btn-back rounded px-2 py-2 ml-2" *ngIf="!(this.actualPeriod === this.periodYear)" (click)="back()">Back</button>
26-
</div>
27-
<div class="chart pt-2">
28-
<canvas baseChart
29-
[datasets]="chartLabels.datasets"
30-
[labels]="chartLabels.labels"
31-
[options]="chartOptions"
32-
[legend]="false"
33-
(chartClick)="chartClicked($event)"
34-
chartType="line">
35-
</canvas>
36-
</div>
37-
<div *ngIf="downloadsButtonClicked" class="pt-3">
38-
<table class="table table-striped">
39-
<thead>
40-
Filewise Statistics
41-
</thead>
42-
<tbody>
43-
<tr *ngFor="let fileData of (filesDownloads | async) | dsKeys">
44-
<td><b>{{fileData.value}}</b></td>
45-
<td>{{fileData.key}}</td>
46-
</tr>
47-
</tbody>
48-
</table>
6+
<div *ngIf="matomoEnabled">
7+
<div class="row px-2 py-2 rounded alert-info">
8+
<i class="fas fa-info-circle pt-1"></i>
9+
<span class="pl-2">{{'item.matomo-statistics.info.message' | translate}}</span>
10+
</div>
11+
<div class="row pt-4 clarin-bottom-line">
12+
<button type="button" class="col-md-2 clarin-chart-button btn btn-outline-success justify-content-start py-2 px-3"
13+
[ngClass]="viewsButtonClicked ? 'clarin-chart-views-clicked' : 'clarin-chart-views'"
14+
role="button"
15+
data-bs-toggle="button"
16+
(click)="toggleViews()">
17+
<div class="align-items-center justify-content-center d-flex"><i class="far fa-eye"></i></div>
18+
<div class="align-items-center justify-content-center d-flex">{{'item.page.matomo-statistics.views.button' | translate}}</div>
19+
</button>
20+
<button type="button" class="col-md-2 clarin-chart-button btn btn-outline-info py-2 px-3 ml-1"
21+
[ngClass]="downloadsButtonClicked ? 'clarin-chart-downloads-clicked' : 'clarin-chart-downloads'"
22+
data-bs-toggle="button"
23+
(click)="toggleDownload()">
24+
<div class="align-items-center justify-content-center d-flex"><i class="far fa-eye"></i></div>
25+
<div class="align-items-center justify-content-center d-flex">{{'item.page.matomo-statistics.downloads.button' | translate}}</div>
26+
</button>
27+
</div>
28+
<div class="row pt-2 justify-content-end">
29+
<span class="chart-message rounded px-2 py-2">{{chartMessage}}</span>
30+
<button class="btn clarin-btn-back rounded px-2 py-2 ml-2" *ngIf="!(this.actualPeriod === this.periodYear)" (click)="back()">Back</button>
31+
</div>
32+
<div class="chart pt-2">
33+
<canvas baseChart
34+
[datasets]="chartLabels.datasets"
35+
[labels]="chartLabels.labels"
36+
[options]="chartOptions"
37+
[legend]="false"
38+
(chartClick)="chartClicked($event)"
39+
chartType="line">
40+
</canvas>
41+
</div>
42+
<div *ngIf="downloadsButtonClicked" class="pt-3">
43+
<table class="table table-striped">
44+
<thead>
45+
Filewise Statistics
46+
</thead>
47+
<tbody>
48+
<tr *ngFor="let fileData of (filesDownloads | async) | dsKeys">
49+
<td><b>{{fileData.value}}</b></td>
50+
<td>{{fileData.key}}</td>
51+
</tr>
52+
</tbody>
53+
</table>
54+
</div>
4955
</div>
5056
</div>

src/app/item-page/clarin-matomo-statistics/clarin-matomo-statistics.component.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { map } from 'rxjs/operators';
1010
import { ActivatedRoute } from '@angular/router';
1111
import { RemoteData } from '../../core/data/remote-data';
1212
import { Item } from '../../core/shared/item.model';
13+
import { environment } from '../../../environments/environment';
1314

1415
@Component({
1516
selector: 'ds-clarin-matomo-statistics',
@@ -26,6 +27,9 @@ export class ClarinMatomoStatisticsComponent implements OnInit {
2627
@ViewChild(BaseChartDirective, { static: true }) chart: BaseChartDirective;
2728

2829
itemRD$: BehaviorSubject<Item> = new BehaviorSubject<Item>(null);
30+
31+
// Flag to track if Matomo is enabled
32+
public matomoEnabled = false;
2933

3034
// Month shortcut with full name
3135
public months = [
@@ -130,6 +134,14 @@ export class ClarinMatomoStatisticsComponent implements OnInit {
130134

131135

132136
ngOnInit(): void {
137+
// Check if Matomo is enabled in the environment
138+
this.matomoEnabled = environment.matomo?.enabled || false;
139+
140+
// If Matomo is disabled, don't fetch statistics
141+
if (!this.matomoEnabled) {
142+
return;
143+
}
144+
133145
this.route.data.pipe(
134146
map((data) => data.dso as RemoteData<Item>))
135147
.subscribe(data => {

src/config/default-app-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ export class DefaultAppConfig implements AppConfig {
452452

453453
// Matomo configuration
454454
matomo: MatomoConfig = {
455+
enabled: false,
455456
hostUrl: 'http://localhost:8135/',
456457
siteId: '1',
457458
dimensionId: 1

src/config/matomo-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { Config } from './config.interface';
55
*/
66
export class MatomoConfig implements Config {
77

8+
public enabled: boolean;
9+
810
public hostUrl: string;
911

1012
public siteId: string;

src/environments/environment.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ export const environment: BuildConfig = {
356356
signpostingEnabled: true,
357357

358358
matomo: {
359+
enabled: false,
359360
hostUrl: 'http://localhost:8135/',
360361
siteId: '1',
361362
dimensionId: 1

src/environments/environment.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ export const environment: Partial<BuildConfig> = {
4949
},
5050

5151
signpostingEnabled: false,
52+
53+
// Matomo configuration
54+
matomo: {
55+
enabled: false,
56+
hostUrl: 'http://localhost:8135/',
57+
siteId: '1',
58+
dimensionId: 1
59+
},
5260
};
5361

5462
/*

src/main.browser.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,39 @@ const main = () => {
4444
};
4545

4646
function addMatomoStatistics() {
47-
(window as any)._paq = (window as any)._paq || [];
47+
// Debug: Log the matomo configuration
48+
console.log('Matomo config check:', {
49+
hasMatomo: !!environment.matomo,
50+
enabled: environment.matomo?.enabled,
51+
full: environment.matomo
52+
});
53+
54+
// Check if Matomo is enabled in the environment configuration
55+
if (!environment.matomo || !environment.matomo.enabled) {
56+
console.log('Matomo is disabled, skipping initialization');
57+
return;
58+
}
4859

49-
void fetch('assets/config.json')
50-
.then((response) => response.json())
51-
.then((config) => {
52-
const matomoConfig = config.matomo;
60+
console.log('Matomo is enabled, initializing...');
61+
62+
(window as any)._paq = (window as any)._paq || [];
5363

54-
// Push all configuration commands first
55-
(window as any)._paq.push(['setTrackerUrl', matomoConfig.hostUrl + 'matomo.php']);
56-
(window as any)._paq.push(['setSiteId', matomoConfig.siteId]);
57-
(window as any)._paq.push(['enableLinkTracking']);
64+
// Push all configuration commands first
65+
(window as any)._paq.push(['setTrackerUrl', environment.matomo.hostUrl + 'matomo.php']);
66+
(window as any)._paq.push(['setSiteId', environment.matomo.siteId]);
67+
(window as any)._paq.push(['enableLinkTracking']);
5868

59-
const g = document.createElement('script');
60-
g.type = 'text/javascript';
61-
g.async = true;
62-
g.defer = true;
63-
g.src = matomoConfig.hostUrl + 'matomo.js';
64-
document.getElementsByTagName('head')[0].appendChild(g);
65-
});
69+
const g = document.createElement('script');
70+
g.type = 'text/javascript';
71+
g.async = true;
72+
g.defer = true;
73+
g.src = environment.matomo.hostUrl + 'matomo.js';
74+
document.getElementsByTagName('head')[0].appendChild(g);
6675
}
6776

6877
// support async tag or hmr
6978
if (document.readyState === 'complete' && !hasTransferState) {
70-
main();
79+
void main();
7180
} else {
72-
document.addEventListener('DOMContentLoaded', main);
81+
document.addEventListener('DOMContentLoaded', () => void main());
7382
}

src/modules/app/browser-init.service.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,30 @@ export class BrowserInitService extends InitService {
107107
this.initRouteListeners();
108108
this.themeService.listenForThemeChanges(true);
109109
this.trackAuthTokenExpiration();
110-
// ideally we'd add the custom dimension to the 'trackPageView' action only, but don't have that information
111-
// in pageTrack context. So we add it to page_view events, and remove it after the page view.
112-
// page_view events are fired via view-track.component, and exposes dc.identifier.uri via properties
113-
this.angulartics2Matomo.eventTrack = function (action: string, properties?: any) {
114-
if (action === 'page_view') {
115-
if (properties.dc_identifier) {
116-
(window as any)._paq.push(['setCustomDimension', environment.matomo.dimensionId, properties.dc_identifier]);
110+
111+
// Only initialize Matomo if it's enabled in configuration
112+
if (this.appConfig.matomo && this.appConfig.matomo.enabled) {
113+
// ideally we'd add the custom dimension to the 'trackPageView' action only, but don't have that information
114+
// in pageTrack context. So we add it to page_view events, and remove it after the page view.
115+
// page_view events are fired via view-track.component, and exposes dc.identifier.uri via properties
116+
this.angulartics2Matomo.eventTrack = function (action: string, properties?: any) {
117+
if (action === 'page_view') {
118+
if (properties.dc_identifier) {
119+
(window as any)._paq.push(['setCustomDimension', this.appConfig.matomo.dimensionId, properties.dc_identifier]);
120+
}
117121
}
118-
}
119-
};
120-
let pageTrack = this.angulartics2Matomo.pageTrack;
121-
this.angulartics2Matomo.pageTrack = function (path: string) {
122-
pageTrack.call(this, path);
123-
(window as any)._paq.push(['deleteCustomDimension', environment.matomo.dimensionId]);
124-
};
125-
this.angulartics2Matomo.startTracking();
122+
}.bind(this);
123+
let pageTrack = this.angulartics2Matomo.pageTrack;
124+
this.angulartics2Matomo.pageTrack = function (path: string) {
125+
pageTrack.call(this, path);
126+
(window as any)._paq.push(['deleteCustomDimension', this.appConfig.matomo.dimensionId]);
127+
}.bind(this);
128+
this.angulartics2Matomo.startTracking();
129+
}
126130

127131
this.initKlaro();
128132

129-
await this.authenticationReady$().toPromise();
133+
await firstValueFrom(this.authenticationReady$());
130134

131135
return true;
132136
};
@@ -197,7 +201,7 @@ export class BrowserInitService extends InitService {
197201
* @private
198202
*/
199203
private closeAuthCheckSubscription() {
200-
firstValueFrom(this.authenticationReady$()).then(() => {
204+
void firstValueFrom(this.authenticationReady$()).then(() => {
201205
this.sub.unsubscribe();
202206
});
203207
}

0 commit comments

Comments
 (0)