Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Relic Solution: Creating Insights PageAction events for JS Errors

insights
browser_agent
js-errors

#1

Creating events for JavaScript Errors in Insights

One common question I see customers ask is “how can I view and query my JavaScript errors in Insights?” Currrently, JS errors are not an event type for the Browser agent. However, using the Browser agent API (Browser Pro or Browser Pro Trial subscription required) we can easily create our own custom PageAction events for JS errors which we can then filter and view in Insights. :smiley:

There are two methods in the API that I’ll touch on, setErrorHandler and addPageAction.

The setErrorHandler method gets called every time the Browser agent encounters a JavaScript error. This method allows for you to conditionally determine whether to report an error to New Relic. The method takes a callback function with an error object as its parameter. If the callback function returns false then the error is reported to NR, if the function returns true then the error is ignored and not reported. This may seem a bit counter-intuitive but think of the function as “handling” the error if it returns true.

The addPageAction method creates a PageAction event which can later be queried in Insights. It takes two arguments: a name of type string, and a JavaScript object of attributes to include in the PageAction event. PageAction events will also include all of the default PageView attributes which you can use to filter and facet your results in your NRQL queries.

By nesting the addPageAction method inside the callback function of setErrorHandler we can ensure that we create a custom PageAction event for each time the Browser agent encounters a JavaScript error. Make sure to remember to return false in the callback function so the error will be available in the Browser JS Errors page.

Here is an example of how you might combine these methods to record your own JSError Page Action:

newrelic.setErrorHandler(function (err) {
   newrelic.addPageAction('JSError', {
    "type": err.name,
    "message": err.message,
    "stack": err.stack
   });

// Return false to have the error reported to NR Browser
   return false;
})

This creates a PageAction event with an actionName attribute set to JSError. The type, message, and stack are all JavaScript error properties which you can use as attributes to filter your queries.

Viewing errors in Insights

Now that you’ve created these PageAction events, how do you view these errors in Insights? Here are some example NRQL queries to help get you started:

View All Errors

SELECT * FROM PageAction WHERE actionName = 'JSError'

Counts of different types of errors

SELECT count(*) FROM PageAction WHERE actionName = 'JSError' FACET type

How many errors are we seeing for different browsers

SELECT count(*) FROM PageAction WHERE actionName = 'JSError' FACET userAgentName

Your Browser Pro subscription provides you with 8 days of Insights event retention for Browser events


Explorers Hub Treasure Hunt