I want to get and save user interaction with angular app, and app interactions with the APIs the add this log to a file.
Advertisement
Answer
If I understood your question correctly, you can write common logic on one place for user page navigation and API call.
For page navigation, subscribe to router events. Ex.
constructor(router:Router) { router.events.subscribe(event => { if(event instanceof NavigationStart) { } if(event instanceof NavigationEnd) { // log record to file here --> 'user navigated to XYZ page.' } // NavigationCancel // NavigationError // RoutesRecognized } });
For API calls, you can implement HTTP interceptor.
@NgModule({ ... providers: [{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptorService, multi: true }] }) export class AppModule { } @Injectable() export class MyInterceptor implements HttpInterceptor { intercept( req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { // log record to file --> 'calling api {url}' return next.handle(req); } }