How to Use Sentry with Vue 3?
Tags: vue, frontend, monitoring, sentry
Published: 2023-07-17 17:54:02


Don’t we often encounter errors and find it difficult to identify them? One of the error and performance monitoring tools is Sentry. In this article, I will explain how to set up Sentry and make improvements with performance monitoring. Are you ready? Let’s get started! 🚀


## What is Sentry?

**Sentry** can be described as an error monitoring and reporting platform for many of our applications if we need to provide a general definition. We can even add that it is a performance reporting platform.

**Sentry** enables developers to monitor application errors in real time, receive and analyze error reports, and quickly diagnose issues.

The _key features_ of Sentry include:

**Error Tracking**: Sentry tracks and captures errors in applications. It can detect various types of errors such as exceptions, error messages, errors in the user interface, and more.
**Real-time Monitoring**: Sentry monitors application errors in real-time and sends notifications promptly. This allows developers to quickly notice mistakes and take action.
**Error Reporting**: Sentry reports errors in detail. Error reports provide crucial information about when, where, and under what conditions the error occurred. This helps developers diagnose and fix errors.
**Integrations**: Sentry supports many popular programming languages and various platforms. This allows it to integrate with different types of applications and systems.
**Performance Monitoring**: Sentry can monitor the performance of applications and detect performance issues. With performance monitoring features, developers can optimize the performance of their applications and enhance the user experience. We’ll even provide an example in this regard 😉

