Geolocation
How Bklit collects geolocation data and Cloudflare proxy setup
Geolocation
Bklit automatically collects geolocation data (country, city, region) from your users to provide geographic analytics insights.
How It Works
Cloudflare Proxy (Production)
In production, Bklit reads geolocation from Cloudflare headers when your domain uses Cloudflare as a proxy:
- CF-IPCountry - Country code
- CF-Region - State/region
- CF-City - City name
- CF-Latitude / CF-Longitude - Coordinates
- CF-Timezone - User timezone
- CF-PostalCode - ZIP/postal code
Advantages:
- ✅ Fast (no external API calls)
- ✅ Accurate
- ✅ Free
- ✅ Privacy-friendly
Fallback (Development)
In development (localhost), Bklit falls back to ip-api.com for geolocation since Cloudflare headers aren't available.
Setting Up Cloudflare Proxy
Prerequisites
- Domain name
- Cloudflare account (free tier works)
Step 1: Add Site to Cloudflare
- Go to https://dash.cloudflare.com
- Click "Add a Site"
- Enter your domain name
- Select free plan
- Click "Continue"
Step 2: Update Nameservers
- Cloudflare will provide 2 nameservers
- Go to your domain registrar (GoDaddy, Namecheap, etc.)
- Update DNS nameservers to Cloudflare's nameservers
- Wait for DNS propagation (up to 24 hours, usually minutes)
Step 3: Enable IP Geolocation
- In Cloudflare dashboard, select your domain
- Go to Network tab
- Scroll to IP Geolocation
- Toggle ON
This adds geolocation headers to all requests.
Step 4: Configure DNS
- Go to DNS tab in Cloudflare
- Add A record pointing to your server IP:
- Type: A
- Name: @ (for root domain) or www
- IPv4 address: Your server IP
- Proxy status: ✅ Proxied (orange cloud)
Important: The orange cloud must be ON (Proxied) for headers to be added.
Step 5: Enable Managed Transforms (Optional)
For additional headers:
- Go to Transform Rules → Managed Transforms
- Enable Add visitor location headers
- This adds additional CF-* headers with more detail
Step 6: Verify Setup
Deploy your app and check if geolocation works:
- Visit your site
- Trigger a pageview
- Check dashboard analytics
- Location data (country, city) should appear
Geolocation Data Collected
When using Cloudflare proxy, Bklit collects:
| Field | Description | Example |
|---|---|---|
| Country | Full country name | "United States" |
| Country Code | ISO 2-letter code | "US" |
| Region | State/province code | "CA" |
| Region Name | State/province name | "California" |
| City | City name | "San Francisco" |
| Latitude | GPS coordinate | 37.7749 |
| Longitude | GPS coordinate | -122.4194 |
| Timezone | IANA timezone | "America/Los_Angeles" |
| ZIP | Postal code | "94102" |
| ISP | Internet provider | "Cloudflare" |
Testing Geolocation
In Development (Localhost)
Geolocation uses ip-api.com fallback:
- Works automatically
- Detects your real location
- Free tier (no setup needed)
In Production
Geolocation uses Cloudflare headers:
- No external API calls
- Faster and more reliable
- Requires Cloudflare proxy setup
Verifying Cloudflare Headers
Check if Cloudflare headers are present:
// In your browser console (on your deployed site)
fetch('/api/test-headers')
.then(r => r.json())
.then(console.log);Create test endpoint in your app to return headers:
// app/api/test-headers/route.ts
export async function GET(request: Request) {
const headers = Object.fromEntries(request.headers.entries());
const cfHeaders = Object.entries(headers)
.filter(([key]) => key.startsWith('cf-'))
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {});
return Response.json({ cfHeaders });
}Troubleshooting
No Location Data in Dashboard
Check:
- Cloudflare proxy is enabled (orange cloud in DNS)
- IP Geolocation is turned ON in Cloudflare → Network
- Request is going through Cloudflare (check for CF-* headers)
- Not using localhost (use deployed domain)
Showing Wrong Location
Possible causes:
- VPN usage by visitor
- Cloudflare's IP database (generally accurate)
- ISP routing through different location
Privacy Considerations
- Bklit doesn't store precise IP addresses permanently
- Only stores geolocation data (city-level)
- Designed to help meet GDPR requirements when used as described and configured appropriately
- No personal identification
Without Cloudflare
If you don't use Cloudflare:
- Geolocation will not work in production
- Consider adding Cloudflare as a proxy (free)
- Or implement custom geolocation service
- Analytics will still work (just without location data)
Related Documentation
- Local Development - Local setup guide
- Configuration - SDK configuration options
- Token Security - API security