import { Actions , ofActionSuccessful , Store } from '@ngxs/store' ; import { Injectable } from '@angular/core' ; import { distinctUntilChanged , filter , map } from 'rxjs/operators' ; import { NavigationEnd , Router , RouterEvent } from '@angular/router' ; import { RouterNavigation , RouterState } from '@ngxs/router-plugin' ; import { PageTitleService } from '../services/page-title.service' ; import { GoogleAnalyticsService } from '../services/google-analytics.service' ; import { RouterStateData } from './custom-router-state.serializer' ; @ Injectable ( { providedIn : 'root' , } ) export class RouteHandler { constructor ( private router , private actions$ , private analytics , private pageTitle ) { this . actions$ . pipe ( ofActionSuccessful ( RouterNavigation ) , map ( ( action ) => action . routerState as any ) , distinctUntilChanged ( ( previous , current ) => { return previous . url === current . url ; } ) ) . subscribe ( ( data ) => { this . pageTitle . setTitle ( data . breadcrumbs ) ; this . analytics . setPage ( data . url ) ; } ) ; } }