diff --git a/src/Logger.ts b/src/Logger.ts index 07a1bd3..1e8bfcb 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -1,4 +1,5 @@ -import { SetLocalStorage } from './SetLocalStorage'; +import { SetLocalStorage, SetLocalStorageClear } from './SetLocalStorage'; +import { postLog } from './apis/postLog'; import { LogPayloadParams, ServiceNameType } from './types/LogType'; import CryptoJS from 'crypto-js'; @@ -89,3 +90,13 @@ export const Logger = ({ userId, serviceName, name, message, path, tags }: LogPa }, }; }; + +window.addEventListener('beforeunload', async (event) => { + // 명세에 따라 preventDefault는 호출해야하며, 기본 동작을 방지합니다. + event.preventDefault(); + console.log('이탈감지'); + const logList: any[] = JSON.parse(localStorage.getItem('yls-web') as string) || []; + console.log(logList); + // const res = await postLog(); + SetLocalStorageClear(); +}); diff --git a/src/SetLocalStorage.ts b/src/SetLocalStorage.ts index a96331d..34cd089 100644 --- a/src/SetLocalStorage.ts +++ b/src/SetLocalStorage.ts @@ -1,7 +1,7 @@ import { postLog } from './apis/postLog'; import { LogType } from './types/LogType'; -const SetLocalStorageClear = () => { +export const SetLocalStorageClear = () => { const list: any[] = []; localStorage.setItem('yls-web', JSON.stringify(list)); }; @@ -17,8 +17,8 @@ export const SetLocalStorage = async (logger: LogType) => { const updateList = [...remainList, logger]; localStorage.setItem('yls-web', JSON.stringify(updateList)); } else { + //const res = await postLog(); SetLocalStorageClear(); - const res = await postLog(); } } };