Netlify Edge Functions
Use a Netlify Edge Function to stream HTML page views to AI Search Index. Works on any Netlify plan including free.
Tracking Endpoint
aisearchindex.com/api/v1/logs/customURL: https://aisearchindex.com/api/v1/logs/custom
Headers:
X-API-Key: YOUR_BOT_TRACKING_KEY
Content-Type: application/json
Required fields:
timestamp, method, host, path, status_code, ip, user_agentEdge Function Script
Create a new Edge Function at netlify/edge-functions/tracking.ts:
/**
* Netlify Edge Function – stream HTML page views to AI Search Index
* Runs at the edge with zero-latency tracking using waitUntil.
*/
import type { Context } from "@netlify/edge-functions";
const TRACKING_KEY = "YOUR_BOT_TRACKING_KEY";
const TRACKING_ENDPOINT = "https://aisearchindex.com/api/v1/logs/custom";
// Skip common static assets and system paths
const SKIP_PATHS = ['/robots.txt', '/sitemap.xml', '/.well-known/', '/favicon.ico'];
const SKIP_EXT = /\.(js|css|png|jpg|jpeg|gif|svg|ico|woff2?|ttf|webp|pdf|mp4|webm|mp3|wav|ogg|json|xml|zip|rar|gz)$/i;
export default async function handler(request: Request, context: Context) {
const url = new URL(request.url);
// Skip static assets and system paths
if (SKIP_EXT.test(url.pathname) || SKIP_PATHS.some(p => url.pathname.startsWith(p))) {
return context.next();
}
// Build tracking payload with all required fields
const payload = {
timestamp: new Date().toISOString(),
method: request.method,
host: url.hostname,
path: url.pathname + url.search,
status_code: 200,
ip: context.ip || request.headers.get('x-forwarded-for') || '',
user_agent: request.headers.get('user-agent') || '',
referer: request.headers.get('referer') || undefined,
};
console.log('[AIS] Sending tracking:', url.pathname);
// Send tracking request asynchronously (non-blocking)
context.waitUntil(
fetch(TRACKING_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': TRACKING_KEY,
},
body: JSON.stringify(payload),
})
.then(res => console.log('[AIS] Response:', res.status))
.catch(err => console.error('[AIS] Error:', err.message))
);
return context.next();
}
export const config = { path: "/*" };Configuration
Replace YOUR_BOT_TRACKING_KEY with your Bot Tracking Key from the dashboard.
| Variable | Description |
|---|---|
| TRACKING_KEY | Your Bot Tracking Key (e.g., bot_xxx...) |
| SKIP_PATHS | Array of paths to exclude from tracking |
| SKIP_EXT | Regex pattern for file extensions to skip |
Deployment Steps
- 1
Create Edge Functions Directory
In your project root, create a folder named
netlify/edge-functions/ - 2
Add the Edge Function
Create
tracking.tswith the script above - 3
Update Your Tracking Key
Replace
YOUR_BOT_TRACKING_KEYwith your actual key from the dashboard - 4
Deploy
Push your changes to trigger a Netlify deployment. The Edge Function will be automatically detected.
Using Environment Variables (Recommended)
For better security, store your tracking key as an environment variable:
// Use Deno.env to access environment variables
const TRACKING_KEY = Deno.env.get("AIS_BOT_TRACKING_KEY") || "";
const TRACKING_ENDPOINT = "https://aisearchindex.com/api/v1/logs/custom";
// ... rest of the scriptThen set AIS_BOT_TRACKING_KEY in Netlify: Site settings → Environment variables → Add variable
Project Structure
your-project/
├── netlify/
│ └── edge-functions/
│ └── tracking.ts ← Edge Function
├── src/ ← Your site source
├── public/
└── netlify.toml ← Optional configOptional: netlify.toml Configuration
You can also configure the Edge Function path in netlify.toml:
[[edge_functions]]
path = "/*"
function = "tracking"Edge Functions vs Log Drains
| Feature | Edge Functions | Log Drains |
|---|---|---|
| Plan Required | Free and above | Pro and above |
| Setup Complexity | Add code file | Dashboard config |
| Customization | Full control | Limited |
| Latency Impact | None (async) | None |
Tip: If you're on a Netlify Pro plan or higher and prefer a no-code solution, you can also use Netlify Log Drains.