Skip to content

Commit cace253

Browse files
author
Andrea Barbasso
committed
[DURACOM-359] fix matomo downloading its .js even if it's disabled
1 parent 353d660 commit cace253

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/app/statistics/matomo.service.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
createSuccessfulRemoteDataObject$,
2424
} from '../shared/remote-data.utils';
2525
import {
26+
MATOMO_ENABLED,
2627
MATOMO_SITE_ID,
2728
MATOMO_TRACKER_URL,
2829
MatomoService,
@@ -84,6 +85,9 @@ describe('MatomoService', () => {
8485
configService.findByPropertyName.withArgs(MATOMO_TRACKER_URL).and.returnValue(
8586
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(),{ values: ['http://matomo'] })),
8687
);
88+
configService.findByPropertyName.withArgs(MATOMO_ENABLED).and.returnValue(
89+
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(),{ values: ['true'] })),
90+
);
8791
configService.findByPropertyName.withArgs(MATOMO_SITE_ID).and.returnValue(
8892
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), { values: ['1'] })));
8993
orejimeService.getSavedPreferences.and.returnValue(of({ matomo: true }));
@@ -102,6 +106,9 @@ describe('MatomoService', () => {
102106
configService.findByPropertyName.withArgs(MATOMO_TRACKER_URL).and.returnValue(
103107
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(),{ values: ['http://example.com'] })),
104108
);
109+
configService.findByPropertyName.withArgs(MATOMO_ENABLED).and.returnValue(
110+
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(),{ values: ['true'] })),
111+
);
105112
configService.findByPropertyName.withArgs(MATOMO_SITE_ID).and.returnValue(
106113
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), { values: ['1'] })));
107114
orejimeService.getSavedPreferences.and.returnValue(of({ matomo: true }));
@@ -123,6 +130,24 @@ describe('MatomoService', () => {
123130
expect(matomoInitializer.initializeTracker).not.toHaveBeenCalled();
124131
});
125132

133+
it('should not initialize tracker if matomo is disabled', () => {
134+
environment.production = true;
135+
environment.matomo = { trackerUrl: '' };
136+
configService.findByPropertyName.withArgs(MATOMO_TRACKER_URL).and.returnValue(
137+
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(),{ values: ['http://example.com'] })),
138+
);
139+
configService.findByPropertyName.withArgs(MATOMO_ENABLED).and.returnValue(
140+
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(),{ values: ['false'] })),
141+
);
142+
configService.findByPropertyName.withArgs(MATOMO_SITE_ID).and.returnValue(
143+
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), { values: ['1'] })));
144+
orejimeService.getSavedPreferences.and.returnValue(of({ matomo: true }));
145+
146+
service.init();
147+
148+
expect(matomoInitializer.initializeTracker).not.toHaveBeenCalled();
149+
});
150+
126151
describe('with visitorId set', () => {
127152
beforeEach(() => {
128153
matomoTracker.getVisitorId.and.returnValue(Promise.resolve('12345'));

src/app/statistics/matomo.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ export class MatomoService {
7777
preferences$
7878
.pipe(
7979
tap(preferences => this.changeMatomoConsent(preferences?.matomo)),
80-
switchMap(_ => combineLatest([this.getSiteId$(), this.getTrackerUrl$()])),
80+
switchMap(_ => combineLatest([this.isMatomoEnabled$(), this.getSiteId$(), this.getTrackerUrl$()])),
8181
)
82-
.subscribe(([siteId, trackerUrl]) => {
83-
if (siteId && trackerUrl) {
82+
.subscribe(([isMatomoEnabled, siteId, trackerUrl]) => {
83+
if (isMatomoEnabled && siteId && trackerUrl) {
8484
this.matomoInitializer.initializeTracker({ siteId, trackerUrl });
8585
}
8686
});

0 commit comments

Comments
 (0)