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
errorevent, with a message, the file and the line. - Unhandled promise rejections. Errors in asynchronous code, such as a
fetchthat fails with nobody handling it. These arrive through theunhandledrejectionevent, and many setups miss them. - Console errors and warnings. What your own code and your plugins write with
console.errorandconsole.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:
- Errors on pages that make you money: checkout, sign-up, pricing, contact.
- Errors that come with a rage click or a failed request at the same moment. Something visibly broke for the visitor.
- New errors. An error that first appeared yesterday usually came from yesterday's change, which is the easiest kind to fix.
- 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.