/** * 문의 유입 기록(UTM). * * webforai.kr·company·region(webforai-landing 저장소 site/assets/attr.js)과 같은 규칙·같은 쿠키(wfa_attr, Domain=.webforai.kr)를 쓴다. * 그래서 네이버 블로그 → infinith.webforai.kr → webforai.kr 처럼 서브도메인을 옮겨 다녀도 같은 기록이 남는다. * * 규칙: 마지막으로 들어온 '직접 방문이 아닌' 경로를 30일 기억(last non-direct touch). * - 주소에 utm_* 가 있으면 덮어쓴다 * - utm 없이 외부 사이트(또는 광고 클릭 ID)로 왔으면, 살아 있는 utm 기록이 없을 때만 ref 로 남긴다 * - 직접 방문·같은 사이트 안 이동은 기록을 바꾸지 않는다 * 문의 함수로 보내는 source 뒤에 붙는다. 예: footer | utm:naver_blog/organic/2611_webforai_hospital | land:infinith.webforai.kr/ */ export const ATTR_KEY = 'wfa_attr'; export const ATTR_TTL_MS = 30 * 24 * 60 * 60 * 1000; const UTM_KEYS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term'] as const; const CLICK_IDS = ['fbclid', 'gclid', 'NaPm'] as const; export interface Attribution { utm?: Partial>; click?: string; ref?: string; land: string; at: number; } const clean = (s: string) => s.replace(/[|\s]+/g, ' ').trim().slice(0, 60); const baseDomain = (h: string) => h.split('.').slice(-2).join('.'); export function nextAttribution( prev: Attribution | null, search: string, referrer: string, host: string, land: string, now: number, ): Attribution | null { const q = new URLSearchParams(search); const utm: NonNullable = {}; for (const k of UTM_KEYS) { const v = q.get(k); if (v) utm[k] = clean(v); } const click = CLICK_IDS.find((k) => q.has(k)); const alive = prev && now - prev.at < ATTR_TTL_MS ? prev : null; if (Object.keys(utm).length) return { utm, click, land, at: now }; let refHost = ''; try { refHost = referrer ? new URL(referrer).hostname : ''; } catch { refHost = ''; } const external = !!refHost && refHost !== host && baseDomain(refHost) !== baseDomain(host); if (alive?.utm) return null; if (click) return { click, ref: refHost || undefined, land, at: now }; if (external) return { ref: refHost, land, at: now }; return null; } export function attributionSuffix(a: Attribution | null, now: number): string { if (!a || now - a.at >= ATTR_TTL_MS) return ''; const parts: string[] = []; if (a.utm) { const u = a.utm; parts.push('utm:' + [u.utm_source ?? '-', u.utm_medium ?? '-', u.utm_campaign ?? '-'].join('/')); if (u.utm_content) parts.push('content:' + u.utm_content); if (u.utm_term) parts.push('term:' + u.utm_term); } if (a.click) parts.push('click:' + a.click); if (a.ref) parts.push('ref:' + a.ref); parts.push('land:' + a.land.slice(0, 80)); return ' | ' + parts.join(' | '); } function read(): Attribution | null { const m = document.cookie.match(new RegExp('(?:^|; )' + ATTR_KEY + '=([^;]*)')); if (!m) return null; try { return JSON.parse(decodeURIComponent(m[1])) as Attribution; } catch (e) { console.warn('[attribution] 유입 기록을 읽지 못했습니다', e); return null; } } /** 앱 시작 때 한 번 부른다(main.tsx). SPA 라 첫 진입 주소에 utm 이 있다. */ export function captureAttribution(): void { const host = location.hostname; const rec = nextAttribution(read(), location.search, document.referrer, host, host + location.pathname, Date.now()); if (!rec) return; const domain = /(^|\.)webforai\.kr$/.test(host) ? '; Domain=.webforai.kr' : ''; const secure = location.protocol === 'https:' ? '; Secure' : ''; document.cookie = `${ATTR_KEY}=${encodeURIComponent(JSON.stringify(rec))}; Max-Age=${ATTR_TTL_MS / 1000}; Path=/${domain}; SameSite=Lax${secure}`; } /** 문의 source 에 붙일 꼬리. */ export function currentAttributionSuffix(): string { return attributionSuffix(read(), Date.now()); }