Skip to content

Commit 3d36cd2

Browse files
author
Andrea Barbasso
committed
[DURACOM-339] fix downloads not working when matomo is disabled
1 parent c9654b2 commit 3d36cd2

File tree

4 files changed

+81
-13
lines changed

4 files changed

+81
-13
lines changed

src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.spec.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { of as observableOf } from 'rxjs';
1818
import { getForbiddenRoute } from '../../app-routing-paths';
1919
import { AuthService } from '../../core/auth/auth.service';
2020
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
21+
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
2122
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
2223
import { SignpostingDataService } from '../../core/data/signposting-data.service';
2324
import { HardRedirectService } from '../../core/services/hard-redirect.service';
@@ -112,7 +113,10 @@ describe('BitstreamDownloadPageComponent', () => {
112113
signpostingDataService = jasmine.createSpyObj('SignpostingDataService', {
113114
getLinks: observableOf([mocklink, mocklink2]),
114115
});
115-
matomoService = jasmine.createSpyObj('MatomoService', ['appendVisitorId']);
116+
matomoService = jasmine.createSpyObj('MatomoService', {
117+
appendVisitorId: observableOf(''),
118+
isMatomoEnabled$: observableOf(true),
119+
});
116120
matomoService.appendVisitorId.and.callFake((link) => observableOf(link));
117121
}
118122

@@ -132,6 +136,7 @@ describe('BitstreamDownloadPageComponent', () => {
132136
{ provide: PLATFORM_ID, useValue: 'server' },
133137
{ provide: Location, useValue: location },
134138
{ provide: DSONameService, useValue: dsoNameService },
139+
{ provide: ConfigurationDataService, useValue: {} },
135140
],
136141
})
137142
.compileComponents();
@@ -227,4 +232,42 @@ describe('BitstreamDownloadPageComponent', () => {
227232
}));
228233
});
229234
});
235+
236+
describe('when Matomo is enabled', () => {
237+
beforeEach(waitForAsync(() => {
238+
init();
239+
(matomoService.appendVisitorId as jasmine.Spy).and.callFake((link) => observableOf(link + '?visitorId=12345'));
240+
initTestbed();
241+
}));
242+
beforeEach(() => {
243+
fixture = TestBed.createComponent(BitstreamDownloadPageComponent);
244+
component = fixture.componentInstance;
245+
fixture.detectChanges();
246+
});
247+
it('should append visitor ID to the file link', waitForAsync(() => {
248+
fixture.whenStable().then(() => {
249+
expect(matomoService.appendVisitorId).toHaveBeenCalledWith('content-url-with-headers');
250+
expect(hardRedirectService.redirect).toHaveBeenCalledWith('content-url-with-headers?visitorId=12345');
251+
});
252+
}));
253+
});
254+
255+
describe('when Matomo is not enabled', () => {
256+
beforeEach(waitForAsync(() => {
257+
init();
258+
(matomoService.isMatomoEnabled$ as jasmine.Spy).and.returnValue(observableOf(false));
259+
initTestbed();
260+
}));
261+
beforeEach(() => {
262+
fixture = TestBed.createComponent(BitstreamDownloadPageComponent);
263+
component = fixture.componentInstance;
264+
fixture.detectChanges();
265+
});
266+
it('should not append visitor ID to the file link', waitForAsync(() => {
267+
fixture.whenStable().then(() => {
268+
expect(matomoService.appendVisitorId).not.toHaveBeenCalled();
269+
expect(hardRedirectService.redirect).toHaveBeenCalledWith('content-url-with-headers');
270+
});
271+
}));
272+
});
230273
});

