Debugging

How to find the JavaScript errors your users actually hit.

Your site works on your laptop. It is your visitors, on other browsers, phones, connections and extensions, who find the errors. Most of them never tell you; they just leave. This is how to see those errors for yourself.

A replay paused at a broken coupon button beside a console showing a TypeError and a failed 500 request

Here is an uncomfortable fact about websites: the errors that cost you the most are the ones you never see. Your own browser has the right extensions, a fast connection and a warm cache. Your visitors have old phones, ad blockers, translation plugins, flaky mobile data and browsers you have never tested. When something breaks for them, the page simply stops working, and they leave. Hardly anyone writes in to tell you.

The fix is to collect errors from real visitors' browsers, and to keep enough of the visit around each error to understand it.

What to capture

Four kinds of problem cover most of what goes wrong in the browser:

  • Uncaught JavaScript errors. The classic "TypeError: Cannot read properties of null". The browser reports them through the error event, with a message, the file and the line.
  • Unhandled promise rejections. Errors in asynchronous code, such as a fetch that fails with nobody handling it. These arrive through the unhandledrejection event, and many setups miss them.
  • Console errors and warnings. What your own code and your plugins write with console.error and console.warn. Often the most helpful clue of all.
  • Failed requests. Calls to your server or other services that come back with an error status, or no response at all. The page's code may not throw anything, but the visitor's action did not work.

Option 1: write it yourself

The bare minimum is a few lines that listen for both events and send them somewhere:

window.addEventListener('error', function (e) {
  navigator.sendBeacon('/log-error', JSON.stringify({
    message: e.message, file: e.filename, line: e.lineno, page: location.href
  }));
});
window.addEventListener('unhandledrejection', function (e) {
  navigator.sendBeacon('/log-error', JSON.stringify({
    message: String(e.reason), page: location.href
  }));
});

This tells you that errors happen. It quickly shows its limits: a thousand slightly different copies of the same error, no idea which visitors it affected, and no way to know what they did just before it. You end up with a log you cannot act on.

Option 2: errors attached to the visit

The more useful approach is to record the errors alongside the visit itself. A session replay tool with error logging, such as GetSiteTape, keeps the console, the errors and every request beside the recording. When you open an error, you are looking at the visitor's screen at the moment it happened: which page, which button, what they typed into, which request failed just before.

A recording paused at an error, with the failed request beside it. Example data.

Two other things make the list usable:

  • Grouping. The same bug produces different messages: different IDs, addresses, numbers. Grouping errors after setting those parts aside turns a thousand rows into one.
  • Counting visits, not occurrences. One visitor stuck in a loop can fire an error five hundred times. Five hundred visitors hitting it once each is a much bigger problem. Sort by visits affected.

The "Script error." problem

Sooner or later you will see an error that just says Script error. with no file and no line. That is the browser protecting you: when a script is loaded from a different domain, the browser hides the details of its errors unless that script allows sharing them.

For scripts you control on another domain or a CDN, two changes fix it: serve the file with an Access-Control-Allow-Origin header, and load it with the crossorigin attribute on the script tag. For third-party scripts you do not control, you usually cannot. In that case the recording is your best clue: watch what the visitor did when the error appeared.

Which errors to fix first

An honest error list is long, and a lot of it is noise: browser extensions, bots, old browsers. Work through it in this order:

  1. Errors on pages that make you money: checkout, sign-up, pricing, contact.
  2. Errors that come with a rage click or a failed request at the same moment. Something visibly broke for the visitor.
  3. New errors. An error that first appeared yesterday usually came from yesterday's change, which is the easiest kind to fix.
  4. Everything else, by visits affected.

Know the day something breaks

Most errors arrive with a change: a release, a theme update, a plugin update, a new tag in your tag manager. An alert on new errors, sent once per error rather than on every occurrence, tells you within minutes. Send it somewhere the person who can fix it will see: email for a small team, a Slack channel for a larger one, or a webhook into your own tools.

Write tickets your developer can act on

"The checkout is broken" is not a bug report. A useful one has the page, the browser and device, the steps the visitor took, the error and the failed request, with times. GetSiteTape builds that from the recording in one click, with a link that opens the replay at the right second. Whatever tool you use, include those things and the fix arrives faster.

What browser error logging does not do

It shows what went wrong in the visitor's browser. It does not see errors that happen only on your server, and GetSiteTape keeps the file and line of an error rather than a full source-mapped stack trace. If you need both of those, keep a dedicated error monitoring tool for the code, and use the recording to see what the visitor experienced.

Questions about this.

Something else? Ask us and a person will answer.

How do I log JavaScript errors from real users?

Add a script that listens for uncaught errors and unhandled promise rejections in the visitor's browser and sends them to a server. Session replay tools such as GetSiteTape do this for you and attach each error to the recording of the visit it happened in.

Why do I only see "Script error." with no details?

Browsers hide the details of errors thrown by scripts loaded from another domain unless the script is served with CORS headers and loaded with the crossorigin attribute. Add both to the scripts you control and their errors are reported in full.

Which JavaScript errors should I fix first?

Group repeated errors together and sort them by the number of visits they affect, not by how often they fire. An error on the checkout page that reaches a few hundred visitors matters more than a noisy one on a page nobody uses.

Watch your first visitor today.

Free for 1,000 replays a month. No card needed. Setup takes about a minute.