Event tracking and monitoring
Basic concepts of event tracking and monitoring
November 19, 2024Have you ever wonder how websites know what you're doing? How they know when you click a button or scroll to the bottom of the page? As a former marketer, I used to use Google Analytics (GA) to track user behavior on websites, did many data analysis and reports in order to understand user behavior and optimize marketing strategies, but I found out that sometimes the data was not accurate or not detailed or clear enough, and this was a big problem for me.
Now I am a frontend developer, I've got a chance to learn more about how websites work, and what's under the hood.
What is event tracking?
Event Tracking, the name itself is self-explanatory. It's the process of tracking user events on a website via scripts or tools, such as clicks, scrolls, form submissions, and the key metrics for business, such as what kind of product category users are interested in or how many users participated in a survey.
There're three main ways to track events:
- Manually add tracking code to the website: Implementing event tracking code to capture specific events.
- Automatically track with the help of browser APIs: Via DOM APIs.
- Third-party tracking tools.
What is monitoring?
The main purpose of monitoring is to ensure the stability of the system. Data like error logs, loading time, resource requests, etc. are collected and analyzed to identify and resolve issues.
Normally, there're 3 types of monitoring:
- Error monitoring: Capture JS errors, loading errors, etc.
- Performance monitoring: Collecting and analyzing data like loading time, resource request errors, etc. to identify and resolve issues.
- User experience monitoring: Collecting data like white screen, long task, etc.
Basic event tracking implementation
General Tracking function
This function is used to track general events, such as clicks, scrolls, form submissions, etc, and a "POST" request will be sent to the server to record the event.
In this function, I want to get the element that triggers the event,
Let's say I want to track a "CTA" button, this data can be used to analyze the user behavior, and optimize the marketing campaign or website design.
Basic monitoring implementation
Page load monitoring
Just like the general tracking function, we can create a general monitoring function to track the page load time or API request time.
API request monitoring
For API monitoring, there're two stages that we can monitor:
- Before the request is sent.
- After the request is received.
JavaScript error monitoring
If we want to monitor the JavaScript errors, we can use the window.onerror
event, when this event is triggered, execute the tracking function, for example:
We can check the error in the console, it should show an object like this: