← Back to Tech

How to Add Privacy-First Analytics to Your Site

10/10/2025

James Oluwaleye

Learn how to add privacy-first analytics to your site, no third-party tracker required.

How to Add Privacy-First Analytics to Your Site

How to Add Privacy-First Analytics to Your Site

Web analytics are important for understanding how users behave online, but traditional tools, like Google Analytics, often use cookies and tracking scripts that can invade privacy and slow down performance. A privacy-first approach collects only basic data on the server, without using cookies or fingerprinting. In this method, you keep track of simple metrics like the page visited, the time, and where the user came from on your own server using Next.js API routes or Edge Functions, avoiding any third-party scripts in the browser.

For instance, Cloudflare’s new analytics states that "we don’t use any client-side state, like cookies… for tracking users. And we don’t ‘fingerprint’ individuals using their IP address, User Agent, or any other data." This means no cookies, no tracking scripts, just straightforward, gathered data.

Setting this up in Next.js is simple. You create an API endpoint (like pages/api/analytics/track.js) that logs each page view and call it from your client code whenever the route changes. On the server, you store only the necessary information (like the path, referrer, and timestamp) and you can anonymize any sensitive data if needed. This do-it-yourself analytics setup provides basic insights (like pageviews by URL, referring sites, and top pages) while keeping your users’ information private and your app running quickly.

Why Choose Privacy-First Analytics?

Privacy-first analytics means not tracking individual users at all. There are no cookies or localStorage used for analytics, and no lasting IDs. Instead of counting unique visitors, you just count each page load (or session) as a visit. This approach has some important advantages:

  • User Trust & Compliance: Without cookies or fingerprinting, you don’t store personal data. This makes it much easier to follow laws like GDPR and CCPA. For example, a visit can be counted just by a page view where the referrer is external, without ever knowing the user’s identity. Since you don’t use tracking cookies, you usually don’t need a consent banner just to count page views.
  • Performance: Getting rid of all third-party scripts makes your pages load faster. In one custom Next.js analytics setup, the tracking code added less than 1KB to the client and took only about 5 milliseconds per view, with no effect on Core Web Vitals. In contrast, Google Analytics scripts can often be much larger.
  • Accuracy: Running analytics on the server is actually more reliable. As Cloudflare points out, “it’s more accurate because you don’t miss users who block third-party scripts or JavaScript altogether.” Even if a visitor has disabled JavaScript or uses an ad-blocker, your server will still record the visit.

In short, a self-hosted, privacy-first setup respects user data and often works better than traditional analytics. As one developer said, his goal was to create an analytics system that “respects user privacy (no cookies, no fingerprinting)” and provides “real insights (not just vanity metrics).” We’ll explain how to do this with Next.js below.

Server-Side Tracking with Next.js

With Next.js, you can track everything on the server side. For example, you can create an API Route like pages/api/analytics/track.js (or use an Edge Function) that accepts a POST request containing page information. Whenever a client visits a page or changes routes, you send a simple fetch to this endpoint. The server then logs the path, referrer, timestamp, and other details.

Collecting data on the server means you capture all traffic to your site. It even picks up visits where JavaScript doesn’t run (like from bots or users who have disabled it). Cloudflare highlights this as a major advantage: server-side collection won’t miss users who block scripts.

Here’s how an API route handler in Next.js might look:

JAVASCRIPT

This route accepts a JSON body with the { path, referrer } and writes a log entry. In a real application, you would use a proper database (like SQL or NoSQL) or a key-value store. For example, you might keep a count of pageviews and track unique visitors. The key point is that all tracking happens in your code, not through an external service.

Integrating the Client Side

To make the tracking work, your client code needs to inform the server each time a page is viewed. In Next.js (with a custom _app.js), you can set up the router to send a POST request whenever the route changes. For example:

JAVASCRIPT

This code sends the current path and referrer to your API route. You can also include other non-identifying information, like the page title, but keep it minimal. The fetch call is very small, so it won’t slow down the user’s browsing experience.

If you want, you can put this logic into a custom React hook (like useAnalytics) that your pages can call. The idea remains the same: with each navigation, post the page data to /api/analytics/track.

