Frontend Monitoring Overview
🎯 Introduction
During local development, errors appear in your console. Once deployed, your code runs on users' devices, where you cannot see what goes wrong. Frontend monitoring helps you find out promptly what is happening in production.
By the end of this article, you will be able to:
- Explain why frontend applications need monitoring.
- Identify its four main areas: error collection, event tracking, performance reporting, and logging and alerts.
- Write a basic working monitoring script and understand the core flow: collection → reporting.
- Follow the learning path through the six articles in this course.
🧱 Why Frontend Monitoring Matters
You can see local errors because the code runs in your own browser. Production code runs on thousands of unfamiliar devices, in environments you cannot fully reproduce locally. This creates three common problems:
- Problems are hard to reproduce: a user reports a blank page, but the issue may depend on their device, network, and sequence of actions.
- Feedback arrives late: most users simply close the page when they encounter a bug instead of reporting it.
- Optimization lacks evidence: without knowing where real users experience delays, you can only guess what to optimize.
Running a production application without monitoring is like running a convenience store without cameras or sales records: you cannot tell what went missing, when, or how much. Monitoring provides both cameras and a ledger: records of what happened, how many people it affected, and where it happened.
Why monitor if you already have tests? Tests verify code before release using predefined environments and cases. Monitoring observes real users in real environments after release. They complement each other: tests catch predictable problems, while monitoring catches those that slip through.
🧱 The Monitoring System at a Glance
Most monitoring systems cover four areas, each answering a different set of questions:
| Area | Questions answered | Articles in this course |
|---|---|---|
| Error collection | Did the code throw an error? In which file and on which line? | Article 2: Error Collection |
| Event tracking | What did users do? Which buttons did they click and which pages did they visit? | Article 3: Event Tracking and Data Reporting |
| Performance reporting | Are pages fast for real users? Where are the delays? | Article 4: Performance Data Reporting |
| Logging and alerts | Where is data aggregated? Who notifies me when something goes wrong? | Articles 5 and 6: Integrating Sentry; Logging and Alerts |
Error collection watches for JavaScript runtime errors, unhandled promise rejections, and resource loading failures, recording the message, location, and user environment. It directly affects whether features work, making it a high priority.
Event tracking adds recording points to your code: record an event when someone clicks Buy Now, and another when they enter checkout. Together, these events form clickstreams and conversion funnels.
Performance reporting collects performance data, such as Web Vitals, from real users' devices. For metric definitions and web-vitals usage, see the Performance Optimization course. This course focuses on collecting production data continuously and automatically.
Logging and alerts require a platform to aggregate and display collected data, plus notifications that alert the on-call developer when errors spike, before users complain. We will use the open-source platform Sentry for this part.
🛠 Try It: A Basic Monitoring Script
Monitoring has two essential steps: collect data in the browser and send it to a server. Let's implement that flow without third-party libraries. Browsers already provide collection hooks: JavaScript runtime errors trigger error, and promise rejections without a handler trigger unhandledrejection:
// Collect JavaScript runtime errors
window.addEventListener('error', (event) => {
report({
type: 'error',
message: event.message, // Error message
file: event.filename, // File where the error occurred
line: event.lineno, // Line where the error occurred
});
});
// Collect unhandled promise rejections
window.addEventListener('unhandledrejection', (event) => {
report({
type: 'promise-error',
message: String(event.reason),
});
});
// Report data to your server endpoint
function report(data) {
navigator.sendBeacon('/api/monitor', JSON.stringify(data));
}
We use navigator.sendBeacon rather than fetch because monitoring reports often happen just as a page is closing. The browser may cancel ordinary requests, while sendBeacon queues the data and makes a best effort to deliver it even after the page closes.
Import the script and deliberately introduce an error to verify the complete flow:
import './monitor.js';
// Deliberately trigger an error to simulate a production incident
const user = null;
console.log(user.name);
The receiving endpoint's logs should contain a record with the error message, filename, and line number. Locally, any backend framework with an endpoint that prints incoming data will do. Production monitoring builds on this with richer context, more robust reporting strategies such as batching and retries, and a dashboard.
🧭 Learning Path
The course has six articles, moving from concepts to practice:
- Frontend Monitoring Overview (this article): the problems monitoring solves and the parts of a monitoring system.
- Error Collection: collecting
errorevents,unhandledrejectionevents, resource loading errors, and errors through framework error boundaries. - Event Tracking and Data Reporting: designing events, comparing manual and automatic tracking, and choosing when to report.
- Performance Data Reporting: automatically collecting real users' Web Vitals and other metrics to build performance dashboards.
- Integrating Sentry: using the open-source platform for error grouping and locating errors with source maps.
- Logging and Alerts: organizing collected data into logs and configuring alert rules.
❓ Questions and Answers
Q1: Why monitor production when I can already see errors in the local console?
Local output only shows your own execution. Many production errors depend on a particular device, network, or sequence of actions and cannot be reproduced locally. Monitoring collects those errors for you.
Q2: Will monitoring slow down my page?
Well-designed monitoring has little overhead: collection attaches event listeners, and sendBeacon sends data without blocking the main thread. Avoid excessive monitoring: reporting everything wastes bandwidth and increases server load. Production solutions typically use sampling and batching.
Q3: Does a small personal project need monitoring?
Yes, but it can be lightweight. Add at least basic error collection, either with a few lines of code or Sentry's free tier. Otherwise, a production blank-page issue could go unnoticed for weeks.
Q4: Can monitoring data affect user privacy?
Yes. Address that risk during collection: exclude sensitive input such as passwords and government ID numbers, redact data before reporting, and describe what you collect in your privacy policy.
🧪 Exercises
Exercise 1: Complete the script to also report failures to load resources such as images and scripts. Hint: these failures also trigger error, but you need a capture-phase listener, and event.target is the element that failed:
// Write your code here: report resource loading failures; check addEventListener's third argument
function report(data) {
navigator.sendBeacon('/api/monitor', JSON.stringify(data));
}
Exercise 2: Try event tracking. Complete the code to report an event when the user clicks the login button, including the event name login_click and the timestamp of the click:
const loginBtn = document.querySelector('#login-btn');
// Write your code here: report { event: 'login_click', time: timestamp } on click
Afterward, consider how the questions answered by these two records differ from those answered by the error records above.
🎉 You now have an overview of frontend monitoring! Next, work through Error Collection to explore listeners such as error and unhandledrejection in detail.
