Welcome to the final chapter of our Passes tutorial!
In the previous chapter, Keybinding Management, we brought our application to life. We allowed users to press Enter to copy a referral link and Esc to close the window.
Right now, your application works perfectly on your computer. But once you release it to thousands of users, you are "flying blind."
Esc)?Without a system to record these actions, you have no way of knowing if your feature is a success or a failure.
The Analytics Service acts like the "Black Box" flight recorder on an airplane.
It runs quietly in the background. Whenever a significant action happens (like a user copying a link), we send a signal to this service. The service records:
We are going to modify our Passes.tsx file one last time. We want to record exactly when a user copies a guest pass link.
First, we import the helper function.
// Importing the analytics logger
import { logEvent } from '../../services/analytics/index.js';
In Chapter 4, we set up a useInput hook to listen for the Enter key. We will add one line of code inside that logic.
useInput((_input, key) => {
// When the user presses Enter AND a link exists
if (key.return && referralLink) {
// 1. Copy to clipboard (System Action)
void setClipboard(referralLink).then(() => {
// 2. Record the event (Analytics Action)
logEvent('tengu_guest_passes_link_copied', {});
// 3. Close the UI (User Feedback)
onDone(`Referral link copied to clipboard!`);
});
}
});
Breakdown:
logEvent: This is the function we call to record data.'tengu_guest_passes_link_copied': This is the specific Event Name. We use long, descriptive names so data scientists know exactly what this means later.{}: The second argument is for Properties. Here, we don't need extra details, so we pass an empty object.
We place the log event inside the success block of setClipboard. This ensures we only count the action if the link was actually successfully prepared for copying.
You might wonder: "Does logging an event slow down the app?"
The answer is no. The Analytics Service is "fire and forget." The UI tells the service to log something, and the UI immediately moves on without waiting for the log to finish uploading to the internet.
logEvent
While the real file handles buffering and network retries, here is a simplified version of what logEvent does conceptually:
// Simplified concept of the Analytics Service
export function logEvent(eventName: string, properties: object) {
// 1. Create the data packet
const payload = {
event: eventName,
props: properties,
timestamp: Date.now(),
userId: getCurrentUserId(), // Fetched from config
};
// 2. Send it to the background worker
// We don't await this; we let it happen in the background
sendToBackend(payload);
}
Key Concepts:
link_copied). The service automatically added the timestamp and userId. This keeps your UI code clean.Congratulations! You have completed the Passes project tutorial. Let's recap what we built:
You have built a fully functional, professional-grade CLI feature that is beautiful, interactive, and data-driven.
Tutorial Complete.
Generated by Code IQ