[feat] solution/backend,frontend,postgres-init: 서비스 사이트 공개 화면 방문 수집 — 전체 방문 통계에 합산하고 사이트 표에 한 줄로, 기능 문서 추가

This commit is contained in:
skkim committed 2026-10-13 16:31:45 +09:00
1 parent 2fc50de161
commit 99a344f65c
21 files changed
+378 -21

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
import {useEffect, useRef} from 'react';
import {useLocation} from 'react-router';
/**
* 서비스 사이트 공개 화면 방문 비콘 — 발행 사이트 비콘(solution/site/src/lib/visit-beacon.ts)과 같은 방식이다.
* 쿠키·식별자를 남기지 않고, 방문자 구분은 서버가 IP·UA·월로 해시해 한다.
*/
const ENDPOINT = '/v1/track/view';
/** 운영자가 자기 방문을 빼고 싶을 때 콘솔에서 `localStorage.setItem('web4ai.stats.disabled', '1')` — 발행 사이트와 같은 키. */
const OPT_OUT_KEY = 'web4ai.stats.disabled';
/** 세는 공개 화면 — 백엔드 `services/visit_service.py` SERVICE_PATHS 와 같게 둔다. 작업 화면(빌더·내 사이트·운영)은 뺀다. */
const SERVICE_PATHS = new Set(['/', '/pricing', '/showcase', '/login', '/signup']);
function servicePath(pathname: string): string | null {
const path = pathname.replace(/\/+$/, '') || '/';
return SERVICE_PATHS.has(path) ? path : null;
}
function isOptedOut(): boolean {
try {
return window.localStorage.getItem(OPT_OUT_KEY) === '1';
} catch {
// 저장소 접근이 막힌 브라우저 — 그냥 센다. 통계 때문에 화면이 깨지면 안 된다.
return false;
}
}
function send(payload: {path: string; referrer: string; utm_source: string}): void {
const body = JSON.stringify(payload);
// text/plain — sendBeacon 이 프리플라이트 없이 보낼 수 있는 형식이다.
const blob = new Blob([body], {type: 'text/plain;charset=UTF-8'});
if (navigator.sendBeacon?.(ENDPOINT, blob)) return;
// 전송 실패는 방문자에게 보일 일이 아니다 — 결과를 버린다.
void fetch(ENDPOINT, {method: 'POST', body, keepalive: true, headers: {'Content-Type': 'text/plain'}}).catch(
() => undefined,
);
}
/** 공개 화면에 들어올 때마다 한 번. 앱 안에서 옮겨 온 화면은 리퍼러를 우리 주소로 보낸다 — 첫 유입처를 화면마다 되풀이하지 않는다. */
export function useServiceVisitBeacon(): void {
const {pathname} = useLocation();
const first = useRef(true);
useEffect(() => {
const isFirst = first.current;
first.current = false;
const path = servicePath(pathname);
if (!path || isOptedOut()) return;
send({
path,
referrer: isFirst ? document.referrer : `${window.location.origin}/`,
utm_source: new URLSearchParams(window.location.search).get('utm_source') ?? '',
});
}, [pathname]);
}