11import { Inject , Injectable , InjectionToken } from '@angular/core' ;
2- import { combineLatest as observableCombineLatest , Observable , of as observableOf } from 'rxjs' ;
2+ import { BehaviorSubject , combineLatest as observableCombineLatest , Observable , of as observableOf } from 'rxjs' ;
33import { AuthService } from '../../core/auth/auth.service' ;
44import { TranslateService } from '@ngx-translate/core' ;
55import { environment } from '../../../environments/environment' ;
66import { map , switchMap , take } from 'rxjs/operators' ;
77import { EPerson } from '../../core/eperson/models/eperson.model' ;
8- import { KlaroService } from './klaro.service' ;
8+ import { CookieConsents , KlaroService } from './klaro.service' ;
99import { hasValue , isEmpty , isNotEmpty } from '../empty.util' ;
1010import { CookieService } from '../../core/services/cookie.service' ;
1111import { EPersonDataService } from '../../core/eperson/eperson-data.service' ;
1212import cloneDeep from 'lodash/cloneDeep' ;
1313import debounce from 'lodash/debounce' ;
1414import { ANONYMOUS_STORAGE_NAME_KLARO , klaroConfiguration } from './klaro-configuration' ;
15- import { Operation } from 'fast-json-patch' ;
15+ import { deepClone , Operation } from 'fast-json-patch' ;
1616import { getFirstCompletedRemoteData } from '../../core/shared/operators' ;
1717import { ConfigurationDataService } from '../../core/data/configuration-data.service' ;
1818import { CAPTCHA_NAME } from '../../core/google-recaptcha/google-recaptcha.service' ;
19+ import isEqual from 'lodash/isEqual' ;
1920
2021/**
2122 * Metadata field to store a user's cookie consent preferences in
@@ -65,11 +66,19 @@ export class BrowserKlaroService extends KlaroService {
6566
6667 private readonly GOOGLE_ANALYTICS_SERVICE_NAME = 'google-analytics' ;
6768
69+ private lastCookiesConsents : CookieConsents ;
70+
6871 /**
6972 * Initial Klaro configuration
7073 */
7174 klaroConfig = cloneDeep ( klaroConfiguration ) ;
7275
76+ /**
77+ * Subject to emit updates in the consents
78+ */
79+ consentsUpdates$ : BehaviorSubject < CookieConsents > = new BehaviorSubject < CookieConsents > ( null ) ;
80+
81+
7382 constructor (
7483 private translateService : TranslateService ,
7584 private authService : AuthService ,
@@ -94,6 +103,20 @@ export class BrowserKlaroService extends KlaroService {
94103 this . klaroConfig . translations . zz . consentNotice . description = 'cookies.consent.content-notice.description.no-privacy' ;
95104 }
96105
106+ if ( hasValue ( environment . info . metricsConsents ) ) {
107+ environment . info . metricsConsents . forEach ( ( metric ) => {
108+ if ( metric . enabled ) {
109+ this . klaroConfig . services . push (
110+ {
111+ name : metric . key ,
112+ purposes : [ 'thirdPartyJs' ] ,
113+ required : false ,
114+ }
115+ ) ;
116+ }
117+ } ) ;
118+ }
119+
97120 const hideGoogleAnalytics$ = this . configService . findByPropertyName ( this . GOOGLE_ANALYTICS_KEY ) . pipe (
98121 getFirstCompletedRemoteData ( ) ,
99122 map ( remoteData => ! remoteData . hasSucceeded || ! remoteData . payload || isEmpty ( remoteData . payload . values ) ) ,
@@ -331,6 +354,25 @@ export class BrowserKlaroService extends KlaroService {
331354 return 'klaro-' + identifier ;
332355 }
333356
357+ watchConsentUpdates ( ) : void {
358+ this . lazyKlaro . then ( ( { getManager} ) => {
359+ const manager = getManager ( this . klaroConfig ) ;
360+ const consentsSubject$ = this . consentsUpdates$ ;
361+ let lastCookiesConsents = this . lastCookiesConsents ;
362+
363+ consentsSubject$ . next ( manager . consents ) ;
364+ manager . watch ( {
365+ update ( _ , eventName , consents ) {
366+
367+ if ( eventName === 'consents' && ! isEqual ( consents , lastCookiesConsents ) ) {
368+ lastCookiesConsents = deepClone ( consents ) ;
369+ consentsSubject$ . next ( consents ) ;
370+ }
371+ }
372+ } ) ;
373+ } ) ;
374+ }
375+
334376 /**
335377 * remove the google analytics from the services
336378 */
0 commit comments