11import {
2+ EnvironmentInjector ,
23 inject ,
34 Injectable ,
5+ runInInjectionContext ,
46} from '@angular/core' ;
57import {
68 MatomoInitializerService ,
@@ -11,12 +13,10 @@ import {
1113 from as fromPromise ,
1214 Observable ,
1315 of ,
14- switchMap ,
1516} from 'rxjs' ;
1617import {
1718 map ,
1819 take ,
19- tap ,
2020} from 'rxjs/operators' ;
2121
2222import { environment } from '../../environments/environment' ;
@@ -31,6 +31,12 @@ import { isNotEmpty } from '../shared/empty.util';
3131export const MATOMO_TRACKER_URL = 'matomo.tracker.url' ;
3232export const MATOMO_SITE_ID = 'matomo.request.siteid' ;
3333
34+ export const MATOMO_ENABLED = 'matomo.enabled' ;
35+
36+ /**
37+ * Service to manage Matomo analytics integration.
38+ * Handles initialization and consent management for Matomo tracking.
39+ */
3440@Injectable ( {
3541 providedIn : 'root' ,
3642} )
@@ -40,14 +46,21 @@ export const MATOMO_SITE_ID = 'matomo.request.siteid';
4046 */
4147export class MatomoService {
4248
43- matomoInitializer = inject ( MatomoInitializerService ) ;
44- matomoTracker = inject ( MatomoTracker ) ;
49+ /** Injects the MatomoInitializerService to initialize the Matomo tracker. */
50+ matomoInitializer : MatomoInitializerService ;
51+
52+ /** Injects the MatomoTracker to manage Matomo tracking operations. */
53+ matomoTracker : MatomoTracker ;
4554 klaroService = inject ( KlaroService ) ;
4655 _window = inject ( NativeWindowService ) ;
4756
4857 /** Injects the ConfigurationService. */
4958 configService = inject ( ConfigurationDataService ) ;
5059
60+ constructor ( private injector : EnvironmentInjector ) {
61+
62+ }
63+
5164 /**
5265 * Initializes the Matomo tracker if in production environment.
5366 * Sets up the changeMatomoConsent function on the native window object.
@@ -61,24 +74,29 @@ export class MatomoService {
6174 if ( environment . production ) {
6275 const preferences$ = this . klaroService . getSavedPreferences ( ) ;
6376
64- preferences$
65- . pipe (
66- tap ( preferences => this . changeMatomoConsent ( preferences ?. matomo ) ) ,
67- switchMap ( _ => combineLatest ( [ this . isMatomoEnabled$ ( ) , this . getSiteId$ ( ) , this . getTrackerUrl$ ( ) ] ) ) ,
68- )
69- . subscribe ( ( [ isMatomoEnabled , siteId , trackerUrl ] ) => {
77+ combineLatest ( [ preferences$ , this . isMatomoEnabled$ ( ) , this . getSiteId$ ( ) , this . getTrackerUrl$ ( ) ] )
78+ . subscribe ( ( [ preferences , isMatomoEnabled , siteId , trackerUrl ] ) => {
7079 if ( isMatomoEnabled && siteId && trackerUrl ) {
80+ runInInjectionContext ( this . injector , ( ) => {
81+ this . matomoTracker = inject ( MatomoTracker ) ;
82+ this . matomoInitializer = inject ( MatomoInitializerService ) ;
83+ } ) ;
7184 this . matomoInitializer . initializeTracker ( { siteId, trackerUrl } ) ;
85+ this . changeMatomoConsent ( preferences ?. matomo ) ;
7286 }
7387 } ) ;
7488 }
7589 }
7690
91+ /**
92+ * Changes the Matomo consent status based on the given consent value.
93+ * @param consent - A boolean indicating whether consent is given for Matomo tracking.
94+ */
7795 changeMatomoConsent = ( consent : boolean ) => {
7896 if ( consent ) {
79- this . matomoTracker . setConsentGiven ( ) ;
97+ this . matomoTracker ? .setConsentGiven ( ) ;
8098 } else {
81- this . matomoTracker . forgetConsentGiven ( ) ;
99+ this . matomoTracker ? .forgetConsentGiven ( ) ;
82100 }
83101 } ;
84102
@@ -88,7 +106,7 @@ export class MatomoService {
88106 * @returns An Observable that emits the URL with the visitor ID appended.
89107 */
90108 appendVisitorId ( url : string ) : Observable < string > {
91- return fromPromise ( this . matomoTracker . getVisitorId ( ) )
109+ return fromPromise ( this . matomoTracker ? .getVisitorId ( ) )
92110 . pipe (
93111 map ( visitorId => this . appendTrackerId ( url , visitorId ) ) ,
94112 take ( 1 ) ,
@@ -128,6 +146,21 @@ export class MatomoService {
128146 ) ;
129147 }
130148
149+ /**
150+ * Checks if Matomo tracking is enabled by retrieving the configuration property.
151+ * @returns An Observable that emits a boolean indicating whether Matomo tracking is enabled.
152+ */
153+ isMatomoEnabled$ ( ) : Observable < boolean > {
154+ return this . configService . findByPropertyName ( MATOMO_ENABLED )
155+ . pipe (
156+ getFirstCompletedRemoteData ( ) ,
157+ map ( ( res : RemoteData < ConfigurationProperty > ) => {
158+ return res . hasSucceeded && res . payload && isNotEmpty ( res . payload . values ) &&
159+ res . payload . values [ 0 ] ?. toLowerCase ( ) === 'true' ;
160+ } ) ,
161+ ) ;
162+ }
163+
131164 /**
132165 * Appends the visitor ID as a query parameter to the given URL.
133166 * @param url - The original URL to modify
0 commit comments