From 6ea5dfff423592aac697174f5de48494a25cff86 Mon Sep 17 00:00:00 2001 From: Diamantis Tziotzios Date: Thu, 10 Dec 2020 15:29:24 +0200 Subject: [PATCH] added matomo integration --- dmp-frontend/package.json | 1 + dmp-frontend/src/app/app.component.ts | 6 ++- dmp-frontend/src/app/app.module.ts | 17 ++++---- .../src/app/core/core-service.module.ts | 38 +++++++++--------- .../configuration/configuration.service.ts | 26 +++++++++++-- .../core/services/matomo/matomo-service.ts | 39 +++++++++++++++++++ .../src/app/ui/auth/login/login.component.ts | 5 ++- .../app/ui/dashboard/dashboard.component.ts | 5 ++- dmp-frontend/src/assets/config/config.json | 5 +++ 9 files changed, 109 insertions(+), 33 deletions(-) create mode 100644 dmp-frontend/src/app/core/services/matomo/matomo-service.ts diff --git a/dmp-frontend/package.json b/dmp-frontend/package.json index a12f8bb5f..2f48fb926 100644 --- a/dmp-frontend/package.json +++ b/dmp-frontend/package.json @@ -35,6 +35,7 @@ "ngx-cookieconsent": "^2.2.3", "ngx-dropzone": "^2.2.2", "ngx-guided-tour": "^1.1.10", + "ngx-matomo": "^0.1.4", "rxjs": "^6.3.2", "tinymce": "^5.4.2", "tslib": "^1.10.0", diff --git a/dmp-frontend/src/app/app.component.ts b/dmp-frontend/src/app/app.component.ts index cde53ffed..fb6133528 100644 --- a/dmp-frontend/src/app/app.component.ts +++ b/dmp-frontend/src/app/app.component.ts @@ -16,6 +16,8 @@ import { LanguageService } from './core/services/language/language.service'; import { ConfigurationService } from './core/services/configuration/configuration.service'; import { Location } from '@angular/common'; +import { MatomoInjector } from 'ngx-matomo'; +import { MatomoService } from './core/services/matomo/matomo-service'; declare const gapi: any; @@ -46,9 +48,11 @@ export class AppComponent implements OnInit { private ccService: NgcCookieConsentService, private language: LanguageService, private configurationService: ConfigurationService, - private location: Location + private location: Location, + private matomoService: MatomoService ) { this.initializeServices(); + this.matomoService.init(); this.helpContentEnabled = configurationService.helpService.enabled; } diff --git a/dmp-frontend/src/app/app.module.ts b/dmp-frontend/src/app/app.module.ts index a460a2e19..3582aa783 100644 --- a/dmp-frontend/src/app/app.module.ts +++ b/dmp-frontend/src/app/app.module.ts @@ -1,7 +1,8 @@ import { OverlayModule } from '@angular/cdk/overlay'; import { HttpClient, HttpClientModule } from '@angular/common/http'; -import { LOCALE_ID, NgModule, APP_INITIALIZER } from '@angular/core'; +import { LOCALE_ID, NgModule } from '@angular/core'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; +import { MatFormFieldDefaultOptions, MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material'; import { MatMomentDateModule, MAT_MOMENT_DATE_FORMATS } from '@angular/material-moment-adapter'; import { DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE } from '@angular/material/core'; import { BrowserModule, Title } from '@angular/platform-browser'; @@ -23,16 +24,14 @@ import { MomentUtcDateAdapter } from '@common/date/moment-utc-date-adapter'; import { CommonHttpModule } from '@common/http/common-http.module'; import { CommonUiModule } from '@common/ui/common-ui.module'; import { TranslateLoader, TranslateModule } from '@ngx-translate/core'; -import { TranslateHttpLoader } from '@ngx-translate/http-loader'; -import { environment } from 'environments/environment'; import { CookieService } from 'ngx-cookie-service'; import { NgcCookieConsentConfig, NgcCookieConsentModule } from 'ngx-cookieconsent'; -import { TranslateServerLoader } from './core/services/language/server.loader'; -import { BaseHttpService } from './core/services/http/base-http.service'; +import { MatomoModule } from 'ngx-matomo'; import { ConfigurationService } from './core/services/configuration/configuration.service'; -import { Oauth2DialogModule } from './ui/misc/oauth2-dialog/oauth2-dialog.module'; -import { MAT_FORM_FIELD_DEFAULT_OPTIONS, MatFormFieldDefaultOptions } from '@angular/material'; +import { TranslateServerLoader } from './core/services/language/server.loader'; +import { MatomoService } from './core/services/matomo/matomo-service'; import { GuidedTourModule } from './library/guided-tour/guided-tour.module'; +import { Oauth2DialogModule } from './ui/misc/oauth2-dialog/oauth2-dialog.module'; // AoT requires an exported function for factories export function HttpLoaderFactory(http: HttpClient, appConfig: ConfigurationService) { @@ -95,6 +94,7 @@ const appearance: MatFormFieldDefaultOptions = { CommonHttpModule, MatMomentDateModule, LoginModule, + MatomoModule, //Ui NotificationModule, NavigationModule, @@ -131,7 +131,8 @@ const appearance: MatFormFieldDefaultOptions = { useValue: appearance }, Title, - CookieService + CookieService, + MatomoService ], bootstrap: [AppComponent] }) diff --git a/dmp-frontend/src/app/core/core-service.module.ts b/dmp-frontend/src/app/core/core-service.module.ts index 02a2fca62..854728f85 100644 --- a/dmp-frontend/src/app/core/core-service.module.ts +++ b/dmp-frontend/src/app/core/core-service.module.ts @@ -1,8 +1,14 @@ -import { ModuleWithProviders, NgModule, Optional, SkipSelf, APP_INITIALIZER } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { APP_INITIALIZER, ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core'; import { CookieService } from 'ngx-cookie-service'; +import { AdminAuthGuard } from './admin-auth-guard.service'; import { AuthGuard } from './auth-guard.service'; import { AuthService } from './services/auth/auth.service'; +import { ConfigurationService } from './services/configuration/configuration.service'; +import { ContactSupportService } from './services/contact-support/contact-support.service'; import { CultureService } from './services/culture/culture-service'; +import { LanguageInfoService } from './services/culture/language-info-service'; +import { CurrencyService } from './services/currency/currency.service'; import { DashboardService } from './services/dashboard/dashboard.service'; import { DatasetProfileService } from './services/dataset-profile/dataset-profile.service'; import { DatasetWizardService } from './services/dataset-wizard/dataset-wizard.service'; @@ -11,6 +17,7 @@ import { DatasetService } from './services/dataset/dataset.service'; import { DmpInvitationService } from './services/dmp/dmp-invitation.service'; import { DmpProfileService } from './services/dmp/dmp-profile.service'; import { DmpService } from './services/dmp/dmp.service'; +import { EmailConfirmationService } from './services/email-confirmation/email-confirmation.service'; import { ExternalDataRepositoryService } from './services/external-sources/data-repository/extternal-data-repository.service'; import { ExternalDatasetService } from './services/external-sources/dataset/external-dataset.service'; import { ExternalSourcesConfigurationService } from './services/external-sources/external-sources-configuration.service'; @@ -18,32 +25,25 @@ import { ExternalSourcesService } from './services/external-sources/external-sou import { ExternalRegistryService } from './services/external-sources/registry/external-registry.service'; import { ExternalResearcherService } from './services/external-sources/researcher/external-researcher.service'; import { ExternalServiceService } from './services/external-sources/service/external-service.service'; -import { BaseHttpService } from './services/http/base-http.service'; -import { LoggingService } from './services/logging/logging-service'; -import { UiNotificationService } from './services/notification/ui-notification-service'; -import { ProgressIndicationService } from './services/progress-indication/progress-indication-service'; +import { FunderService } from './services/funder/funder.service'; import { GrantFileUploadService } from './services/grant/grant-file-upload.service'; import { GrantService } from './services/grant/grant.service'; +import { BaseHttpService } from './services/http/base-http.service'; +import { LanguageService } from './services/language/language.service'; +import { LockService } from './services/lock/lock.service'; +import { LoggingService } from './services/logging/logging-service'; +import { MergeEmailConfirmationService } from './services/merge-email-confirmation/merge-email-confirmation.service'; +import { UiNotificationService } from './services/notification/ui-notification-service'; +import { OrganisationService } from './services/organisation/organisation.service'; +import { ProgressIndicationService } from './services/progress-indication/progress-indication-service'; import { ProjectService } from './services/project/project.service'; +import { QuickWizardService } from './services/quick-wizard/quick-wizard.service'; import { SearchBarService } from './services/search-bar/search-bar.service'; import { TimezoneService } from './services/timezone/timezone-service'; +import { UserGuideService } from './services/user-guide/user-guide.service'; import { UserService } from './services/user/user.service'; import { CollectionUtils } from './services/utilities/collection-utils.service'; import { TypeUtils } from './services/utilities/type-utils.service'; -import { QuickWizardService } from './services/quick-wizard/quick-wizard.service'; -import { OrganisationService } from './services/organisation/organisation.service'; -import { EmailConfirmationService } from './services/email-confirmation/email-confirmation.service'; -import { FunderService } from './services/funder/funder.service'; -import { ContactSupportService } from './services/contact-support/contact-support.service'; -import { LanguageService } from './services/language/language.service'; -import { AdminAuthGuard } from './admin-auth-guard.service'; -import { LockService } from './services/lock/lock.service'; -import { UserGuideService } from './services/user-guide/user-guide.service'; -import { ConfigurationService } from './services/configuration/configuration.service'; -import { HttpClient } from '@angular/common/http'; -import { LanguageInfoService } from './services/culture/language-info-service'; -import { CurrencyService } from './services/currency/currency.service'; -import { MergeEmailConfirmationService } from './services/merge-email-confirmation/merge-email-confirmation.service'; // // // This is shared module that provides all the services. Its imported only once on the AppModule. 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 e789a6b91..b0c9b8ae9 100644 --- a/dmp-frontend/src/app/core/services/configuration/configuration.service.ts +++ b/dmp-frontend/src/app/core/services/configuration/configuration.service.ts @@ -11,7 +11,7 @@ import { HttpClient } from '@angular/common/http'; @Injectable({ providedIn: 'root', - }) +}) export class ConfigurationService extends BaseComponent { constructor(private http: HttpClient) { super(); } @@ -52,12 +52,12 @@ export class ConfigurationService extends BaseComponent { } private _guideAssets: string; - get guideAssets():string { + get guideAssets(): string { return this._guideAssets; } private _allowOrganizationCreator: boolean; - get allowOrganizationCreator():boolean { + get allowOrganizationCreator(): boolean { return this._allowOrganizationCreator; } @@ -76,6 +76,21 @@ export class ConfigurationService extends BaseComponent { return this._orcidPath; } + private _matomoEnabled: boolean; + get matomoEnabled(): boolean { + return this._matomoEnabled; + } + + private _matomoSiteUrl: string; + get matomoSiteUrl(): string { + return this._matomoSiteUrl; + } + + private _matomoSiteId: number; + get matomoSiteId(): number { + return this._matomoSiteId; + } + public loadConfiguration(): Promise { return new Promise((r, e) => { @@ -114,6 +129,11 @@ export class ConfigurationService extends BaseComponent { this._doiLink = config.doiLink; this._useSplash = config.useSplash; this._orcidPath = config.orcidPath; + if (config.matomo) { + this._matomoEnabled = config.matomo.enabled; + this._matomoSiteUrl = config.matomo.url; + this._matomoSiteId = config.matomo.siteId; + } } } diff --git a/dmp-frontend/src/app/core/services/matomo/matomo-service.ts b/dmp-frontend/src/app/core/services/matomo/matomo-service.ts new file mode 100644 index 000000000..7ad2660c9 --- /dev/null +++ b/dmp-frontend/src/app/core/services/matomo/matomo-service.ts @@ -0,0 +1,39 @@ +import { Injectable } from '@angular/core'; +import { MatomoInjector, MatomoTracker } from 'ngx-matomo'; +import { AuthService } from '../auth/auth.service'; +import { ConfigurationService } from '../configuration/configuration.service'; + +@Injectable() +export class MatomoService { + + constructor( + private configurationService: ConfigurationService, + private matomoInjector: MatomoInjector, + private matomoTracker: MatomoTracker, + private authService: AuthService + ) { + + } + + init() { + if (this.configurationService.matomoEnabled) { + this.matomoInjector.init(this.configurationService.matomoSiteUrl, this.configurationService.matomoSiteId); + } + } + + trackPageView(customTitle?: string): void { + if (this.configurationService.matomoEnabled) { + var principal = this.authService.current(); + this.matomoTracker.setUserId(principal ? principal.id : null); + this.matomoTracker.trackPageView(customTitle); + } + } + + trackSiteSearch(keyword: string, category?: string, resultsCount?: number): void { + if (this.configurationService.matomoEnabled) { + var principal = this.authService.current(); + this.matomoTracker.setUserId(principal ? principal.id : null); + this.matomoTracker.trackSiteSearch(keyword, category, resultsCount); + } + } +} diff --git a/dmp-frontend/src/app/ui/auth/login/login.component.ts b/dmp-frontend/src/app/ui/auth/login/login.component.ts index 87d99fa12..4050b12c0 100644 --- a/dmp-frontend/src/app/ui/auth/login/login.component.ts +++ b/dmp-frontend/src/app/ui/auth/login/login.component.ts @@ -17,6 +17,7 @@ import { FormControl } from '@angular/forms'; import { OrcidUser } from '@app/core/model/orcid/orcidUser'; import { ZenodoToken } from '@app/core/model/zenodo/zenodo-token.model'; import { LoginInfo } from '@app/core/model/auth/login-info'; +import { MatomoService } from '@app/core/services/matomo/matomo-service'; /// /// @@ -52,10 +53,12 @@ export class LoginComponent extends BaseComponent implements OnInit, AfterViewIn private configurationService: ConfigurationService, private mergeLoginService: MergeLoginService, private oauth2DialogService: Oauth2DialogService, - private httpClient: HttpClient + private httpClient: HttpClient, + private matomoService: MatomoService ) { super(); } ngOnInit(): void { + this.matomoService.trackPageView('loginPage'); this.route.queryParams .pipe(takeUntil(this._destroyed)) .subscribe((params: Params) => { diff --git a/dmp-frontend/src/app/ui/dashboard/dashboard.component.ts b/dmp-frontend/src/app/ui/dashboard/dashboard.component.ts index 25fa71ba6..35702bdbe 100644 --- a/dmp-frontend/src/app/ui/dashboard/dashboard.component.ts +++ b/dmp-frontend/src/app/ui/dashboard/dashboard.component.ts @@ -34,6 +34,7 @@ import { TranslateService } from '@ngx-translate/core'; import { UiNotificationService, SnackBarNotificationLevel } from '@app/core/services/notification/ui-notification-service'; import { GuidedTourService } from '@app/library/guided-tour/guided-tour.service'; import { GuidedTour, Orientation } from '@app/library/guided-tour/guided-tour.constants'; +import { MatomoService } from '@app/core/services/matomo/matomo-service'; @Component({ @@ -86,7 +87,8 @@ export class DashboardComponent extends BaseComponent implements OnInit, IBreadC private dialog: MatDialog, private language: TranslateService, private uiNotificationService: UiNotificationService, - private guidedTourService: GuidedTourService + private guidedTourService: GuidedTourService, + private matomoService: MatomoService ) { super(); // this.dashboardStatisticsData.totalDataManagementPlanCount = 0; @@ -96,6 +98,7 @@ export class DashboardComponent extends BaseComponent implements OnInit, IBreadC ngOnInit() { + this.matomoService.trackPageView('Home Dashboard'); // if (this.isAuthenticated()) { // this.userService.getRecentActivity() // .pipe(takeUntil(this._destroyed)) diff --git a/dmp-frontend/src/assets/config/config.json b/dmp-frontend/src/assets/config/config.json index cff19d7d4..ea878f347 100644 --- a/dmp-frontend/src/assets/config/config.json +++ b/dmp-frontend/src/assets/config/config.json @@ -48,6 +48,11 @@ "enabled": true, "logLevels": ["debug", "info", "warning", "error"] }, + "matomo": { + "enabled": true, + "url": "https://beta.analytics.openaire.eu/", + "siteId": 361 + }, "lockInterval": 60000, "guideAssets": "assets/images/guide", "allowOrganizationCreator": true,