Microsoft Application Insights JavaScript SDK

repository·main·Indexed 20 days ago

https://github.com/microsoft/applicationinsights-js

A JavaScript SDK for telemetry collection and monitoring in browser-based applications, enabling developers to track usage, performance, and errors in Azure. The @microsoft/applicationinsights-web package (v3.4.3) provides a bundled distribution for web scenarios. The SDK is available via npm and CDN, and includes tools for CDN resource deployment and management via PowerShell scripts.

Tokens
116.6K
Snippets
263
Records
396
Agent score
65%

What's inside applicationinsights-js

  1. Explore Application Insights JavaScript SDK Examples

    main

    The examples/ directory contains a collection of implementation patterns and use cases for the Application Insights SDK. These examples demonstrate how to use the SDK in specific environments or with specific plugins.

    Available example modules include:

    • Shared Worker: Implementation patterns for using Application Insights within a Web Worker context.
    • AISKU: Examples related to the AISKU functionality.
    • Dependency: How to track dependencies using the SDK.
    • CfgSync Plugin: Usage and configuration of the Configuration Synchronization (CfgSync) plugin.
  2. Understand the Application Insights JavaScript SDK components and SKUs

    main

    The Application Insights JavaScript SDK is composed of a core package and various SKUs, components, and extensions.

    • Core Package: Application Insights Core is the mandatory foundation required by all SKUs, components, and extensions.
    • SKUs: There are different versions of the SDK available, such as the full Application Insights Web Sku and the lightweight Microsoft Application Insights JavaScript SDK - Web Basic.
    • Components: These handle data transmission and specific channel logic (e.g., Offline Channel, Tee Channel, 1DS Post Channel).
    • Extensions: These provide additional functionality like Web Analytics, Click Analytics, Performance Mark and Measure Manager, and XHR dependencies tracking.
  3. Security Note: Error Handler and Unencrypted Stack Traces

    main
    The SDK's error handler sends error stack traces without encryption. While this provides full visibility for diagnostics, be aware that sensitive information contained within error messages or stack traces will be transmitted in plain text.
  4. How to send synchronous events with PostChannel

    main

    By default, events are batched and sent asynchronously. To send an event immediately within the same JavaScript execution cycle, set the sync property on the event object.

    Warning: Use this sparingly. Each sync event creates a new outbound connection, which can negatively impact user experience if overused. Additionally, sendBeacon() and fetch() (with keepalive) have a maximum payload size of 64Kb; if the serialized JSON event exceeds this, it will be dropped.

    Supported sync values

    NameValueDescription
    Batchedundefined, false, 0Default behavior. Events are batched and sent asynchronously.
    Synchronoustrue, 1Attempts to send using the first available transport: httpXHROverride, XMLHttpRequest (sync), fetch() [keepalive], or sendBeacon().
    SendBeacon2Attempts to send with a preference for the sendBeacon() API. Fallbacks: fetch() [keepalive], XMLHttpRequest (sync), httpXHROverride.
    SyncFetch3Attempts to send with a preference for fetch() with the keepalive flag. Fallbacks: sendBeacon(), XMLHttpRequest (sync), httpXHROverride.
    // Example of setting a sync property (conceptual usage)
    event.sync = 2; // Using SendBeacon preference
  5. Understand CDN deployment endpoints and schedules

    main

    The SDK uses different CDN endpoints to manage the rollout of new releases. It is recommended to use beta or next URLs in test or canary environments to catch issues before they reach production users.

    EndpointURL PatternRollout Schedule
    Betahttps://js.monitor.azure.com/beta/ai.3.gbl.min.jsSame day as the NPM release
    Nexthttps://js.monitor.azure.com/next/ai.3.gbl.min.jsOne work day after beta promotion
    Publichttps://js.monitor.azure.com/scripts/b/ai.3.gbl.min.jsOne work day after next promotion (typically 2 work days after release)

    Note: If a release falls on a Friday, the Public promotion is delayed until the following Monday.

  6. Browser Support and ES3 Requirements

    main

    The Application Insights JS SDK v3.x no longer supports ES3 environments (such as IE8).

    • Reasoning: Internal polyfills for ES5 features have been removed, and the SDK no longer wraps ES3 reserved words (like catch) during packaging.
    • Requirement: The SDK requires Object.defineProperty support to function correctly.
    • Mitigation: If your application must maintain support for IE8, you must continue using the latest v2.x releases.