Astro ile Flowsery Analytics için proxy kurulumu
Reklam engelleyicilerin müdahalesini önlemek ve daha doğru ziyaretçi verileri toplamak için Flowsery Analytics trafiğini middleware kullanarak Astro uygulamanız üzerinden yönlendirin.
1. Middleware'i oluşturun
src/middleware.js konumunda bir middleware dosyası oluşturun (TypeScript projeleri için src/middleware.ts):
// src/middleware.js
export async function onRequest(context, next) {
const { request } = context;
const url = new URL(request.url);
// Proxy the Flowsery Analytics tracking script
if (url.pathname === '/js/main.js') {
const response = await fetch('https://cdn.flowsery.com/main.js');
const script = await response.text();
return new Response(script, {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'public, max-age=31536000',
},
});
}
// Proxy the event collection endpoint
if (url.pathname === '/api/track' && request.method === 'POST') {
const body = await request.text();
// Resolve the real visitor IP for accurate geolocation
const clientIp =
request.headers.get('cf-connecting-ip') || request.headers.get('x-real-ip') || request.headers.get('x-forwarded-for')?.split(',')[0] || '';
const response = await fetch('https://analytics.flowsery.com/analytics/events', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': request.headers.get('User-Agent') || '',
Origin: request.headers.get('Origin') || url.origin,
// Flowsery reads `x-flowsery-real-ip` as the authoritative visitor IP.
// Without it, every visitor resolves to your server's region because
// Cloudflare rewrites `cf-connecting-ip` on the proxy hop.
...(clientIp && { 'x-flowsery-real-ip': clientIp }),
},
body: body,
});
const responseText = await response.text();
return new Response(responseText, {
status: response.status,
headers: {
'Content-Type': 'application/json',
},
});
}
return next();
}2. Astro'da sunucu taraflı oluşturmayı (SSR) etkinleştirin
astro.config.mjs dosyanızı SSR'yi destekleyecek şekilde güncelleyin (middleware için gereklidir):
// astro.config.mjs
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
output: 'server', // or 'hybrid'
adapter: node({
mode: 'standalone',
}),
});Node.js adaptörüne de ihtiyacınız olacak: npm install @astrojs/node
3. Alternatif: API rotalarıyla statik site
Sitenizi tamamen statik tutmayı tercih ediyorsanız middleware yerine API rotaları oluşturun:
Script proxy rotası
src/pages/js/main.js.js dosyasını oluşturun:
// src/pages/js/main.js.js
export async function GET() {
const response = await fetch('https://cdn.flowsery.com/main.js');
const script = await response.text();
return new Response(script, {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'public, max-age=31536000',
},
});
}Olay proxy rotası
src/pages/api/track.js dosyasını oluşturun:
// src/pages/api/track.js
export async function POST({ request }) {
const body = await request.text();
const url = new URL(request.url);
// Resolve the real visitor IP for accurate geolocation
const clientIp =
request.headers.get('x-real-ip') || request.headers.get('x-forwarded-for')?.split(',')[0] || request.headers.get('cf-connecting-ip') || '';
const response = await fetch('https://analytics.flowsery.com/analytics/events', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': request.headers.get('User-Agent') || '',
Origin: request.headers.get('Origin') || url.origin,
'x-forwarded-for': clientIp,
},
body: body,
});
const responseText = await response.text();
return new Response(responseText, {
status: response.status,
headers: {
'Content-Type': 'application/json',
},
});
}Not: Projeniz zaten bir /api/track rotası kullanıyorsa olayları başka bir yola yönlendirmek için Flowsery Analytics script etiketine data-api ekleyin. Örneğin data-api="/flowsery-events" verileri bunun yerine /flowsery-events adresine gönderir.
Önemli: Panelinizde tüm ziyaretçiler aynı konumda görünüyorsa, istekleri Flowsery Analytics /events uç noktasına iletirken x-flowsery-real-ip değerinin proxy sunucusunun IP'sine değil gerçek ziyaretçi IP'sine ayarlandığını doğrulayın.
4. Script etiketini değiştirin
Layout'unuzdaki orijinal Flowsery Analytics kod parçacığını proxy'li sürümle değiştirin:
---
// src/layouts/Layout.astro
const { title } = Astro.props;
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>{title}</title>
<script
defer
data-fl-website-id="flid_******"
src="/js/main.js"
></script>
</head>
<body>
<slot />
</body>
</html>5. Yayına alın
Yayına aldıktan sonra proxy yapılandırması otomatik olarak devreye girer. Hosting sağlayıcınızın şunları desteklediğini doğrulayın:
- Sunucu taraflı oluşturma (middleware yaklaşımı için)
- API rotaları (statik site yaklaşımı için)
Astro ile uyumlu yaygın hosting sağlayıcıları arasında Vercel, Netlify ve Cloudflare Pages bulunur.
Çalıştığını doğrulama
Proxy'nin doğru çalıştığını doğrulamak için:
- Web sitenize gidin
- Tarayıcınızın geliştirici araçlarını açın ve Network sekmesine geçin
- Analiz isteklerinin analytics.flowsery.com yerine kendi alan adınızdan sunulduğunu doğrulayın
Not: Middleware yaklaşımı sunucu taraflı oluşturma gerektirir; API rotaları ise hem statik hem de sunucuda oluşturulan sitelerle çalışır.
Sorun giderme
Tüm ziyaretçiler aynı konumdan görünüyor
Tüm ziyaretçiler sunucunuzun bölgesi gibi tek bir coğrafi konum gösteriyorsa proxy gerçek ziyaretçi IP'lerini doğru iletmiyordur.
Çözüm:
- Proxy'nizin, istekleri Flowsery Analytics
/eventsuç noktasına iletirkenx-flowsery-real-ipdeğerini sunucu IP'sine değil gerçek ziyaretçi IP'sine ayarladığından emin olun. - Astro dağıtımınız başka bir proxy'nin (Vercel, Cloudflare, Netlify) arkasındaysa istediğiniz IP genellikle
cf-connecting-ip,x-vercel-forwarded-forveyax-real-ipiçindedir: edge sağlayıcınızın sunduğu başlığı önce okuyun.