Enable Azure Monitor Application Insights Real User Monitoring

The Microsoft Azure Monitor Application Insights JavaScript SDK collects usage data, which allows you to monitor and analyze the performance of JavaScript web applications. This is commonly referred to as Real User Monitoring or RUM.

The Application Insights JavaScript SDK has a base SDK and several plugins for more capabilities.

Conceptual diagram that shows the Application Insights JavaScript SDK, its plugins/extensions, and their relationship to each other.

We collect page views by default. But if you want to also collect clicks by default, consider adding the Click Analytics Auto-Collection plug-in:

We provide the Debug plugin and Performance plugin for debugging/testing. In rare cases, it's possible to build your own extension by adding a custom plugin.

Prerequisites

Get started

Follow the steps in this section to instrument your application with the Application Insights JavaScript SDK.

Tip

Good news! We're making it even easier to enable JavaScript with JavaScript (Web) SDK Loader Script injection by configuration.

Add the JavaScript code

Two methods are available to add the code to enable Application Insights via the Application Insights JavaScript SDK:

Method When would I use this method?
JavaScript (Web) SDK Loader Script For most customers, we recommend the JavaScript (Web) SDK Loader Script because you never have to update the SDK and you get the latest updates automatically. Also, you have control over which pages you add the Application Insights JavaScript SDK to.
npm package You want to bring the SDK into your code and enable IntelliSense. This option is only needed for developers who require more custom events and configuration. This method is required if you plan to use the React, React Native, or Angular Framework Extension.
  1. Use the following command to install the Microsoft Application Insights JavaScript SDK - Web package.

    npm i --save @microsoft/applicationinsights-web
    

    Typings are included with this package, so you don't need to install a separate typings package.

  2. Add the following JavaScript to your application's code.

    Where and also how you add this JavaScript code depends on your application code. For example, you might be able to add it exactly as it appears below or you may need to create wrappers around it.

    import { ApplicationInsights } from '@microsoft/applicationinsights-web'
    
    const appInsights = new ApplicationInsights({ config: {
      connectionString: 'YOUR_CONNECTION_STRING'
      /* ...Other Configuration Options... */
    } });
    appInsights.loadAppInsights();
    appInsights.trackPageView();
    

Paste the connection string in your environment

To paste the connection string in your environment, follow these steps:

  1. Navigate to the Overview pane of your Application Insights resource.

  2. Locate the Connection String.

  3. Select the Copy to clipboard icon to copy the connection string to the clipboard.

    Screenshot that shows Application Insights overview and connection string.

  4. Replace the placeholder "YOUR_CONNECTION_STRING" in the JavaScript code with your connection string copied to the clipboard.

    The connectionString format must follow "InstrumentationKey=xxxx;....". If the string provided doesn't meet this format, the SDK load process fails.

    The connection string isn't considered a security token or key. For more information, see Do new Azure regions require the use of connection strings?.

    Note

    The Application Insights JavaScript SDK requires the connection string to be provided during initialization and configuration. This connection string is visible in plain text in client browsers, and there is no straightforward way to use Microsoft Entra ID-based authentication for browser telemetry. We recommend that you consider creating a separate Application Insights resource with local authentication enabled for JavaScript browser-based telemetry if you need to secure the service telemetry separately using Microsoft Entra ID-based authentication.

(Optional) Add SDK configuration

The optional SDK configuration is passed to the Application Insights JavaScript SDK during initialization.

To add SDK configuration, add each configuration option directly under connectionString. For example:

Screenshot of JavaScript code with SDK configuration options added and highlighted.

(Optional) Add advanced SDK configuration

If you want to use the extra features provided by plugins for specific frameworks and optionally enable the Click Analytics plug-in, see:

Confirm data is flowing

  1. Go to your Application Insights resource that you enabled the SDK for.

  2. In the Application Insights resource menu on the left, under Investigate, select the Transaction search pane.

  3. Open the Event types dropdown menu and select Select all to clear the checkboxes in the menu.

  4. From the Event types dropdown menu, select:

    • Page View for Azure Monitor Application Insights Real User Monitoring
    • Custom Event for the Click Analytics Auto-Collection plug-in.

    It might take a few minutes for data to show up in the portal. If the only data you see showing up is a load failure exception, see Troubleshoot SDK load failure for JavaScript web apps.

    In some cases, if multiple instances of different versions of Application Insights are running on the same page, errors can occur during initialization. For these cases and the error message that appears, see Running multiple versions of the Application Insights JavaScript SDK in one session. If you encounter one of these errors, try changing the namespace by using the name setting. For more information, see JavaScript (Web) SDK Loader Script configuration.

    Screenshot of the Application Insights Transaction search pane in the Azure portal with the Page View option selected. The page views are highlighted.

  5. If you want to query data to confirm data is flowing:

    1. Select Logs in the left pane.

      When you select Logs, the Queries dialog opens, which contains sample queries relevant to your data.

    2. Select Run for the sample query you want to run.

    3. If needed, you can update the sample query or write a new query by using Kusto Query Language (KQL).

      For essential KQL operators, see Learn common KQL operators.

Frequently asked questions

This section provides answers to common questions.

What are the user and session counts?

  • The JavaScript SDK sets a user cookie on the web client, to identify returning users, and a session cookie to group activities.
  • If there's no client-side script, you can set cookies at the server.
  • If one real user uses your site in different browsers, or by using in-private/incognito browsing, or different machines, they're counted more than once.
  • To identify a signed-in user across machines and browsers, add a call to setAuthenticatedUserContext().

What is the JavaScript SDK performance/overhead?

The Application Insights JavaScript SDK has a minimal overhead on your website. At just 36 KB gzipped, and taking only ~15 ms to initialize, the SDK adds a negligible amount of load time to your website. The minimal components of the library are quickly loaded when you use the SDK, and the full script is downloaded in the background.

Additionally, while the script is downloading from the CDN, all tracking of your page is queued, so you don't lose any telemetry during the entire life cycle of your page. This setup process provides your page with a seamless analytics system that's invisible to your users.

What browsers are supported by the JavaScript SDK?

Chrome Firefox IE Opera Safari
Chrome Latest ✔ Firefox Latest ✔ v3.x: IE 9+ & Microsoft Edge ✔
v2.x: IE 8+ Compatible & Microsoft Edge ✔
Opera Latest ✔ Safari Latest ✔

Where can I find code examples for the JavaScript SDK?

For runnable examples, see Application Insights JavaScript SDK samples.

What is the ES3/Internet Explorer 8 compatibility with the JavaScript SDK?

We need to take necessary measures to ensure that this SDK continues to "work" and doesn't break the JavaScript execution when loaded by an older browser. It would be ideal to not support older browsers, but numerous large customers can't control which browser their users choose to use.

This statement doesn't mean that we only support the lowest common set of features. We need to maintain ES3 code compatibility. New features need to be added in a manner that wouldn't break ES3 JavaScript parsing and added as an optional feature.

See GitHub for full details on Internet Explorer 8 support.

Is the JavaScript SDK open-source?

Yes, the Application Insights JavaScript SDK is open source. To view the source code or to contribute to the project, see the official GitHub repository.

Support

  • If you can't run the application or you aren't getting data as expected, see the dedicated troubleshooting article.
  • For common question about the JavaScript SDK, see the FAQ.
  • For Azure support issues, open an Azure support ticket.
  • For a list of open issues related to the Application Insights JavaScript SDK, see the GitHub Issues Page.
  • Use the Telemetry Viewer extension to list out the individual events in the network payload and monitor the internal calls within Application Insights.

Next steps