Application Insights Web SDK tracks several page lifecycle events to ensure that telemetry data is sent before a page is unloaded, helping to prevent data loss during page navigation or browser closure. The SDK hooks into different unload and visibility change events depending on the browser environment.
The SDK listens to the following events to detect when a page is being unloaded:
Modern browsers and frameworks are deprecating or changing how some page unload events work:
unload
event, showing warning messages when it’s used.beforeunload
event for better performance and reduced tracking potential.pagehide
event and visibilitychange
events are becoming the recommended alternatives.The SDK provides configuration options to control which page lifecycle events are used:
This configuration option allows you to specify which page unload events should not be used by the SDK.
TypeScript Example:
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
const appInsights = new ApplicationInsights({
config: {
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
disablePageUnloadEvents: ["unload"],
/* ...Other Configuration Options... */
}
});
Important notes:
disablePageUnloadEvents
or if the only working events in the current browser environment are the ones you’ve disabled, the SDK will still use one of them to ensure functionality.Similarly, this configuration option controls which page show events are not used by the SDK. Page show events include:
TypeScript Example:
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
const appInsights = new ApplicationInsights({
config: {
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
disablePageShowEvents: ["pageshow"],
/* ...Other Configuration Options... */
}
});
The SDK implements a robust fallback mechanism to ensure that telemetry can be sent before the page unloads:
disablePageUnloadEvents
.disablePageUnloadEvents
setting and force registration of at least one event.If you’re using jQuery 3.7.1 or newer, you’ll encounter deprecation warnings when the ‘unload’ event is used. Configure the SDK to not use this deprecated event:
TypeScript Example:
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
const appInsights = new ApplicationInsights({
config: {
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
// Disable the deprecated 'unload' event to avoid jQuery deprecation warnings
disablePageUnloadEvents: ["unload"],
}
});
For the best experience in modern browsers, you might want to prioritize newer events:
TypeScript Example:
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
const appInsights = new ApplicationInsights({
config: {
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
// Use only modern events
disablePageUnloadEvents: ["unload", "beforeunload"],
}
});
Note that the SDK will still use an older event if none of the modern events are supported in the browser environment.
You can configure both unload and show events simultaneously for fine-grained control:
TypeScript Example:
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
const appInsights = new ApplicationInsights({
config: {
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
// Disable specific unload events
disablePageUnloadEvents: ["unload"],
// Disable specific show events
disablePageShowEvents: ["pageshow"],
}
});
The SDK’s approach to page lifecycle events reflects a progressive enhancement strategy, ensuring that telemetry works across diverse browser environments while offering configuration options for optimal behavior in modern contexts.