Proper paranoia: geolocking analytics to stay GDPR-compliant without a cookie banner
- Published
- 2025-06-02
Under GDPR you can't track your European users without them consenting by clicking 'Accept' on a cookie banner. You can however track visitors outside the EU. Here's how I geolocked Meta's Pixel to the US so that Doof! can optimize their campaigns while staying GDPR compliant, without adding a cookie banner to their website.
The whole thing is pretty simple: Don't track visitors from the EU. If you don't track visitors from the EU, you don't need consent to track them. That means no cookie banner messing up the looks of your website.
There's two parts to this:
- Make sure that only visitors from the US are tracked
- Make sure that only people coming from Facebook are tracked
The first part is obvious: We want to geolock the tracking. That means we only want to have analytics code delivered to the visitor's browser if they're from the US. The good news is, that's pretty easy. Putting Cloudflare in front of the website brings a few advantages, one of which is that they add a few headers with metadata. This includes the country from which they're connecting from, in cf-ipcountry. For the US, that value is US.
The second part is a bit more complicated: We only want the visitor to be tracked if they visit the website coming from an ad campaign. If you roll out the tracking code independently of that, Meta tracks every visitor to your website. Not just the ones clicking links on Facebook. The solution to that is pretty simple though: Just make a special URL that is only used on Facebook, and when that URL is used, only then do you also deliver the tracking code to the frontend. And just like that, Facebook can't track anyone else. In this case Doof! just appends ?from=fb to any URL they put on Facebook, and thus can rest assured that only clicks from Facebook get tracked. Given that they only advertise to users from the US, that alone keeps EU visitors pretty safe.
A minor issue though: To track the visitor's journey through the website, they need to track when they click links on it, including links that don't have the ?from=fb on it. Fortunately the solution is pretty simple as well: If they ever visited the website using the link from Facebook, they get the tracking code and Meta sets a cookie. On the backend of the website I then check if the cookie is present (which means they used that link before), and then roll out the tracking code.
Meta's tracking Pixel is thus only added to the website if people use a link from Facebook, or have previously done so.
But why both, and not just one solution?
Fair question. My reason is that I don't really trust Facebook to only perfectly market to US users only. If any EU visitor ends up clicking that link, they get tracked, accident or not.
On the flip side I generally trust Cloudflare to get the location right, but it's no guarantee that it's always routed perfectly. For example, things get really funky on Cloudflare's free plan for Telekom users. Telekom currently ends up routing your request from inside the EU to a data center in the US if you're on the free plan. That can end up making it look like it's a request coming from the US, not from the EU.
Both solutions combined however are as safe as it gets. It's pretty unlikely that both Meta and Cloudflare both mess up at the same time, and an EU user ends up getting tracked.
Here's the relevant code snippets:
const trackableCountries: string[] = ['US']
// where the click came from
const source = url.searchParams.get('from')
// what country they're connecting from
const country = request.headers.get('cf-ipcountry')
// user has the Global Privacy Control signal set
// https://github.com/globalprivacycontrol/reference-server-express
const gpc = request.headers.get('Sec-GPC') === '1'
// is the country one that allows tracking
const legal = !!country && trackableCountries.includes(country)
// we want to enable Meta's Pixel if it's legal and they're either coming from facebook or already have their cookie
const fromFacebook = source === 'fb' || !!cookies.get('_fbp')
const pixel = legal && fromFacebook && !gpc
// delete Meta Pixel cookie otherwise
if (!pixel) {
cookies.delete('_fbp', { path: '/' })
}
// tell the frontend to include the pixel or not
return {
pixel
}And on the frontend:
if (pixel) {
// tracking code
}Anything else?
Global Privacy Control is a thing! It's not widely adopted yet, but it's gaining traction. You can see it added in the code above: It's just a header that browsers can send when visiting a website. If it's enabled there's no tracking either. Easy to add, and if the user is putting in the effort of giving us an actual signal on whether we can track them or not, I think we should respect it.
Note
I'm not a lawyer. I can't guarantee that this is perfectly GDPR compliant - in theory a European visitor could go out of their way to make sure they end up getting tracked. They could setup their Facebook account in a way that makes them look like an American to Meta, get served their ads that way, and then use a VPN to connect from the US to appear American to me as well. So... in theory Europeans could end up getting tracked, despite this whole setup? But at that point I don't think there's anything that can reasonably be done to prevent that. If you want privacy, maybe don't role-play as an American?
If you have any idea on how to improve this even more though, shoot me a message! I always appreciate feedback and advice on how to improve my services.