Experience
Projects
My Stack
Case studies
Contact me
Open my CV

·9 min read

First-party analytics for a Next.js portfolio, built from scratch

This portfolio used to be a static Vite app. I wanted to know which projects people actually open and whether anyone reads the blog, and I did not want to hand every visitor's address and a cookie to a third party just to learn that. So I rebuilt the site on Next.js and taught it to count itself. This post covers that counter, the admin panel around it, and the deploy problems I hit on the way.

The repository is private, so I describe the design and the commits, not the whole code.

Role
Designer and developer
Period
Jan 2026 to Nov 2026
Status
Site online, repository private
Stack
Next.js, TypeScript, MySQL, Tailwind CSS

The challenge

Three constraints shaped everything. I wanted analytics that a privacy-minded visitor would not object to. The host replaces the application folder on every start, so nothing can be saved to disk next to the code. And the whole thing, blog, contact form, admin and analytics, had to run as one small Node process I could redeploy by pushing to Git.

The static Vite site could do none of that. It had no server, so analytics meant a third-party script, and a blog meant editing files and rebuilding.

My responsibility

I did all of it, and the history is my own:

  • the move from a Vite single page app to Next.js, with an English and a Georgian site (e9df413, Nov 2026)
  • the tracker, the ingestion endpoint, MySQL storage, the reports and the CSV export
  • the admin login, its middleware and the blog editor
  • the deploy: start script, host entry file, caching headers and link previews
  • the bilingual project write-ups and the visual components that render them

How I approached it

What gets stored, and what does not

The rule is simple: the IP address is never written anywhere. Each request is reduced to a visitor hash on the server:

ts
sha256(`${ip}|${userAgent}|${daySalt}`).slice(0, 16)
  1. 01

    Day starts

    a random daySalt exists

  2. 02

    Request arrives

    hash of IP, user agent and salt, first 16 characters

  3. 03

    IP is dropped

    it is never written anywhere

  4. 04

    Day passes

    salts older than yesterday are deleted

  5. 05

    Result

    two days of one person cannot be linked

Figure 1. How a visitor hash is born and why it cannot be reproduced after the day ends.

The daySalt is random, lives for one day, and older salts are deleted. After that, nobody, including me, can link two days of one person together, because the value that made the hash reproducible no longer exists. Everything else in an event is coarse on purpose:

  • the page path and the language (/ka pages count as Georgian)
  • the referrer host only, never the full URL
  • device class and browser family from the user agent
  • the country, read from the cf-ipcountry header when a proxy such as Cloudflare sets it
  • for attention events, visible seconds and scroll depth
DataStored?How
IP addressNoUsed only to compute the hash, never written
Visitor identityHash only16 characters, changes every day with the salt
ReferrerHost onlyNever the full URL
CountryCoarse valueFrom cf-ipcountry when a proxy sets it
Live pingsMemory only60 second window, gone on restart
My own visitsNot recordedSkipped when the admin session cookie is present
Note. The browser decides nothing about identity. The tracker sends a path, a random per-tab session id and a few numbers. The server derives the visitor hash, device and country itself, so a forged request cannot claim to be somebody else.

The tracker

A small client component sends a pageview on every route change, a "still here" ping every 20 seconds while the tab is visible, and one attention event per page when you leave it. It also records clicks on outbound links, mailto:, tel: and the CV, plus the first few uncaught JavaScript errors.

Requests go out with navigator.sendBeacon so they survive the page closing, and the server answers every one with an empty 204. Bots get the same 204 and are not recorded. Each address also has a budget of 120 requests a minute, so a script cannot flood the table.

eventspingsstored eventsone INSERTindexed querylive cardTrackersendBeaconTrack endpointreplies 204Server derivationhash, device, countryLive windowmemory, 60 secondsEvent queueflushed every 2 sMySQL400 day retentionAdmin reportscharts, CSV
Figure 2. The tracker beacons to the server, which derives the visitor hash itself, keeps live pings in memory only and writes the remaining events to MySQL in batches for the admin reports.

Live visitors without storing anything

The "who is here now" card is fed by the pings, but those are held in memory only, with a 60 second window. A tab left open all day costs no storage, and the live list disappears when the process restarts. Only pageview and attention events go to the database.

Why the data lives in MySQL

My host replaces the application folder on every start. Anything written beside the code would vanish at the next restart. So posts, contact messages and analytics events all live in MySQL, and the app creates its own tables on first use, so a fresh database works without a separate migration step.

0

