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.
JavaScript
x
13
13
1
constructor(router:Router) {
2
router.events.subscribe(event => {
3
if(event instanceof NavigationStart) {
4
}
5
if(event instanceof NavigationEnd) {
6
// log record to file here --> 'user navigated to XYZ page.'
7
}
8
// NavigationCancel
9
// NavigationError
10
// RoutesRecognized
11
}
12
});
13
For API calls, you can implement HTTP interceptor.
JavaScript
1
18
18
1
@NgModule({
2
3
providers: [{
4
provide: HTTP_INTERCEPTORS,
5
useClass: TokenInterceptorService,
6
multi: true
7
}]
8
})
9
export class AppModule { }
10
11
@Injectable()
12
export class MyInterceptor implements HttpInterceptor {
13
intercept( req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
14
// log record to file --> 'calling api {url}'
15
return next.handle(req);
16
}
17
}
18