EU Region Auto-Detection
Detection Modes
Configure EU detection with the euDetection option:
createConsentPlugin({
euDetection: 'auto', // Recommended
});| Mode | Description |
|---|---|
'auto' | Try all methods in order (recommended) |
'cloudflare' | Only use Cloudflare headers |
'api' | Only use IP API |
'always' | Treat all users as EU (always show banner) |
'never' | Treat all users as non-EU (never show banner) |
Detection Chain
In 'auto' mode, detection methods are tried in order:
1. Cloudflare Headers (Fastest)
Checks for X-Is-EU-Country header set by Cloudflare.
Setup with Transform Rules:
- Go to Cloudflare Dashboard
- Select your domain
- Rules > Transform Rules > Modify Request Header
- Add rule:
- Header name:
X-Is-EU-Country - Value: Dynamic expression
ip.geoip.is_in_european_union
- Header name:
Setup with Worker:
export default {
async fetch(request) {
const isEU = request.cf?.isEUCountry === true;
const response = await fetch(request);
const newResponse = new Response(response.body, response);
newResponse.headers.set('X-Is-EU-Country', isEU ? 'true' : 'false');
return newResponse;
}
}2. IP API (Fallback)
If Cloudflare header is not available, the library calls ipapi.co:
GET https://ipapi.co/json/Returns country code and EU membership status.
Rate Limits
ipapi.co has rate limits on the free tier. For high-traffic sites, use Cloudflare detection.
3. Timezone Heuristics (Last Resort)
If API call fails, the library checks the browser timezone:
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
// Maps timezone to likely EU countryThis is not 100% accurate but provides a reasonable fallback.
Custom Detector
Implement your own detection logic:
import type { GeoDetector } from '@structured-world/vue-privacy';
const myDetector: GeoDetector = {
async detect() {
const response = await fetch('/api/geo');
const data = await response.json();
return {
isEU: data.is_eu,
countryCode: data.country,
method: 'custom' as const,
};
}
};
createConsentPlugin({
geoDetector: myDetector,
});EU Countries
The following countries are considered EU for consent purposes:
Austria, Belgium, Bulgaria, Croatia, Cyprus, Czech Republic, Denmark, Estonia, Finland, France, Germany, Greece, Hungary, Ireland, Italy, Latvia, Lithuania, Luxembourg, Malta, Netherlands, Poland, Portugal, Romania, Slovakia, Slovenia, Spain, Sweden.
Plus EEA countries: Iceland, Liechtenstein, Norway.