src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import {
77
Component,
88
Inject,
9+
inject,
910
OnInit,
1011
PLATFORM_ID,
1112
} from '@angular/core';
@@ -30,6 +31,7 @@ import {
3031
import { getForbiddenRoute } from '../../app-routing-paths';
3132
import { AuthService } from '../../core/auth/auth.service';
3233
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
34+
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
3335
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
3436
import { FeatureID } from '../../core/data/feature-authorization/feature-id';
3537
import { RemoteData } from '../../core/data/remote-data';
@@ -64,6 +66,8 @@ export class BitstreamDownloadPageComponent implements OnInit {
6466
bitstream$: Observable<Bitstream>;
6567
bitstreamRD$: Observable<RemoteData<Bitstream>>;
6668

69+
configService = inject(ConfigurationDataService);
70+
6771
constructor(
6872
private route: ActivatedRoute,
6973
protected router: Router,
@@ -103,30 +107,33 @@ export class BitstreamDownloadPageComponent implements OnInit {
103107
switchMap((bitstream: Bitstream) => {
104108
const isAuthorized$ = this.authorizationService.isAuthorized(FeatureID.CanDownload, isNotEmpty(bitstream) ? bitstream.self : undefined);
105109
const isLoggedIn$ = this.auth.isAuthenticated();
106-
return observableCombineLatest([isAuthorized$, isLoggedIn$, accessToken$, observableOf(bitstream)]);
110+
const isMatomoEnabled$ = this.matomoService.isMatomoEnabled$();
111+
return observableCombineLatest([isAuthorized$, isLoggedIn$, isMatomoEnabled$, accessToken$, observableOf(bitstream)]);
107112
}),
108-
filter(([isAuthorized, isLoggedIn, accessToken, bitstream]: [boolean, boolean, string, Bitstream]) => (hasValue(isAuthorized) && hasValue(isLoggedIn)) || hasValue(accessToken)),
113+
filter(([isAuthorized, isLoggedIn, isMatomoEnabled, accessToken, bitstream]: [boolean, boolean, boolean, string, Bitstream]) => (hasValue(isAuthorized) && hasValue(isLoggedIn)) || hasValue(accessToken)),
109114
take(1),
110-
switchMap(([isAuthorized, isLoggedIn, accessToken, bitstream]: [boolean, boolean, string, Bitstream]) => {
115+
switchMap(([isAuthorized, isLoggedIn, isMatomoEnabled, accessToken, bitstream]: [boolean, boolean, boolean, string, Bitstream]) => {
111116
if (isAuthorized && isLoggedIn) {
112117
return this.fileService.retrieveFileDownloadLink(bitstream._links.content.href).pipe(
113118
filter((fileLink) => hasValue(fileLink)),
114119
take(1),
115120
map((fileLink) => {
116-
return [isAuthorized, isLoggedIn, bitstream, fileLink];
121+
return [isAuthorized, isLoggedIn, isMatomoEnabled, bitstream, fileLink];
117122
}));
118123
} else if (hasValue(accessToken)) {
119-
return [[isAuthorized, !isLoggedIn, bitstream, '', accessToken]];
124+
return [[isAuthorized, !isLoggedIn, isMatomoEnabled, bitstream, '', accessToken]];
120125
} else {
121-
return [[isAuthorized, isLoggedIn, bitstream, bitstream._links.content.href]];
126+
return [[isAuthorized, isLoggedIn, isMatomoEnabled, bitstream, bitstream._links.content.href]];
122127
}
123128
}),
124-
switchMap(([isAuthorized, isLoggedIn, bitstream, fileLink, accessToken]: [boolean, boolean, Bitstream, string, string]) =>
125-
this.matomoService.appendVisitorId(fileLink)
126-
.pipe(
129+
switchMap(([isAuthorized, isLoggedIn, isMatomoEnabled, bitstream, fileLink, accessToken]: [boolean, boolean, boolean, Bitstream, string, string]) => {
130+
if (isMatomoEnabled) {
131+
return this.matomoService.appendVisitorId(fileLink).pipe(
127132
map((fileLinkWithVisitorId) => [isAuthorized, isLoggedIn, bitstream, fileLinkWithVisitorId, accessToken]),
128-
),
129-
),
133+
);
134+
}
135+
return observableOf([isAuthorized, isLoggedIn, bitstream, fileLink, accessToken]);
136+
}),
130137
).subscribe(([isAuthorized, isLoggedIn, bitstream, fileLink, accessToken]: [boolean, boolean, Bitstream, string, string]) => {
131138
if (isAuthorized && isLoggedIn && isNotEmpty(fileLink)) {
132139
this.hardRedirectService.redirect(fileLink);

src/app/shared/cookies/browser-orejime.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
NativeWindowService,
3131
} from '../../core/services/window.service';
3232
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
33+
import { MATOMO_ENABLED } from '../../statistics/matomo.service';
3334
import {
3435
hasValue,
3536
isEmpty,
@@ -90,7 +91,7 @@ export class BrowserOrejimeService extends OrejimeService {
9091

9192
private readonly GOOGLE_ANALYTICS_SERVICE_NAME = 'google-analytics';
9293

93-
private readonly MATOMO_ENABLED = 'matomo.enabled';
94+
private readonly MATOMO_ENABLED = MATOMO_ENABLED;
9495

9596
/**
9697
* Initial Orejime configuration

src/app/statistics/matomo.service.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import { isNotEmpty } from '../shared/empty.util';
3131
export const MATOMO_TRACKER_URL = 'matomo.tracker.url';
3232
export const MATOMO_SITE_ID = 'matomo.request.siteid';
3333

34+
export const MATOMO_ENABLED = 'matomo.enabled';
35+
3436
/**
3537
* Service to manage Matomo analytics integration.
3638
* Handles initialization and consent management for Matomo tracking.
@@ -143,6 +145,21 @@ export class MatomoService {
143145
);
144146
}
145147

148+
/**
149+
* Checks if Matomo tracking is enabled by retrieving the configuration property.
150+
* @returns An Observable that emits a boolean indicating whether Matomo tracking is enabled.
151+
*/
152+
isMatomoEnabled$(): Observable<boolean> {
153+
return this.configService.findByPropertyName(MATOMO_ENABLED)
154+
.pipe(
155+
getFirstCompletedRemoteData(),
156+
map((res: RemoteData<ConfigurationProperty>) => {
157+
return res.hasSucceeded && res.payload && isNotEmpty(res.payload.values) &&
158+
res.payload.values[0]?.toLowerCase() === 'true';
159+
}),
160+
);
161+
}
162+
146163
/**
147164
* Appends the visitor ID as a query parameter to the given URL.
148165
* @param url - The original URL to modify

0 commit comments

Comments
 (0)