**Additionally**, in addition to the basic explanation provided here, I recommend _reading Onur Dayıbaş’s_ Medium article titled “[Some Concepts in Sentry](https://medium.com/frontend-development-with-js/sentrydeki-baz%C4%B1-kavramlar-project-env-release-ve-sourcemap-2a041395c43b)”

---

## Installation
Firstly, I will support the steps we will take here with visuals. Sentry itself will also provide us with sufficient assistance in this regard.

📌 Go to [Sentry](https://sentry.io/)’s website and create an account if you don’t have one, or log in if you already have an account.

📌 If you’re logging in for the first time, you’ll be prompted to create an organization. One of the main reasons for this is to enable collaboration with your team members. In this example, I’ll skip this step by creating an organization named “_Example_”.



![Create a New Organization in Sentry](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mm99xticd85azu6vps7f.jpg)

📌 We are presented with a screen where we can choose the platform we want to set up. Here, we select **Vue**.



![Choose Platform with Vue](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nw6o952qn5eebigda65.jpg)

📌 Here, we select the alert frequency. We can adjust this setting later, but for now, we select the option “_Alert me on every new issue._” Then, we proceed to the next step, where we create our project and choose our team. If you don’t have a team, by default, it will use the name of your organization as the team. If you want to create a different team, you can click on the “+” sign to create one. Finally, we click on “_Create Project._”


![Create a Project with alert frequency](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3b2x1fageca97uhtnex2.jpg)

📌 Here, Sentry provides us with instructions. It automatically adjusts settings based on three main scenarios. Some important points to understand are as follows:

**Error Monitoring**: This is our error monitoring setting. It is selected by default, and we cannot disable it because that’s our primary goal 😉
**Performance Monitoring**: Here, we measure the performance of our page load. If we don’t want this option, we can remove it.
**Session Replay**: This is actually one of the features we love. It provides a small video snippet showing how errors occurred. However, it is not recommended to collect these data at high rates. It’s sufficient to enable it only for errors.


![Install Configure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0r2z9mcvc4pqv81tf5ed.jpg)

Let’s move on to the installation process. As shown in the screenshot above, we install the Sentry package in our project using either “_npm_” or “_yarn_” depending on our preference. Then, we directly copy the Vue3 sample code and paste it into our “_main.js_” file. Of course, you may have a different “_main.js_” file in your project. The important point here is to include the portion starting with `Sentry.init({])` (_Don’t forget to import it_). The key settings and their meanings in this configuration are as follows:

- **`App`**: It is used to specify the application object, which is the application we created with createApp or the application we want to monitor.
- **`dns`**: The address of the Sentry server where error reports will be sent. I recommend keeping this address in a secure manner, such as using a .env file. Exposing this address can lead to potential security issues.
- **`integrations`**: It is used to configure Sentry integrations. In this example, **`Sentry.BrowserTracing`** and **`Sentry.Replay` **integrations are used.
- **`tracesSampleRate`**: Performance monitoring sample rate. It is expressed as a percentage, and in this example, it is set to **`100%`**. It is recommended to lower this rate in real production environments.
- **`replaysSessionSampleRate`**: Session replay sample rate. It is expressed as a percentage, and in this example, it is set to **`10%`**. During development, it can be set **`100%`** for better analysis, while in production, a lower sampling rate can be used.
- **`replaysOnErrorSampleRate`**: Sample rate for sessions with errors. If you are not already sampling sessions, you can set the sampling rate for sessions with errors to **`100%`**.

For more advanced configuration options that are not shown in the screenshot, you can refer to the [Sentry Basic Options](https://docs.sentry.io/platforms/javascript/guides/vue/configuration/options/) in the documentation to make detailed adjustments. Essentially, we have completed the basic installation process here. Now, let’s see how we can capture our **first error**.

---

## Issues
After completing the installations,** Sentry** automatically redirects us to a page like the one below and expects us to send our errors. In fact, we will see or track all the errors here.


![Sentry Issues Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d9dc5p5uqderas2iige5.jpg)

> How can we send this error or different data to Sentry? 🤔

👩‍💻We are creating a simple button and function.


```
<button @click="sendSentryData">Click me</button>
```

```
function sendSentryData() {
   Sentry.captureMessage('Button clicked');
}
```

If you haven’t imported and initialized **Sentry** when creating this function, you will receive the error shown in the image below.



![Sentry ReferenceError](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm0n6m3wme1mduegsp70.jpg)


<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExMGJ2NXJuNmllNDlicG9obGV4dmNybDFvaXNyendxMXdmenlmbDZzdyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/JqDeI2yjpSRgdh35oe/giphy.gif">

![Show Reference Error in Issues](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sz0hl76tux3uthv0gpsl.jpg)

> **We encountered our first error! So how did this happen?**

Because we encountered a reference error at its core, and since our **`replaysOnErrorSampleRate`** value is set to **`100%`**, **Sentry** detected it and sent it to us directly. By clicking on its content, we can learn more about the occurrence of the error or get detailed information. However, to keep it brief, I won’t go into that here. We received the error because we hadn’t defined Sentry. To resolve this, we add the code **`import * as Sentry from '@sentry/vue'`** to our script and click the button again. 🖱️


![Issues](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pgr9vglodd3lismuol8.jpg)

As you can see, we can also view the message we sent. One important aspect here is that we can see how many times this error occurred and how many users experienced it. The “_Events_” count shows how many times the error occurred, while the “_Users_” count indicates how many different users encountered this issue. By clicking into the error, we can access more detailed information. Essentially, we have successfully performed a basic error and data reporting process. Now, let’s move on to performance analysis.

---

## Performance Monitoring
<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExaW5iMGx1dXQ5ZXByN2p3dXdxcmkyODZ0MXlzNGh0aTB1YjRuYzEwZSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/rM0wxzvwsv5g4/giphy.gif">

As you know, one of the important aspects for all of us is the page loading speed, in other words, the performance of our project. We mentioned that with _Sentry_, it is possible to analyze this performance. If we selected the **`Performance Monitoring`** option during the installation process, we actually gain some speed in the setup phase. When we return to our “_main.js_” file, we will see thee **`BrowserTracing`** and **`tranceSampleRate`** values. In **BrowserTracing**, the adjustments we make involve entering the addresses of our domains. “_localhost_” is set as the default. Additionally, we need to add the address of the deployed application.


![Tracing URL Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhyt9kitrvf3tm1js7iw.jpg)

I have added the example “[https://moviepal.vercel.app](https://moviepal.vercel.app)" as you mentioned. This way, I will be able to measure its performance even in the deployed state.

Another value we have is **`tranceSampleRate`**, and it is recommended to lower this value in production environments. However, if you are doing local development, it is recommended to set this value to **1.0**. This way, you will be able to analyze all page reloads.

> **How will performance monitoring be beneficial for us? Let’s see it in practice.**

<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExZGMwMXE1eWxkamY4eXoycTg0aTlhOHpzcHVvMzMxN3pzZDd3dTRyZCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/2IudUHdI075HL02Pkk/giphy.gif">

In fact, while working on this project, I was monitoring the performance metrics. And then I realized that the page loading was taking longer due to the images. I was using **UseImage** for this purpose, thinking it would be effective, but it doesn’t seem to be the case.


📌 I have shared the development examples I made here on [Gist](https://gist.github.com/Eralpozcan/ae1f7d40b16b8420d4ab3f0ac6b6b430).



![Performance Result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ska0oqzji2u12im06xah.jpg)

📌 The first thing I did here was to remove **UseImage** and use a regular Img tag to observe the situation.


![Performance Result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9n7tfernq5ayhzn0yvwp.jpg)

📌 As you can see, there is some improvement in our metrics. Although the CLS value has increased, other parameters show a decrease of over 50%. Next, I enable the lazy loading feature of Swiper and the lazy attribute of the img tag. This leads to a significant decrease in our TTFB value.


![Performance Result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqkk4qljnu4arxa70p9x.jpg)

📌 Then, as observed in the images, I notice that CSS files are being repeatedly imported and loaded within the components. I address this by globally importing them into the root directory of our project. This way, there will be no need to load them repeatedly on this page.


![Performance Result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyfseewezhymy46xx716.jpg)


> _You can find a table below that shows the processes we do in a more comparable way.👇_


|  | Swiper CSS Imported* | Event Duration | CLS | FCP | FP | LCP | TTFB | Gist |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Swiper with UseImage | Yes | 2.93s | 0.226 | 733.4ms | 733.4ms | 1.207s | 80.20ms | [Gist](https://gist.github.com/Eralpozcan/ae1f7d40b16b8420d4ab3f0ac6b6b430#file-swiper1useimage-vue) |
| Swiper with Img Tag | Yes | 2.67s | 0.392 | 383.4ms | 383.4ms | 1.157s | 35.60ms | [Gist](https://gist.github.com/Eralpozcan/ae1f7d40b16b8420d4ab3f0ac6b6b430#file-swiper2img-vue) |
| Swiper with Img and Lazy | Yes | 2.06s | 0.253 | 391ms | 391ms | 791.5ms | 18.60ms | [Gist] (https://gist.github.com/Eralpozcan/ae1f7d40b16b8420d4ab3f0ac6b6b430#file-swiper3imglazy-vue) |
| Swiper with Img and Lazy | No | 1.78s | 0.218 | 312.9ms | 312.9m | 693.7ms | 8ms | [Gist](https://gist.github.com/Eralpozcan/ae1f7d40b16b8420d4ab3f0ac6b6b430#file-swiper4imglazycss-vue) |



**Swiper CSS Imported**: Indicates whether the Swiper CSS is being reloaded within the page. If the value is “No”, it means that it has been imported into the project’s root directory as well.

If you feel that your knowledge about abbreviations and metrics is not sufficient, Google web.dev can be helpful for you.

As has seen, we created a simple example using Sentry’s Performance Monitoring tool and optimized our code. Of course, there are different optimization methods available, but here we made some basic improvements and significantly increased our page loading speed through Sentry.

> **With these optimizations, we were able to increase our page loading speed by approximately 60%.**

In this article, I have tried to explain the usage and installation of **Sentry** and how it can be beneficial to us by providing examples.

After reading my post, feel free to reach out to me on my [Linkedin](https://www.linkedin.com/in/eralpozcan/) account if you have any questions or suggestions.

See you in my next article! 📩


![Contact QR Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yh60ti1grxxhpqeg8ni7.png)