import { Injectable } from "@angular/core" ; import { Angulartics2Matomo } from "angulartics2/matomo" ; import { environment } from "../../../environments/environment" ; import { AppConfig } from "../app-config/app-config" ; import { ConfigService } from "../config/config.service" ; import { SessionService } from "../session/session-service/session.service" ; import { LoginState } from "../session/session-states/login-state.enum" ; import { USAGE_ANALYTICS_CONFIG_ID , UsageAnalyticsConfig , } from "./usage-analytics-config" ; import { Angulartics2 } from "angulartics2" ; const md5 = require ( "md5" ) ; /**
 * Track usage analytics data and report it to a backend server like Matomo.
 *
 * This is automatically disabled if the config.json does not specify "usage_analytics" settings.
 */ @ Injectable ( { providedIn : "root" , } ) export class AnalyticsService { private static getUserHash ( username ) { return md5 ( AppConfig . settings ?. site_name + username ) ; } constructor ( private angulartics2 , private angulartics2Matomo , private configService , private sessionService ) { this . subscribeToUserChanges ( ) ; } private setUser ( username ) { this . angulartics2Matomo . setUsername ( AnalyticsService . getUserHash ( username ?? "" ) ) ; } private setVersion ( ) { this . angulartics2 . setUserProperties . next ( { dimension1 : "ndb-core@" + environment . appVersion , } ) ; } private setOrganization ( orgName ) { this . angulartics2 . setUserProperties . next ( { dimension2 : orgName , } ) ; } private subscribeToUserChanges ( ) { this . sessionService . loginState . subscribe ( ( newState ) => { if ( newState === LoginState . LOGGED_IN ) { this . setUser ( this . sessionService . getCurrentUser ( ) . name ) ; } else { this . setUser ( undefined ) ; } } ) ; } /**
     * Set up usage analytics tracking - if the AppConfig specifies the required settings.
     */ init ( ) { const config = this . configService . getConfig < UsageAnalyticsConfig > ( USAGE_ANALYTICS_CONFIG_ID ) ; if ( ! config || ! config . url || ! config . site_id ) { return ; } this . setUpMatomo ( config . url , config . site_id , config . no_cookies ) ; this . setVersion ( ) ; this . setOrganization ( AppConfig . settings . site_name ) ; this . setUser ( undefined ) ; this . angulartics2Matomo . startTracking ( ) ; } param url param id param disableCookies private private setUpMatomo ( url , id , disableCookies = false ) { window [ "_paq" ] = window [ "_paq" ] || [ ] ; window [ "_paq" ] . push ( [ "setDocumentTitle" , document . domain + "/" + document . title , ] ) ; if ( disableCookies ) { window [ "_paq" ] . push ( [ "disableCookies" ] ) ; } window [ "_paq" ] . push ( [ "trackPageView" ] ) ; window [ "_paq" ] . push ( [ "enableLinkTracking" ] ) ; const u = url . endsWith ( "/" ) ? url : url + "/" ; window [ "_paq" ] . push ( [ "setTrackerUrl" , u + "matomo.php" ] ) ; window [ "_paq" ] . push ( [ "setSiteId" , id ] ) ; const d = document ; const g = d . createElement ( "script" ) ; const s = d . getElementsByTagName ( "script" ) [ 0 ] ; g . type = "text/javascript" ; g . async = true ; g . defer = true ; g . src = u + "matomo.js" ; s . parentNode . insertBefore ( g , s ) ; } param action param properties eventTrack ( action , properties = { category : "no_category" , label : "no_label" , } ) { this . angulartics2 . eventTrack . next ( { action : action , properties : properties , } ) ; } }