Linkify
December 31, 2024
3 min read
10hoursofcoding
firebase
Assalamu alaykum everybody
blog goals
- why did I talk so much about this project?
- What was the purpose of the project?
- Briefly about the project
- I recently launched a small project called linkify.uz, a compact link shortener. While the concept is straightforward and I don’t expect it to bring significant benefits, it was a meaningful learning experience for me. At the time, I approached it as a junior frontend engineer, so completing it felt like an accomplishment. The entire project took about 10 hours, with a short break for sleep in between. It may not seem like much, but I’m glad I was able to see it through.
- The main reason I created this project was personal. As those who know me can attest, whenever I’m upset or feel the need to prove something, I turn to coding. This time was no different—I was angry and deeply hurt, so I channeled those emotions into building something. In the end, the project turned out to be quite straightforward, but it served its purpose.
How the Link Shortener Works
At its core, linkify.uz is designed to transform long URLs into short, easy-to-share links. The project uses Firebase for its backend, managing data such as shortened URLs, click tracking, and status updates. Additionally, Recharts is integrated to visually display analytics for user activity.
Code Overview
The main functionality is handled through this key function:
typescript
export const checkUrlStatus = async (
shortenedUrl: string
): Promise<string | null> => {
try {
// Query the "links" collection to find the active URL
const q = query(
collection(db, "links"),
where("shortenedUrl", "==", shortenedUrl),
where("status", "==", "active")
);
const querySnapshot = await getDocs(q);
if (querySnapshot.empty) {
return null; // Return null if the URL is inactive or doesn't exist
}
const linkDoc = querySnapshot.docs[0];
const linkData = linkDoc.data();
// Fetch the latest click count for this link
const clicksQuery = query(
collection(db, "clicks"),
where("linkId", "==", linkDoc.id),
orderBy("timestamp", "desc"),
limit(1)
);
const clicksSnapshot = await getDocs(clicksQuery);
const lastClickCount = clicksSnapshot.empty
? 0
: (clicksSnapshot.docs[0].data() as any).totalClickCount || 0;
// Record a new click in the "clicks" collection
await addDoc(collection(db, "clicks"), {
linkId: linkDoc.id,
timestamp: serverTimestamp(),
ipAddress: "", // IP tracking is optional and anonymized here
totalClickCount: lastClickCount + 1,
});
// Update click counters in the "links" collection
await updateDoc(doc(db, "links", linkDoc.id), {
clickCount: increment(1),
totalClicks: increment(1),
});
// Return the original URL
return linkData.originalUrl;
} catch (error) {
console.error("Failed to check URL status:", error);
return null;
}
};
How It Works in Practice
When a user accesses a shortened URL, the checkUrlStatus
function runs. It checks the Firebase Firestore database to see if the URL is active and retrieves the corresponding original URL
- The system queries the "clicks" collection to find the most recent click count.
- A new record is added to the "clicks" collection, logging the timestamp and incrementing the click count.
- The "links" collection is updated to increment both the
clickCount
(session-specific) andtotalClicks
(cumulative) fields.
If the URL is active, the original URL is returned to redirect the user.