Skip to content

Commit e580c42

Browse files
add new klaro section config for metrics, refactor loading
1 parent e6d1c9b commit e580c42

4 files changed

Lines changed: 44 additions & 11 deletions

File tree

src/app/shared/cookies/browser-klaro.service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import { EPersonDataService } from '../../core/eperson/eperson-data.service';
1212
import cloneDeep from 'lodash/cloneDeep';
1313
import debounce from 'lodash/debounce';
1414
import { ANONYMOUS_STORAGE_NAME_KLARO, klaroConfiguration } from './klaro-configuration';
15-
import { Operation } from 'fast-json-patch';
15+
import { deepClone, Operation } from 'fast-json-patch';
1616
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
1717
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
1818
import { CAPTCHA_NAME } from '../../core/google-recaptcha/google-recaptcha.service';
19+
import isEqual from 'lodash/isEqual';
1920

2021
export interface CookieConsents {
2122
acknowledgement: boolean;
@@ -70,6 +71,8 @@ export class BrowserKlaroService extends KlaroService {
7071

7172
private readonly GOOGLE_ANALYTICS_SERVICE_NAME = 'google-analytics';
7273

74+
private lastCookiesConsents: CookieConsents;
75+
7376
/**
7477
* Initial Klaro configuration
7578
*/
@@ -346,9 +349,12 @@ export class BrowserKlaroService extends KlaroService {
346349
this.lazyKlaro.then(({getManager}) => {
347350
const manager = getManager(this.klaroConfig);
348351
const consentsSubject$ = this.consentsUpdates$;
352+
let lastCookiesConsents = this.lastCookiesConsents;
349353
manager.watch({
350354
update(_, eventName, consents) {
351-
if (eventName === 'consents') {
355+
356+
if (eventName === 'consents' && !isEqual(consents, lastCookiesConsents)) {
357+
lastCookiesConsents = deepClone(consents);
352358
consentsSubject$.next(consents);
353359
}
354360
}

src/app/shared/cookies/klaro-configuration.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,21 @@ export const klaroConfiguration: any = {
135135
HAS_AGREED_END_USER
136136
]
137137
},
138+
{
139+
name: 'plumX',
140+
purposes: ['thirdPartiesJs'],
141+
required: false,
142+
},
143+
{
144+
name: 'altmetric',
145+
purposes: ['thirdPartiesJs'],
146+
required: false,
147+
},
148+
{
149+
name: 'dimensions',
150+
purposes: ['thirdPartiesJs'],
151+
required: false,
152+
},
138153
{
139154
name: GOOGLE_ANALYTICS_KLARO_KEY,
140155
purposes: ['statistical'],

src/app/shared/metric/metric-loader/metric-loader.component.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { MetricLoaderService } from './metric-loader.service';
1616
import { hasValue } from '../../empty.util';
1717
import { BrowserKlaroService, CookieConsents } from '../../cookies/browser-klaro.service';
1818
import { KlaroService } from '../../cookies/klaro.service';
19-
import { distinctUntilChanged, startWith } from 'rxjs/operators';
19+
import { startWith } from 'rxjs/operators';
2020

2121
@Component({
2222
// eslint-disable-next-line @angular-eslint/component-selector
@@ -52,6 +52,8 @@ export class MetricLoaderComponent implements OnInit, OnDestroy {
5252

5353
private browserKlaroService: BrowserKlaroService;
5454

55+
private hasLoadedScript: boolean;
56+
5557
constructor(
5658
private componentFactoryResolver: ComponentFactoryResolver,
5759
private metricLoaderService: MetricLoaderService,
@@ -72,6 +74,7 @@ export class MetricLoaderComponent implements OnInit, OnDestroy {
7274
if (!metric) {
7375
return;
7476
}
77+
this.hasLoadedScript = !!canLoadScript;
7578
this.metricLoaderService.loadMetricTypeComponent(metric.metricType, canLoadScript).then((component) => {
7679
if (hasValue(this.cookiesSubscription) && canLoadScript) {
7780
this.container.clear();
@@ -101,8 +104,7 @@ export class MetricLoaderComponent implements OnInit, OnDestroy {
101104
componentInstance.canLoadScript = canLoadScript;
102105
componentInstance.visibleWithoutData = forceRendering;
103106

104-
105-
if (!canLoadScript) {
107+
if (!canLoadScript && !this.settingsSubscription) {
106108
this.reloadComponentOnConsentsChange(componentInstance, canLoadScript);
107109
}
108110

@@ -119,7 +121,7 @@ export class MetricLoaderComponent implements OnInit, OnDestroy {
119121
* @param consents
120122
*/
121123
private getCanLoadScript(consents: CookieConsents): boolean {
122-
return (hasValue(consents) && consents.acknowledgement && this.thirdPartyMetrics.includes(this.metric.metricType))
124+
return (hasValue(consents) && consents[this.metric.metricType] && this.thirdPartyMetrics.includes(this.metric.metricType))
123125
|| !this.thirdPartyMetrics.includes(this.metric.metricType);
124126
}
125127

@@ -132,9 +134,7 @@ export class MetricLoaderComponent implements OnInit, OnDestroy {
132134
*/
133135
private reloadComponentOnConsentsChange(componentInstance: BaseMetricComponent, canLoadScript: boolean): void {
134136
this.settingsSubscription = combineLatest([
135-
this.consentUpdates$.pipe(distinctUntilChanged(
136-
(previousConsents, currentConsents) => JSON.stringify(previousConsents) === JSON.stringify(currentConsents))
137-
),
137+
this.consentUpdates$,
138138
componentInstance.requestSettingsConsent.pipe(startWith(undefined))
139139
]).subscribe(([consents, request]) => {
140140
canLoadScript = this.getCanLoadScript(consents);
@@ -143,7 +143,7 @@ export class MetricLoaderComponent implements OnInit, OnDestroy {
143143
this.browserKlaroService.showSettings();
144144
}
145145

146-
if (canLoadScript) {
146+
if (canLoadScript && !this.hasLoadedScript) {
147147
this.loadComponent(this.metric, canLoadScript, true);
148148
}
149149
});

src/assets/i18n/en.json5

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,9 +1807,21 @@
18071807

18081808
"cookies.consent.purpose.sharing": "Sharing",
18091809

1810+
"cookies.consent.app.title.plumX": "PlumX",
18101811

1811-
"curation-task.task.citationpage.label": "Generate Citation Page",
1812+
"cookies.consent.app.description.plumX": "PlumX Metrics provide insights into the ways people interact with individual pieces of research output (articles, conference proceedings, book chapters, and many more) in the online environment. (https://plumanalytics.com/)",
1813+
1814+
"cookies.consent.app.title.altmetric": "Altmetric",
1815+
1816+
"cookies.consent.app.description.altmetric": "Altmetric’s interface tracks online engagement to reveal how and where your research is being visualized. (https://www.altmetric.com)",
1817+
1818+
"cookies.consent.app.title.dimensions": "Dimensions",
18121819

1820+
"cookies.consent.app.description.dimensions": "Dimensions analyses references from publications and calculates a set of article-level indicators. (https://www.dimensions.ai)",
1821+
1822+
"cookies.consent.purpose.thirdPartiesJs": "Third parties JavaScript",
1823+
1824+
"curation-task.task.citationpage.label": "Generate Citation Page",
18131825

18141826
"cris-layout.toggle.open": "Open section",
18151827

0 commit comments

Comments
 (0)