Skip to content

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.

feat(traffic): only persist platform-attributable AI visits (referrer or utm) (#310)

Gökhan Geyikf395b5a7a503
diff --git a/server/src/public/t.js b/server/src/public/t.js
index 1f3e2dd01..848b99d7c 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:"text/plain;charset=UTF-8"}));else fetch(e,{method:"POST",body:b,headers:{"Content-Type":"text/plain;charset=UTF-8"},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"],pf="";for(var i=0;i<d.length;i++){var a=d[i];if(h===a||h.endsWith("."+a)){pf=a;break}}if(!pf){var s=(new URLSearchParams(location.search).get("utm_source")||"").toLowerCase();if(s.indexOf("chatgpt")!==-1||s.indexOf("openai")!==-1)pf="chatgpt.com";else if(s.indexOf("perplexity")!==-1)pf="perplexity.ai";else if(s.indexOf("claude")!==-1)pf="claude.ai";else if(s.indexOf("gemini")!==-1)pf="gemini.google.com";else if(s.indexOf("copilot")!==-1)pf="copilot.microsoft.com"}if(!pf&&h.indexOf("bing.com")!==-1&&r.indexOf("/ck/a")!==-1)pf="bing.com";if(!pf)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:pf,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.js
index 6040ac66c..5fd14ec34 100644
--- a/server/src/routes/traffic.js
+++ b/server/src/routes/traffic.js
@@ -107,11 +107,23 @@ router.post('/track/:trackingCode', trafficLimiter, beaconBody, async (req, res)
const country = req.headers['cf-ipcountry'] || null;
+ // Only persist platform-attributable visits (#288). t.js already resolves a
+ // concrete platform (AI referrer host or a known utm_source) and returns
+ // early otherwise — but older cached snippets still send an empty platform
+ // for utm-detected visits, which previously stored a null "Unknown" row.
+ // Drop those defensively so the data has no unattributable rows. The body is
+ // untrusted: only accept a string, and trim so whitespace-only values can't
+ // slip past the emptiness check (a non-string can't reach .slice and crash).
+ const sourcePlatform = (typeof body.s === 'string' ? body.s : '').trim().slice(0, 255);
+ if (!sourcePlatform) {
+ return res.status(204).end();
+ }
+
await supabaseAdmin.from('ai_traffic_logs').insert({
brand_id: brand.id,
url: (body.u || '').slice(0, 2048),
referrer: (body.r || '').slice(0, 2048) || null,
- source_platform: (body.s || '').slice(0, 255) || null,
+ source_platform: sourcePlatform,
user_agent: (body.a || '').slice(0, 512) || null,
ip_address: ip || null,
country,
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_charts.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_charts.tsx
index 8d80c5dd1..e18a13156 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_charts.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_charts.tsx
@@ -47,6 +47,12 @@ function ChartContainer({
}
// ─── Platform colors ─────────────────────────────────────────────────────────
+//
+// Keys must stay in sync with the detection host list in the tracking snippet
+// (`server/src/public/t.js`): every platform t.js can resolve and store needs a
+// label + color here, otherwise it renders as a raw hostname. `unknown` is kept
+// only for historical rows that predate #288 (null source_platform); new visits
+// always carry a concrete platform.
const PLATFORM_COLORS: Record<string, string> = {
'chatgpt.com': '#6366f1',
@@ -60,6 +66,8 @@ const PLATFORM_COLORS: Record<string, string> = {
'phind.com': '#a5b4fc',
'meta.ai': '#7c3aed',
'poe.com': '#e9d5ff',
+ 'labs.google': '#b794f6',
+ 'bing.com': '#67c5d6',
unknown: '#94a3b8',
};
@@ -75,6 +83,8 @@ const PLATFORM_NAMES: Record<string, string> = {
'phind.com': 'Phind',
'meta.ai': 'Meta AI',
'poe.com': 'Poe',
+ 'labs.google': 'Google Labs',
+ 'bing.com': 'Bing',
unknown: 'Unknown',
};