Filtering App Insights Client Exceptions from 3rd party JavaScript

So, we are using App Insights, and when we moved the client side script for recording JavaScript errors to our integration test instance we started to get a lot of errors of the form:

Script error: The browser’s same-origin policy prevents us from getting the details of this exception. Consider using the ‘crossorigin’ attribute.

There were a lot of these errors:

… and investigating them showed that the problems came from 3rd party JavaScript. All of these are inserted by Google Tag Manager, and aren’t in the site, or local development.

Well, I’m not going to be able to fix JavaScript written by 2 third-parties, and that isn’t even loaded directly by my page – so instead I’m going to ignore that error…

It turns out that the client-side script has an onInit event, which lets us load filters, as described here, and that others have written filters for exactly this error :

 !function (T, l, y) { <snipped>
	src: "https://js.monitor.azure.com/scripts/b/ai.2.min.js",
	crossOrigin: "anonymous",
	onInit: function (sdk) {
		sdk.addTelemetryInitializer(function (envelope) {
			if (envelope.baseType === 'ExceptionData') {
				if (typeof (envelope.baseData.exceptions) === 'undefined' &&
					envelope.baseData.message &&
					envelope.baseData.message.indexOf('same-origin policy prevents us from getting the details of this exception') >= 0) {
					return false;
				}
			}
		});
	},
	...
});

And that should do it. On Initialization we should add a filter that looks for that error message, and blocks it.

As a bonus thought, consider using the samplingPercentage setting on your configuration in JavaScript to reduce the amount you’re logging; this could be a lot in high traffic sites.

Filtering App Insights Client Exceptions from 3rd party JavaScript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.