92319: Move Klaro & GA steps to BrowserInitService

This commit is contained in:
Yura Bondarenko
2022-07-26 09:50:39 +02:00
parent 5ff80a8a02
commit 67b4cce25d
3 changed files with 31 additions and 49 deletions

View File

@@ -16,14 +16,9 @@ import { AppState } from './app.reducer';
import { isEqual } from 'lodash';
import { TranslateService } from '@ngx-translate/core';
import { LocaleService } from './core/locale/locale.service';
import { hasValue } from './shared/empty.util';
import { Angulartics2DSpace } from './statistics/angulartics/dspace-provider';
import { GoogleAnalyticsService } from './statistics/google-analytics.service';
import { MetadataService } from './core/metadata/metadata.service';
import { BreadcrumbsService } from './breadcrumbs/breadcrumbs.service';
import { distinctUntilChanged, filter, take, tap } from 'rxjs/operators';
import { isAuthenticationBlocking } from './core/auth/selectors';
import { KlaroService } from './shared/cookies/klaro.service';
import { ThemeService } from './shared/theme-support/theme.service';
/**
@@ -50,10 +45,8 @@ export abstract class InitService {
protected translate: TranslateService,
protected localeService: LocaleService,
protected angulartics2DSpace: Angulartics2DSpace,
@Optional() protected googleAnalyticsService: GoogleAnalyticsService,
protected metadata: MetadataService,
protected breadcrumbsService: BreadcrumbsService,
@Optional() protected klaroService: KlaroService,
protected themeService: ThemeService,
) {
}
@@ -174,15 +167,10 @@ export abstract class InitService {
}
/**
* Initialize analytics services
* - Angulartics
* - Google Analytics (if enabled)
* Initialize Angulartics
* @protected
*/
protected initAnalytics(): void {
if (hasValue(this.googleAnalyticsService)) {
this.googleAnalyticsService.addTrackingIdToPage();
}
protected initAngulartics(): void {
this.angulartics2DSpace.startTracking();
}
@@ -198,21 +186,4 @@ export abstract class InitService {
this.breadcrumbsService.listenForRouteChanges();
this.themeService.listenForRouteChanges();
}
/**
* Initialize Klaro (if enabled)
* @protected
*/
protected initKlaro() {
if (hasValue(this.klaroService)) {
this.store.pipe(
select(isAuthenticationBlocking),
distinctUntilChanged(),
filter((isBlocking: boolean) => isBlocking === false),
take(1)
).subscribe(() => {
this.klaroService.initialize();
});
}
}
}

View File