Collecting and Anonymizing Data

In your tracking endpoint, only collect what you really need. Common fields include:

  • The path or URL of the page visited (omit query strings unless necessary).
  • The referrer (previous page or domain, if there is one).
  • The timestamp of the visit (server time).
  • Optional: a session or visit identifier (but not tied to a user’s identity).
  • (Possibly the country or locale based on IP, but be careful with this.)

Importantly, don’t store personal identifiers like email, login state, or full IP addresses. For IPs, either leave it out completely or anonymize it. For example, you can hide part of an IPv4 address by changing the last part to zero: 192.168.1.1 → 192.168.1.0. This hides the user's exact location but still gives general regional information.

As one privacy tutorial points out: “IP anonymization usually involves removing the last few digits… making the IP address less specific but still useful.” The Cloudflare analytics team also avoids using IP for fingerprinting.

If you need a way to identify unique visits, you could hash something like the anonymized IP with the date to create a temporary visitor ID. However, remember: under GDPR, any stored IP or device fingerprint counts as personal data, so it’s best to minimize this. Many privacy-focused tools skip counting “unique visitors” entirely and concentrate on total hits or sessions.

For storage, a simple demonstration is appending JSON lines to a file as shown earlier. In a real-world application, use a solid solution: a database, an analytics store, or a cache/kv store. You might use a table with columns (date, path, count) to track counts, or log each event and analyze it later. The main idea is that all data stays on your servers. You can even set up a system to automatically remove old data (like keeping only the last 30 days) so you retain useful insights without hoarding unnecessary information.

Compliance and Privacy Notes

By not using cookies and heavy tracking methods, you can avoid most cookie consent rules. Cookie laws, like GDPR and ePrivacy, usually focus on data stored on a user’s device. If you’re not saving anything in the browser, there’s nothing for users to agree to. All tracking happens on your server and is visible on your own website. This means you probably won’t need a cookie banner, but you should still have a privacy policy.

For GDPR and CCPA compliance, the rules center around personal data. If you anonymize IP addresses and don’t collect names, emails, or login details, you’re not tracking personal data. Essentially, you’re doing very basic analytics that shouldn’t break any privacy laws. Always check with a legal expert, but many privacy-friendly platforms say “no cookie banner required” for setups like this.

Benefits of This Lightweight Approach

This DIY privacy-focused analytics has several important benefits:

  • Performance: There’s almost no cost on the user side. For instance, the tracking adds 0KB to the client bundle (all the code runs on your server) and less than 1KB for the fetch logic. The impact per page view is about 5ms, which is very small.
  • No Third-Party Dependencies: You’re not using scripts from Google or other vendors. This means fewer requests to external servers, so you won’t be affected by their outages or slowdowns. You also have full control over your data.
  • Privacy & Trust: Users and privacy regulators will appreciate that you’re only collecting what’s necessary. In this model, there are “no cookies, no tracking scripts, no privacy worries.” You can even avoid showing a cookie-consent popup.
  • Actionable Metrics: Even though it’s simple, the data you collect (like page views, top referrers, and maybe countries) is often enough to improve your site. You won’t get every tiny detail, but you’ll understand the big picture: which pages are important, where visitors come from, and how they navigate your site.

In summary, creating your own analytics with Next.js gives you complete control and flexibility. You can choose exactly what to measure and change it over time. The example setup provided can be expanded (like adding an admin dashboard or visualizations) or simplified to the basics. Even a minimal setup allows you to see how your site is performing without compromising user privacy or speed.

Summary

Privacy-first web analytics means knowing what pages your visitors are looking at and where they come from without knowing who they are. In Next.js, you can do this by using server-side API routes or Edge Functions to track page views. The client only calls your endpoint; all processing is done on your side. By only storing paths, referrers, and timestamps (and masking any IP addresses), you ensure compliance with GDPR and CCPA while avoiding personal data. As mentioned earlier, you’ll have “a privacy-respecting, insightful analytics system” with less than 200 lines of code. Best of all, your pages remain fast, and your users remain in control.

More from Tech

Powered by Melhorar Studio