added analytics service (in progress)

This commit is contained in:
Sofia Papacharalampous 2024-05-21 18:04:57 +03:00
parent e4786ed5f5
commit d63e87c9d6
6 changed files with 121 additions and 2 deletions

View File

@ -40,6 +40,7 @@ import { OpenDMPCustomTranslationCompiler } from './utilities/translate/opendmp-
import { CoreAnnotationServiceModule } from 'annotation-service/services/core-service.module'; import { CoreAnnotationServiceModule } from 'annotation-service/services/core-service.module';
import { CoreNotificationServiceModule } from '@notification-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 { 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 // AoT requires an exported function for factories
export function HttpLoaderFactory(languageHttpService: LanguageHttpService) { export function HttpLoaderFactory(languageHttpService: LanguageHttpService) {
@ -182,7 +183,8 @@ export function InstallationConfigurationFactory(appConfig: ConfigurationService
}, },
Title, Title,
CookieService, CookieService,
MatomoService MatomoService,
AnalyticsService
], ],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })

View File

@ -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"
}

View File

@ -10,6 +10,7 @@ import { HttpClient } from '@angular/common/http';
import { KeycloakConfiguration } from '@app/core/model/configuration-models/keycloak-configuration.model'; import { KeycloakConfiguration } from '@app/core/model/configuration-models/keycloak-configuration.model';
import { Guid } from '@common/types/guid'; import { Guid } from '@common/types/guid';
import { AuthProviders } from '@app/core/model/configuration-models/auth-providers.model'; import { AuthProviders } from '@app/core/model/configuration-models/auth-providers.model';
import { AnalyticsProviders } from '@app/core/model/configuration-models/analytics-providers.model';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -154,6 +155,11 @@ export class ConfigurationService extends BaseComponent {
return this._authProviders; return this._authProviders;
} }
private _analyticsProviders: AnalyticsProviders;
get analyticsProviders(): AnalyticsProviders {
return this._analyticsProviders;
}
private _researcherId: any; private _researcherId: any;
get researcherId(): boolean { get researcherId(): boolean {
return this._researcherId; return this._researcherId;
@ -239,6 +245,7 @@ export class ConfigurationService extends BaseComponent {
this._newReleaseNotificationLink = config.newReleaseNotification?.link; this._newReleaseNotificationLink = config.newReleaseNotification?.link;
this._newReleaseNotificationVersionCode = config.newReleaseNotification?.versionCode; this._newReleaseNotificationVersionCode = config.newReleaseNotification?.versionCode;
this._authProviders = AuthProviders.parseValue(config.authProviders); this._authProviders = AuthProviders.parseValue(config.authProviders);
this._analyticsProviders = AnalyticsProviders.parseValue(config.analytics);
this._researcherId = config.referenceTypes.researcherId; this._researcherId = config.referenceTypes.researcherId;
this._grantId = config.referenceTypes.grantId; this._grantId = config.referenceTypes.grantId;
this._organizationId = config.referenceTypes.organizationId; this._organizationId = config.referenceTypes.organizationId;

View File

@ -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 {
}
}

View File

@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { MatomoInitializerService, MatomoTracker } from 'ngx-matomo-client'; import { MatomoInitializerService, MatomoTracker } from 'ngx-matomo-client';
import { AuthService } from '../auth/auth.service'; import { AuthService } from '../auth/auth.service';
import { ConfigurationService } from '../configuration/configuration.service'; import { ConfigurationService } from '../configuration/configuration.service';
import { AnalyticsProvider } from '@app/core/model/configuration-models/analytics-providers.model';
@Injectable() @Injectable()
export class MatomoService { export class MatomoService {
@ -29,6 +30,14 @@ export class MatomoService {
} }
} }
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 { trackSiteSearch(keyword: string, category?: string, resultsCount?: number): void {
if (this.configurationService.matomoEnabled) { if (this.configurationService.matomoEnabled) {
var principalid = this.authService.userId(); var principalid = this.authService.userId();

View File

@ -18,6 +18,7 @@ import { CookieService } from 'ngx-cookie-service';
import { takeUntil } from 'rxjs/operators'; import { takeUntil } from 'rxjs/operators';
import { StartNewDescriptionDialogComponent } from '../description/start-new-description-dialog/start-new-description-dialog.component'; 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 { StartNewDmpDialogComponent } from '../dmp/new/start-new-dmp-dialogue/start-new-dmp-dialog.component';
import { AnalyticsService } from '@app/core/services/matomo/analytics-service';
@Component({ @Component({
@ -43,6 +44,7 @@ export class DashboardComponent extends BaseComponent implements OnInit {
private language: TranslateService, private language: TranslateService,
private guidedTourService: GuidedTourService, private guidedTourService: GuidedTourService,
private matomoService: MatomoService, private matomoService: MatomoService,
private analyticsService: AnalyticsService,
public referenceTypeService: ReferenceTypeService, public referenceTypeService: ReferenceTypeService,
private fb: UntypedFormBuilder, private fb: UntypedFormBuilder,
private cookieService: CookieService, 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()) { if (!this.isAuthenticated()) {
this.dashboardService.getPublicDashboardStatistics() this.dashboardService.getPublicDashboardStatistics()