I built a link shortener called Briefly a few months ago. It started as a weekend project. "How hard can it be?" I thought. Redirect one URL to another. Done.
The redirect part was easy. The analytics part taught me things I didn't expect.
The Redirect Is the Easy Part
Seriously. A link shortener's core logic is maybe 20 lines:
app.get('/:code', async (req, res) => {
const link = await db.findByCode(req.params.code);
if (!link) return res.status(404).send('Not found');
// Track the click (async, don't block redirect)
trackClick(req, link.id).catch(console.error);
res.redirect(301, link.originalUrl);
});
But the interesting stuff happens in trackClick.
What Analytics Actually Matter
When I first added analytics, I tracked everything. Timestamp. IP. User agent. Referrer. Screen size. Language. Every header I could grab.
Most of it was useless noise.
Here's what actually matters when you're sharing links:
1. Click Count Over Time
Not just total clicks. Clicks per hour. Per day. This tells you when your audience is active. I found that links I shared on Twitter got 80% of their clicks in the first 4 hours. Reddit links had a slower burn over 2-3 days.
2. Referrer Source
Where did the click come from? This is gold. You'll quickly learn which platforms drive real traffic and which are vanity metrics.
function parseReferrer(referrerUrl) {
if (!referrerUrl) return 'direct';
const hostname = new URL(referrerUrl).hostname;
const sources = {
't.co': 'twitter',
'twitter.com': 'twitter',
'x.com': 'twitter',
'reddit.com': 'reddit',
'old.reddit.com': 'reddit',
'linkedin.com': 'linkedin',
'facebook.com': 'facebook',
};
return sources[hostname] || hostname;
}
3. Geographic Data (Country Level)
You don't need city-level precision. Country is enough. It tells you if your content is reaching the right audience. I was surprised to find that 40% of my traffic came from India and Brazil — markets I wasn't even thinking about.
4. Device Type
Mobile vs desktop. That's it. Don't overthink this. But it matters because if 70% of your clicks are mobile, your landing page better work on phones.
Mistakes I Made
Mistake 1: Blocking the redirect for analytics. Don't do this. Track asynchronously. Every millisecond of redirect delay costs you clicks.
Mistake 2: Storing raw IPs. Privacy laws exist. Hash them or use them only for unique visitor counting, then discard.
Mistake 3: Not deduplicating. One person refreshing counts as one click, not ten. Use a combination of hashed IP + user agent + time window.
async function isUniqueClick(linkId, visitorHash) {
const window = 24 * 60 * 60 * 1000; // 24 hours
const recent = await db.findRecentClick(linkId, visitorHash, Date.now() - window);
return !recent;
}
What I'd Do Differently
If I started over, I'd build the analytics dashboard before the shortener. Sounds backwards, but the dashboard design tells you exactly what data to collect. No more, no less.
I'd also add UTM parameter support from day one. Being able to append UTM tags automatically when creating short links saves a ton of manual work.
Try It
Briefly is free to use. Create short links, track clicks, see where your traffic comes from. No signup wall for basic features.
The code taught me more about web analytics than any course could. Sometimes the best way to learn is to build the thing.
Top comments (0)