UNPKG

@adstage/web-sdk

Version:

AdStage Web SDK - Production-ready marketing platform SDK with React Provider support for seamless integration

167 lines (148 loc) 3.83 kB
/** * AdStage SDK - 이벤트용 사용자 정보 수집기 * Events API 스키마에 최적화된 사용자 정보 수집 */ import { DOMUtils } from '../../utils/dom-utils'; export class EventUserCollector { private static _userProperties: { gender?: 'male' | 'female' | 'other' | 'unknown'; country?: string; city?: string; age?: string; language?: string; [key: string]: any; } = {}; /** * Events API용 사용자 정보 반환 * TrackEventDto.UserAttributesInput 형태에 맞춤 */ static getUserInfo(): { gender?: 'male' | 'female' | 'other' | 'unknown'; country?: string; city?: string; age?: string; language?: string; } { const baseInfo = EventUserCollector.getBaseUserInfo(); // 설정된 사용자 속성과 병합 return { ...baseInfo, ...EventUserCollector._userProperties }; } /** * 기본 사용자 정보 수집 (브라우저 기반) */ private static getBaseUserInfo(): { language?: string; country?: string; } { if (!DOMUtils.isBrowser()) { return { language: 'ko-KR', country: 'KR' }; } // 브라우저 언어 설정에서 국가 추출 const language = navigator.language || 'ko-KR'; const country = EventUserCollector.extractCountryFromLanguage(language); return { language, country }; } /** * 언어 코드에서 국가 추출 */ private static extractCountryFromLanguage(language: string): string { const countryMap: { [key: string]: string } = { 'ko': 'KR', 'ko-KR': 'KR', 'en': 'US', 'en-US': 'US', 'en-GB': 'GB', 'ja': 'JP', 'ja-JP': 'JP', 'zh': 'CN', 'zh-CN': 'CN', 'zh-TW': 'TW', 'de': 'DE', 'de-DE': 'DE', 'fr': 'FR', 'fr-FR': 'FR' }; // 정확한 매칭 시도 if (countryMap[language]) { return countryMap[language]; } // 언어 코드만 추출해서 매칭 const langCode = language.split('-')[0]; return countryMap[langCode] || 'KR'; } /** * 사용자 속성 설정 */ static setUserProperties(properties: { gender?: 'male' | 'female' | 'other' | 'unknown'; country?: string; city?: string; age?: string; language?: string; [key: string]: any; }): void { EventUserCollector._userProperties = { ...EventUserCollector._userProperties, ...properties }; } /** * 특정 사용자 속성 설정 */ static setUserProperty(key: string, value: any): void { EventUserCollector._userProperties[key] = value; } /** * 사용자 속성 초기화 */ static clearUserProperties(): void { EventUserCollector._userProperties = {}; } /** * 현재 설정된 사용자 속성 반환 */ static getCurrentUserProperties(): typeof EventUserCollector._userProperties { return { ...EventUserCollector._userProperties }; } /** * 사용자 지역 정보 추정 (타임존 기반) */ static estimateLocation(): { timezone: string; estimatedCountry?: string; } { if (!DOMUtils.isBrowser()) { return { timezone: 'UTC', estimatedCountry: 'KR' }; } const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; // 타임존 기반 국가 추정 const timezoneCountryMap: { [key: string]: string } = { 'Asia/Seoul': 'KR', 'Asia/Tokyo': 'JP', 'Asia/Shanghai': 'CN', 'Asia/Hong_Kong': 'HK', 'Asia/Taipei': 'TW', 'America/New_York': 'US', 'America/Los_Angeles': 'US', 'Europe/London': 'GB', 'Europe/Berlin': 'DE', 'Europe/Paris': 'FR' }; return { timezone, estimatedCountry: timezoneCountryMap[timezone] || 'KR' }; } }