Open Closed

What is the best practice in ABP angular project to connect the front end with Azure AppInsights #9857


User avatar
0
sinancondo created

So FE can send telemetries to Azure AppInsights


1 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent

    To connect your ABP Angular frontend to Azure Application Insights for telemetry, follow these best practices:

    1. Install the Application Insights JavaScript SDK in your Angular project:
    npm install @microsoft/applicationinsights-web
    
    1. Create an Angular service to encapsulate Application Insights logic:
    // app-insights.service.ts
       import { Injectable } from '@angular/core';
       import { ApplicationInsights } from '@microsoft/applicationinsights-web';
    
       @Injectable({ providedIn: 'root' })
       export class AppInsightsService {
         appInsights: ApplicationInsights;
    
         constructor() {
           this.appInsights = new ApplicationInsights({
             config: {
               instrumentationKey: 'YOUR_INSTRUMENTATION_KEY'
             }
           });
           this.appInsights.loadAppInsights();
         }
    
         logPageView(name?: string, url?: string) {
           this.appInsights.trackPageView({ name, uri: url });
         }
    
         logEvent(name: string, properties?: { [key: string]: any }) {
           this.appInsights.trackEvent({ name }, properties);
         }
    
         logException(exception: Error, severityLevel?: number) {
           this.appInsights.trackException({ exception, severityLevel });
         }
       }
    
    1. Use the service in your components or interceptors to log telemetry:
    constructor(private appInsightsService: AppInsightsService) {}
    
       ngOnInit() {
         this.appInsightsService.logPageView('HomePage', window.location.href);
       }
    
    1. (Optional) Integrate with Angular Router to automatically track page views:
    import { Router, NavigationEnd } from '@angular/router';
    
       constructor(router: Router, appInsightsService: AppInsightsService) {
         router.events.subscribe(event => {
           if (event instanceof NavigationEnd) {
             appInsightsService.logPageView(undefined, event.urlAfterRedirects);
           }
         });
       }
    
    1. Store the instrumentation key in environment files for different deployments (e.g., environment.prod.ts).

    2. For advanced scenarios, refer to the official Application Insights JavaScript SDK documentation.

    Sources:


    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

Learn More, Pay Less
33% OFF
All Trainings!
Get Your Deal
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on September 12, 2025, 10:20