Skip to content

Commit 37cef0f

Browse files
author
Andrea Barbasso
committed
[DURACOM-367] fix matomo SSR error on bitstream download page
1 parent 73bd0d6 commit 37cef0f

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

src/app/footer/footer.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class FooterComponent implements OnInit {
6060
}
6161

6262
showCookieSettings() {
63-
if (hasValue(this.cookies)) {
63+
if (hasValue(this.cookies) && this.cookies.showSettings instanceof Function) {
6464
this.cookies.showSettings();
6565
}
6666
return false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Observable } from 'rxjs';
44
/**
55
* Abstract class representing a service for handling Orejime consent preferences and UI
66
*/
7-
@Injectable()
7+
@Injectable({ providedIn: 'root' })
88
export abstract class OrejimeService {
99
/**
1010
* Initializes the service

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
Injector,
3+
runInInjectionContext,
4+
} from '@angular/core';
15
import {
26
fakeAsync,
37
TestBed,
@@ -53,6 +57,7 @@ describe('MatomoService', () => {
5357
{ provide: OrejimeService, useValue: orejimeService },
5458
{ provide: NativeWindowService, useValue: nativeWindowService },
5559
{ provide: ConfigurationDataService, useValue: configService },
60+
{ provide: Injector, useValue: TestBed },
5661
],
5762
});
5863

@@ -70,11 +75,13 @@ describe('MatomoService', () => {
7075
});
7176

7277
it('should call setConsentGiven when consent is true', () => {
78+
service.matomoTracker = matomoTracker;
7379
service.changeMatomoConsent(true);
7480
expect(matomoTracker.setConsentGiven).toHaveBeenCalled();
7581
});
7682

7783
it('should call forgetConsentGiven when consent is false', () => {
84+
service.matomoTracker = matomoTracker;
7885
service.changeMatomoConsent(false);
7986
expect(matomoTracker.forgetConsentGiven).toHaveBeenCalled();
8087
});
@@ -91,7 +98,10 @@ describe('MatomoService', () => {
9198
configService.findByPropertyName.withArgs(MATOMO_SITE_ID).and.returnValue(
9299
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), { values: ['1'] })));
93100
orejimeService.getSavedPreferences.and.returnValue(of({ matomo: true }));
94-
service.init();
101+
102+
runInInjectionContext(TestBed, () => {
103+
service.init();
104+
});
95105

96106
expect(matomoTracker.setConsentGiven).toHaveBeenCalled();
97107
expect(matomoInitializer.initializeTracker).toHaveBeenCalledWith({
@@ -100,7 +110,7 @@ describe('MatomoService', () => {
100110
});
101111
});
102112

103-
it('should initialize tracker with REST configuration correct parameters in production', () => {
113+
it('should initialize tracker with REST configuration correct parameters in production', fakeAsync(() => {
104114
environment.production = true;
105115
environment.matomo = { trackerUrl: '' };
106116
configService.findByPropertyName.withArgs(MATOMO_TRACKER_URL).and.returnValue(
@@ -113,19 +123,25 @@ describe('MatomoService', () => {
113123
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), { values: ['1'] })));
114124
orejimeService.getSavedPreferences.and.returnValue(of({ matomo: true }));
115125

116-
service.init();
126+
runInInjectionContext(TestBed, () => {
127+
service.init();
128+
});
129+
130+
tick();
117131

118132
expect(matomoTracker.setConsentGiven).toHaveBeenCalled();
119133
expect(matomoInitializer.initializeTracker).toHaveBeenCalledWith({
120134
siteId: '1',
121135
trackerUrl: 'http://example.com',
122136
});
123-
});
137+
}));
124138

125139
it('should not initialize tracker if not in production', () => {
126140
environment.production = false;
127141

128-
service.init();
142+
runInInjectionContext(TestBed, () => {
143+
service.init();
144+
});
129145

130146
expect(matomoInitializer.initializeTracker).not.toHaveBeenCalled();
131147
});
@@ -143,14 +159,17 @@ describe('MatomoService', () => {
143159
createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), { values: ['1'] })));
144160
orejimeService.getSavedPreferences.and.returnValue(of({ matomo: true }));
145161

146-
service.init();
162+
runInInjectionContext(TestBed, () => {
163+
service.init();
164+
});
147165

148166
expect(matomoInitializer.initializeTracker).not.toHaveBeenCalled();
149167
});
150168

151169
describe('with visitorId set', () => {
152170
beforeEach(() => {
153171
matomoTracker.getVisitorId.and.returnValue(Promise.resolve('12345'));
172+
service.matomoTracker = matomoTracker;
154173
});
155174

156175
it('should add trackerId parameter', fakeAsync(() => {

src/app/statistics/matomo.service.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
2+
EnvironmentInjector,
23
inject,
34
Injectable,
5+
runInInjectionContext,
46
} from '@angular/core';
57
import {
68
MatomoInitializerService,
@@ -11,12 +13,10 @@ import {
1113
from as fromPromise,
1214
Observable,
1315
of,
14-
switchMap,
1516
} from 'rxjs';
1617
import {
1718
map,
1819
take,
19-
tap,
2020
} from 'rxjs/operators';
2121

2222
import { environment } from '../../environments/environment';
@@ -47,10 +47,10 @@ export const MATOMO_ENABLED = 'matomo.enabled';
4747
export class MatomoService {
4848

4949
/** Injects the MatomoInitializerService to initialize the Matomo tracker. */
50-
matomoInitializer = inject(MatomoInitializerService);
50+
matomoInitializer: MatomoInitializerService;
5151

5252
/** Injects the MatomoTracker to manage Matomo tracking operations. */
53-
matomoTracker = inject(MatomoTracker);
53+
matomoTracker: MatomoTracker;
5454

5555
/** Injects the OrejimeService to manage cookie consent preferences. */
5656
orejimeService = inject(OrejimeService);
@@ -61,6 +61,10 @@ export class MatomoService {
6161
/** Injects the ConfigurationService. */
6262
configService = inject(ConfigurationDataService);
6363

64+
constructor(private injector: EnvironmentInjector) {
65+
66+
}
67+
6468
/**
6569
* Initializes the Matomo tracker if in production environment.
6670
* Sets up the changeMatomoConsent function on the native window object.
@@ -74,14 +78,15 @@ export class MatomoService {
7478
if (environment.production) {
7579
const preferences$ = this.orejimeService.getSavedPreferences();
7680

77-
preferences$
78-
.pipe(
79-
tap(preferences => this.changeMatomoConsent(preferences?.matomo)),
80-
switchMap(_ => combineLatest([this.isMatomoEnabled$(), this.getSiteId$(), this.getTrackerUrl$()])),
81-
)
82-
.subscribe(([isMatomoEnabled, siteId, trackerUrl]) => {
81+
combineLatest([preferences$, this.isMatomoEnabled$(), this.getSiteId$(), this.getTrackerUrl$()])
82+
.subscribe(([preferences, isMatomoEnabled, siteId, trackerUrl]) => {
8383
if (isMatomoEnabled && siteId && trackerUrl) {
84+
runInInjectionContext(this.injector, () => {
85+
this.matomoTracker = inject(MatomoTracker);
86+
this.matomoInitializer = inject(MatomoInitializerService);
87+
});
8488
this.matomoInitializer.initializeTracker({ siteId, trackerUrl });
89+
this.changeMatomoConsent(preferences?.matomo);
8590
}
8691
});
8792
}
@@ -93,9 +98,9 @@ export class MatomoService {
9398
*/
9499
changeMatomoConsent = (consent: boolean) => {
95100
if (consent) {
96-
this.matomoTracker.setConsentGiven();
101+
this.matomoTracker?.setConsentGiven();
97102
} else {
98-
this.matomoTracker.forgetConsentGiven();
103+
this.matomoTracker?.forgetConsentGiven();
99104
}
100105
};
101106

@@ -105,7 +110,7 @@ export class MatomoService {
105110
* @returns An Observable that emits the URL with the visitor ID appended.
106111
*/
107112
appendVisitorId(url: string): Observable<string> {
108-
return fromPromise(this.matomoTracker.getVisitorId())
113+
return fromPromise(this.matomoTracker?.getVisitorId())
109114
.pipe(
110115
map(visitorId => this.appendTrackerId(url, visitorId)),
111116
take(1),

0 commit comments

Comments
 (0)