argos/dmp-frontend/src/app/core/services/matomo/matomo-service.ts

51 lines
1.8 KiB
TypeScript

import { Injectable } from '@angular/core';
import { MatomoInitializerService, MatomoTracker } from 'ngx-matomo-client';
import { AuthService } from '../auth/auth.service';
import { ConfigurationService } from '../configuration/configuration.service';
@Injectable()
export class MatomoService {
constructor(
private configurationService: ConfigurationService,
private matomoInitializerService: MatomoInitializerService,
private matomoTracker: MatomoTracker,
private authService: AuthService
) {
}
init() {
if (this.configurationService.matomoEnabled) {
this.matomoInitializerService.initializeTracker({ trackerUrl: this.configurationService.matomoSiteUrl, siteId: this.configurationService.matomoSiteId });
}
}
trackPageView(customTitle?: string): void {
if (this.configurationService.matomoEnabled) {
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) {
var principalid = this.authService.userId();
if (principalid != null) { this.matomoTracker.setUserId(principalid.toString()); }
this.matomoTracker.trackSiteSearch(keyword, category, resultsCount);
}
}
trackDownload(category: "dmps" | "datasets" | "descriptions", type: string, id: string): void {
if (this.configurationService.matomoEnabled) {
var principalid = this.authService.userId();
if (principalid != null) { this.matomoTracker.setUserId(principalid.toString()); }
this.matomoTracker.trackLink(this.configurationService.server + category + "/" + type + "/" + id, "download");
// this.matomoTracker.trackLink(url, "download");
// this.matomoTracker.trackEvent(category, "Downloaded", type);
}
}
}