Script Proxying
Route tracking through your domain
Why proxy?
Ad blockers and privacy extensions often block requests to known analytics domains. By proxying the Loamly script through your own domain, you can:
- Improve tracking accuracy — Capture visitors who use ad blockers
- Reduce blocked requests — First-party scripts are rarely blocked
- Better performance — Fewer DNS lookups for your visitors
Optional feature
Proxying is optional. Most websites see 85-95% tracking accuracy with the standard installation. Consider proxying if you have a privacy-conscious audience or see significant gaps in your data.
Next.js setup
Create an API route to proxy the tracker script:
// app/api/t/route.ts
import { NextRequest, NextResponse } from 'next/server';
export async function GET(request: NextRequest) {
const url = new URL(request.url);
const domain = url.searchParams.get('d');
const response = await fetch(
`https://app.loamly.ai/t.js?d=${domain}`,
{
headers: {
'User-Agent': request.headers.get('user-agent') || '',
},
}
);
const script = await response.text();
return new NextResponse(script, {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'public, max-age=3600',
},
});
}
export async function POST(request: NextRequest) {
const body = await request.json();
const response = await fetch('https://app.loamly.ai/t', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': request.headers.get('user-agent') || '',
'X-Forwarded-For': request.headers.get('x-forwarded-for') || '',
},
body: JSON.stringify(body),
});
return NextResponse.json(await response.json());
}Update your script tag to use the proxy:
<Script
src="/api/t?d=yourdomain.com"
strategy="afterInteractive"
/>Vercel rewrites
Vercel rewrites are the simplest approach if you are on Vercel:
// vercel.json
{
"rewrites": [
{
"source": "/t.js",
"destination": "https://app.loamly.ai/t.js"
},
{
"source": "/t",
"destination": "https://app.loamly.ai/t"
}
]
}Then use the proxied path:
<script src="/t.js?d=yourdomain.com" defer></script>Query parameters
Vercel rewrites pass query parameters automatically. Make sure your domain parameter is included in the script src.
Cloudflare Workers
Create a Cloudflare Worker to proxy requests:
// worker.js
export default {
async fetch(request) {
const url = new URL(request.url);
// Proxy script requests
if (url.pathname === '/t.js') {
const response = await fetch(
`https://app.loamly.ai/t.js${url.search}`,
{
headers: {
'User-Agent': request.headers.get('User-Agent'),
},
}
);
return new Response(await response.text(), {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'public, max-age=3600',
},
});
}
// Proxy event requests
if (url.pathname === '/t' && request.method === 'POST') {
return fetch('https://app.loamly.ai/t', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': request.headers.get('User-Agent'),
'X-Forwarded-For': request.headers.get('CF-Connecting-IP'),
},
body: request.body,
});
}
return new Response('Not Found', { status: 404 });
},
};Nginx configuration
If you self-host with Nginx, add these location blocks:
# Script proxy
location /t.js {
proxy_pass https://app.loamly.ai/t.js;
proxy_set_header Host app.loamly.ai;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
# Cache the script for 1 hour
proxy_cache_valid 200 1h;
}
# Event proxy
location /t {
proxy_pass https://app.loamly.ai/t;
proxy_set_header Host app.loamly.ai;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
}Verify setup
- Test script loading: Visit
yourdomain.com/t.js?d=yourdomain.comin your browser. You should see JavaScript code. - Check network requests: Open DevTools Network tab, visit your site, and verify requests go to your domain, not app.loamly.ai.
- Verify data: Check your Loamly dashboard to confirm visits are still being tracked.
- Test with ad blocker: Enable an ad blocker and verify tracking still works.
IP forwarding
Make sure your proxy forwards the visitor's IP address using X-Forwarded-For or equivalent headers. This ensures accurate geolocation data.