diff --git a/dmp-frontend/src/app/app.module.ts b/dmp-frontend/src/app/app.module.ts index e61742887..8c4c7e3a9 100644 --- a/dmp-frontend/src/app/app.module.ts +++ b/dmp-frontend/src/app/app.module.ts @@ -40,6 +40,7 @@ import { OpenDMPCustomTranslationCompiler } from './utilities/translate/opendmp- import { CoreAnnotationServiceModule } from 'annotation-service/services/core-service.module'; import { CoreNotificationServiceModule } from '@notification-service/services/core-service.module'; import { DepositOauth2DialogModule } from './ui/misc/deposit-oauth2-dialog/deposit-oauth2-dialog.module'; +import { AnalyticsService } from './core/services/matomo/analytics-service'; // AoT requires an exported function for factories export function HttpLoaderFactory(languageHttpService: LanguageHttpService) { @@ -182,7 +183,8 @@ export function InstallationConfigurationFactory(appConfig: ConfigurationService }, Title, CookieService, - MatomoService + MatomoService, + AnalyticsService ], bootstrap: [AppComponent] }) diff --git a/dmp-frontend/src/app/core/model/configuration-models/analytics-providers.model.ts b/dmp-frontend/src/app/core/model/configuration-models/analytics-providers.model.ts new file mode 100644 index 000000000..0c223ceec --- /dev/null +++ b/dmp-frontend/src/app/core/model/configuration-models/analytics-providers.model.ts @@ -0,0 +1,67 @@ + +export class AnalyticsProviders { + private _providers: AnalyticsProvider[]; + get providers(): AnalyticsProvider[] { + return this._providers; + } + + set providers(providers: AnalyticsProvider[]) { + this._providers = providers; + } + + public static parseValue(value: any): AnalyticsProviders { + const analyticsProvidersObj: AnalyticsProviders = new AnalyticsProviders(); + + analyticsProvidersObj.providers = []; + for (let providerValue of value.providers) { + const providerObj: AnalyticsProvider = AnalyticsProvider.parseValue(providerValue); + analyticsProvidersObj.providers.push(providerObj); + } + + return analyticsProvidersObj; + } +} + +export class AnalyticsProvider { + private _type: AnalyticsProviderType; + + get type(): AnalyticsProviderType { + return this._type; + } + + set type(type: AnalyticsProviderType) { + this._type = type; + } + + private _enabled: boolean; + + get enabled(): boolean { + return this._enabled; + } + + set enabled(enabled: boolean) { + this._enabled = enabled; + } + + private _options: any; + + get options(): any { + return this._options; + } + + set options(options: any) { + this._options = options; + } + + public static parseValue(value: any): AnalyticsProvider { + const obj: AnalyticsProvider = new AnalyticsProvider(); + obj.type = value.type; + obj.enabled = value.enabled; + obj.options = value.options; + return obj; + } +} + +export enum AnalyticsProviderType { + Matomo = "matomo" +} diff --git a/dmp-frontend/src/app/core/services/configuration/configuration.service.ts b/dmp-frontend/src/app/core/services/configuration/configuration.service.ts index e19029555..eb28daed4 100644 --- a/dmp-frontend/src/app/core/services/configuration/configuration.service.ts +++ b/dmp-frontend/src/app/core/services/configuration/configuration.service.ts @@ -10,6 +10,7 @@ import { HttpClient } from '@angular/common/http'; import { KeycloakConfiguration } from '@app/core/model/configuration-models/keycloak-configuration.model'; import { Guid } from '@common/types/guid'; import { AuthProviders } from '@app/core/model/configuration-models/auth-providers.model'; +import { AnalyticsProviders } from '@app/core/model/configuration-models/analytics-providers.model'; @Injectable({ providedIn: 'root', @@ -153,6 +154,11 @@ export class ConfigurationService extends BaseComponent { get authProviders(): AuthProviders { return this._authProviders; } + + private _analyticsProviders: AnalyticsProviders; + get analyticsProviders(): AnalyticsProviders { + return this._analyticsProviders; + } private _researcherId: any; get researcherId(): boolean { @@ -239,6 +245,7 @@ export class ConfigurationService extends BaseComponent { this._newReleaseNotificationLink = config.newReleaseNotification?.link; this._newReleaseNotificationVersionCode = config.newReleaseNotification?.versionCode; this._authProviders = AuthProviders.parseValue(config.authProviders); + this._analyticsProviders = AnalyticsProviders.parseValue(config.analytics); this._researcherId = config.referenceTypes.researcherId; this._grantId = config.referenceTypes.grantId; this._organizationId = config.referenceTypes.organizationId; diff --git a/dmp-frontend/src/app/core/services/matomo/analytics-service.ts b/dmp-frontend/src/app/core/services/matomo/analytics-service.ts new file mode 100644 index 000000000..1a705c3a1 --- /dev/null +++ b/dmp-frontend/src/app/core/services/matomo/analytics-service.ts @@ -0,0 +1,31 @@ +import { Injectable } from "@angular/core"; +import { ConfigurationService } from "../configuration/configuration.service"; +import { MatomoService } from "./matomo-service"; +import { AnalyticsProviderType, AnalyticsProviders } from "@app/core/model/configuration-models/analytics-providers.model"; + +@Injectable() +export class AnalyticsService { + + public static Dashboard: string = 'Home Dashboard'; + + constructor( + private configurationService: ConfigurationService, + private matomoService: MatomoService + ) { } + + trackPageView(customTitle?: string): void { + const analytics: AnalyticsProviders = this.configurationService.analyticsProviders; + for (let provider of analytics.providers) { + switch(provider.type) { + case(AnalyticsProviderType.Matomo): + this.matomoService.trackPageViewCopy(provider, customTitle); + } + } + } + + trackSiteSearch(keyword: string, category?: string, resultsCount?: number): void { + } + + trackDownload(category: "dmps" | "datasets" | "descriptions", type: string, id: string): void { + } +} \ No newline at end of file diff --git a/dmp-frontend/src/app/core/services/matomo/matomo-service.ts b/dmp-frontend/src/app/core/services/matomo/matomo-service.ts index 16c07734a..f1e994d79 100644 --- a/dmp-frontend/src/app/core/services/matomo/matomo-service.ts +++ b/dmp-frontend/src/app/core/services/matomo/matomo-service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core'; import { MatomoInitializerService, MatomoTracker } from 'ngx-matomo-client'; import { AuthService } from '../auth/auth.service'; import { ConfigurationService } from '../configuration/configuration.service'; +import { AnalyticsProvider } from '@app/core/model/configuration-models/analytics-providers.model'; @Injectable() export class MatomoService { @@ -28,6 +29,14 @@ export class MatomoService { this.matomoTracker.trackPageView(customTitle); } } + + trackPageViewCopy(provider: AnalyticsProvider, customTitle?: string): void { + if (provider.enabled) { + var principalid = this.authService.userId(); + if (principalid != null) { this.matomoTracker.setUserId(principalid.toString()); } + this.matomoTracker.trackPageView(customTitle); + } + } trackSiteSearch(keyword: string, category?: string, resultsCount?: number): void { if (this.configurationService.matomoEnabled) { diff --git a/dmp-frontend/src/app/ui/dashboard/dashboard.component.ts b/dmp-frontend/src/app/ui/dashboard/dashboard.component.ts index 2a360b5ec..7a3244041 100644 --- a/dmp-frontend/src/app/ui/dashboard/dashboard.component.ts +++ b/dmp-frontend/src/app/ui/dashboard/dashboard.component.ts @@ -18,6 +18,7 @@ import { CookieService } from 'ngx-cookie-service'; import { takeUntil } from 'rxjs/operators'; import { StartNewDescriptionDialogComponent } from '../description/start-new-description-dialog/start-new-description-dialog.component'; import { StartNewDmpDialogComponent } from '../dmp/new/start-new-dmp-dialogue/start-new-dmp-dialog.component'; +import { AnalyticsService } from '@app/core/services/matomo/analytics-service'; @Component({ @@ -43,6 +44,7 @@ export class DashboardComponent extends BaseComponent implements OnInit { private language: TranslateService, private guidedTourService: GuidedTourService, private matomoService: MatomoService, + private analyticsService: AnalyticsService, public referenceTypeService: ReferenceTypeService, private fb: UntypedFormBuilder, private cookieService: CookieService, @@ -62,7 +64,8 @@ export class DashboardComponent extends BaseComponent implements OnInit { } }); - this.matomoService.trackPageView('Home Dashboard'); + this.analyticsService.trackPageView(AnalyticsService.Dashboard); + // this.matomoService.trackPageView('Home Dashboard'); if (!this.isAuthenticated()) { this.dashboardService.getPublicDashboardStatistics()