[CST-6565] LYRASIS7: Cookie consent shows analytics usage also when GA is disabled

This commit is contained in:
Sufiyan Shaikh
2022-08-09 15:10:59 +05:30
parent bfbe38974b
commit bf48971047
2 changed files with 39 additions and 1 deletions

View File

@@ -11,8 +11,13 @@ import { CookieService } from '../../core/services/cookie.service';
import { getTestScheduler } from 'jasmine-marbles';
import { MetadataValue } from '../../core/shared/metadata.models';
import { cloneDeep } from 'lodash';
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
import { createSuccessfulRemoteDataObject$ } from '../remote-data.utils';
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
describe('BrowserKlaroService', () => {
const trackingIdProp = 'google.analytics.key';
const trackingIdTestValue = 'mock-tracking-id';
let translateService;
let ePersonService;
let authService;
@@ -20,6 +25,14 @@ describe('BrowserKlaroService', () => {
let user;
let service: BrowserKlaroService;
let configurationDataService: ConfigurationDataService;
const createConfigSuccessSpy = (...values: string[]) => jasmine.createSpyObj('configurationDataService', {
findByPropertyName: createSuccessfulRemoteDataObject$({
... new ConfigurationProperty(),
name: trackingIdProp,
values: values,
}),
});
let mockConfig;
let appName;
@@ -38,6 +51,7 @@ describe('BrowserKlaroService', () => {
isAuthenticated: observableOf(true),
getAuthenticatedUserFromStore: observableOf(user)
});
configurationDataService = createConfigSuccessSpy(trackingIdTestValue);
cookieService = jasmine.createSpyObj('cookieService', {
get: '{%22token_item%22:true%2C%22impersonation%22:true%2C%22redirect%22:true%2C%22language%22:true%2C%22klaro%22:true%2C%22has_agreed_end_user%22:true%2C%22google-analytics%22:true}',
set: () => {
@@ -63,6 +77,10 @@ describe('BrowserKlaroService', () => {
{
provide: CookieService,
useValue: cookieService
},
{
provide: ConfigurationDataService,
useValue: configurationDataService
}
]
});

View File

@@ -4,7 +4,7 @@ import { combineLatest as observableCombineLatest, Observable, of as observableO
import { AuthService } from '../../core/auth/auth.service';
import { TranslateService } from '@ngx-translate/core';
import { environment } from '../../../environments/environment';
import { switchMap, take } from 'rxjs/operators';
import { catchError, switchMap, take } from 'rxjs/operators';
import { EPerson } from '../../core/eperson/models/eperson.model';
import { KlaroService } from './klaro.service';
import { hasValue, isNotEmpty } from '../empty.util';
@@ -13,6 +13,8 @@ import { EPersonDataService } from '../../core/eperson/eperson-data.service';
import { cloneDeep, debounce } from 'lodash';
import { ANONYMOUS_STORAGE_NAME_KLARO, klaroConfiguration } from './klaro-configuration';
import { Operation } from 'fast-json-patch';
import { getFirstCompletedRemoteData } from 'src/app/core/shared/operators';
import { ConfigurationDataService } from 'src/app/core/data/configuration-data.service';
/**
* Metadata field to store a user's cookie consent preferences in
@@ -52,6 +54,7 @@ export class BrowserKlaroService extends KlaroService {
private translateService: TranslateService,
private authService: AuthService,
private ePersonService: EPersonDataService,
private configService: ConfigurationDataService,
private cookieService: CookieService) {
super();
}
@@ -63,6 +66,15 @@ export class BrowserKlaroService extends KlaroService {
* - Add and translate klaro configuration messages
*/
initialize() {
this.configService.findByPropertyName('google.analytics.key').pipe(
getFirstCompletedRemoteData(),
catchError(this.removeGoogleAnalytics())
).subscribe((remoteData) => {
// make sure we got a success response from the backend
if (!remoteData.hasSucceeded) {
this.removeGoogleAnalytics();
}
});
this.translateService.setDefaultLang(environment.defaultLanguage);
const user$: Observable<EPerson> = this.getUser$();
@@ -253,4 +265,12 @@ export class BrowserKlaroService extends KlaroService {
getStorageName(identifier: string) {
return 'klaro-' + identifier;
}
/**
* remove the google analytics from the services
*/
removeGoogleAnalytics() {
this.klaroConfig.services = klaroConfiguration.services.filter(config => config.name !== 'google-analytics');
return this.klaroConfig.services;
}
}