Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(traffic): send the tracking beacon as text/plain so it isn't CORS-blocked (#287)
Gökhan Geyik981259280bce
diff --git a/server/src/public/t.js b/server/src/public/t.jsindex 6e932eb88..1f3e2dd01 100644--- a/server/src/public/t.js+++ b/server/src/public/t.js@@ -1 +1 @@-(function(){if(window.__at)return;var r=document.referrer,h="";try{h=new URL(r).hostname.replace(/^www\./,"")}catch(e){}var d=["chatgpt.com","chat.openai.com","search.chatgpt.com","perplexity.ai","claude.ai","gemini.google.com","copilot.microsoft.com","you.com","phind.com","meta.ai","poe.com","labs.google"],f=d.some(function(a){return h===a||h.endsWith("."+a)});if(!f){var p=new URLSearchParams(location.search),s=(p.get("utm_source")||"").toLowerCase(),m=(p.get("utm_medium")||"").toLowerCase();f=["chatgpt","chatgpt.com","openai","perplexity","perplexity.ai","claude","claude.ai","gemini","copilot","ai-chat"].indexOf(s)!==-1||s.indexOf("chatgpt")!==-1||s.indexOf("perplexity")!==-1||s.indexOf("claude")!==-1||s.indexOf("gemini")!==-1||s.indexOf("copilot")!==-1||m==="ai"||m==="ai-referral"}if(!f&&h.indexOf("bing.com")!==-1)f=r.indexOf("/ck/a")!==-1;if(!f)return;window.__at=1;var c=document.currentScript,k=c.getAttribute("data-t"),o=c.src.replace(/\/t\.js(\?.*)?$/,"");var b=JSON.stringify({u:location.href,r:r,s:h,a:navigator.userAgent,t:Date.now(),l:navigator.language,d:screen.width+"x"+screen.height});var e=o+"/track/"+k;if(navigator.sendBeacon)navigator.sendBeacon(e,new Blob([b],{type:"application/json"}));else fetch(e,{method:"POST",body:b,headers:{"Content-Type":"application/json"},keepalive:!0}).catch(function(){})})();+(function(){if(window.__at)return;var r=document.referrer,h="";try{h=new URL(r).hostname.replace(/^www\./,"")}catch(e){}var d=["chatgpt.com","chat.openai.com","search.chatgpt.com","perplexity.ai","claude.ai","gemini.google.com","copilot.microsoft.com","you.com","phind.com","meta.ai","poe.com","labs.google"],f=d.some(function(a){return h===a||h.endsWith("."+a)});if(!f){var p=new URLSearchParams(location.search),s=(p.get("utm_source")||"").toLowerCase(),m=(p.get("utm_medium")||"").toLowerCase();f=["chatgpt","chatgpt.com","openai","perplexity","perplexity.ai","claude","claude.ai","gemini","copilot","ai-chat"].indexOf(s)!==-1||s.indexOf("chatgpt")!==-1||s.indexOf("perplexity")!==-1||s.indexOf("claude")!==-1||s.indexOf("gemini")!==-1||s.indexOf("copilot")!==-1||m==="ai"||m==="ai-referral"}if(!f&&h.indexOf("bing.com")!==-1)f=r.indexOf("/ck/a")!==-1;if(!f)return;window.__at=1;var c=document.currentScript,k=c.getAttribute("data-t"),o=c.src.replace(/\/t\.js(\?.*)?$/,"");var b=JSON.stringify({u:location.href,r:r,s:h,a:navigator.userAgent,t:Date.now(),l:navigator.language,d:screen.width+"x"+screen.height});var e=o+"/track/"+k;if(navigator.sendBeacon)navigator.sendBeacon(e,new Blob([b],{type:"text/plain;charset=UTF-8"}));else fetch(e,{method:"POST",body:b,headers:{"Content-Type":"text/plain;charset=UTF-8"},keepalive:!0}).catch(function(){})})();diff --git a/server/src/routes/traffic.js b/server/src/routes/traffic.jsindex 0c26d94be..6040ac66c 100644--- a/server/src/routes/traffic.js+++ b/server/src/routes/traffic.js@@ -1,4 +1,4 @@-import { Router } from 'express';+import express, { Router } from 'express';import { readFileSync } from 'fs';import { join, dirname } from 'path';import { fileURLToPath } from 'url';@@ -7,6 +7,14 @@ import { trafficLimiter } from '../middleware/rate-limiter.js';const router = Router();+// Beacons are sent as text/plain so navigator.sendBeacon stays a CORS-safelisted+// request — application/json would require a preflight, which beacons can't do,+// so the browser silently blocks them. Read the body as text here; the handler+// JSON-parses it. application/json is still accepted (older cached t.js, direct+// API callers): the global json parser handles those and this middleware no-ops+// once a body has already been parsed.+const beaconBody = express.text({ type: ['text/plain', 'application/json'], limit: '64kb' });+const __dirname = dirname(fileURLToPath(import.meta.url));const scriptPath = join(__dirname, '..', 'public', 't.js');@@ -58,7 +66,7 @@ router.options('/track/:trackingCode', (req, res) => {/*** POST /track/:trackingCode — receive beacon data from t.js*/-router.post('/track/:trackingCode', trafficLimiter, async (req, res) => {+router.post('/track/:trackingCode', trafficLimiter, beaconBody, async (req, res) => {res.set('Access-Control-Allow-Origin', '*');try {const { trackingCode } = req.params;@@ -77,7 +85,16 @@ router.post('/track/:trackingCode', trafficLimiter, async (req, res) => {return res.status(404).json({ ok: false });}- const body = req.body;+ // Body arrives as a JSON string (text/plain beacon) or an already-parsed+ // object (application/json via the global json parser). Normalize both.+ let body = req.body;+ if (typeof body === 'string') {+ try {+ body = JSON.parse(body);+ } catch {+ return res.status(400).json({ ok: false });+ }+ }if (!body || !body.u) {return res.status(400).json({ ok: false });}