@@ -4,15 +4,17 @@ import { combineLatest as observableCombineLatest, Observable, of as observableO
44import { AuthService } from '../../core/auth/auth.service' ;
55import { TranslateService } from '@ngx-translate/core' ;
66import { environment } from '../../../environments/environment' ;
7- import { switchMap , take } from 'rxjs/operators' ;
7+ import { map , switchMap , take } from 'rxjs/operators' ;
88import { EPerson } from '../../core/eperson/models/eperson.model' ;
99import { KlaroService } from './klaro.service' ;
10- import { hasValue , isNotEmpty } from '../empty.util' ;
10+ import { hasValue , isEmpty , isNotEmpty } from '../empty.util' ;
1111import { CookieService } from '../../core/services/cookie.service' ;
1212import { EPersonDataService } from '../../core/eperson/eperson-data.service' ;
1313import { cloneDeep , debounce } from 'lodash' ;
1414import { ANONYMOUS_STORAGE_NAME_KLARO , klaroConfiguration } from './klaro-configuration' ;
1515import { Operation } from 'fast-json-patch' ;
16+ import { getFirstCompletedRemoteData } from '../../core/shared/operators' ;
17+ import { ConfigurationDataService } from '../../core/data/configuration-data.service' ;
1618
1719/**
1820 * Metadata field to store a user's cookie consent preferences in
@@ -38,23 +40,31 @@ const cookiePurposeMessagePrefix = 'cookies.consent.purpose.';
3840 * Update request debounce in ms
3941 */
4042const updateDebounce = 300 ;
43+
4144/**
4245 * Browser implementation for the KlaroService, representing a service for handling Klaro consent preferences and UI
4346 */
4447@Injectable ( )
4548export class BrowserKlaroService extends KlaroService {
49+
50+ private readonly GOOGLE_ANALYTICS_KEY = 'google.analytics.key' ;
51+
52+ private readonly GOOGLE_ANALYTICS_SERVICE_NAME = 'google-analytics' ;
53+
4654 /**
4755 * Initial Klaro configuration
4856 */
49- klaroConfig = klaroConfiguration ;
57+ klaroConfig = cloneDeep ( klaroConfiguration ) ;
5058
5159 constructor (
5260 private translateService : TranslateService ,
5361 private authService : AuthService ,
5462 private ePersonService : EPersonDataService ,
63+ private configService : ConfigurationDataService ,
5564 private cookieService : CookieService ) {
5665 super ( ) ;
5766 }
67+
5868 /**
5969 * Initializes the service:
6070 * - Retrieves the current authenticated user
@@ -68,14 +78,25 @@ export class BrowserKlaroService extends KlaroService {
6878 this . klaroConfig . translations . en . consentNotice . description = 'cookies.consent.content-notice.description.no-privacy' ;
6979 }
7080
81+ const servicesToHide$ : Observable < string [ ] > = this . configService . findByPropertyName ( this . GOOGLE_ANALYTICS_KEY ) . pipe (
82+ getFirstCompletedRemoteData ( ) ,
83+ map ( remoteData => {
84+ if ( ! remoteData . hasSucceeded || ! remoteData . payload || isEmpty ( remoteData . payload . values ) ) {
85+ return [ this . GOOGLE_ANALYTICS_SERVICE_NAME ] ;
86+ } else {
87+ return [ ] ;
88+ }
89+ } ) ,
90+ ) ;
91+
7192 this . translateService . setDefaultLang ( environment . defaultLanguage ) ;
7293
7394 const user$ : Observable < EPerson > = this . getUser$ ( ) ;
7495
7596 const translationServiceReady$ = this . translateService . get ( 'loading.default' ) . pipe ( take ( 1 ) ) ;
7697
77- observableCombineLatest ( [ user$ , translationServiceReady$ ] )
78- . subscribe ( ( [ user , translation ] : [ EPerson , string ] ) => {
98+ observableCombineLatest ( [ user$ , servicesToHide$ , translationServiceReady$ ] )
99+ . subscribe ( ( [ user , servicesToHide , _ ] : [ EPerson , string [ ] , string ] ) => {
79100 user = cloneDeep ( user ) ;
80101
81102 if ( hasValue ( user ) ) {
@@ -93,6 +114,9 @@ export class BrowserKlaroService extends KlaroService {
93114 * Show the configuration if the configuration has not been confirmed
94115 */
95116 this . translateConfiguration ( ) ;
117+
118+ this . klaroConfig . services = this . filterConfigServices ( servicesToHide ) ;
119+
96120 Klaro . setup ( this . klaroConfig ) ;
97121 } ) ;
98122 }
@@ -168,7 +192,10 @@ export class BrowserKlaroService extends KlaroService {
168192 */
169193 addAppMessages ( ) {
170194 this . klaroConfig . services . forEach ( ( app ) => {
171- this . klaroConfig . translations . en [ app . name ] = { title : this . getTitleTranslation ( app . name ) , description : this . getDescriptionTranslation ( app . name ) } ;
195+ this . klaroConfig . translations . en [ app . name ] = {
196+ title : this . getTitleTranslation ( app . name ) ,
197+ description : this . getDescriptionTranslation ( app . name )
198+ } ;
172199 app . purposes . forEach ( ( purpose ) => {
173200 this . klaroConfig . translations . en . purposes [ purpose ] = this . getPurposeTranslation ( purpose ) ;
174201 } ) ;
@@ -257,4 +284,11 @@ export class BrowserKlaroService extends KlaroService {
257284 getStorageName ( identifier : string ) {
258285 return 'klaro-' + identifier ;
259286 }
287+
288+ /**
289+ * remove the google analytics from the services
290+ */
291+ private filterConfigServices ( servicesToHide : string [ ] ) : Pick < typeof klaroConfiguration , 'services' > [ ] {
292+ return this . klaroConfig . services . filter ( service => ! servicesToHide . some ( name => name === service . name ) ) ;
293+ }
260294}
0 commit comments