45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
"""AI 답변 엔진에서 들어온 방문 판별.
|
|
|
|
표는 AEORanking(frontend/src/lib/analytics/ai-referral.ts)과 같다. referrer 를 지우고 링크에
|
|
utm_source 만 붙이는 앱(ChatGPT 는 utm_source=chatgpt.com)이 있어 utm 도 본다.
|
|
referrer·utm 이 모두 없는 AI 유입은 직접 유입으로 잡히므로 이 수치는 최소치다.
|
|
"""
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AiReferral:
|
|
engine: str
|
|
#: 어떤 신호로 판별했나: referrer 호스트 또는 utm_source
|
|
signal: str
|
|
|
|
|
|
_SOURCES: tuple[tuple[str, tuple[str, ...], tuple[str, ...]], ...] = (
|
|
("chatgpt", ("chatgpt.com", "chat.openai.com"), ("chatgpt.com", "chatgpt")),
|
|
("perplexity", ("perplexity.ai",), ("perplexity.ai", "perplexity")),
|
|
("gemini", ("gemini.google.com", "bard.google.com"), ("gemini.google.com", "gemini")),
|
|
("claude", ("claude.ai",), ("claude.ai", "claude")),
|
|
("copilot", ("copilot.microsoft.com",), ("copilot.microsoft.com", "copilot")),
|
|
)
|
|
|
|
|
|
def _matches_host(host: str, domain: str) -> bool:
|
|
return host == domain or host.endswith(f".{domain}")
|
|
|
|
|
|
def classify_ai_referral(referrer_host: str | None, utm_source: str | None) -> AiReferral | None:
|
|
if referrer_host:
|
|
host = referrer_host.lower()
|
|
for engine, hosts, _ in _SOURCES:
|
|
if any(_matches_host(host, d) for d in hosts):
|
|
return AiReferral(engine, "referrer")
|
|
|
|
utm = (utm_source or "").strip().lower()
|
|
if utm:
|
|
for engine, _, utm_sources in _SOURCES:
|
|
if utm in utm_sources:
|
|
return AiReferral(engine, "utm")
|
|
|
|
return None
|