@@ -6,7 +6,7 @@
* http://www.dspace.org/license/
*/
import { InitService } from '../../app/init.service';
import { Store } from '@ngrx/store';
import { select, Store } from '@ngrx/store';
import { AppState } from '../../app/app.reducer';
import { TransferState } from '@angular/platform-browser';
import { APP_CONFIG, APP_CONFIG_STATE, AppConfig } from '../../config/app-config.interface';
@@ -14,7 +14,7 @@ import { DefaultAppConfig } from '../../config/default-app-config';
import { extendEnvironmentWithAppConfig } from '../../config/config.util';
import { environment } from '../../environments/environment';
import { CorrelationIdService } from '../../app/correlation-id/correlation-id.service';
import { Inject, Injectable, Optional } from '@angular/core';
import { Inject, Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { LocaleService } from '../../app/core/locale/locale.service';
import { Angulartics2DSpace } from '../../app/statistics/angulartics/dspace-provider';
@@ -27,8 +27,9 @@ import { AuthService } from '../../app/core/auth/auth.service';
import { ThemeService } from '../../app/shared/theme-support/theme.service';
import { StoreAction, StoreActionTypes } from '../../app/store.actions';
import { coreSelector } from '../../app/core/core.selectors';
import { find, map } from 'rxjs/operators';
import { distinctUntilChanged, filter, find, map, take } from 'rxjs/operators';
import { isNotEmpty } from '../../app/shared/empty.util';
import { isAuthenticationBlocking } from '../../app/core/auth/selectors';
/**
* Performs client-side initialization.
@@ -43,11 +44,11 @@ export class BrowserInitService extends InitService {
protected translate: TranslateService,
protected localeService: LocaleService,
protected angulartics2DSpace: Angulartics2DSpace,
@Optional() protected googleAnalyticsService: GoogleAnalyticsService,
protected googleAnalyticsService: GoogleAnalyticsService,
protected metadata: MetadataService,
protected breadcrumbsService: BreadcrumbsService,
protected cssService: CSSVariableService,
@Optional() protected klaroService: KlaroService,
protected klaroService: KlaroService,
protected authService: AuthService,
protected themeService: ThemeService,
) {
@@ -58,10 +59,8 @@ export class BrowserInitService extends InitService {
translate,
localeService,
angulartics2DSpace,
googleAnalyticsService,
metadata,
breadcrumbsService,
klaroService,
themeService,
);
}
@@ -85,7 +84,8 @@ export class BrowserInitService extends InitService {
this.checkEnvironment();
this.initI18n();
this.initAnalytics();
this.initAngulartics();
this.initGoogleAnalytics();
this.initRouteListeners();
this.themeService.listenForThemeChanges(true);
this.trackAuthTokenExpiration();
@@ -116,4 +116,23 @@ export class BrowserInitService extends InitService {
private trackAuthTokenExpiration(): void {
this.authService.trackTokenExpiration();
}
/**
* Initialize Klaro
* @protected
*/
protected initKlaro() {
this.store.pipe(
select(isAuthenticationBlocking),
distinctUntilChanged(),
filter((isBlocking: boolean) => isBlocking === false),
take(1)
).subscribe(() => {
this.klaroService.initialize();
});
}
protected initGoogleAnalytics() {
this.googleAnalyticsService.addTrackingIdToPage();
}
}

View File

@@ -12,15 +12,13 @@ import { TransferState } from '@angular/platform-browser';
import { CorrelationIdService } from '../../app/correlation-id/correlation-id.service';
import { APP_CONFIG, APP_CONFIG_STATE, AppConfig } from '../../config/app-config.interface';
import { environment } from '../../environments/environment';
import { Inject, Injectable, Optional } from '@angular/core';
import { Inject, Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { LocaleService } from '../../app/core/locale/locale.service';
import { Angulartics2DSpace } from '../../app/statistics/angulartics/dspace-provider';
import { GoogleAnalyticsService } from '../../app/statistics/google-analytics.service';
import { MetadataService } from '../../app/core/metadata/metadata.service';
import { BreadcrumbsService } from '../../app/breadcrumbs/breadcrumbs.service';
import { CSSVariableService } from '../../app/shared/sass-helper/sass-helper.service';
import { KlaroService } from '../../app/shared/cookies/klaro.service';
import { ThemeService } from '../../app/shared/theme-support/theme.service';
import { take } from 'rxjs/operators';
@@ -37,11 +35,9 @@ export class ServerInitService extends InitService {
protected translate: TranslateService,
protected localeService: LocaleService,
protected angulartics2DSpace: Angulartics2DSpace,
@Optional() protected googleAnalyticsService: GoogleAnalyticsService,
protected metadata: MetadataService,
protected breadcrumbsService: BreadcrumbsService,
protected cssService: CSSVariableService,
@Optional() protected klaroService: KlaroService,
protected themeService: ThemeService,
) {
super(
@@ -51,10 +47,8 @@ export class ServerInitService extends InitService {
translate,
localeService,
angulartics2DSpace,
googleAnalyticsService,
metadata,
breadcrumbsService,
klaroService,
themeService,
);
}
@@ -68,12 +62,10 @@ export class ServerInitService extends InitService {
this.checkEnvironment();
this.initI18n();
this.initAnalytics();
this.initAngulartics();
this.initRouteListeners();
this.themeService.listenForThemeChanges(false);
this.initKlaro();
return true;
};
}