Keeping your own visits out of Vercel Analytics
I wanted a simple way to keep my own visits out of my site's analytics. I
ended up using a browser preference with Vercel's
beforeSend hook.
Filtering by public IP is a bit awkward. One address can cover several devices, and it can change when you switch networks. A browser preference is a better fit for keeping just my own visits out.
A browser preference
The approach is straightforward: save
exclude-self-visits=true in localStorage, then check it before
sending an analytics event.
Vercel's beforeSend hook lets a callback return the event or
return null to skip it. The central logic looks like this:
function beforeSend(event) {
try {
const excluded =
localStorage.getItem("exclude-self-visits") === "true";
if (excluded) {
return null;
}
} catch {
// Allow events if storage cannot be accessed.
}
return event;
}
I also added a URL option, so I can enable the preference by visiting my site
with ?exclude-self-visits=true. Using false removes
it. The parameter disappears from the address bar after the preference is
saved.
This needs to happen before analytics starts, so the first page view respects the preference too.
The trade-offs
I do have to set this up in each browser I use, including on my phone. Once it's set, switching between Wi-Fi and mobile data doesn't matter.
It isn't permanent. Clearing site data removes it, and private browsing storage is temporary. Safari's tracking prevention can also remove localStorage after seven days of Safari use without interaction with the site. That counts days of browser use, rather than simply seven calendar days.
The code’s on GitHub if you’d like to try it. There’s a small example in the repo to get started.