seconds between still-here pings

0

second live window, memory only

0

seconds between batch writes

0

days of event retention

Figure 3. The timings behind the tracker and the storage.

Events are queued and written as a single multi-row INSERT every two seconds, so a burst of traffic is one query, not one per pageview. Reading a date range is one indexed query on the timestamp column, and a daily job removes events older than 400 days. The admin overview shows visitors per day, pageviews, average time on page, bounce rate and busiest hours, with tabs by page, country, device and referrer, a list of JavaScript errors and a CSV export. A humans only switch keeps sessions that had a click or at least a second of visible time.

An admin that fails closed

There is exactly one admin, so I did not build a user table. The session is a token of the form base64url(expiry).base64url(HMAC(secret, expiry)), valid for seven days, checked with Web Crypto because the same check has to run in the Edge middleware, where Node's crypto module does not exist, and in the Node route handlers. If the secret is missing or too short, nothing verifies, and if the credentials are not set, nothing matches. Rotating the secret signs everyone out.

Login compares username and password with a constant-time hash comparison and always compares both, so the response time does not say which half was wrong. It allows five attempts per address per fifteen minutes plus thirty failures overall, so rotating a forged proxy header cannot widen the guess budget. The middleware is the first gate, but every admin page and API route checks the session again, so a middleware bypass such as CVE-2025-29927 would still expose nothing. These are design decisions from the single migration commit e9df413 (Nov 2026), not fixes made after an incident.

The host that replaced the folder, twice

Two small commits on 12 Nov 2026 came from the same platform quirk. The host runs a generic Node egg that starts a file named index.js. My app had no such file, so 2559bbc (Nov 2026) added one that only hands off to the real start script and forwards termination signals. Then 324b7ac (Nov 2026) fixed the start script itself. It built once if no build existed, so a newly pulled commit kept serving the previous build. It now records the commit it built from in a stamp file and rebuilds whenever the checked-out commit differs, and it rebuilds always when git is unavailable, because then it cannot tell.

The images that would not update

Replaced brand images kept showing their old version. They keep their file names, and I had told browsers to cache them for a week. f77bf81 (Nov 2026) cut that to one day with background revalidation and put a version query on the icon, manifest and share-image URLs, so a replaced image gets a new URL. A cache header cannot take back a week it has already granted, so the version string is the only reliable fix after the fact.

Project journey

  1. Jan 2026

    Vite portfolio

    contact form, SEO tags and a Google tag in the first week

  2. Sep 2026

    Rebuilt from my CV

    project cards, roles, career timeline, Microsoft Clarity added

  3. Nov 2026

    Next.js migration

    admin, blog, MySQL storage and the first-party tracker in one commit

  4. Nov 2026

    Host fixes

    index.js entry for the host, rebuild when the commit changes

  5. Nov 2026

    Research-style posts

    animated diagrams and bilingual case studies

  6. Nov 2026

    Cache busting

    versioned image URLs after stale previews

Figure 4. From a static Vite site to a self-measuring Next.js site, from the first commit to now.

Beyond the launch

The gap I wrote about earlier is closed. The Google tag arrived in Jan 2026 (4abb603) and Microsoft Clarity in Sep 2026 (60ab671), both before the first-party counter existed, and for a while they ran next to it. I removed both in Nov 2026 and moved the Inter font files onto my own server, so the page makes no request to a third party. Lighthouse used to flag third-party cookies and a console error that came from Clarity's own script, and both went away with it.

Other limits are known. Unique visitors are counted per day, so a person who visits on three days counts three times. That is the honest consequence of not linking days, and for a portfolio I prefer it to the alternative. The login throttle and the tracker budget live in process memory and reset on restart, which is fine for one instance. And the first commit of the migration also stopped tracking an environment file that had been committed since Jan 2026 (6217a1e) and replaced it with an example file. The old file is still in the repository history, so whatever it held should be treated as exposed and rotated. The git history cannot show whether that has happened.

The outcome

datunashvili.com is online as a Next.js site in two languages with a blog, an admin panel and the first-party counter described above. The code paths in this post are the ones I reviewed in the repository on 12 Nov 2026. I have no verified traffic numbers to quote, and I do not describe the analytics as a replacement for the older tags.

Evidence

Not verified: I did not benchmark the batched insert under heavy traffic, and I did not measure how much bot traffic the humans only switch removes. I did not check whether the old environment values were rotated.

#nextjs#analytics#privacy#mysql

Keep reading

Have a project in mind?